Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Create slider for menus. |
| 3 | * The slider will only be used by mouse - touch support |
| 4 | * shouldn't be necessary, as the menu can be scrolled using touch. |
| 5 | * |
| 6 | * @author Nils Diewald |
| 7 | */ |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 8 | define({ |
| 9 | |
| 10 | /** |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 11 | * Create new slider for Menu. |
| 12 | * @this {Slider} |
| 13 | * @constructor |
| 14 | * @param {Menu} menu object |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 15 | */ |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 16 | create : function (menu) { |
| 17 | return Object.create(this)._init(menu); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 18 | }, |
| 19 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 20 | /** |
| 21 | * Length attribute of the slider |
| 22 | * (as number of items). |
| 23 | * |
| 24 | * @param {number} Number of items (optional) |
| 25 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 26 | length : function (i) { |
| 27 | if (arguments.length === 0) |
| 28 | return this._length; |
| 29 | if (i == this._length) |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 30 | return this; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 31 | this._length = i; |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 32 | return this; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 33 | }, |
| 34 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 35 | /** |
| 36 | * Limit of items per screen. |
| 37 | * |
| 38 | * @param {number} Number of items per screen (optional) |
| 39 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 40 | limit : function (i) { |
| 41 | if (arguments.length === 0) |
| 42 | return this._limit; |
| 43 | if (i == this._limit) |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 44 | return this; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 45 | this._limit = i; |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 46 | return this; |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 47 | }, |
| 48 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 49 | /** |
| 50 | * Is the slider active or not. |
| 51 | * |
| 52 | * @param {bool} true or false (optional) |
| 53 | */ |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 54 | active : function (bool) { |
| 55 | if (arguments.length === 1) { |
| 56 | if (bool) { |
| 57 | if (!this._active) { |
| 58 | this._element.classList.add('active'); |
| 59 | this._active = true; |
| 60 | }; |
| 61 | } |
| 62 | else if (this._active) { |
| 63 | this._element.classList.remove('active'); |
| 64 | this._active = false; |
| 65 | } |
| 66 | }; |
| 67 | return this._active; |
| 68 | }, |
| 69 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 70 | /** |
| 71 | * Move the slider to a relative position |
| 72 | * |
| 73 | * @param {number} relative position |
| 74 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 75 | movetoRel : function (relativePos) { |
Akron | 71b91e4 | 2016-06-01 22:12:43 +0200 | [diff] [blame] | 76 | |
| 77 | // This is important to find the correct percentage! |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 78 | var diffHeight = (this._rulerHeight - this._sliderHeight); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 79 | var relativeOffset = (relativePos / diffHeight); |
| 80 | |
Akron | 71b91e4 | 2016-06-01 22:12:43 +0200 | [diff] [blame] | 81 | // Offset is a value 0 to this._screens |
| 82 | var off = this.offset( |
| 83 | parseInt(relativeOffset * this._screens) + this._event.initOffset |
| 84 | ); |
| 85 | |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 86 | if (off !== undefined) { |
| 87 | this._menu.screen(off); |
| 88 | }; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 89 | }, |
| 90 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 91 | /** |
| 92 | * Move the slider to an absolute position |
| 93 | * |
| 94 | * @param {number} absolute position |
| 95 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 96 | movetoAbs : function (absPos) { |
| 97 | var absOffset = (absPos / this._rulerHeight); |
| 98 | |
| 99 | var off = this.offset(parseInt(absOffset * (this._screens + 1))); |
| 100 | if (off !== undefined) { |
| 101 | this._menu.screen(off); |
| 102 | }; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 103 | }, |
| 104 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 105 | /** |
| 106 | * Screen offset of the slider |
| 107 | * |
| 108 | * @param {number} Offset position of the slider (optional) |
| 109 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 110 | offset : function (off) { |
| 111 | if (arguments.length === 0) |
| 112 | return this._offset; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 113 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 114 | // Normalize offset |
| 115 | if (off > this._screens) |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 116 | off = this._screens; |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 117 | else if (off < 0) |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 118 | off = 0; |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 119 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 120 | // Identical with old value |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 121 | if (off === this._offset) |
| 122 | return undefined; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 123 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 124 | // Set offset and move |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 125 | this._offset = off; |
| 126 | this._slider.style.top = (this._step * off) + '%'; |
| 127 | return off; |
| 128 | }, |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 129 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 130 | /** |
| 131 | * Get the associated dom element. |
| 132 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 133 | element : function () { |
| 134 | return this._element; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 135 | }, |
| 136 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 137 | /** |
| 138 | * Reinitialize the size of the slider. |
| 139 | * Necessary to call after each adjustment of the list. |
| 140 | */ |
| 141 | reInit : function () { |
| 142 | |
| 143 | var s = this._element.style; |
| 144 | |
| 145 | // Do not show the slider, in case there is nothing to scroll |
| 146 | if (this._length <= this._limit) { |
| 147 | s.display = 'none'; |
| 148 | return; |
| 149 | } |
| 150 | else { |
| 151 | s.display = 'block'; |
| 152 | }; |
| 153 | |
| 154 | this._height = ((this._limit / this._length) * 100); |
| 155 | this._screens = this._length - this._limit; |
| 156 | this._step = (100 - this._height) / this._screens; |
| 157 | this._slider.style.height = this._height + '%'; |
| 158 | }, |
| 159 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 160 | // Initialize prefix object |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 161 | _init : function (menu) { |
| 162 | |
| 163 | this._menu = menu; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 164 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 165 | this._offset = 0; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 166 | this._event = {}; |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 167 | this._active = false; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 168 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 169 | var el = this._element = document.createElement('div'); |
| 170 | el.setAttribute('class', 'ruler'); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 171 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 172 | this._slider = el.appendChild( |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 173 | document.createElement('span') |
| 174 | ); |
| 175 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 176 | this._ruler = el.appendChild(document.createElement('div')); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 177 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 178 | // Do not mark the menu on mousedown |
| 179 | this._ruler.addEventListener('mousedown', function (e) { |
| 180 | e.halt() |
| 181 | }, false); |
| 182 | |
| 183 | // Move the slider to the click position |
| 184 | this._ruler.addEventListener('click', this._mouseclick.bind(this), false); |
| 185 | |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 186 | this._slider.addEventListener('mousedown', this._mousedown.bind(this), false); |
| 187 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 188 | return this; |
| 189 | }, |
| 190 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 191 | // Reinit height based on dom position |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 192 | _initClientHeight : function () { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 193 | this._rulerHeight = this._element.clientHeight; |
| 194 | this._sliderHeight = this._slider.clientHeight; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 195 | }, |
| 196 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 197 | // Release mousemove event |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 198 | _mousemove : function (e) { |
| 199 | this.movetoRel(e.clientY - this._event.init); |
| 200 | e.halt(); |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 201 | }, |
| 202 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 203 | // Release mouseup event |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 204 | _mouseup : function (e) { |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 205 | this.active(false); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 206 | window.removeEventListener('mousemove', this._event.mov); |
| 207 | window.removeEventListener('mouseup', this._event.up); |
| 208 | this._menu.focus(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 209 | }, |
| 210 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 211 | // Release mousedown event |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 212 | _mousedown : function (e) { |
| 213 | // Bind drag handler |
| 214 | var ev = this._event; |
Akron | 71b91e4 | 2016-06-01 22:12:43 +0200 | [diff] [blame] | 215 | |
| 216 | // _step * _offset is the distance of the ruler to the top |
| 217 | ev.init = e.clientY; |
| 218 | ev.initOffset = this._offset; |
| 219 | // By substracting that, it is like initializing to the first point |
| 220 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 221 | ev.mov = this._mousemove.bind(this); |
| 222 | ev.up = this._mouseup.bind(this); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 223 | |
| 224 | // TODO: This may not be necessary all the time |
| 225 | this._initClientHeight(); |
| 226 | |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 227 | this.active(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 228 | |
| 229 | window.addEventListener('mousemove', ev.mov); |
| 230 | window.addEventListener('mouseup', ev.up); |
| 231 | |
| 232 | e.halt(); |
| 233 | }, |
| 234 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame] | 235 | // Release event to reposition slider on ruler |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 236 | _mouseclick : function (e) { |
| 237 | this._initClientHeight(); |
| 238 | |
| 239 | this.movetoAbs( |
| 240 | e.clientY - this._ruler.getClientRects()[0].top |
| 241 | ); |
| 242 | e.halt(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 243 | } |
| 244 | }); |