Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 1 | define({ |
| 2 | |
| 3 | /** |
| 4 | * Create new prefix object. |
| 5 | */ |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 6 | create : function (menu) { |
| 7 | return Object.create(this)._init(menu); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 8 | }, |
| 9 | |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 10 | _mousemove : function (e) { |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 11 | var relativePos = e.clientY - this._event.init; |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 12 | var diffHeight = (this._rulerHeight - this._sliderHeight); |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 13 | var relativeOffset = (relativePos / diffHeight); |
| 14 | |
| 15 | var off = this.offset(parseInt(relativeOffset * this._screens)); |
| 16 | if (off !== undefined) { |
| 17 | this._menu.screen(off); |
| 18 | }; |
| 19 | |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 20 | e.halt(); |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 21 | |
| 22 | // Support touch! |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 23 | }, |
| 24 | |
| 25 | _mouseup : function (e) { |
Akron | 28b5697 | 2016-05-18 17:55:20 +0200 | [diff] [blame] | 26 | this._element.classList.remove('active'); |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 27 | window.removeEventListener('mousemove', this._event.mov); |
| 28 | window.removeEventListener('mouseup', this._event.up); |
| 29 | }, |
| 30 | |
| 31 | _mousedown : function (e) { |
| 32 | // Bind drag handler |
| 33 | var ev = this._event; |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 34 | ev.init = e.clientY - (this._step * this._offset); |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 35 | ev.mov = this._mousemove.bind(this); |
| 36 | ev.up = this._mouseup.bind(this); |
| 37 | |
Akron | 24b1eaa | 2016-05-18 16:00:25 +0200 | [diff] [blame] | 38 | this._rulerHeight = this._element.clientHeight; // offsetHeight? |
| 39 | this._sliderHeight = this._slider.clientHeight; // offsetHeight? |
| 40 | |
Akron | 28b5697 | 2016-05-18 17:55:20 +0200 | [diff] [blame] | 41 | this._element.classList.add('active'); |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 42 | |
| 43 | window.addEventListener('mousemove', ev.mov); |
| 44 | window.addEventListener('mouseup', ev.up); |
| 45 | |
| 46 | e.halt(); |
| 47 | }, |
| 48 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 49 | // Initialize prefix object |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 50 | _init : function (menu) { |
| 51 | |
| 52 | this._menu = menu; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 53 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 54 | this._offset = 0; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 55 | this._event = {}; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 56 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 57 | this._element = document.createElement('div'); |
| 58 | this._element.setAttribute('class', 'ruler'); |
| 59 | |
| 60 | this._slider = this._element.appendChild( |
| 61 | document.createElement('span') |
| 62 | ); |
| 63 | |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 64 | this._element.appendChild(document.createElement('div')) |
| 65 | // Do not mark the menu on mousedown |
| 66 | .addEventListener('mousedown', function (e) { |
| 67 | e.halt() |
| 68 | }); |
| 69 | |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 70 | // TODO: Support touch! |
| 71 | this._slider.addEventListener('mousedown', this._mousedown.bind(this), false); |
| 72 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 73 | return this; |
| 74 | }, |
| 75 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 76 | _initSize : function () { |
| 77 | this._height = ((this._limit / this._length) * 100); |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 78 | this._screens = this._length - this._limit; |
| 79 | this._step = (100 - this._height) / this._screens; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 80 | }, |
| 81 | |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 82 | show : function (i) { |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 83 | this._slider.style.height = this._height + '%'; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 84 | }, |
| 85 | |
| 86 | length : function (i) { |
| 87 | this._length = i; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 88 | this._initSize(); |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 89 | }, |
| 90 | |
| 91 | limit : function (i) { |
| 92 | this._limit = i; |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 93 | this._initSize(); |
| 94 | }, |
| 95 | |
| 96 | offset : function (off) { |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 97 | if (arguments.length === 0) |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 98 | return this._offset; |
| 99 | |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 100 | if (off === this._offset || off > this._screens || off < 0) |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 101 | return undefined; |
Akron | 6b24b20 | 2016-05-17 23:04:36 +0200 | [diff] [blame] | 102 | |
Akron | f86eaea | 2016-05-13 18:02:27 +0200 | [diff] [blame] | 103 | this._offset = off; |
| 104 | this._slider.style.top = (this._step * off) + '%'; |
Akron | 47c086c | 2016-05-18 21:22:06 +0200 | [diff] [blame] | 105 | return off; |
Akron | 9905e2a | 2016-05-10 16:06:44 +0200 | [diff] [blame] | 106 | }, |
| 107 | |
| 108 | element : function () { |
| 109 | return this._element; |
| 110 | } |
| 111 | }); |