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