forked from ghusse/jQRangeSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQDateRangeSlider.js
137 lines (108 loc) · 3.88 KB
/
jQDateRangeSlider.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
/**
* 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.dateRangeSlider", $.ui.rangeSlider, {
options: {
bounds: {min: new Date(2010,0,1).valueOf(), max: new Date(2012,0,1).valueOf()},
defaultValues: {min: new Date(2010,1,11).valueOf(), max: new Date(2011,1,11).valueOf()}
},
_create: function(){
$.ui.rangeSlider.prototype._create.apply(this);
this.element.addClass("ui-dateRangeSlider");
},
destroy: function(){
this.element.removeClass("ui-dateRangeSlider");
$.ui.rangeSlider.prototype.destroy.apply(this);
},
_setDefaultValues: function(){
this._values = {
min: this.options.defaultValues.min.valueOf(),
max: this.options.defaultValues.max.valueOf()
};
},
_setRulerParameters: function(){
this.ruler.ruler({
min: new Date(this.options.bounds.min),
max: new Date(this.options.bounds.max),
scales: this.options.scales
});
},
_setOption: function(key, value){
if ((key === "defaultValues" || key === "bounds") && typeof value !== "undefined" && value !== null && this._isValidDate(value.min) && this._isValidDate(value.max)){
$.ui.rangeSlider.prototype._setOption.apply(this, [key, {min:value.min.valueOf(), max:value.max.valueOf()}]);
}else{
$.ui.rangeSlider.prototype._setOption.apply(this, this._toArray(arguments));
}
},
_handleType: function(){
return "dateRangeSliderHandle";
},
option: function(key){
if (key === "bounds" || key === "defaultValues"){
var result = $.ui.rangeSlider.prototype.option.apply(this, arguments);
return {min:new Date(result.min), max:new Date(result.max)};
}
return $.ui.rangeSlider.prototype.option.apply(this, this._toArray(arguments));
},
_defaultFormatter: function(value){
var month = value.getMonth() + 1,
day = value.getDate();
return "" + value.getFullYear() + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
},
_getFormatter: function(){
var formatter = this.options.formatter;
if (this.options.formatter === false || this.options.formatter === null){
formatter = this._defaultFormatter;
}
return (function(formatter){
return function(value){
return formatter(new Date(value));
}
}(formatter));
},
values: function(min, max){
var values = null;
if (this._isValidDate(min) && this._isValidDate(max))
{
values = $.ui.rangeSlider.prototype.values.apply(this, [min.valueOf(), max.valueOf()]);
}else{
values = $.ui.rangeSlider.prototype.values.apply(this, this._toArray(arguments));
}
return {min: new Date(values.min), max: new Date(values.max)};
},
min: function(min){
if (this._isValidDate(min)){
return new Date($.ui.rangeSlider.prototype.min.apply(this, [min.valueOf()]));
}
return new Date($.ui.rangeSlider.prototype.min.apply(this));
},
max: function(max){
if (this._isValidDate(max)){
return new Date($.ui.rangeSlider.prototype.max.apply(this, [max.valueOf()]));
}
return new Date($.ui.rangeSlider.prototype.max.apply(this));
},
bounds: function(min, max){
var result;
if (this._isValidDate(min) && this._isValidDate(max)) {
result = $.ui.rangeSlider.prototype.bounds.apply(this, [min.valueOf(), max.valueOf()]);
} else {
result = $.ui.rangeSlider.prototype.bounds.apply(this, this._toArray(arguments));
}
return {min: new Date(result.min), max: new Date(result.max)};
},
_isValidDate: function(value){
return typeof value !== "undefined" && value instanceof Date;
},
_toArray: function(argsObject){
return Array.prototype.slice.call(argsObject);
}
});
}(jQuery));