outpost-cms 0.1.1 → 0.1.2

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