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