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 | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 76 | var diffHeight = (this._rulerHeight - this._sliderHeight); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 77 | var relativeOffset = (relativePos / diffHeight); |
| 78 | |
| 79 | var off = this.offset(parseInt(relativeOffset * this._screens)); |
| 80 | if (off !== undefined) { |
| 81 | this._menu.screen(off); |
| 82 | }; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 83 | }, |
| 84 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 85 | /** |
| 86 | * Move the slider to an absolute position |
| 87 | * |
| 88 | * @param {number} absolute position |
| 89 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 90 | movetoAbs : function (absPos) { |
| 91 | var absOffset = (absPos / this._rulerHeight); |
| 92 | |
| 93 | var off = this.offset(parseInt(absOffset * (this._screens + 1))); |
| 94 | if (off !== undefined) { |
| 95 | this._menu.screen(off); |
| 96 | }; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 97 | }, |
| 98 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 99 | /** |
| 100 | * Screen offset of the slider |
| 101 | * |
| 102 | * @param {number} Offset position of the slider (optional) |
| 103 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 104 | offset : function (off) { |
| 105 | if (arguments.length === 0) |
| 106 | return this._offset; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 107 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 108 | // Normalize offset |
| 109 | if (off > this._screens) |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 110 | off = this._screens; |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 111 | else if (off < 0) |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 112 | off = 0; |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 113 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 114 | // Identical with old value |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 115 | if (off === this._offset) |
| 116 | return undefined; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 117 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 118 | // Set offset and move |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 119 | this._offset = off; |
| 120 | this._slider.style.top = (this._step * off) + '%'; |
| 121 | return off; |
| 122 | }, |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 123 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 124 | /** |
| 125 | * Get the associated dom element. |
| 126 | */ |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 127 | element : function () { |
| 128 | return this._element; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 129 | }, |
| 130 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 131 | /** |
| 132 | * Reinitialize the size of the slider. |
| 133 | * Necessary to call after each adjustment of the list. |
| 134 | */ |
| 135 | reInit : function () { |
| 136 | |
| 137 | var s = this._element.style; |
| 138 | |
| 139 | // Do not show the slider, in case there is nothing to scroll |
| 140 | if (this._length <= this._limit) { |
| 141 | s.display = 'none'; |
| 142 | return; |
| 143 | } |
| 144 | else { |
| 145 | s.display = 'block'; |
| 146 | }; |
| 147 | |
| 148 | this._height = ((this._limit / this._length) * 100); |
| 149 | this._screens = this._length - this._limit; |
| 150 | this._step = (100 - this._height) / this._screens; |
| 151 | this._slider.style.height = this._height + '%'; |
| 152 | }, |
| 153 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 154 | // Initialize prefix object |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 155 | _init : function (menu) { |
| 156 | |
| 157 | this._menu = menu; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 158 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 159 | this._offset = 0; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 160 | this._event = {}; |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 161 | this._active = false; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 162 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 163 | var el = this._element = document.createElement('div'); |
| 164 | el.setAttribute('class', 'ruler'); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 165 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 166 | this._slider = el.appendChild( |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 167 | document.createElement('span') |
| 168 | ); |
| 169 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 170 | this._ruler = el.appendChild(document.createElement('div')); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 171 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 172 | // Do not mark the menu on mousedown |
| 173 | this._ruler.addEventListener('mousedown', function (e) { |
| 174 | e.halt() |
| 175 | }, false); |
| 176 | |
| 177 | // Move the slider to the click position |
| 178 | this._ruler.addEventListener('click', this._mouseclick.bind(this), false); |
| 179 | |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 180 | this._slider.addEventListener('mousedown', this._mousedown.bind(this), false); |
| 181 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 182 | return this; |
| 183 | }, |
| 184 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 185 | // Reinit height based on dom position |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 186 | _initClientHeight : function () { |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 187 | this._rulerHeight = this._element.clientHeight; |
| 188 | this._sliderHeight = this._slider.clientHeight; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 189 | }, |
| 190 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 191 | // Release mousemove event |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 192 | _mousemove : function (e) { |
| 193 | this.movetoRel(e.clientY - this._event.init); |
| 194 | e.halt(); |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 195 | }, |
| 196 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 197 | // Release mouseup event |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 198 | _mouseup : function (e) { |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 199 | this.active(false); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 200 | window.removeEventListener('mousemove', this._event.mov); |
| 201 | window.removeEventListener('mouseup', this._event.up); |
| 202 | this._menu.focus(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 203 | }, |
| 204 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 205 | // Release mousedown event |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 206 | _mousedown : function (e) { |
| 207 | // Bind drag handler |
| 208 | var ev = this._event; |
| 209 | ev.init = e.clientY - (this._step * this._offset); |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 210 | ev.mov = this._mousemove.bind(this); |
| 211 | ev.up = this._mouseup.bind(this); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 212 | |
| 213 | // TODO: This may not be necessary all the time |
| 214 | this._initClientHeight(); |
| 215 | |
Akron | a92fd8d | 2016-05-24 21:13:41 +0200 | [diff] [blame] | 216 | this.active(true); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 217 | |
| 218 | window.addEventListener('mousemove', ev.mov); |
| 219 | window.addEventListener('mouseup', ev.up); |
| 220 | |
| 221 | e.halt(); |
| 222 | }, |
| 223 | |
Akron | 9c4d1ae | 2016-05-25 21:43:22 +0200 | [diff] [blame^] | 224 | // Release event to reposition slider on ruler |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 225 | _mouseclick : function (e) { |
| 226 | this._initClientHeight(); |
| 227 | |
| 228 | this.movetoAbs( |
| 229 | e.clientY - this._ruler.getClientRects()[0].top |
| 230 | ); |
| 231 | e.halt(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 232 | } |
| 233 | }); |