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