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