forked from jhurt/timepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimepicker.js
121 lines (109 loc) · 3.91 KB
/
timepicker.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
var Template = {
renderTemplate: function(selector, data, callback) {
if (callback != null) {
callback(Mustache.render($(selector).html(), data));
}
}
};
function Timepicker(input) {
this.input = input;
this.hours = null;
this.minute = null;
this.amPm = null;
this.timepickerSelector = null;
this.init();
}
Timepicker.prototype.display = function() {
if (this.hours != null && this.minutes != null && this.amPm != null) {
this.input.val(this.hours + ':' + this.minutes + ' ' + this.amPm);
}
};
Timepicker.prototype.hide = function() {
if (this.hours != null && this.minutes != null && this.amPm != null) {
$(this.timepickerSelector).hide();
}
};
Timepicker.prototype.wireRowHover = function(selector, property, lowerRows) {
var self = this;
$(selector).hover(
function () {
//hover on
$(selector).each(function(i, li) {
$(li).find('span').removeClass('ui-state-hover');
});
var span = $(this).find('span');
span.addClass("ui-state-hover");
self[property] = span.text();
self.display();
for (var i = 0; i < lowerRows.length; i++) {
var row = lowerRows[i];
var left = $(this).position().left + $(this).parent().position().left;
$(row).css('left', left + 'px');
if (i == 0) {
$(row).show();
}
}
},
function () {
//hover off, no op
}
);
};
Timepicker.prototype.setValue = function (selector, value) {
if (value == '0') {
value = '00';
}
$(selector).each(function(i, li) {
var span = $(li).find('span');
if (value == span.text()) {
span.addClass('ui-state-hover');
$(li).parent().show();
}
});
};
//reset to the value in the input
Timepicker.prototype.reset = function() {
var self = this;
var value = self.input.val();
var tmp = value.split(' ');
var hoursMinutes = tmp[0].split(':');
this.hours = hoursMinutes[0];
this.minutes = hoursMinutes[1];
this.amPm = tmp[1];
this.setValue(self.timepickerSelector + ' .hours li', this.hours);
this.setValue(self.timepickerSelector + ' .minutes li', this.minutes);
this.setValue(self.timepickerSelector + ' .ampm li', this.amPm);
};
Timepicker.prototype.init = function() {
var self = this;
var timepickerName = this.input.attr('name') + 'Timepicker';
self.timepickerSelector = '#' + timepickerName;
Template.renderTemplate('#timepicker_template', {name:timepickerName}, function(template) {
//add the timepicker div to the dom
self.input.after(template);
//wire up the input click event
self.input.click(function(e) {
$(self.timepickerSelector).show();
});
$('body').click(function(e) {
if (!($(e.target).hasClass('timepicker')) && !($(e.target).hasClass('timeBox'))) {
$('.ui-timepickr').hide();
}
});
//hover events for hours,minutes,ampm
self.wireRowHover(self.timepickerSelector + ' .hours li', 'hours', [self.timepickerSelector + ' .minutes', self.timepickerSelector + ' .ampm']);
self.wireRowHover(self.timepickerSelector + ' .minutes li', 'minutes', [self.timepickerSelector + ' .ampm']);
self.wireRowHover(self.timepickerSelector + ' .ampm li', 'amPm', []);
//click events for each hours,minutes,ampm
var liClick = function (e) {
self.hide();
};
$(self.timepickerSelector + ' .hours li').click(liClick);
$(self.timepickerSelector + ' .minutes li').click(liClick);
$(self.timepickerSelector + ' .ampm li').click(liClick);
var value = self.input.val();
if (value != null && value != '') {
self.reset();
}
});
};