-
Notifications
You must be signed in to change notification settings - Fork 147
/
jQDateRangeSliderHandle.js
210 lines (165 loc) · 4.56 KB
/
jQDateRangeSliderHandle.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* jQRangeSlider
* A javascript slider selector that supports dates
*
* Copyright (C) Guillaume Gautreau 2012
* Dual licensed under the MIT or GPL Version 2 licenses.
*
*/
(function($, undefined){
"use strict";
$.widget("ui.dateRangeSliderHandle", $.ui.rangeSliderHandle, {
_steps: false,
_boundsValues: {},
_create: function(){
this._createBoundsValues();
this._super();
},
_getValueForPosition: function(position){
var raw = this._getRawValueForPositionAndBounds(position, this.options.bounds.min.valueOf(), this.options.bounds.max.valueOf());
return this._constraintValue(new Date(raw));
},
_setOption: function(key, value){
if (key === "step"){
this.options.step = value;
this._createSteps();
this.update();
return;
}
this._super(key, value);
if (key === "bounds"){
this._createBoundsValues();
}
},
_createBoundsValues: function(){
this._boundsValues = {
min: this.options.bounds.min.valueOf(),
max: this.options.bounds.max.valueOf()
};
},
_bounds: function(){
return this._boundsValues;
},
_createSteps: function(){
if (this.options.step === false || !this._isValidStep()){
this._steps = false;
return;
}
var minDate = new Date(this.options.bounds.min.valueOf()),
maxDate = new Date(this.options.bounds.max.valueOf()),
stepDate = minDate,
i = 0,
previous = new Date();
this._steps = [];
while (stepDate <= maxDate && (i === 1 || previous.valueOf() !== stepDate.valueOf())){
previous = stepDate;
this._steps.push(stepDate.valueOf());
stepDate = this._addStep(minDate, i, this.options.step);
i++;
}
if (previous.valueOf() === stepDate.valueOf()){
this._steps = false;
}
},
_isValidStep: function(){
return typeof this.options.step === "object";
},
_addStep: function(reference, factor, step){
var result = new Date(reference.valueOf());
result = this._addThing(result, "FullYear", factor, step.years);
result = this._addThing(result, "Month", factor, step.months);
result = this._addThing(result, "Date", factor, step.weeks * 7);
result = this._addThing(result, "Date", factor, step.days);
result = this._addThing(result, "Hours", factor, step.hours);
result = this._addThing(result, "Minutes", factor, step.minutes);
result = this._addThing(result, "Seconds", factor, step.seconds);
return result;
},
_addThing: function(date, thing, factor, base){
if (factor === 0 || (base || 0) === 0){
return date;
}
date["set" + thing](
date["get" + thing]() + factor * (base || 0)
);
return date;
},
_round: function(value){
if (this._steps === false){
return value;
}
var max = this.options.bounds.max.valueOf(),
min = this.options.bounds.min.valueOf(),
ratio = Math.max(0, (value - min) / (max - min)),
index = Math.floor(this._steps.length * ratio),
before, after;
while (this._steps[index] > value){
index--;
}
while (index + 1 < this._steps.length && this._steps[index + 1] <= value){
index++;
}
if (index >= this._steps.length - 1){
return this._steps[this._steps.length - 1];
} else if (index === 0){
return this._steps[0];
}
before = this._steps[index];
after = this._steps[index + 1];
if (value - before < after - value){
return before;
}
return after;
},
update: function(){
this._createBoundsValues();
this._createSteps();
this._super();
},
add: function(date, step){
return this._addStep(new Date(date), 1, step).valueOf();
},
substract: function(date, step){
return this._addStep(new Date(date), -1, step).valueOf();
},
stepsBetween: function(date1, date2){
if (this.options.step === false){
return date2 - date1;
}
var min = Math.min(date1, date2),
max = Math.max(date1, date2),
steps = 0,
negative = false,
negativeResult = date1 > date2;
if (this.add(min, this.options.step) - min < 0){
negative = true;
}
while (min < max){
if (negative){
max = this.add(max, this.options.step);
}else{
min = this.add(min, this.options.step);
}
steps++;
}
return negativeResult ? -steps : steps;
},
multiplyStep: function(step, factor){
var result = {};
for (var name in step){
if (step.hasOwnProperty(name)){
result[name] = step[name] * factor;
}
}
return result;
},
stepRatio: function(){
if (this.options.step === false){
return 1;
}else{
var steps = this._steps.length;
return this.cache.parent.width / steps;
}
}
});
}(jQuery));