plan_b 0.0.1.pre → 0.0.1.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1004 @@
1
+ /* =========================================================
2
+ * foundation-datepicker.js
3
+ * Copyright 2013 Peter Beno, najlepsiwebdesigner@gmail.com, @benopeter
4
+ * project website http://foundation-datepicker.peterbeno.com
5
+ *
6
+ * original project:
7
+ * bootstrap-datepicker.js
8
+ * http://www.eyecon.ro/bootstrap-datepicker
9
+ * =========================================================
10
+ * Copyright 2012 Stefan Petre
11
+ * Improvements by Andrew Rowls
12
+ *
13
+ * Licensed under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License.
15
+ * You may obtain a copy of the License at
16
+ *
17
+ * http://www.apache.org/licenses/LICENSE-2.0
18
+ *
19
+ * Unless required by applicable law or agreed to in writing, software
20
+ * distributed under the License is distributed on an "AS IS" BASIS,
21
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ * See the License for the specific language governing permissions and
23
+ * limitations under the License.
24
+ * ========================================================= */
25
+
26
+ !function( $ ) {
27
+
28
+ function UTCDate(){
29
+ return new Date(Date.UTC.apply(Date, arguments));
30
+ }
31
+ function UTCToday(){
32
+ var today = new Date();
33
+ return UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
34
+ }
35
+
36
+ // Picker object
37
+
38
+ var Datepicker = function(element, options) {
39
+ var that = this;
40
+
41
+ this.element = $(element);
42
+ this.closeButton = options.closeButton;
43
+ this.language = options.language||this.element.data('date-language')||"en";
44
+ this.language = this.language in dates ? this.language : this.language.split('-')[0]; //Check if "de-DE" style date is available, if not language should fallback to 2 letter code eg "de"
45
+ this.language = this.language in dates ? this.language : "en";
46
+ this.isRTL = dates[this.language].rtl||false;
47
+ this.format = DPGlobal.parseFormat(options.format||this.element.data('date-format')||dates[this.language].format||'mm/dd/yyyy');
48
+ this.isInline = false;
49
+ this.isInput = this.element.is('input');
50
+ this.component = this.element.is('.date') ? this.element.find('.prefix') : false;
51
+ this.hasInput = this.component && this.element.find('input').length;
52
+
53
+
54
+ this.onRender = options.onRender || function () {};
55
+ if(this.component && this.component.length === 0)
56
+ this.component = false;
57
+
58
+ this._attachEvents();
59
+
60
+ this.forceParse = true;
61
+ if ('forceParse' in options) {
62
+ this.forceParse = options.forceParse;
63
+ } else if ('dateForceParse' in this.element.data()) {
64
+ this.forceParse = this.element.data('date-force-parse');
65
+ }
66
+
67
+
68
+ this.picker = $(DPGlobal.template)
69
+ .appendTo(this.isInline ? this.element : 'body')
70
+ .on({
71
+ click: $.proxy(this.click, this),
72
+ mousedown: $.proxy(this.mousedown, this)
73
+ });
74
+ if (this.closeButton){
75
+ this.picker.find('a.datepicker-close').show();
76
+ }
77
+
78
+ if(this.isInline) {
79
+ this.picker.addClass('datepicker-inline');
80
+ } else {
81
+ this.picker.addClass('datepicker-dropdown dropdown-menu');
82
+ }
83
+ if (this.isRTL){
84
+ this.picker.addClass('datepicker-rtl');
85
+ this.picker.find('.prev i, .next i')
86
+ .toggleClass('icon-arrow-left icon-arrow-right');
87
+ }
88
+ $(document).on('mousedown', function (e) {
89
+ // Clicked outside the datepicker, hide it
90
+ if ($(e.target).closest('.datepicker.datepicker-inline, .datepicker.datepicker-dropdown').length === 0) {
91
+ that.hide();
92
+ }
93
+ });
94
+
95
+ this.autoclose = true;
96
+ if ('autoclose' in options) {
97
+ this.autoclose = options.autoclose;
98
+ } else if ('dateAutoclose' in this.element.data()) {
99
+ this.autoclose = this.element.data('date-autoclose');
100
+ }
101
+
102
+ this.keyboardNavigation = true;
103
+ if ('keyboardNavigation' in options) {
104
+ this.keyboardNavigation = options.keyboardNavigation;
105
+ } else if ('dateKeyboardNavigation' in this.element.data()) {
106
+ this.keyboardNavigation = this.element.data('date-keyboard-navigation');
107
+ }
108
+
109
+ this.viewMode = this.startViewMode = 0;
110
+ switch(options.startView || this.element.data('date-start-view')){
111
+ case 2:
112
+ case 'decade':
113
+ this.viewMode = this.startViewMode = 2;
114
+ break;
115
+ case 1:
116
+ case 'year':
117
+ this.viewMode = this.startViewMode = 1;
118
+ break;
119
+ }
120
+
121
+ this.todayBtn = (options.todayBtn||this.element.data('date-today-btn')||false);
122
+ this.todayHighlight = (options.todayHighlight||this.element.data('date-today-highlight')||false);
123
+
124
+ this.calendarWeeks = false;
125
+ if ('calendarWeeks' in options) {
126
+ this.calendarWeeks = options.calendarWeeks;
127
+ } else if ('dateCalendarWeeks' in this.element.data()) {
128
+ this.calendarWeeks = this.element.data('date-calendar-weeks');
129
+ }
130
+ if (this.calendarWeeks)
131
+ this.picker.find('tfoot th.today')
132
+ .attr('colspan', function(i, val){
133
+ return parseInt(val) + 1;
134
+ });
135
+
136
+ this.weekStart = ((options.weekStart||this.element.data('date-weekstart')||dates[this.language].weekStart||0) % 7);
137
+ this.weekEnd = ((this.weekStart + 6) % 7);
138
+ this.startDate = -Infinity;
139
+ this.endDate = Infinity;
140
+ this.daysOfWeekDisabled = [];
141
+ this.setStartDate(options.startDate||this.element.data('date-startdate'));
142
+ this.setEndDate(options.endDate||this.element.data('date-enddate'));
143
+ this.setDaysOfWeekDisabled(options.daysOfWeekDisabled||this.element.data('date-days-of-week-disabled'));
144
+
145
+ this.fillDow();
146
+ this.fillMonths();
147
+ this.update();
148
+ this.showMode();
149
+
150
+ if(this.isInline) {
151
+ this.show();
152
+ }
153
+ };
154
+
155
+ Datepicker.prototype = {
156
+ constructor: Datepicker,
157
+
158
+ _events: [],
159
+ _attachEvents: function(){
160
+ this._detachEvents();
161
+ if (this.isInput) { // single input
162
+ this._events = [
163
+ [this.element, {
164
+ focus: $.proxy(this.show, this),
165
+ keyup: $.proxy(this.update, this),
166
+ keydown: $.proxy(this.keydown, this)
167
+ }]
168
+ ];
169
+ }
170
+ else if (this.component && this.hasInput){ // component: input + button
171
+ this._events = [
172
+ // For components that are not readonly, allow keyboard nav
173
+ [this.element.find('input'), {
174
+ focus: $.proxy(this.show, this),
175
+ keyup: $.proxy(this.update, this),
176
+ keydown: $.proxy(this.keydown, this)
177
+ }],
178
+ [this.component, {
179
+ click: $.proxy(this.show, this)
180
+ }]
181
+ ];
182
+ }
183
+ else if (this.element.is('div')) { // inline datepicker
184
+ this.isInline = true;
185
+ }
186
+ else {
187
+ this._events = [
188
+ [this.element, {
189
+ click: $.proxy(this.show, this)
190
+ }]
191
+ ];
192
+ }
193
+ for (var i=0, el, ev; i<this._events.length; i++){
194
+ el = this._events[i][0];
195
+ ev = this._events[i][1];
196
+ el.on(ev);
197
+ }
198
+ },
199
+ _detachEvents: function(){
200
+ for (var i=0, el, ev; i<this._events.length; i++){
201
+ el = this._events[i][0];
202
+ ev = this._events[i][1];
203
+ el.off(ev);
204
+ }
205
+ this._events = [];
206
+ },
207
+
208
+ show: function(e) {
209
+ this.picker.show();
210
+ this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
211
+ this.update();
212
+ this.place();
213
+ $(window).on('resize', $.proxy(this.place, this));
214
+ if (e ) {
215
+ e.stopPropagation();
216
+ e.preventDefault();
217
+ }
218
+ this.element.trigger({
219
+ type: 'show',
220
+ date: this.date
221
+ });
222
+ },
223
+
224
+ hide: function(e){
225
+ if(this.isInline) return;
226
+ if (!this.picker.is(':visible')) return;
227
+ this.picker.hide();
228
+ $(window).off('resize', this.place);
229
+ this.viewMode = this.startViewMode;
230
+ this.showMode();
231
+ if (!this.isInput) {
232
+ $(document).off('mousedown', this.hide);
233
+ }
234
+
235
+ if (
236
+ this.forceParse &&
237
+ (
238
+ this.isInput && this.element.val() ||
239
+ this.hasInput && this.element.find('input').val()
240
+ )
241
+ )
242
+ this.setValue();
243
+ this.element.trigger({
244
+ type: 'hide',
245
+ date: this.date
246
+ });
247
+ },
248
+
249
+ remove: function() {
250
+ this._detachEvents();
251
+ this.picker.remove();
252
+ delete this.element.data().datepicker;
253
+ },
254
+
255
+ getDate: function() {
256
+ var d = this.getUTCDate();
257
+ return new Date(d.getTime() + (d.getTimezoneOffset()*60000));
258
+ },
259
+
260
+ getUTCDate: function() {
261
+ return this.date;
262
+ },
263
+
264
+ setDate: function(d) {
265
+ this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset()*60000)));
266
+ },
267
+
268
+ setUTCDate: function(d) {
269
+ this.date = d;
270
+ this.setValue();
271
+ },
272
+
273
+ setValue: function() {
274
+ var formatted = this.getFormattedDate();
275
+ if (!this.isInput) {
276
+ if (this.component){
277
+ this.element.find('input').val(formatted);
278
+ }
279
+ this.element.data('date', formatted);
280
+ } else {
281
+ this.element.val(formatted);
282
+ }
283
+ },
284
+
285
+ getFormattedDate: function(format) {
286
+ if (format === undefined)
287
+ format = this.format;
288
+ return DPGlobal.formatDate(this.date, format, this.language);
289
+ },
290
+
291
+ setStartDate: function(startDate){
292
+ this.startDate = startDate||-Infinity;
293
+ if (this.startDate !== -Infinity) {
294
+ this.startDate = DPGlobal.parseDate(this.startDate, this.format, this.language);
295
+ }
296
+ this.update();
297
+ this.updateNavArrows();
298
+ },
299
+
300
+ setEndDate: function(endDate){
301
+ this.endDate = endDate||Infinity;
302
+ if (this.endDate !== Infinity) {
303
+ this.endDate = DPGlobal.parseDate(this.endDate, this.format, this.language);
304
+ }
305
+ this.update();
306
+ this.updateNavArrows();
307
+ },
308
+
309
+ setDaysOfWeekDisabled: function(daysOfWeekDisabled){
310
+ this.daysOfWeekDisabled = daysOfWeekDisabled||[];
311
+ if (!$.isArray(this.daysOfWeekDisabled)) {
312
+ this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
313
+ }
314
+ this.daysOfWeekDisabled = $.map(this.daysOfWeekDisabled, function (d) {
315
+ return parseInt(d, 10);
316
+ });
317
+ this.update();
318
+ this.updateNavArrows();
319
+ },
320
+
321
+ place: function(){
322
+ if(this.isInline) return;
323
+ var zIndex = parseInt(this.element.parents().filter(function() {
324
+ return $(this).css('z-index') != 'auto';
325
+ }).first().css('z-index'))+10;
326
+ var textbox = this.component ? this.component : this.element;
327
+ var offset = textbox.offset();
328
+ var height = textbox.outerHeight() + parseInt(textbox.css('margin-top'));
329
+ this.picker.css({
330
+ top: offset.top + height,
331
+ left: offset.left,
332
+ zIndex: zIndex
333
+ });
334
+ },
335
+
336
+ update: function(){
337
+ var date, fromArgs = false;
338
+ if(arguments && arguments.length && (typeof arguments[0] === 'string' || arguments[0] instanceof Date)) {
339
+ date = arguments[0];
340
+ fromArgs = true;
341
+ } else {
342
+ date = this.isInput ? this.element.val() : this.element.data('date') || this.element.find('input').val();
343
+ }
344
+
345
+ this.date = DPGlobal.parseDate(date, this.format, this.language);
346
+
347
+ if(fromArgs) this.setValue();
348
+
349
+ if (this.date < this.startDate) {
350
+ this.viewDate = new Date(this.startDate);
351
+ } else if (this.date > this.endDate) {
352
+ this.viewDate = new Date(this.endDate);
353
+ } else {
354
+ this.viewDate = new Date(this.date);
355
+ }
356
+ this.fill();
357
+ },
358
+
359
+ fillDow: function(){
360
+ var dowCnt = this.weekStart,
361
+ html = '<tr>';
362
+ if(this.calendarWeeks){
363
+ var cell = '<th class="cw">&nbsp;</th>';
364
+ html += cell;
365
+ this.picker.find('.datepicker-days thead tr:first-child').prepend(cell);
366
+ }
367
+ while (dowCnt < this.weekStart + 7) {
368
+ html += '<th class="dow">'+dates[this.language].daysMin[(dowCnt++)%7]+'</th>';
369
+ }
370
+ html += '</tr>';
371
+ this.picker.find('.datepicker-days thead').append(html);
372
+ },
373
+
374
+ fillMonths: function(){
375
+ var html = '',
376
+ i = 0;
377
+ while (i < 12) {
378
+ html += '<span class="month">'+dates[this.language].monthsShort[i++]+'</span>';
379
+ }
380
+ this.picker.find('.datepicker-months td').html(html);
381
+ },
382
+
383
+ fill: function() {
384
+ var d = new Date(this.viewDate),
385
+ year = d.getUTCFullYear(),
386
+ month = d.getUTCMonth(),
387
+ startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
388
+ startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
389
+ endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
390
+ endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
391
+ currentDate = this.date && this.date.valueOf(),
392
+ today = new Date();
393
+ this.picker.find('.datepicker-days thead th.date-switch')
394
+ .text(dates[this.language].months[month]+' '+year);
395
+ this.picker.find('tfoot th.today')
396
+ .text(dates[this.language].today)
397
+ .toggle(this.todayBtn !== false);
398
+ this.updateNavArrows();
399
+ this.fillMonths();
400
+ var prevMonth = UTCDate(year, month-1, 28,0,0,0,0),
401
+ day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
402
+ prevMonth.setUTCDate(day);
403
+ prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
404
+ var nextMonth = new Date(prevMonth);
405
+ nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
406
+ nextMonth = nextMonth.valueOf();
407
+ var html = [];
408
+ var clsName;
409
+ while(prevMonth.valueOf() < nextMonth) {
410
+ if (prevMonth.getUTCDay() == this.weekStart) {
411
+ html.push('<tr>');
412
+ if(this.calendarWeeks){
413
+ // adapted from https://github.com/timrwood/moment/blob/master/moment.js#L128
414
+ var a = new Date(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth(), prevMonth.getUTCDate() - prevMonth.getDay() + 10 - (this.weekStart && this.weekStart%7 < 5 && 7)),
415
+ b = new Date(a.getFullYear(), 0, 4),
416
+ calWeek = ~~((a - b) / 864e5 / 7 + 1.5);
417
+ html.push('<td class="cw">'+ calWeek +'</td>');
418
+ }
419
+ }
420
+ clsName = ' '+this.onRender(prevMonth)+' ';
421
+ if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
422
+ clsName += ' old';
423
+ } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
424
+ clsName += ' new';
425
+ }
426
+ // Compare internal UTC date with local today, not UTC today
427
+ if (this.todayHighlight &&
428
+ prevMonth.getUTCFullYear() == today.getFullYear() &&
429
+ prevMonth.getUTCMonth() == today.getMonth() &&
430
+ prevMonth.getUTCDate() == today.getDate()) {
431
+ clsName += ' today';
432
+ }
433
+ if (currentDate && prevMonth.valueOf() == currentDate) {
434
+ clsName += ' active';
435
+ }
436
+ if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
437
+ $.inArray(prevMonth.getUTCDay(), this.daysOfWeekDisabled) !== -1) {
438
+ clsName += ' disabled';
439
+ }
440
+ html.push('<td class="day'+clsName+'">'+prevMonth.getUTCDate() + '</td>');
441
+ if (prevMonth.getUTCDay() == this.weekEnd) {
442
+ html.push('</tr>');
443
+ }
444
+ prevMonth.setUTCDate(prevMonth.getUTCDate()+1);
445
+ }
446
+ this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
447
+ var currentYear = this.date && this.date.getUTCFullYear();
448
+
449
+ var months = this.picker.find('.datepicker-months')
450
+ .find('th:eq(1)')
451
+ .text(year)
452
+ .end()
453
+ .find('span').removeClass('active');
454
+ if (currentYear && currentYear == year) {
455
+ months.eq(this.date.getUTCMonth()).addClass('active');
456
+ }
457
+ if (year < startYear || year > endYear) {
458
+ months.addClass('disabled');
459
+ }
460
+ if (year == startYear) {
461
+ months.slice(0, startMonth).addClass('disabled');
462
+ }
463
+ if (year == endYear) {
464
+ months.slice(endMonth+1).addClass('disabled');
465
+ }
466
+
467
+ html = '';
468
+ year = parseInt(year/10, 10) * 10;
469
+ var yearCont = this.picker.find('.datepicker-years')
470
+ .find('th:eq(1)')
471
+ .text(year + '-' + (year + 9))
472
+ .end()
473
+ .find('td');
474
+ year -= 1;
475
+ for (var i = -1; i < 11; i++) {
476
+ html += '<span class="year'+(i == -1 || i == 10 ? ' old' : '')+(currentYear == year ? ' active' : '')+(year < startYear || year > endYear ? ' disabled' : '')+'">'+year+'</span>';
477
+ year += 1;
478
+ }
479
+ yearCont.html(html);
480
+ },
481
+
482
+ updateNavArrows: function() {
483
+ var d = new Date(this.viewDate),
484
+ year = d.getUTCFullYear(),
485
+ month = d.getUTCMonth();
486
+ switch (this.viewMode) {
487
+ case 0:
488
+ if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
489
+ this.picker.find('.prev').css({visibility: 'hidden'});
490
+ } else {
491
+ this.picker.find('.prev').css({visibility: 'visible'});
492
+ }
493
+ if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
494
+ this.picker.find('.next').css({visibility: 'hidden'});
495
+ } else {
496
+ this.picker.find('.next').css({visibility: 'visible'});
497
+ }
498
+ break;
499
+ case 1:
500
+ case 2:
501
+ if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
502
+ this.picker.find('.prev').css({visibility: 'hidden'});
503
+ } else {
504
+ this.picker.find('.prev').css({visibility: 'visible'});
505
+ }
506
+ if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
507
+ this.picker.find('.next').css({visibility: 'hidden'});
508
+ } else {
509
+ this.picker.find('.next').css({visibility: 'visible'});
510
+ }
511
+ break;
512
+ }
513
+ },
514
+
515
+ click: function(e) {
516
+ e.stopPropagation();
517
+ e.preventDefault();
518
+
519
+ if ($(e.target).hasClass('datepicker-close')){
520
+ this.hide();
521
+ }
522
+
523
+ var target = $(e.target).closest('span, td, th');
524
+ if (target.length == 1) {
525
+ switch(target[0].nodeName.toLowerCase()) {
526
+ case 'th':
527
+ switch(target[0].className) {
528
+ case 'date-switch':
529
+ this.showMode(1);
530
+ break;
531
+ case 'prev':
532
+ case 'next':
533
+ var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
534
+ switch(this.viewMode){
535
+ case 0:
536
+ this.viewDate = this.moveMonth(this.viewDate, dir);
537
+ break;
538
+ case 1:
539
+ case 2:
540
+ this.viewDate = this.moveYear(this.viewDate, dir);
541
+ break;
542
+ }
543
+ this.fill();
544
+ break;
545
+ case 'today':
546
+ var date = new Date();
547
+ date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
548
+
549
+ this.showMode(-2);
550
+ var which = this.todayBtn == 'linked' ? null : 'view';
551
+ this._setDate(date, which);
552
+ break;
553
+ }
554
+ break;
555
+ case 'span':
556
+ if (!target.is('.disabled')) {
557
+ this.viewDate.setUTCDate(1);
558
+ if (target.is('.month')) {
559
+ var month = target.parent().find('span').index(target);
560
+ this.viewDate.setUTCMonth(month);
561
+ this.element.trigger({
562
+ type: 'changeMonth',
563
+ date: this.viewDate
564
+ });
565
+ } else {
566
+ var year = parseInt(target.text(), 10)||0;
567
+ this.viewDate.setUTCFullYear(year);
568
+ this.element.trigger({
569
+ type: 'changeYear',
570
+ date: this.viewDate
571
+ });
572
+ }
573
+ this.showMode(-1);
574
+ this.fill();
575
+ }
576
+ break;
577
+ case 'td':
578
+ if (target.is('.day') && !target.is('.disabled')){
579
+ var day = parseInt(target.text(), 10)||1;
580
+ var year = this.viewDate.getUTCFullYear(),
581
+ month = this.viewDate.getUTCMonth();
582
+ if (target.is('.old')) {
583
+ if (month === 0) {
584
+ month = 11;
585
+ year -= 1;
586
+ } else {
587
+ month -= 1;
588
+ }
589
+ } else if (target.is('.new')) {
590
+ if (month == 11) {
591
+ month = 0;
592
+ year += 1;
593
+ } else {
594
+ month += 1;
595
+ }
596
+ }
597
+ this._setDate(UTCDate(year, month, day,0,0,0,0));
598
+ }
599
+ break;
600
+ }
601
+ }
602
+ },
603
+
604
+ _setDate: function(date, which){
605
+ if (!which || which == 'date')
606
+ this.date = date;
607
+ if (!which || which == 'view')
608
+ this.viewDate = date;
609
+ this.fill();
610
+ this.setValue();
611
+ this.element.trigger({
612
+ type: 'changeDate',
613
+ date: this.date
614
+ });
615
+ var element;
616
+ if (this.isInput) {
617
+ element = this.element;
618
+ } else if (this.component){
619
+ element = this.element.find('input');
620
+ }
621
+ if (element) {
622
+ element.change();
623
+ if (this.autoclose && (!which || which == 'date')) {
624
+ this.hide();
625
+ }
626
+ }
627
+ },
628
+
629
+ moveMonth: function(date, dir){
630
+ if (!dir) return date;
631
+ var new_date = new Date(date.valueOf()),
632
+ day = new_date.getUTCDate(),
633
+ month = new_date.getUTCMonth(),
634
+ mag = Math.abs(dir),
635
+ new_month, test;
636
+ dir = dir > 0 ? 1 : -1;
637
+ if (mag == 1){
638
+ test = dir == -1
639
+ // If going back one month, make sure month is not current month
640
+ // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
641
+ ? function(){ return new_date.getUTCMonth() == month; }
642
+ // If going forward one month, make sure month is as expected
643
+ // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
644
+ : function(){ return new_date.getUTCMonth() != new_month; };
645
+ new_month = month + dir;
646
+ new_date.setUTCMonth(new_month);
647
+ // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
648
+ if (new_month < 0 || new_month > 11)
649
+ new_month = (new_month + 12) % 12;
650
+ } else {
651
+ // For magnitudes >1, move one month at a time...
652
+ for (var i=0; i<mag; i++)
653
+ // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
654
+ new_date = this.moveMonth(new_date, dir);
655
+ // ...then reset the day, keeping it in the new month
656
+ new_month = new_date.getUTCMonth();
657
+ new_date.setUTCDate(day);
658
+ test = function(){ return new_month != new_date.getUTCMonth(); };
659
+ }
660
+ // Common date-resetting loop -- if date is beyond end of month, make it
661
+ // end of month
662
+ while (test()){
663
+ new_date.setUTCDate(--day);
664
+ new_date.setUTCMonth(new_month);
665
+ }
666
+ return new_date;
667
+ },
668
+
669
+ moveYear: function(date, dir){
670
+ return this.moveMonth(date, dir*12);
671
+ },
672
+
673
+ dateWithinRange: function(date){
674
+ return date >= this.startDate && date <= this.endDate;
675
+ },
676
+
677
+ keydown: function(e){
678
+ if (this.picker.is(':not(:visible)')){
679
+ if (e.keyCode == 27) // allow escape to hide and re-show picker
680
+ this.show();
681
+ return;
682
+ }
683
+ var dateChanged = false,
684
+ dir, day, month,
685
+ newDate, newViewDate;
686
+ switch(e.keyCode){
687
+ case 27: // escape
688
+ this.hide();
689
+ e.preventDefault();
690
+ break;
691
+ case 37: // left
692
+ case 39: // right
693
+ if (!this.keyboardNavigation) break;
694
+ dir = e.keyCode == 37 ? -1 : 1;
695
+ if (e.ctrlKey){
696
+ newDate = this.moveYear(this.date, dir);
697
+ newViewDate = this.moveYear(this.viewDate, dir);
698
+ } else if (e.shiftKey){
699
+ newDate = this.moveMonth(this.date, dir);
700
+ newViewDate = this.moveMonth(this.viewDate, dir);
701
+ } else {
702
+ newDate = new Date(this.date);
703
+ newDate.setUTCDate(this.date.getUTCDate() + dir);
704
+ newViewDate = new Date(this.viewDate);
705
+ newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
706
+ }
707
+ if (this.dateWithinRange(newDate)){
708
+ this.date = newDate;
709
+ this.viewDate = newViewDate;
710
+ this.setValue();
711
+ this.update();
712
+ e.preventDefault();
713
+ dateChanged = true;
714
+ }
715
+ break;
716
+ case 38: // up
717
+ case 40: // down
718
+ if (!this.keyboardNavigation) break;
719
+ dir = e.keyCode == 38 ? -1 : 1;
720
+ if (e.ctrlKey){
721
+ newDate = this.moveYear(this.date, dir);
722
+ newViewDate = this.moveYear(this.viewDate, dir);
723
+ } else if (e.shiftKey){
724
+ newDate = this.moveMonth(this.date, dir);
725
+ newViewDate = this.moveMonth(this.viewDate, dir);
726
+ } else {
727
+ newDate = new Date(this.date);
728
+ newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
729
+ newViewDate = new Date(this.viewDate);
730
+ newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
731
+ }
732
+ if (this.dateWithinRange(newDate)){
733
+ this.date = newDate;
734
+ this.viewDate = newViewDate;
735
+ this.setValue();
736
+ this.update();
737
+ e.preventDefault();
738
+ dateChanged = true;
739
+ }
740
+ break;
741
+ case 13: // enter
742
+ this.hide();
743
+ e.preventDefault();
744
+ break;
745
+ case 9: // tab
746
+ this.hide();
747
+ break;
748
+ }
749
+ if (dateChanged){
750
+ this.element.trigger({
751
+ type: 'changeDate',
752
+ date: this.date
753
+ });
754
+ var element;
755
+ if (this.isInput) {
756
+ element = this.element;
757
+ } else if (this.component){
758
+ element = this.element.find('input');
759
+ }
760
+ if (element) {
761
+ element.change();
762
+ }
763
+ }
764
+ },
765
+
766
+ showMode: function(dir) {
767
+ if (dir) {
768
+ this.viewMode = Math.max(0, Math.min(2, this.viewMode + dir));
769
+ }
770
+ /*
771
+ vitalets: fixing bug of very special conditions:
772
+ jquery 1.7.1 + webkit + show inline datepicker in bootstrap popover.
773
+ Method show() does not set display css correctly and datepicker is not shown.
774
+ Changed to .css('display', 'block') solve the problem.
775
+ See https://github.com/vitalets/x-editable/issues/37
776
+
777
+ In jquery 1.7.2+ everything works fine.
778
+ */
779
+ //this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
780
+ this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).css('display', 'block');
781
+ this.updateNavArrows();
782
+ }
783
+ };
784
+
785
+ $.fn.fdatepicker = function ( option ) {
786
+ var args = Array.apply(null, arguments);
787
+ args.shift();
788
+ return this.each(function () {
789
+ var $this = $(this),
790
+ data = $this.data('datepicker'),
791
+ options = typeof option == 'object' && option;
792
+ if (!data) {
793
+ $this.data('datepicker', (data = new Datepicker(this, $.extend({}, $.fn.fdatepicker.defaults,options))));
794
+ }
795
+ if (typeof option == 'string' && typeof data[option] == 'function') {
796
+ data[option].apply(data, args);
797
+ }
798
+ });
799
+ };
800
+
801
+ $.fn.fdatepicker.defaults = {
802
+ onRender: function(date) {
803
+ return '';
804
+ }
805
+ };
806
+ $.fn.fdatepicker.Constructor = Datepicker;
807
+ var dates = $.fn.fdatepicker.dates = {
808
+ en: {
809
+ days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
810
+ daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
811
+ daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
812
+ months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
813
+ monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
814
+ today: "Today"
815
+ }
816
+ };
817
+
818
+ var DPGlobal = {
819
+ modes: [
820
+ {
821
+ clsName: 'days',
822
+ navFnc: 'Month',
823
+ navStep: 1
824
+ },
825
+ {
826
+ clsName: 'months',
827
+ navFnc: 'FullYear',
828
+ navStep: 1
829
+ },
830
+ {
831
+ clsName: 'years',
832
+ navFnc: 'FullYear',
833
+ navStep: 10
834
+ }],
835
+ isLeapYear: function (year) {
836
+ return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
837
+ },
838
+ getDaysInMonth: function (year, month) {
839
+ return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
840
+ },
841
+ validParts: /dd?|DD?|mm?|MM?|yy(?:yy)?/g,
842
+ nonpunctuation: /[^ -\/:-@\[\u3400-\u9fff-`{-~\t\n\r]+/g,
843
+ parseFormat: function(format){
844
+ // IE treats \0 as a string end in inputs (truncating the value),
845
+ // so it's a bad format delimiter, anyway
846
+ var separators = format.replace(this.validParts, '\0').split('\0'),
847
+ parts = format.match(this.validParts);
848
+ if (!separators || !separators.length || !parts || parts.length === 0){
849
+ throw new Error("Invalid date format.");
850
+ }
851
+ return {separators: separators, parts: parts};
852
+ },
853
+ parseDate: function(date, format, language) {
854
+ if (date instanceof Date) return date;
855
+ if (/^[\-+]\d+[dmwy]([\s,]+[\-+]\d+[dmwy])*$/.test(date)) {
856
+ var part_re = /([\-+]\d+)([dmwy])/,
857
+ parts = date.match(/([\-+]\d+)([dmwy])/g),
858
+ part, dir;
859
+ date = new Date();
860
+ for (var i=0; i<parts.length; i++) {
861
+ part = part_re.exec(parts[i]);
862
+ dir = parseInt(part[1]);
863
+ switch(part[2]){
864
+ case 'd':
865
+ date.setUTCDate(date.getUTCDate() + dir);
866
+ break;
867
+ case 'm':
868
+ date = Datepicker.prototype.moveMonth.call(Datepicker.prototype, date, dir);
869
+ break;
870
+ case 'w':
871
+ date.setUTCDate(date.getUTCDate() + dir * 7);
872
+ break;
873
+ case 'y':
874
+ date = Datepicker.prototype.moveYear.call(Datepicker.prototype, date, dir);
875
+ break;
876
+ }
877
+ }
878
+ return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
879
+ }
880
+ var parts = date && date.match(this.nonpunctuation) || [],
881
+ date = new Date(),
882
+ parsed = {},
883
+ setters_order = ['yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
884
+ setters_map = {
885
+ yyyy: function(d,v){ return d.setUTCFullYear(v); },
886
+ yy: function(d,v){ return d.setUTCFullYear(2000+v); },
887
+ m: function(d,v){
888
+ v -= 1;
889
+ while (v<0) v += 12;
890
+ v %= 12;
891
+ d.setUTCMonth(v);
892
+ while (d.getUTCMonth() != v)
893
+ d.setUTCDate(d.getUTCDate()-1);
894
+ return d;
895
+ },
896
+ d: function(d,v){ return d.setUTCDate(v); }
897
+ },
898
+ val, filtered, part;
899
+ setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
900
+ setters_map['dd'] = setters_map['d'];
901
+ date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
902
+ var fparts = format.parts.slice();
903
+ // Remove noop parts
904
+ if (parts.length != fparts.length) {
905
+ fparts = $(fparts).filter(function(i,p){
906
+ return $.inArray(p, setters_order) !== -1;
907
+ }).toArray();
908
+ }
909
+ // Process remainder
910
+ if (parts.length == fparts.length) {
911
+ for (var i=0, cnt = fparts.length; i < cnt; i++) {
912
+ val = parseInt(parts[i], 10);
913
+ part = fparts[i];
914
+ if (isNaN(val)) {
915
+ switch(part) {
916
+ case 'MM':
917
+ filtered = $(dates[language].months).filter(function(){
918
+ var m = this.slice(0, parts[i].length),
919
+ p = parts[i].slice(0, m.length);
920
+ return m == p;
921
+ });
922
+ val = $.inArray(filtered[0], dates[language].months) + 1;
923
+ break;
924
+ case 'M':
925
+ filtered = $(dates[language].monthsShort).filter(function(){
926
+ var m = this.slice(0, parts[i].length),
927
+ p = parts[i].slice(0, m.length);
928
+ return m == p;
929
+ });
930
+ val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
931
+ break;
932
+ }
933
+ }
934
+ parsed[part] = val;
935
+ }
936
+ for (var i=0, s; i<setters_order.length; i++){
937
+ s = setters_order[i];
938
+ if (s in parsed && !isNaN(parsed[s]))
939
+ setters_map[s](date, parsed[s]);
940
+ }
941
+ }
942
+ return date;
943
+ },
944
+ formatDate: function(date, format, language){
945
+ var val = {
946
+ d: date.getUTCDate(),
947
+ D: dates[language].daysShort[date.getUTCDay()],
948
+ DD: dates[language].days[date.getUTCDay()],
949
+ m: date.getUTCMonth() + 1,
950
+ M: dates[language].monthsShort[date.getUTCMonth()],
951
+ MM: dates[language].months[date.getUTCMonth()],
952
+ yy: date.getUTCFullYear().toString().substring(2),
953
+ yyyy: date.getUTCFullYear()
954
+ };
955
+ val.dd = (val.d < 10 ? '0' : '') + val.d;
956
+ val.mm = (val.m < 10 ? '0' : '') + val.m;
957
+ var date = [],
958
+ seps = $.extend([], format.separators);
959
+ for (var i=0, cnt = format.parts.length; i < cnt; i++) {
960
+ if (seps.length)
961
+ date.push(seps.shift());
962
+ date.push(val[format.parts[i]]);
963
+ }
964
+ return date.join('');
965
+ },
966
+ headTemplate: '<thead>'+
967
+ '<tr>'+
968
+ '<th class="prev"><i class="icon-chevron-left"/></th>'+
969
+ '<th colspan="5" class="date-switch"></th>'+
970
+ '<th class="next"><i class="icon-chevron-right"/></th>'+
971
+ '</tr>'+
972
+ '</thead>',
973
+ contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
974
+ footTemplate: '<tfoot><tr><th colspan="7" class="today"></th></tr></tfoot>'
975
+ };
976
+ DPGlobal.template = '<div class="datepicker">'+
977
+
978
+ '<div class="datepicker-days">'+
979
+ '<table class=" table-condensed">'+
980
+ DPGlobal.headTemplate+
981
+ '<tbody></tbody>'+
982
+ DPGlobal.footTemplate+
983
+ '</table>'+
984
+ '</div>'+
985
+ '<div class="datepicker-months">'+
986
+ '<table class="table-condensed">'+
987
+ DPGlobal.headTemplate+
988
+ DPGlobal.contTemplate+
989
+ DPGlobal.footTemplate+
990
+ '</table>'+
991
+ '</div>'+
992
+ '<div class="datepicker-years">'+
993
+ '<table class="table-condensed">'+
994
+ DPGlobal.headTemplate+
995
+ DPGlobal.contTemplate+
996
+ DPGlobal.footTemplate+
997
+ '</table>'+
998
+ '</div>'+
999
+ '<a class="button datepicker-close small alert right" style="width:auto;"><i class="icon-remove"></i></a>'+
1000
+ '</div>';
1001
+
1002
+ $.fn.fdatepicker.DPGlobal = DPGlobal;
1003
+
1004
+ }( window.jQuery );