-
Notifications
You must be signed in to change notification settings - Fork 2
/
dragvalue.js
106 lines (75 loc) · 3.01 KB
/
dragvalue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*!
* dragvalue.js 0.0.3 - https://github.com/yckart/DragValue.js
* Change values by dragging it
*
* Copyright (c) 2013 Yannick Albert (http://yckart.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
* 2013/09/23
**/
(function ($, undefined) {
var defaults = {
step: 1,
min: -Infinity,
max: Infinity,
ratio: 10,
axis: 'x'
};
var on = function (elem, event, fn) {
return elem.addEventListener ? elem.addEventListener(event, fn, false) : elem.attachEvent('on' + event, fn);
};
this.DragValue = function (elem, options) {
this.elem = elem;
this.options = defaults;
if (options) for (var key in options) {
this.options[key] = options[key];
}
this.init();
};
DragValue.prototype.init = function () {
var elem = this.elem;
this.value = elem.value !== undefined ? 'value' : 'innerHTML';
this.axis = elem.getAttribute('data-axis') || this.options.axis;
this.ratio = elem.getAttribute('data-ratio') || this.options.ratio;
this.step = elem.getAttribute('step') || elem.getAttribute('data-step') || this.options.step;
this.min = elem.getAttribute('min') || elem.getAttribute('data-min') || this.options.min;
this.max = elem.getAttribute('max') || elem.getAttribute('data-max') || this.options.max;
this.min = (this.min - this.min) + this.min;
this.max = (this.max - this.max) + this.max;
this.horizontal = this.axis === 'x';
elem.className += this.horizontal ? ' w-resize' : ' n-resize';
this.handleEvents();
};
DragValue.prototype.handleEvents = function () {
var self = this;
var dragging, last, val;
var ondown = function (e) {
dragging = true;
if ( e.touches ) e = e.touches[0];
last = self.horizontal ? e.clientX : -e.clientY;
val = (self.elem[self.value] || self.min || self.max || 0) - 0;
};
var onup = function () {
dragging = false;
};
var onmove = function (e) {
if ( !dragging || (e.touches && e.touches.length > 1) ) return;
if ( e.touches ) e = e.touches[0];
var now = self.horizontal ? e.clientX : -e.clientY;
var tmp = val + Math.floor((now - last) / self.ratio) * self.step;
tmp = tmp < self.min ? self.min : tmp;
tmp = tmp > self.max ? self.max : tmp;
self.elem[self.value] = (tmp - 0).toFixed(self.step.length - 2);
};
on(self.elem, 'mousedown', ondown);
on(document, 'mouseup', onup);
on(document, 'mousemove', onmove);
on(self.elem, 'touchstart', ondown);
on(document, 'touchend', onup);
on(document, 'touchmove', onmove);
};
if ($) $.fn.dragvalue = function (options) {
return this.each(function () {
new DragValue(this, options);
});
};
}(this.jQuery || this.Zepto));