blob: 272cbd19179c16fa64fe6dcb9d2143e19b47f64a [file] [log] [blame]
Akron9905e2a2016-05-10 16:06:44 +02001define({
2
3 /**
4 * Create new prefix object.
5 */
6 create : function () {
7 return Object.create(this)._init();
8 },
9
Akron6b24b202016-05-17 23:04:36 +020010 _mousemove : function (e) {
Akron24b1eaa2016-05-18 16:00:25 +020011 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));
Akron6b24b202016-05-17 23:04:36 +020016 e.halt();
Akron24b1eaa2016-05-18 16:00:25 +020017
18 // Support touch!
Akron6b24b202016-05-17 23:04:36 +020019 },
20
21 _mouseup : function (e) {
Akron24b1eaa2016-05-18 16:00:25 +020022 this._slider.classList.remove('active');
Akron6b24b202016-05-17 23:04:36 +020023 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;
Akron24b1eaa2016-05-18 16:00:25 +020030 ev.init = e.clientY - (this._step * this._offset);
Akron6b24b202016-05-17 23:04:36 +020031 ev.mov = this._mousemove.bind(this);
32 ev.up = this._mouseup.bind(this);
33
Akron24b1eaa2016-05-18 16:00:25 +020034 this._rulerHeight = this._element.clientHeight; // offsetHeight?
35 this._sliderHeight = this._slider.clientHeight; // offsetHeight?
36
37 this._slider.classList.add('active');
Akron6b24b202016-05-17 23:04:36 +020038
39 window.addEventListener('mousemove', ev.mov);
40 window.addEventListener('mouseup', ev.up);
41
42 e.halt();
43 },
44
Akron9905e2a2016-05-10 16:06:44 +020045 // Initialize prefix object
46 _init : function () {
47
Akronf86eaea2016-05-13 18:02:27 +020048 this._offset = 0;
Akron6b24b202016-05-17 23:04:36 +020049 this._event = {};
Akronf86eaea2016-05-13 18:02:27 +020050
Akron9905e2a2016-05-10 16:06:44 +020051 this._element = document.createElement('div');
52 this._element.setAttribute('class', 'ruler');
53
54 this._slider = this._element.appendChild(
55 document.createElement('span')
56 );
57
Akron6b24b202016-05-17 23:04:36 +020058 // TODO: Support touch!
59 this._slider.addEventListener('mousedown', this._mousedown.bind(this), false);
60
Akron9905e2a2016-05-10 16:06:44 +020061 this._element.appendChild(document.createElement('div'));
Akron9905e2a2016-05-10 16:06:44 +020062 return this;
63 },
64
Akronf86eaea2016-05-13 18:02:27 +020065 _initSize : function () {
66 this._height = ((this._limit / this._length) * 100);
Akron6b24b202016-05-17 23:04:36 +020067 this._screens = this._length - this._limit;
68 this._step = (100 - this._height) / this._screens;
Akronf86eaea2016-05-13 18:02:27 +020069 },
70
Akron9905e2a2016-05-10 16:06:44 +020071 show : function (i) {
Akronf86eaea2016-05-13 18:02:27 +020072 this._slider.style.height = this._height + '%';
Akron9905e2a2016-05-10 16:06:44 +020073 },
74
75 length : function (i) {
76 this._length = i;
Akronf86eaea2016-05-13 18:02:27 +020077 this._initSize();
Akron9905e2a2016-05-10 16:06:44 +020078 },
79
80 limit : function (i) {
81 this._limit = i;
Akronf86eaea2016-05-13 18:02:27 +020082 this._initSize();
83 },
84
85 offset : function (off) {
86 if (off === undefined)
87 return this._offset;
88
Akron6b24b202016-05-17 23:04:36 +020089 if (off === this._offset || off > this._screens || off < 0)
90 return;
91
Akronf86eaea2016-05-13 18:02:27 +020092 this._offset = off;
93 this._slider.style.top = (this._step * off) + '%';
Akron9905e2a2016-05-10 16:06:44 +020094 },
95
96 element : function () {
97 return this._element;
98 }
99});