-
Notifications
You must be signed in to change notification settings - Fork 17
/
jquery.ui.timeslider.js
160 lines (130 loc) · 4.93 KB
/
jquery.ui.timeslider.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
(function($) {
$.widget("ui.timeslider", {
getTime: function(value, format){
if (format == 'undefined')
format = 12;
var time = null,
minutes,
hours;
hours = Math.floor(value / 60);
minutes = value - (hours * 60);
if (format == 12)
{
if (hours === 0) {
hours = 12;
}
if (hours > 12) {
hours = hours - 12;
time = "PM";
}
else {
time = "AM";
}
if (minutes < 10) {
minutes = "0" + minutes;
}
return hours + ":" + minutes + " " + time;
}
else
{
if (minutes < 10) {
minutes = "0" + minutes;
}
return hours + ":" + minutes;
}
},
_slideTime: function (event, ui){
var that = $(this),
startTime = null,
endTime = null;
if(that.slider( "option", "range" )){
startTime = that.timeslider('getTime',that.slider("values", 0), that.timeslider('option', 'clockFormat'));
endTime = that.timeslider('getTime', that.slider("values", 1), that.timeslider('option', 'clockFormat'));
that.timeslider('option', 'timeDisplay').text(startTime + ' - ' + endTime);
if(that.timeslider('option', 'addInputs'))
{
that.timeslider('option', 'inputsContainer').find('input.start_time').val(startTime);
that.timeslider('option', 'inputsContainer').find('input.end_time').val(endTime);
}
}
else {
startTime = that.timeslider('getTime', that.slider("value"), that.timeslider('option', 'clockFormat'));
that.timeslider('option', 'timeDisplay').text(startTime);
if(that.timeslider('option', 'addInputs'))
{
that.timeslider('option', 'inputsContainer').find('input.start_time').val(startTime);
}
}
},
_checkMax: function(event, ui) {
var that = $(this);
if(that.slider( "option", "range" )){
var div = that.find('div'),
size = that.slider("values", 1) - that.slider("values", 0);
if( size >= 1435) {
div.addClass("ui-state-error");
that.timeslider('option', 'submitButton').attr("disabled","disabled").addClass("ui-state-disabled");
that.timeslider('option', 'errorMessage').text("Cannot be more than 24 hours");
}
else {
div.removeClass("ui-state-error");
that.timeslider('option', 'submitButton').attr("disabled",null).removeClass("ui-state-disabled");
that.timeslider('option', 'errorMessage').text("");
}
}
},
options: {
sliderOptions: {},
errorMessage: null,
timeDisplay: null,
submitButton: null,
clickSubmit: null,
inputsContainer: '.timesliderInputsContainer',
addInputs: false,
clockFormat: 12,
startTime: null,
endTime: null
},
_create: function() {
var that = this,
o = that.options,
el = that.element;
o.sliderOptions.slide = that._slideTime;
o.sliderOptions.change = that._checkMax;
o.sliderOptions.stop = that._slideTime;
o.errorMessage = $(o.errorMessage);
o.timeDisplay = $(o.timeDisplay);
o.submitButton = $(o.submitButton).click(o.clickSubmit);
if(o.addInputs)
{
var container = o.inputsContainer;
if(container.indexOf(".") != -1)
var inputsContainer_html = '<div class="'+container.split(".").join("")+'"></div>';
else
var inputsContainer_html = '<div id="'+container.split("#").join("")+'"></div>';
if (!$(o.inputsContainer).size())
{
el.append(inputsContainer_html);
}
if (!$("input.start_time",o.inputsContainer).size())
$(o.inputsContainer).append('<input type="hidden" name="start_time" value="" class="start_time" />');
if (!$("input.end_time",o.inputsContainer).size())
$(o.inputsContainer).append('<input type="hidden" name="end_time" value="" class="end_time" />');
o.inputsContainer = $(o.inputsContainer);
}
if (o.startTime != null && o.endTime != null)
{
var time_parts = o.startTime.split(":");
var timeslider_start_time = ((time_parts[0]) * 60) + time_parts[1]*1;
time_parts = o.endTime.split(":");
var timeslider_end_time = ((time_parts[0]) * 60) + time_parts[1]*1;
o.sliderOptions.values = [timeslider_start_time, timeslider_end_time];
}
el.slider(o.sliderOptions);
that._slideTime.call(el);
},
_destroy: function() {
this.element.remove();
}
});
})(jQuery);