source: trunk/admin/themes/default/js/datepicker.js @ 30110

Last change on this file since 30110 was 30110, checked in by mistic100, 10 years ago

datepicker display by default -80:+20 for year select (previous -10:+10)

File size: 3.8 KB
Line 
1jQuery.timepicker.log = jQuery.noop; // that's ugly, but the timepicker is acting weird and throws parsing errors
2
3jQuery.fn.pwgDatepicker = function(settings) {
4  var options = jQuery.extend(true, {
5    showTimepicker: false,
6    cancelButton: false,
7  }, settings || {});
8
9  return this.each(function() {
10    var $this = jQuery(this),
11        originalValue = $this.val(),
12        originalDate,
13        $target = jQuery('[name="'+ $this.data('datepicker') +'"]'),
14        linked = !!$target.length,
15        $start, $end;
16   
17    if (linked) {
18      originalValue = $target.val();
19    }
20
21    // custom setter
22    function set(date, init) {
23      if (date === '') date = null;
24      $this.datetimepicker('setDate', date);
25     
26      if ($this.data('datepicker-start') && $start) {
27        $start.datetimepicker('option', 'maxDate', date);
28      }
29      else if ($this.data('datepicker-end') && $end) {
30        if (!init) { // on init, "end" is not initialized yet (assuming "start" is before "end" in the DOM)
31          $end.datetimepicker('option', 'minDate', date);
32        }
33      }
34     
35      if (!date && linked) {
36        $target.val('');
37      }
38    }
39
40    // and custom cancel button
41    if (options.cancelButton) {
42      options.beforeShow = options.onChangeMonthYear = function() {
43        setTimeout(function() {
44          var buttonPane = $this.datepicker('widget')
45              .find('.ui-datepicker-buttonpane');
46         
47          if (buttonPane.find('.pwg-datepicker-cancel').length == 0) {
48            $('<button type="button">'+ options.cancelButton +'</button>')
49              .on('click', function() {
50                set(originalDate, false);
51                $this.datepicker('hide').blur();
52              })
53              .addClass('pwg-datepicker-cancel ui-state-error ui-corner-all')
54              .appendTo(buttonPane);
55          }
56        }, 1);
57      };
58    }
59
60    // init picker
61    $this.datetimepicker(jQuery.extend({
62      dateFormat: linked ? 'DD d MM yy' : 'yy-mm-dd',
63      timeFormat: 'HH:mm',
64     
65      altField: linked ? $target : null,
66      altFormat: 'yy-mm-dd',
67      altTimeFormat: options.showTimepicker ? 'HH:mm:ss' : '',
68     
69      autoSize: true,
70      changeMonth : true,
71      changeYear: true,
72      yearRange: 'c-80:c+20',
73      altFieldTimeOnly: false,
74      showSecond: false,
75      alwaysSetTime: false
76    }, options));
77   
78    // attach range pickers
79    if ($this.data('datepicker-start')) {
80      $start = jQuery('[data-datepicker="'+ $this.data('datepicker-start') +'"]');
81     
82      $this.datetimepicker('option', 'onClose', function(date) {
83        $start.datetimepicker('option', 'maxDate', date);
84      });
85     
86      $this.datetimepicker('option', 'minDate', $start.datetimepicker('getDate'));
87    }
88    else if ($this.data('datepicker-end')) {
89      $end = jQuery('[data-datepicker="'+ $this.data('datepicker-end') +'"]');
90     
91      $this.datetimepicker('option', 'onClose', function(date) {
92        $end.datetimepicker('option', 'minDate', date);
93      });
94    }
95   
96    // attach unset button
97    if ($this.data('datepicker-unset')) {
98      jQuery('#'+ $this.data('datepicker-unset')).on('click', function(e) {
99        e.preventDefault();
100        set(null, false);
101      });
102    }
103   
104    // set value from linked input
105    if (linked) {
106      var splitted = originalValue.split(' ');
107      if (splitted.length == 2 && options.showTimepicker) {
108        set(jQuery.datepicker.parseDateTime('yy-mm-dd', 'HH:mm:ss', originalValue), true);
109      }
110      else if (splitted[0].length == 10) {
111        set(jQuery.datepicker.parseDate('yy-mm-dd', splitted[0]), true);
112      }
113      else {
114        set(null, true);
115      }
116    }
117   
118    originalDate = $this.datetimepicker('getDate');
119   
120    // autoSize not handled by timepicker
121    if (options.showTimepicker) {
122      $this.attr('size', parseInt($this.attr('size'))+6);
123    }
124  });
125};
Note: See TracBrowser for help on using the repository browser.