-
Notifications
You must be signed in to change notification settings - Fork 37
/
jquery.businessHours.js
149 lines (125 loc) · 5.93 KB
/
jquery.businessHours.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
/**
jquery.businessHours v1.0.1
https://github.com/gEndelf/jquery.businessHours
requirements:
- jQuery 1.7+
recommended time-picker:
- jquery-timepicker 1.2.7+ // https://github.com/jonthornton/jquery-timepicker
**/
(function($) {
$.fn.businessHours = function(opts) {
var defaults = {
preInit: function() {
},
postInit: function() {
},
inputDisabled: false,
checkedColorClass: "WorkingDayState",
uncheckedColorClass: "RestDayState",
colorBoxValContainerClass: "colorBoxContainer",
weekdays: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
operationTime: [
{},
{},
{},
{},
{},
{isActive: false},
{isActive: false}
],
defaultOperationTimeFrom: '9:00',
defaultOperationTimeTill: '18:00',
defaultActive: true,
//labelOn: "Working day",
//labelOff: "Day off",
//labelTimeFrom: "from:",
//labelTimeTill: "till:",
containerTmpl: '<div class="clean"/>',
dayTmpl: '<div class="dayContainer">' +
'<div data-original-title="" class="colorBox"><input type="checkbox" class="invisible operationState"/></div>' +
'<div class="weekday"></div>' +
'<div class="operationDayTimeContainer">' +
'<div class="operationTime"><input type="text" name="startTime" class="mini-time operationTimeFrom" value=""/></div>' +
'<div class="operationTime"><input type="text" name="endTime" class="mini-time operationTimeTill" value=""/></div>' +
'</div></div>'
};
var container = $(this);
function initTimeBox(timeBoxSelector, time, isInputDisabled) {
timeBoxSelector.val(time);
if(isInputDisabled) {
timeBoxSelector.prop('readonly', true);
}
}
var methods = {
getValueOrDefault: function(val, defaultVal) {
return (jQuery.type(val) === "undefined" || val == null) ? defaultVal : val;
},
init: function(opts) {
this.options = $.extend(defaults, opts);
container.html("");
if(typeof this.options.preInit === "function") {
this.options.preInit();
}
this.initView(this.options);
if(typeof this.options.postInit === "function") {
//$('.operationTimeFrom, .operationTimeTill').timepicker(options.timepickerOptions);
this.options.postInit();
}
return {
serialize: function() {
var data = [];
container.find(".operationState").each(function(num, item) {
var isWorkingDay = $(item).prop("checked");
var dayContainer = $(item).parents(".dayContainer");
data.push({
isActive: isWorkingDay,
timeFrom: isWorkingDay ? dayContainer.find("[name='startTime']").val() : null,
timeTill: isWorkingDay ? dayContainer.find("[name='endTime']").val() : null
});
});
return data;
}
};
},
initView: function(options) {
var stateClasses = [options.checkedColorClass, options.uncheckedColorClass];
var subContainer = container.append($(options.containerTmpl));
var $this = this;
for(var i = 0; i < options.weekdays.length; i++) {
subContainer.append(options.dayTmpl);
}
$.each(options.weekdays, function(pos, weekday) {
// populate form
var day = options.operationTime[pos];
var operationDayNode = container.find(".dayContainer").eq(pos);
operationDayNode.find('.weekday').html(weekday);
var isWorkingDay = $this.getValueOrDefault(day.isActive, options.defaultActive);
operationDayNode.find('.operationState').prop('checked', isWorkingDay);
var timeFrom = $this.getValueOrDefault(day.timeFrom, options.defaultOperationTimeFrom);
initTimeBox(operationDayNode.find('[name="startTime"]'), timeFrom, options.inputDisabled);
var endTime = $this.getValueOrDefault(day.timeTill, options.defaultOperationTimeTill);
initTimeBox(operationDayNode.find('[name="endTime"]'), endTime, options.inputDisabled);
});
container.find(".operationState").change(function() {
var checkbox = $(this);
var boxClass = options.checkedColorClass;
var timeControlDisabled = false;
if(!checkbox.prop("checked")) {
// disabled
boxClass = options.uncheckedColorClass;
timeControlDisabled = true;
}
checkbox.parents(".colorBox").removeClass(stateClasses.join(' ')).addClass(boxClass);
checkbox.parents(".dayContainer").find(".operationTime").toggle(!timeControlDisabled);
}).trigger("change");
if(!options.inputDisabled) {
container.find(".colorBox").on("click", function() {
var checkbox = $(this).find(".operationState");
checkbox.prop("checked", !checkbox.prop('checked')).trigger("change");
});
}
}
};
return methods.init(opts);
};
})(jQuery);