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