blob: c35f2843dbdb4e17ee53f2f941b2bfbce94d2e37 [file] [log] [blame]
Akron9905e2a2016-05-10 16:06:44 +02001define({
2
3 /**
4 * Create new prefix object.
5 */
Akron47c086c2016-05-18 21:22:06 +02006 create : function (menu) {
7 return Object.create(this)._init(menu);
Akron9905e2a2016-05-10 16:06:44 +02008 },
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;
Akron24b1eaa2016-05-18 16:00:25 +020012 var diffHeight = (this._rulerHeight - this._sliderHeight);
Akron47c086c2016-05-18 21:22:06 +020013 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
Akron6b24b202016-05-17 23:04:36 +020020 e.halt();
Akron24b1eaa2016-05-18 16:00:25 +020021
22 // Support touch!
Akron6b24b202016-05-17 23:04:36 +020023 },
24
25 _mouseup : function (e) {
Akron28b56972016-05-18 17:55:20 +020026 this._element.classList.remove('active');
Akron6b24b202016-05-17 23:04:36 +020027 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;
Akron24b1eaa2016-05-18 16:00:25 +020034 ev.init = e.clientY - (this._step * this._offset);
Akron6b24b202016-05-17 23:04:36 +020035 ev.mov = this._mousemove.bind(this);
36 ev.up = this._mouseup.bind(this);
37
Akron24b1eaa2016-05-18 16:00:25 +020038 this._rulerHeight = this._element.clientHeight; // offsetHeight?
39 this._sliderHeight = this._slider.clientHeight; // offsetHeight?
40
Akron28b56972016-05-18 17:55:20 +020041 this._element.classList.add('active');
Akron6b24b202016-05-17 23:04:36 +020042
43 window.addEventListener('mousemove', ev.mov);
44 window.addEventListener('mouseup', ev.up);
45
46 e.halt();
47 },
48
Akron9905e2a2016-05-10 16:06:44 +020049 // Initialize prefix object
Akron47c086c2016-05-18 21:22:06 +020050 _init : function (menu) {
51
52 this._menu = menu;
Akron9905e2a2016-05-10 16:06:44 +020053
Akronf86eaea2016-05-13 18:02:27 +020054 this._offset = 0;
Akron6b24b202016-05-17 23:04:36 +020055 this._event = {};
Akronf86eaea2016-05-13 18:02:27 +020056
Akron9905e2a2016-05-10 16:06:44 +020057 this._element = document.createElement('div');
58 this._element.setAttribute('class', 'ruler');
59
60 this._slider = this._element.appendChild(
61 document.createElement('span')
62 );
63
Akron47c086c2016-05-18 21:22:06 +020064 this._element.appendChild(document.createElement('div'))
65 // Do not mark the menu on mousedown
66 .addEventListener('mousedown', function (e) {
67 e.halt()
68 });
69
Akron6b24b202016-05-17 23:04:36 +020070 // TODO: Support touch!
71 this._slider.addEventListener('mousedown', this._mousedown.bind(this), false);
72
Akron9905e2a2016-05-10 16:06:44 +020073 return this;
74 },
75
Akronf86eaea2016-05-13 18:02:27 +020076 _initSize : function () {
77 this._height = ((this._limit / this._length) * 100);
Akron6b24b202016-05-17 23:04:36 +020078 this._screens = this._length - this._limit;
79 this._step = (100 - this._height) / this._screens;
Akronf86eaea2016-05-13 18:02:27 +020080 },
81
Akron9905e2a2016-05-10 16:06:44 +020082 show : function (i) {
Akronf86eaea2016-05-13 18:02:27 +020083 this._slider.style.height = this._height + '%';
Akron9905e2a2016-05-10 16:06:44 +020084 },
85
86 length : function (i) {
87 this._length = i;
Akronf86eaea2016-05-13 18:02:27 +020088 this._initSize();
Akron9905e2a2016-05-10 16:06:44 +020089 },
90
91 limit : function (i) {
92 this._limit = i;
Akronf86eaea2016-05-13 18:02:27 +020093 this._initSize();
94 },
95
96 offset : function (off) {
Akron47c086c2016-05-18 21:22:06 +020097 if (arguments.length === 0)
Akronf86eaea2016-05-13 18:02:27 +020098 return this._offset;
99
Akron6b24b202016-05-17 23:04:36 +0200100 if (off === this._offset || off > this._screens || off < 0)
Akron47c086c2016-05-18 21:22:06 +0200101 return undefined;
Akron6b24b202016-05-17 23:04:36 +0200102
Akronf86eaea2016-05-13 18:02:27 +0200103 this._offset = off;
104 this._slider.style.top = (this._step * off) + '%';
Akron47c086c2016-05-18 21:22:06 +0200105 return off;
Akron9905e2a2016-05-10 16:06:44 +0200106 },
107
108 element : function () {
109 return this._element;
110 }
111});