|
37 | 37 | constructor: DateTimePicker, |
38 | 38 |
|
39 | 39 | init: function(element, options) { |
| 40 | + var icon; |
40 | 41 | if (!(options.pickTime || options.pickDate)) |
41 | 42 | throw new Error('Must choose at least one picker'); |
42 | 43 | this.$element = $(element); |
43 | | - this.format = options.format || this.$element.data('date-format') || 'mm/dd/yyyy'; |
| 44 | + this.format = options.format || this.$element.data('date-format') || 'MM/dd/yyyy'; |
44 | 45 | this._compileFormat(); |
45 | 46 | this.language = options.language in dates ? options.language : 'en' |
46 | | - this.picker = $(getTemplate(this.id, options.pickDate, options.pickTime)).appendTo('body'); |
47 | | - if (options.pickDate && options.pickTime) { |
48 | | - this.picker.on('click', '.accordion-toggle', function(e) { |
49 | | - e.stopPropagation(); |
50 | | - var $this = $(this); |
51 | | - var $parent = $this.closest('ul'); |
52 | | - var expanded = $parent.find('.collapse.in'); |
53 | | - var closed = $parent.find('.collapse:not(.in)'); |
54 | | - |
55 | | - if (expanded && expanded.length) { |
56 | | - var hasData = expanded.data('collapse'); |
57 | | - if (hasData && hasData.transitioning) return; |
58 | | - expanded.collapse('hide'); |
59 | | - closed.collapse('show') |
60 | | - $this.find('i').toggleClass('icon-chevron-down icon-chevron-up'); |
61 | | - } |
62 | | - }); |
63 | | - } |
| 47 | + this.pickDate = options.pickDate; |
| 48 | + this.pickTime = options.pickTime; |
64 | 49 | this.isInput = this.$element.is('input'); |
65 | 50 | this.component = this.$element.is('.date') ? this.$element.find('.add-on') : false; |
66 | | - |
| 51 | + if (this.component) { |
| 52 | + icon = this.component.find('i'); |
| 53 | + } |
| 54 | + if (this.pickTime) { |
| 55 | + this.timeIcon = icon.data('time-icon') || 'icon-time'; |
| 56 | + icon.addClass(this.timeIcon); |
| 57 | + } |
| 58 | + if (this.pickDate) { |
| 59 | + this.dateIcon = icon.data('date-icon') || 'icon-calendar'; |
| 60 | + icon.removeClass(this.timeIcon); |
| 61 | + icon.addClass(this.dateIcon); |
| 62 | + } |
| 63 | + this.picker = $(getTemplate(this.timeIcon, options.pickDate, options.pickTime)).appendTo('body'); |
67 | 64 | this.minViewMode = options.minViewMode||this.$element.data('date-minviewmode')||0; |
68 | 65 | if (typeof this.minViewMode === 'string') { |
69 | 66 | switch (this.minViewMode) { |
|
110 | 107 | type: 'show', |
111 | 108 | date: this.date |
112 | 109 | }); |
113 | | - this._attachDatePickerWidgetEvents(); |
| 110 | + this._attachDatePickerGlobalEvents(); |
114 | 111 | if (e) { |
115 | 112 | e.stopPropagation(); |
116 | 113 | e.preventDefault(); |
117 | 114 | } |
118 | 115 | }, |
119 | 116 |
|
120 | 117 | hide: function() { |
| 118 | + // Ignore event if in the middle of a picker transition |
| 119 | + var collapse = this.picker.find('.collapse') |
| 120 | + for (var i = 0; i < collapse.length; i++) { |
| 121 | + var collapseData = collapse.eq(i).data('collapse'); |
| 122 | + if (collapseData && collapseData.transitioning) |
| 123 | + return; |
| 124 | + } |
121 | 125 | this.picker.hide(); |
122 | 126 | this.viewMode = this.startViewMode; |
123 | 127 | this.showMode(); |
|
126 | 130 | type: 'hide', |
127 | 131 | date: this.date |
128 | 132 | }); |
129 | | - this._detachDatePickerWidgetEvents(); |
| 133 | + this._detachDatePickerGlobalEvents(); |
130 | 134 | }, |
131 | 135 |
|
132 | 136 | set: function() { |
|
332 | 336 |
|
333 | 337 | destroy: function() { |
334 | 338 | this._detachDatePickerEvents(); |
335 | | - this._detachDatePickerWidgetEvents(); |
| 339 | + this._detachDatePickerGlobalEvents(); |
336 | 340 | this.picker.remove(); |
337 | 341 | this.$element.removeData('datetimepicker'); |
338 | 342 | this.component.removeData('datetimepicker'); |
|
384 | 388 | }, |
385 | 389 |
|
386 | 390 | _attachDatePickerEvents: function() { |
| 391 | + var self = this; |
387 | 392 | this.picker.on({ |
388 | 393 | click: $.proxy(this.click, this), |
389 | 394 | mousedown: $.proxy(this.mousedown, this) |
390 | 395 | }); |
| 396 | + if (this.pickDate && this.pickTime) { |
| 397 | + this.picker.on('click.togglePicker', '.accordion-toggle', function(e) { |
| 398 | + e.stopPropagation(); |
| 399 | + var $this = $(this); |
| 400 | + var $parent = $this.closest('ul'); |
| 401 | + var expanded = $parent.find('.collapse.in'); |
| 402 | + var closed = $parent.find('.collapse:not(.in)'); |
| 403 | + |
| 404 | + if (expanded && expanded.length) { |
| 405 | + var collapseData = expanded.data('collapse'); |
| 406 | + if (collapseData && collapseData.transitioning) return; |
| 407 | + expanded.collapse('hide'); |
| 408 | + closed.collapse('show') |
| 409 | + $this.find('i').toggleClass(self.timeIcon + ' ' + self.dateIcon); |
| 410 | + self.$element.find('.add-on i').toggleClass(self.timeIcon + ' ' + self.dateIcon); |
| 411 | + } |
| 412 | + }); |
| 413 | + } |
391 | 414 | if (this.isInput) { |
392 | 415 | this.$element.on({ |
393 | 416 | focus: $.proxy(this.show, this), |
|
403 | 426 | } |
404 | 427 | }, |
405 | 428 |
|
406 | | - _attachDatePickerWidgetEvents: function() { |
| 429 | + _attachDatePickerGlobalEvents: function() { |
407 | 430 | $(window).on('resize.datetimepicker' + this.id, $.proxy(this.place, this)); |
408 | 431 | if (!this.isInput) { |
409 | 432 | $(document).on('mousedown.datetimepicker' + this.id, $.proxy(this.hide, this)); |
|
415 | 438 | click: this.click, |
416 | 439 | mousedown: this.mousedown |
417 | 440 | }); |
| 441 | + if (this.pickDate && this.pickTime) { |
| 442 | + this.picker.off('click.togglePicker'); |
| 443 | + } |
418 | 444 | if (this.isInput) { |
419 | 445 | this.$element.off({ |
420 | 446 | focus: this.show, |
|
430 | 456 | } |
431 | 457 | }, |
432 | 458 |
|
433 | | - _detachDatePickerWidgetEvents: function () { |
| 459 | + _detachDatePickerGlobalEvents: function () { |
434 | 460 | $(window).off('resize.datetimepicker' + this.id); |
435 | 461 | if (!this.isInput) { |
436 | 462 | $(document).off('mousedown.datetimepicker' + this.id); |
|
507 | 533 | return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); |
508 | 534 | } |
509 | 535 |
|
510 | | - function getTemplate(id, pickDate, pickTime) { |
| 536 | + function getTemplate(timeIcon, pickDate, pickTime) { |
511 | 537 | if (pickDate && pickTime) { |
512 | 538 | return ( |
513 | 539 | '<ul class="datetimepicker dropdown-menu">' + |
|
516 | 542 | DPGlobal.template + |
517 | 543 | '</div>' + |
518 | 544 | '</li>' + |
519 | | - '<li class="picker-switch"><a class="accordion-toggle"><i class="icon-chevron-down"></i></a></li>' + |
| 545 | + '<li class="picker-switch"><a class="accordion-toggle"><i class="' + timeIcon + '"></i></a></li>' + |
520 | 546 | '<li class="collapse">' + |
521 | 547 | '<div class="timepicker">' + |
522 | 548 | TPGlobal.template + |
|
0 commit comments