Changeset 30341


Ignore:
Timestamp:
Nov 2, 2014, 5:37:17 PM (9 years ago)
Author:
mistic100
Message:

feature:3168 Unuseable datepicker for old dates
modify DatePicker internal methods to replace year select by a numeric input

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/themes/default/js/datepicker.js

    r30110 r30341  
     1(function($) {
    12jQuery.timepicker.log = jQuery.noop; // that's ugly, but the timepicker is acting weird and throws parsing errors
    23
     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
    349jQuery.fn.pwgDatepicker = function(settings) {
    450  var options = jQuery.extend(true, {
     
    1460        linked = !!$target.length,
    1561        $start, $end;
    16    
     62
    1763    if (linked) {
    1864      originalValue = $target.val();
     
    2369      if (date === '') date = null;
    2470      $this.datetimepicker('setDate', date);
    25      
     71
    2672      if ($this.data('datepicker-start') && $start) {
    2773        $start.datetimepicker('option', 'maxDate', date);
     
    3278        }
    3379      }
    34      
     80
    3581      if (!date && linked) {
    3682        $target.val('');
     
    4490          var buttonPane = $this.datepicker('widget')
    4591              .find('.ui-datepicker-buttonpane');
    46          
     92
    4793          if (buttonPane.find('.pwg-datepicker-cancel').length == 0) {
    4894            $('<button type="button">'+ options.cancelButton +'</button>')
     
    62108      dateFormat: linked ? 'DD d MM yy' : 'yy-mm-dd',
    63109      timeFormat: 'HH:mm',
    64      
     110
    65111      altField: linked ? $target : null,
    66112      altFormat: 'yy-mm-dd',
    67113      altTimeFormat: options.showTimepicker ? 'HH:mm:ss' : '',
    68      
     114
    69115      autoSize: true,
    70116      changeMonth : true,
    71117      changeYear: true,
    72       yearRange: 'c-80:c+20',
    73118      altFieldTimeOnly: false,
    74119      showSecond: false,
    75120      alwaysSetTime: false
    76121    }, options));
    77    
     122
    78123    // attach range pickers
    79124    if ($this.data('datepicker-start')) {
    80125      $start = jQuery('[data-datepicker="'+ $this.data('datepicker-start') +'"]');
    81      
     126
    82127      $this.datetimepicker('option', 'onClose', function(date) {
    83128        $start.datetimepicker('option', 'maxDate', date);
    84129      });
    85      
     130
    86131      $this.datetimepicker('option', 'minDate', $start.datetimepicker('getDate'));
    87132    }
    88133    else if ($this.data('datepicker-end')) {
    89134      $end = jQuery('[data-datepicker="'+ $this.data('datepicker-end') +'"]');
    90      
     135
    91136      $this.datetimepicker('option', 'onClose', function(date) {
    92137        $end.datetimepicker('option', 'minDate', date);
    93138      });
    94139    }
    95    
     140
    96141    // attach unset button
    97142    if ($this.data('datepicker-unset')) {
     
    101146      });
    102147    }
    103    
     148
    104149    // set value from linked input
    105150    if (linked) {
     
    115160      }
    116161    }
    117    
     162
    118163    originalDate = $this.datetimepicker('getDate');
    119    
     164
    120165    // autoSize not handled by timepicker
    121166    if (options.showTimepicker) {
     
    124169  });
    125170};
     171
     172
     173// functions for custom year input
     174function setCursor(node,pos){
     175  var node = (typeof node == "string" || node instanceof String) ? document.getElementById(node) : node;
     176
     177  if (!node) {
     178    return false;
     179  }
     180  else if(node.createTextRange) {
     181    var textRange = node.createTextRange();
     182    textRange.collapse(true);
     183    textRange.moveEnd(pos);
     184    textRange.moveStart(pos);
     185    textRange.select();
     186    return true;
     187  }
     188  else if(node.setSelectionRange) {
     189    node.setSelectionRange(pos,pos);
     190    return true;
     191  }
     192
     193  return false;
     194}
     195
     196function getCursor(input) {
     197    // Internet Explorer Caret Position (TextArea)
     198    if (document.selection && document.selection.createRange) {
     199      var range = document.selection.createRange();
     200      var bookmark = range.getBookmark();
     201      return bookmark.charCodeAt(2) - 2;
     202    }
     203    else {
     204      // Firefox Caret Position (TextArea)
     205      if (input.setSelectionRange)
     206       return input.selectionStart;
     207    }
     208
     209    return 0;
     210}
     211
     212function debounce(func, wait, immediate) {
     213  var timeout;
     214  return function() {
     215    var context = this, args = arguments;
     216    var later = function() {
     217      timeout = null;
     218      if (!immediate) func.apply(context, args);
     219    };
     220    var callNow = immediate && !timeout;
     221    clearTimeout(timeout);
     222    timeout = setTimeout(later, wait);
     223    if (callNow) func.apply(context, args);
     224  };
     225}
     226
     227}(jQuery));
Note: See TracChangeset for help on using the changeset viewer.