Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 1 | define({ |
| 2 | |
| 3 | /** |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 4 | * Create new slider object. |
| 5 | * The slider will only be used by mouse - touch support |
| 6 | * shouldn't be necessary. |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 7 | */ |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 8 | create : function (menu) { |
| 9 | return Object.create(this)._init(menu); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 10 | }, |
| 11 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 12 | length : function (i) { |
| 13 | if (arguments.length === 0) |
| 14 | return this._length; |
| 15 | if (i == this._length) |
| 16 | return; |
| 17 | this._length = i; |
| 18 | this._initSize(); |
| 19 | }, |
| 20 | |
| 21 | limit : function (i) { |
| 22 | if (arguments.length === 0) |
| 23 | return this._limit; |
| 24 | if (i == this._limit) |
| 25 | return; |
| 26 | this._limit = i; |
| 27 | this._initSize(); |
| 28 | }, |
| 29 | |
| 30 | movetoRel : function (relativePos) { |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 31 | var diffHeight = (this._rulerHeight - this._sliderHeight); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 32 | var relativeOffset = (relativePos / diffHeight); |
| 33 | |
| 34 | var off = this.offset(parseInt(relativeOffset * this._screens)); |
| 35 | if (off !== undefined) { |
| 36 | this._menu.screen(off); |
| 37 | }; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 38 | }, |
| 39 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 40 | movetoAbs : function (absPos) { |
| 41 | var absOffset = (absPos / this._rulerHeight); |
| 42 | |
| 43 | var off = this.offset(parseInt(absOffset * (this._screens + 1))); |
| 44 | if (off !== undefined) { |
| 45 | this._menu.screen(off); |
| 46 | }; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 47 | }, |
| 48 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 49 | offset : function (off) { |
| 50 | if (arguments.length === 0) |
| 51 | return this._offset; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 52 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 53 | if (off > this._screens) { |
| 54 | off = this._screens; |
| 55 | } |
| 56 | else if (off < 0) { |
| 57 | off = 0; |
| 58 | }; |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 59 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 60 | if (off === this._offset) |
| 61 | return undefined; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 62 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 63 | this._offset = off; |
| 64 | this._slider.style.top = (this._step * off) + '%'; |
| 65 | return off; |
| 66 | }, |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 67 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 68 | element : function () { |
| 69 | return this._element; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 70 | }, |
| 71 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 72 | // Initialize prefix object |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 73 | _init : function (menu) { |
| 74 | |
| 75 | this._menu = menu; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 76 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 77 | this._offset = 0; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 78 | this._event = {}; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 79 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 80 | this._element = document.createElement('div'); |
| 81 | this._element.setAttribute('class', 'ruler'); |
| 82 | |
| 83 | this._slider = this._element.appendChild( |
| 84 | document.createElement('span') |
| 85 | ); |
| 86 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 87 | this._ruler = this._element.appendChild(document.createElement('div')); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 88 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 89 | // Do not mark the menu on mousedown |
| 90 | this._ruler.addEventListener('mousedown', function (e) { |
| 91 | e.halt() |
| 92 | }, false); |
| 93 | |
| 94 | // Move the slider to the click position |
| 95 | this._ruler.addEventListener('click', this._mouseclick.bind(this), false); |
| 96 | |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 97 | this._slider.addEventListener('mousedown', this._mousedown.bind(this), false); |
| 98 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 99 | return this; |
| 100 | }, |
| 101 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 102 | _initSize : function () { |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 103 | if (this._length <= this._limit) { |
| 104 | this._element.style.display = 'none'; |
| 105 | return; |
| 106 | } |
| 107 | else { |
| 108 | this._element.style.display = 'block'; |
| 109 | }; |
| 110 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 111 | this._height = ((this._limit / this._length) * 100); |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 112 | this._screens = this._length - this._limit; |
| 113 | this._step = (100 - this._height) / this._screens; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 114 | this._slider.style.height = this._height + '%'; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 115 | }, |
| 116 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 117 | _initClientHeight : function () { |
| 118 | this._rulerHeight = this._element.clientHeight; // offsetHeight? |
| 119 | this._sliderHeight = this._slider.clientHeight; // offsetHeight? |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 120 | }, |
| 121 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 122 | _mousemove : function (e) { |
| 123 | this.movetoRel(e.clientY - this._event.init); |
| 124 | e.halt(); |
| 125 | // Support touch! |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 126 | }, |
| 127 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 128 | _mouseup : function (e) { |
| 129 | this._element.classList.remove('active'); |
| 130 | window.removeEventListener('mousemove', this._event.mov); |
| 131 | window.removeEventListener('mouseup', this._event.up); |
| 132 | this._menu.focus(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 133 | }, |
| 134 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 135 | _mousedown : function (e) { |
| 136 | // Bind drag handler |
| 137 | var ev = this._event; |
| 138 | ev.init = e.clientY - (this._step * this._offset); |
| 139 | ev.mov = this._mousemove.bind(this); |
| 140 | ev.up = this._mouseup.bind(this); |
| 141 | |
| 142 | // TODO: This may not be necessary all the time |
| 143 | this._initClientHeight(); |
| 144 | |
| 145 | this._element.classList.add('active'); |
| 146 | |
| 147 | window.addEventListener('mousemove', ev.mov); |
| 148 | window.addEventListener('mouseup', ev.up); |
| 149 | |
| 150 | e.halt(); |
| 151 | }, |
| 152 | |
| 153 | _mouseclick : function (e) { |
| 154 | this._initClientHeight(); |
| 155 | |
| 156 | this.movetoAbs( |
| 157 | e.clientY - this._ruler.getClientRects()[0].top |
| 158 | ); |
| 159 | e.halt(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 160 | } |
| 161 | }); |