-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.shiftEditor.js
280 lines (220 loc) · 6.55 KB
/
jquery.shiftEditor.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Work Shift Editor
* A really simple little editor that allows the definition of a work shift for a generic week.
*
* TODO:
* Still not possible to have separate options for multiple widget instances on a single page
*
* Copyright (c) 2014 ResourceKraft Ltd. (www.resourcekraft.com)
* Written By: Liam Relihan
*
* Licensed under the MIT (MIT-LICENSE.txt) licenses.
*
*/
'use strict';
(function ($) {
var selectorOwner;
var selectorShowing = false;
var settings;
var numCells;
$.fn.shiftEditor = function(options){
settings = $.extend({
showPublicHolidays: true
}, options);
if (!document.getElementById("time-selector")){
buildSelector();
}
numCells=168;
if (settings.showPublicHolidays) {
numCells+=24;
}
return this.each(
function(i) {
buildPicker(this);
}
);
}
// toggles the class of the cell between 'cell-on' and 'cell-off'
function toggleCellValue(id) {
var cell = document.getElementById(id);
if (cell.className == 'cell-on') {
cell.className = 'cell-off';
} else {
cell.className = 'cell-on';
}
console.log("after className="+cell.className);
}
function clearAll() {
for ( var i = 0; i < numCells; i++) {
var cellJQ=$("#cell_"+i);
cellJQ.removeClass('cell-on').addClass('cell-off');
}
}
function populateTableFromString(hoursString) {
for ( var i = 0; i < numCells; i++) {
var cellJQ=$("#cell_"+i);
console.log(cellJQ);
switch(hoursString.charAt(i)){
case '0':
cellJQ.removeClass('cell-on').addClass('cell-off');
break;
case '9':
cellJQ.removeClass('cell-off').addClass('cell-on');
break;
default:
cellJQ.removeClass('cell-on').addClass('cell-off');
}
console.log(cellJQ);
}
}
function getStringFromTable() {
var string="";
for ( var i = 0; i < numCells; i++) {
var cellJQ=$("#cell_"+i);
switch(cellJQ.attr("class")){
case 'cell-on':
string += '9';
break;
case 'cell-off':
string += '0';
break;
default:
string += '0';
}
}
return string;
}
function buildPicker(element){
var control = populatePicker(element);
//add the shift editor section
$(element).after(control);
//add event listener to input box
$(element).bind("change", function() {
var selectedValue = $(element).val();
$(element).value= selectedValue;
console.log("populating "+element.id);
var control=populatePicker(element);
// replace the previous control
$(element).next("div").remove();
$(element).after(control);
});
//hide the input box
$(element).hide();
}
function populatePicker(element){
console.log("populate picker "+element.id);
var control = $("<div class='picker'></div>");
var hoursString=element.value;
var cell_num=0;
//add days of week
for (var day=0;day<8;day++){
var day_row = $("<div class='picker-day-row'></div>");
for (var hours=0;hours<24;hours++){
switch(hoursString.charAt(cell_num)){
case '0':
$("<span class='picker-cell-off'></span>").appendTo(day_row);
break;
case '9':
$("<span class='picker-cell-on'></span>").appendTo(day_row);
break;
default:
$("<span class='picker-cell-off'></span>").appendTo(day_row);
break;
}
cell_num++;
}
day_row.appendTo(control);
}
//bind click event to shiftEditor
control.bind("click", toggleSelector);
console.log("populate picker "+element.id+ "end");
return control;
}
function buildSelector(){
var selector = $("<div id='time-selector'></div>");
var selector_grid = $("<div id='selector-grid'></div>");
var days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
if (settings.showPublicHolidays)
days_of_week.push("Public Hols");
// draw header row
var header_row = $("<div class='header-row'></div>");
var empty_day_label = $("<span class='day_label'> </span>");
empty_day_label.appendTo(header_row);
header_row.appendTo(selector_grid);
for (var hours=0;hours<24;hours++){
var header_cell = $("<span class='header-cell'>"+hours+"</span>")
header_cell.css("background-color", "#" + this);
header_cell.appendTo(header_row);
}
header_row.appendTo(selector_grid);
var $cell_num=0;
//add days of week
days_of_week.forEach( function(i){
var day_row = $("<div class='day-row'></div>");
var day_label = $("<span class='day_label'>"+i+"</span>");
day_label.appendTo(day_row);
for (var hours=0;hours<24;hours++){
var time_cell = $("<span id='cell_"+$cell_num+"' class='cell-off'> </span>");
time_cell.css("background-color", "#" + this);
time_cell.bind("click",
function(e){
toggleCellValue(this.id);
}
);
time_cell.appendTo(day_row);
$cell_num++;
}
day_row.appendTo(selector_grid);
});
selector_grid.appendTo(selector)
var ok_button = $("<button type='button'>OK</button>");
ok_button.bind("click", function(e){
$(selectorOwner).prev("input").val(getStringFromTable()).change();
toggleSelector();
});
ok_button.appendTo(selector);
var clear_button = $("<button type='button'>Clear</button>");
clear_button.bind("click", function(e){
clearAll();
});
clear_button.appendTo(selector);
$("body").append(selector);
selector.hide();
$("cell_0").class="cell-on";
}
function checkMouse(event){
//check the click was on selector itself or on selectorOwner
var selector = "div#time-selector";
var selectorParent = $(event.target).parents(selector).length;
if(event.target == $(selector)[0] || event.target == selectorOwner || selectorParent > 0) return
hideSelector();
}
function hideSelector(){
var selector = $("div#time-selector");
$(document).unbind("mousedown", checkMouse);
selector.hide();
selectorShowing = false
}
function showSelector(){
var selector = $("div#time-selector");
//alert($(selectorOwner).offset().top);
selector.css({
top: $(selectorOwner).offset().top + ($(selectorOwner).outerHeight()),
left: $(selectorOwner).offset().left
});
console.log("string="+$(selectorOwner).prev("input")[0].value);
// lets get some data into the table
populateTableFromString($(selectorOwner).prev("input")[0].value);
selector.show();
//bind close event handler
$(document).bind("mousedown", checkMouse);
selectorShowing = true
}
function toggleSelector(event){
console.log("toggling");
selectorOwner = this;
selectorShowing ? hideSelector() : showSelector();
console.log("end toggling");
}
//public methods
})(jQuery);