forked from dubrox/Multiple-Dates-Picker-for-jQuery-UI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-ui.multidatespicker.js
498 lines (452 loc) · 16.4 KB
/
jquery-ui.multidatespicker.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* MultiDatesPicker v1.6.4
* http://multidatespickr.sourceforge.net/
*
* Copyright 2014, Luca Lauretta
* Dual licensed under the MIT or GPL version 2 licenses.
*/
(function( $ ){
$.extend($.ui, { multiDatesPicker: { version: "1.6.4" } });
$.fn.multiDatesPicker = function(method) {
var mdp_arguments = arguments;
var ret = this;
var today_date = new Date();
var day_zero = new Date(0);
var mdp_events = {};
function removeDate(date, type) {
if(!type) type = 'picked';
date = dateConvert.call(this, date);
for(var i = 0; i < this.multiDatesPicker.dates[type].length; i++)
if(!methods.compareDates(this.multiDatesPicker.dates[type][i], date))
return this.multiDatesPicker.dates[type].splice(i, 1).pop();
}
function removeIndex(index, type) {
if(!type) type = 'picked';
return this.multiDatesPicker.dates[type].splice(index, 1).pop();
}
function addDate(date, type, no_sort) {
if(!type) type = 'picked';
date = dateConvert.call(this, date);
// @todo: use jQuery UI datepicker method instead
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
if (methods.gotDate.call(this, date, type) === false) {
this.multiDatesPicker.dates[type].push(date);
if(!no_sort) this.multiDatesPicker.dates[type].sort(methods.compareDates);
}
}
function sortDates(type) {
if(!type) type = 'picked';
this.multiDatesPicker.dates[type].sort(methods.compareDates);
}
function dateConvert(date, desired_type, date_format) {
if(!desired_type) desired_type = 'object';/*
if(!date_format && (typeof date == 'string')) {
date_format = $(this).datepicker('option', 'dateFormat');
if(!date_format) date_format = $.datepicker._defaults.dateFormat;
}
*/
return methods.dateConvert.call(this, date, desired_type, date_format);
}
var methods = {
init : function( options ) {
var $this = $(this);
this.multiDatesPicker.changed = false;
var mdp_events = {
beforeShow: function(input, inst) {
this.multiDatesPicker.changed = false;
if(this.multiDatesPicker.originalBeforeShow)
this.multiDatesPicker.originalBeforeShow.call(this, input, inst);
},
onSelect : function(dateText, inst) {
var $this = $(this);
this.multiDatesPicker.changed = true;
if (dateText) {
$this.multiDatesPicker('toggleDate', dateText);
this.multiDatesPicker.changed = true;
// @todo: this will be optimized when I'll move methods to the singleton.
}
if (this.multiDatesPicker.mode == 'normal' && this.multiDatesPicker.pickableRange) {
if(this.multiDatesPicker.dates.picked.length > 0) {
var min_date = this.multiDatesPicker.dates.picked[0],
max_date = new Date(min_date.getTime());
methods.sumDays(max_date, this.multiDatesPicker.pickableRange-1);
// counts the number of disabled dates in the range
if(this.multiDatesPicker.adjustRangeToDisabled) {
var c_disabled,
disabled = this.multiDatesPicker.dates.disabled.slice(0);
do {
c_disabled = 0;
for(var i = 0; i < disabled.length; i++) {
if(disabled[i].getTime() <= max_date.getTime()) {
if((min_date.getTime() <= disabled[i].getTime()) && (disabled[i].getTime() <= max_date.getTime()) ) {
c_disabled++;
}
disabled.splice(i, 1);
i--;
}
}
max_date.setDate(max_date.getDate() + c_disabled);
} while(c_disabled != 0);
}
if(this.multiDatesPicker.maxDate && (max_date > this.multiDatesPicker.maxDate))
max_date = this.multiDatesPicker.maxDate;
$this
.datepicker("option", "minDate", min_date)
.datepicker("option", "maxDate", max_date);
} else {
$this
.datepicker("option", "minDate", this.multiDatesPicker.minDate)
.datepicker("option", "maxDate", this.multiDatesPicker.maxDate);
}
}
if(this.multiDatesPicker.originalOnSelect && dateText)
this.multiDatesPicker.originalOnSelect.call(this, dateText, inst);
},
beforeShowDay : function(date) {
var $this = $(this),
gotThisDate = $this.multiDatesPicker('gotDate', date) !== false,
isDisabledCalendar = $this.datepicker('option', 'disabled'),
isDisabledDate = $this.multiDatesPicker('gotDate', date, 'disabled') !== false,
areAllSelected = this.multiDatesPicker.maxPicks <= this.multiDatesPicker.dates.picked.length;
var bsdReturn = [true, '', null];
if(this.multiDatesPicker.originalBeforeShowDay)
bsdReturn = this.multiDatesPicker.originalBeforeShowDay.call(this, date);
bsdReturn[1] = gotThisDate ? 'ui-state-highlight '+bsdReturn[1] : bsdReturn[1];
bsdReturn[0] = bsdReturn[0] && !(isDisabledCalendar || isDisabledDate || (areAllSelected && !bsdReturn[1]));
return bsdReturn;
}
};
// value have to be extracted before datepicker is initiated
if($this.val()) var inputDates = $this.val()
if(options) {
// value have to be extracted before datepicker is initiated
//if(options.altField) var inputDates = $(options.altField).val();
if(options.separator) this.multiDatesPicker.separator = options.separator;
if(!this.multiDatesPicker.separator) this.multiDatesPicker.separator = ', ';
this.multiDatesPicker.originalBeforeShow = options.beforeShow;
this.multiDatesPicker.originalOnSelect = options.onSelect;
this.multiDatesPicker.originalBeforeShowDay = options.beforeShowDay;
this.multiDatesPicker.originalOnClose = options.onClose;
// datepicker init
$this.datepicker(options);
this.multiDatesPicker.minDate = $.datepicker._determineDate(this, options.minDate, null);
this.multiDatesPicker.maxDate = $.datepicker._determineDate(this, options.maxDate, null);
if(options.addDates) methods.addDates.call(this, options.addDates);
if(options.addDisabledDates)
methods.addDates.call(this, options.addDisabledDates, 'disabled');
methods.setMode.call(this, options);
} else {
$this.datepicker();
}
$this.datepicker('option', mdp_events);
// adds any dates found in the input or alt field
if(inputDates) $this.multiDatesPicker('value', inputDates);
// generates the new string of added dates
var inputs_values = $this.multiDatesPicker('value');
// fills the input field back with all the dates in the calendar
$this.val(inputs_values);
// Fixes the altField filled with defaultDate by default
var altFieldOption = $this.datepicker('option', 'altField');
if (altFieldOption) $(altFieldOption).val(inputs_values);
// Updates the calendar view
$this.datepicker('refresh');
},
compareDates : function(date1, date2) {
date1 = dateConvert.call(this, date1);
date2 = dateConvert.call(this, date2);
// return > 0 means date1 is later than date2
// return == 0 means date1 is the same day as date2
// return < 0 means date1 is earlier than date2
var diff = date1.getFullYear() - date2.getFullYear();
if(!diff) {
diff = date1.getMonth() - date2.getMonth();
if(!diff)
diff = date1.getDate() - date2.getDate();
}
return diff;
},
sumDays : function( date, n_days ) {
var origDateType = typeof date;
obj_date = dateConvert.call(this, date);
obj_date.setDate(obj_date.getDate() + n_days);
return dateConvert.call(this, obj_date, origDateType);
},
dateConvert : function( date, desired_format, dateFormat ) {
var from_format = typeof date;
var $this = $(this);
if(from_format == desired_format) {
if(from_format == 'object') {
try {
date.getTime();
} catch (e) {
$.error('Received date is in a non supported format!');
return false;
}
}
return date;
}
if(typeof date == 'undefined') date = new Date(0);
if(desired_format != 'string' && desired_format != 'object' && desired_format != 'number')
$.error('Date format "'+ desired_format +'" not supported!');
if(!dateFormat) {
// thanks to bibendus83 -> http://sourceforge.net/tracker/index.php?func=detail&aid=3213174&group_id=358205&atid=1495382
var dp_dateFormat = $this.datepicker('option', 'dateFormat');
if (dp_dateFormat) {
dateFormat = dp_dateFormat;
} else {
dateFormat = $.datepicker._defaults.dateFormat;
}
}
// converts to object as a neutral format
switch(from_format) {
case 'object': break;
case 'string': date = $.datepicker.parseDate(dateFormat, date); break;
case 'number': date = new Date(date); break;
default: $.error('Conversion from "'+ desired_format +'" format not allowed on jQuery.multiDatesPicker');
}
// then converts to the desired format
switch(desired_format) {
case 'object': return date;
case 'string': return $.datepicker.formatDate(dateFormat, date);
case 'number': return date.getTime();
default: $.error('Conversion to "'+ desired_format +'" format not allowed on jQuery.multiDatesPicker');
}
return false;
},
gotDate : function( date, type ) {
if(!type) type = 'picked';
for(var i = 0; i < this.multiDatesPicker.dates[type].length; i++) {
if(methods.compareDates.call(this, this.multiDatesPicker.dates[type][i], date) === 0) {
return i;
}
}
return false;
},
value : function( value ) {
if(value && typeof value == 'string') {
methods.addDates.call(this, value.split(this.multiDatesPicker.separator));
} else {
var dates = methods.getDates.call(this, 'string');
return dates.length
? dates.join(this.multiDatesPicker.separator)
: "";
}
},
getDates : function( format, type ) {
if(!format) format = 'string';
if(!type) type = 'picked';
switch (format) {
case 'object':
return this.multiDatesPicker.dates[type];
case 'string':
case 'number':
var o_dates = new Array();
for(var i in this.multiDatesPicker.dates[type])
o_dates.push(
dateConvert.call(
this,
this.multiDatesPicker.dates[type][i],
format
)
);
return o_dates;
default: $.error('Format "'+format+'" not supported!');
}
},
addDates : function( dates, type ) {
if(dates.length > 0) {
if(!type) type = 'picked';
switch(typeof dates) {
case 'object':
case 'array':
if(dates.length) {
for(var i = 0; i < dates.length; i++)
addDate.call(this, dates[i], type, true);
sortDates.call(this, type);
break;
} // else does the same as 'string'
case 'string':
case 'number':
addDate.call(this, dates, type);
break;
default:
$.error('Date format "'+ typeof dates +'" not allowed on jQuery.multiDatesPicker');
}
//$(this).datepicker('refresh');
} else {
$.error('Empty array of dates received.');
}
},
removeDates : function( dates, type ) {
if(!type) type = 'picked';
var removed = [];
if (Object.prototype.toString.call(dates) === '[object Array]') {
for(var i in dates.sort(function(a,b){return b-a})) {
removed.push(removeDate.call(this, dates[i], type));
}
} else {
removed.push(removeDate.call(this, dates, type));
}
return removed;
},
removeIndexes : function( indexes, type ) {
if(!type) type = 'picked';
var removed = [];
if (Object.prototype.toString.call(indexes) === '[object Array]') {
for(var i in indexes.sort(function(a,b){return b-a})) {
removed.push(removeIndex.call(this, indexes[i], type));
}
} else {
removed.push(removeIndex.call(this, indexes, type));
}
return removed;
},
resetDates : function ( type ) {
if(!type) type = 'picked';
this.multiDatesPicker.dates[type] = [];
},
toggleDate : function( date, type ) {
if(!type) type = 'picked';
switch(this.multiDatesPicker.mode) {
case 'daysRange':
this.multiDatesPicker.dates[type] = []; // deletes all picked/disabled dates
var end = this.multiDatesPicker.autoselectRange[1];
var begin = this.multiDatesPicker.autoselectRange[0];
if(end < begin) { // switch
end = this.multiDatesPicker.autoselectRange[0];
begin = this.multiDatesPicker.autoselectRange[1];
}
for(var i = begin; i < end; i++)
methods.addDates.call(this, methods.sumDays.call(this,date, i), type);
break;
default:
if(methods.gotDate.call(this, date) === false) // adds dates
methods.addDates.call(this, date, type);
else // removes dates
methods.removeDates.call(this, date, type);
break;
}
},
setMode : function( options ) {
var $this = $(this);
if(options.mode) this.multiDatesPicker.mode = options.mode;
switch(this.multiDatesPicker.mode) {
case 'normal':
for(option in options)
switch(option) {
case 'maxPicks':
case 'minPicks':
case 'pickableRange':
case 'adjustRangeToDisabled':
this.multiDatesPicker[option] = options[option];
break;
//default: $.error('Option ' + option + ' ignored for mode "'.options.mode.'".');
}
break;
case 'daysRange':
case 'weeksRange':
var mandatory = 1;
for(option in options)
switch(option) {
case 'autoselectRange':
mandatory--;
case 'pickableRange':
case 'adjustRangeToDisabled':
this.multiDatesPicker[option] = options[option];
break;
//default: $.error('Option ' + option + ' does not exist for setMode on jQuery.multiDatesPicker');
}
if(mandatory > 0) $.error('Some mandatory options not specified!');
break;
}
/*
if(options.pickableRange) {
$this.datepicker("option", "maxDate", options.pickableRange);
$this.datepicker("option", "minDate", this.multiDatesPicker.minDate);
}
*/
if(mdp_events.onSelect)
mdp_events.onSelect();
},
destroy: function(){
this.multiDatesPicker = null;
$(this).datepicker('destroy');
}
};
this.each(function() {
var $this = $(this);
if (!this.multiDatesPicker) {
this.multiDatesPicker = {
dates: {
picked: [],
disabled: []
},
mode: 'normal',
adjustRangeToDisabled: true
};
}
if(methods[method]) {
var exec_result = methods[method].apply(this, Array.prototype.slice.call(mdp_arguments, 1));
switch(method) {
case 'removeDates':
case 'removeIndexes':
case 'resetDates':
case 'toggleDate':
case 'addDates':
var altField = $this.datepicker('option', 'altField');
// @todo: should use altFormat for altField
var dates_string = methods.value.call(this);
if (altField !== undefined && altField != "") {
$(altField).val(dates_string);
}
$this.val(dates_string);
$.datepicker._refreshDatepicker(this);
}
switch(method) {
case 'removeDates':
case 'getDates':
case 'gotDate':
case 'sumDays':
case 'compareDates':
case 'dateConvert':
case 'value':
ret = exec_result;
}
return exec_result;
} else if( typeof method === 'object' || ! method ) {
return methods.init.apply(this, mdp_arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.multiDatesPicker');
}
return false;
});
return ret;
};
var PROP_NAME = 'multiDatesPicker';
var dpuuid = new Date().getTime();
var instActive;
$.multiDatesPicker = {version: false};
//$.multiDatesPicker = new MultiDatesPicker(); // singleton instance
$.multiDatesPicker.initialized = false;
$.multiDatesPicker.uuid = new Date().getTime();
$.multiDatesPicker.version = $.ui.multiDatesPicker.version;
// allows MDP not to hide everytime a date is picked
$.multiDatesPicker._hideDatepicker = $.datepicker._hideDatepicker;
$.datepicker._hideDatepicker = function(){
var target = this._curInst.input[0];
var mdp = target.multiDatesPicker;
if(!mdp || (this._curInst.inline === false && !mdp.changed)) {
return $.multiDatesPicker._hideDatepicker.apply(this, arguments);
} else {
mdp.changed = false;
$.datepicker._refreshDatepicker(target);
return;
}
};
// Workaround for #4055
// Add another global to avoid noConflict issues with inline event handlers
window['DP_jQuery_' + dpuuid] = $;
})( jQuery );