source: branches/2.7/admin/themes/default/js/datepicker.js @ 30957

Last change on this file since 30957 was 30957, checked in by mistic100, 9 years ago

Merged revision(s) 30956 from trunk:
bug 3188: history search, trailing space in dates

File size: 6.5 KB
Line 
1(function($) {
2jQuery.timepicker.log = jQuery.noop; // that's ugly, but the timepicker is acting weird and throws parsing errors
3
4
5// modify DatePicker internal methods to replace year select by a numeric input
6var origGenerateMonthYearHeader = $.datepicker._generateMonthYearHeader,
7    origSelectMonthYear = $.datepicker._selectMonthYear;
8
9$.datepicker._generateMonthYearHeader = function(inst, drawMonth, drawYear, minDate, maxDate,
10      secondary, monthNames, monthNamesShort) {
11
12  var html = origGenerateMonthYearHeader.call(this, inst, drawMonth, drawYear, minDate, maxDate,
13      secondary, monthNames, monthNamesShort);
14
15  var yearshtml = "<input type='number' class='ui-datepicker-year' data-handler='selectYear' data-event='change keyup' value='"+drawYear+"' style='width:4em;margin-left:2px;'>";
16
17  return html.replace(new RegExp('<select class=\'ui-datepicker-year\'.*</select>', 'gm'), yearshtml);
18};
19
20$.datepicker._selectMonthYear = debounce(function(id, select, period) {
21  if (period === 'M') {
22    origSelectMonthYear.call(this, id, select, period);
23  }
24  else {
25    var target = $(id),
26      inst = this._getInst(target[0]),
27      val = parseInt(select.value, 10);
28
29    if (isNaN(val)) {
30      inst['drawYear'] = '';
31    }
32    else {
33      var pos = getCursor($('.ui-datepicker-year')[0]);
34
35      inst['selectedYear'] = inst['drawYear'] = val;
36
37      this._notifyChange(inst);
38      this._adjustDate(target);
39
40      $('.ui-datepicker-year').focus();
41
42      setCursor($('.ui-datepicker-year')[0], pos);
43    }
44  }
45}, 500);
46
47
48// plugin definition
49jQuery.fn.pwgDatepicker = function(settings) {
50  var options = jQuery.extend(true, {
51    showTimepicker: false,
52    cancelButton: false,
53  }, settings || {});
54
55  return this.each(function() {
56    var $this = jQuery(this),
57        originalValue = $this.val(),
58        originalDate,
59        $target = jQuery('[name="'+ $this.data('datepicker') +'"]'),
60        linked = !!$target.length,
61        $start, $end;
62
63    if (linked) {
64      originalValue = $target.val();
65    }
66
67    // custom setter
68    function set(date, init) {
69      if (date === '') date = null;
70      $this.datetimepicker('setDate', date);
71
72      if ($this.data('datepicker-start') && $start) {
73        $start.datetimepicker('option', 'maxDate', date);
74      }
75      else if ($this.data('datepicker-end') && $end) {
76        if (!init) { // on init, "end" is not initialized yet (assuming "start" is before "end" in the DOM)
77          $end.datetimepicker('option', 'minDate', date);
78        }
79      }
80
81      if (!date && linked) {
82        $target.val('');
83      }
84    }
85
86    // and custom cancel button
87    if (options.cancelButton) {
88      options.beforeShow = options.onChangeMonthYear = function() {
89        setTimeout(function() {
90          var buttonPane = $this.datepicker('widget')
91              .find('.ui-datepicker-buttonpane');
92
93          if (buttonPane.find('.pwg-datepicker-cancel').length == 0) {
94            $('<button type="button">'+ options.cancelButton +'</button>')
95              .on('click', function() {
96                set(originalDate, false);
97                $this.datepicker('hide').blur();
98              })
99              .addClass('pwg-datepicker-cancel ui-state-error ui-corner-all')
100              .appendTo(buttonPane);
101          }
102        }, 1);
103      };
104    }
105
106    // init picker
107    $this.datetimepicker(jQuery.extend({
108      dateFormat: linked ? 'DD d MM yy' : 'yy-mm-dd',
109      timeFormat: 'HH:mm',
110      separator: options.showTimepicker ? ' ' : '',
111
112      altField: linked ? $target : null,
113      altFormat: 'yy-mm-dd',
114      altTimeFormat: options.showTimepicker ? 'HH:mm:ss' : '',
115
116      autoSize: true,
117      changeMonth : true,
118      changeYear: true,
119      altFieldTimeOnly: false,
120      showSecond: false,
121      alwaysSetTime: false
122    }, options));
123
124    // attach range pickers
125    if ($this.data('datepicker-start')) {
126      $start = jQuery('[data-datepicker="'+ $this.data('datepicker-start') +'"]');
127
128      $this.datetimepicker('option', 'onClose', function(date) {
129        $start.datetimepicker('option', 'maxDate', date);
130      });
131
132      $this.datetimepicker('option', 'minDate', $start.datetimepicker('getDate'));
133    }
134    else if ($this.data('datepicker-end')) {
135      $end = jQuery('[data-datepicker="'+ $this.data('datepicker-end') +'"]');
136
137      $this.datetimepicker('option', 'onClose', function(date) {
138        $end.datetimepicker('option', 'minDate', date);
139      });
140    }
141
142    // attach unset button
143    if ($this.data('datepicker-unset')) {
144      jQuery('#'+ $this.data('datepicker-unset')).on('click', function(e) {
145        e.preventDefault();
146        set(null, false);
147      });
148    }
149
150    // set value from linked input
151    if (linked) {
152      var splitted = originalValue.split(' ');
153      if (splitted.length == 2 && options.showTimepicker) {
154        set(jQuery.datepicker.parseDateTime('yy-mm-dd', 'HH:mm:ss', originalValue), true);
155      }
156      else if (splitted[0].length == 10) {
157        set(jQuery.datepicker.parseDate('yy-mm-dd', splitted[0]), true);
158      }
159      else {
160        set(null, true);
161      }
162    }
163
164    originalDate = $this.datetimepicker('getDate');
165
166    // autoSize not handled by timepicker
167    if (options.showTimepicker) {
168      $this.attr('size', parseInt($this.attr('size'))+6);
169    }
170  });
171};
172
173
174// functions for custom year input
175function setCursor(node,pos){
176  var node = (typeof node == "string" || node instanceof String) ? document.getElementById(node) : node;
177
178  if (!node) {
179    return false;
180  }
181  else if(node.createTextRange) {
182    var textRange = node.createTextRange();
183    textRange.collapse(true);
184    textRange.moveEnd(pos);
185    textRange.moveStart(pos);
186    textRange.select();
187    return true;
188  }
189  else if(node.setSelectionRange) {
190    node.setSelectionRange(pos,pos);
191    return true;
192  }
193
194  return false;
195}
196
197function getCursor(input) {
198    // Internet Explorer Caret Position (TextArea)
199    if (document.selection && document.selection.createRange) {
200      var range = document.selection.createRange();
201      var bookmark = range.getBookmark();
202      return bookmark.charCodeAt(2) - 2;
203    }
204    else {
205      // Firefox Caret Position (TextArea)
206      if (input.setSelectionRange)
207       return input.selectionStart;
208    }
209
210    return 0;
211}
212
213function debounce(func, wait, immediate) {
214  var timeout;
215  return function() {
216    var context = this, args = arguments;
217    var later = function() {
218      timeout = null;
219      if (!immediate) func.apply(context, args);
220    };
221    var callNow = immediate && !timeout;
222    clearTimeout(timeout);
223    timeout = setTimeout(later, wait);
224    if (callNow) func.apply(context, args);
225  };
226}
227
228}(jQuery));
Note: See TracBrowser for help on using the repository browser.