-
Notifications
You must be signed in to change notification settings - Fork 9
/
nativemultiple.jquery.js
161 lines (135 loc) · 3.85 KB
/
nativemultiple.jquery.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(function( $ ){
/**
* default values
*/
var _defaults = {
stylesheet: "",
tooltip: false,
onCreate: {},
onChange: {},
vertical: false
};
/**
* events
*/
var touchstart = 'touchstart mousedown';
var touchmove = 'touchmove mousemove';
var touchend = 'touchend mouseup';
var touch_down = true;
var inter_value = 0;
/**
* methods
*/
var methods = {
/**
* each all selectors
*/
init : function(options) {
options = $.extend(true, {}, _defaults, options);
// create wrapper
$(this).each(function() {
var $this = $(this);
var values = $this.attr("value");
var last_first = 0;
var last_second = 0;
if(values != undefined) {
values = values.split(',');
}
else {
values = ['0',$this.attr('max')];
}
if(values[1] == undefined) {
values[1] = values[0];
}
last_first = values[0];
last_second = values[1];
var $wrapper = $("<div></div>").addClass(options.stylesheet).addClass('nativeMultiple');
$("<div class='nativeMultiple-one'><div></div></div>").appendTo($wrapper);
$this.wrap($wrapper);
$wrapper = $this.parents(".nativeMultiple");
var $one = $this.parents(".nativeMultiple-one");
var $two = $("<div class='nativeMultiple-two'></div>").appendTo($wrapper);
var $clone_input = $this.clone().removeAttr("id").removeAttr("name").appendTo($two).wrap("<div></div>"); //
var max_width = $wrapper.width();
var one_step = (max_width - 60) / parseFloat($this.attr('max'));
var temp_input = 0;
var temp_clone_input = 0;
$wrapper.find("div > div").width(max_width);
/**
* add events
*/
// input 1
$this.on(touchstart, function() {
touch_down = true;
}).on(touchmove, function(e) {
if(touch_down) {
inter_value = this.value * one_step;
temp_clone_input = $clone_input.val();
if(parseFloat(this.value) > parseFloat(temp_clone_input)) {
$clone_input.val(this.value);
$one.width(inter_value);
last_second = this.value;
}
// event
if(last_first != this.value) {
last_first = this.value;
options.onSlide(this.value, temp_clone_input);
}
}
}).on(touchend, function() {
inter_value = this.value * one_step;
touch_down = false;
last_first = this.value;
//event
options.onChange(this.value, $clone_input.val());
});
// input 2
$clone_input.on(touchstart, function() {
console.log("strt");
touch_down = true;
}).on(touchmove, function(e) {
if(touch_down) {
inter_value = this.value * one_step;
temp_input = $this.val();
$one.width(inter_value);
if(parseFloat(this.value) < parseFloat(temp_input)) {
$this.val(this.value);
}
// event
if(last_second != this.value) {
last_second = this.value;
options.onSlide(temp_input, this.value);
}
}
}).on(touchend, function() {
inter_value = this.value * one_step;
touch_down = false;
$one.width(inter_value);
//event
options.onChange($this.val(), this.value);
});
// change
$clone_input.val(values[1]);
$this.val(values[0]);
$one.width(values[1] * one_step);
touch_down = false;
});
options.onCreate();
return this;
}
//disable: function {},
//enable: function {}
};
$.fn.nativeMultiple = function(opts) {
if ( methods[opts] ) {
return methods[ opts ].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
else if (typeof opts === 'object' || ! opts) {
// Default to "init"
return methods.init.apply( this, arguments );
}
else {
$.error( 'Method ' + opts + ' does not exist on jQuery.nativeMultiple' );
}
};
})( jQuery );