diff options
author | Keith Wood <kbwood.au@gmail.com> | 2009-07-22 08:37:28 +0000 |
---|---|---|
committer | Keith Wood <kbwood.au@gmail.com> | 2009-07-22 08:37:28 +0000 |
commit | ef4000d9df88a4d9dd3bcae72439c078eff8634b (patch) | |
tree | 098bc2517be617eb5fad6a6bc0feae075e304987 /ui/ui.datepicker.js | |
parent | d2bd01aecbd71920523dd08788211db037d5bccc (diff) | |
download | jquery-ui-ef4000d9df88a4d9dd3bcae72439c078eff8634b.tar.gz jquery-ui-ef4000d9df88a4d9dd3bcae72439c078eff8634b.zip |
Datepicker: Fixed #3891 Autosize input field
Diffstat (limited to 'ui/ui.datepicker.js')
-rw-r--r-- | ui/ui.datepicker.js | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/ui/ui.datepicker.js b/ui/ui.datepicker.js index b88460456..5f1891fd8 100644 --- a/ui/ui.datepicker.js +++ b/ui/ui.datepicker.js @@ -100,7 +100,8 @@ function Datepicker() { altField: '', // Selector for an alternate field to store selected dates into altFormat: '', // The date format to use for the alternate field constrainInput: true, // The input is constrained by the current date format - showButtonPanel: false // True to show button panel, false to not show it + showButtonPanel: false, // True to show button panel, false to not show it + autoSize: false // True to size the input for the date format, false to leave as is }; $.extend(this._defaults, this.regional['']); this.dpDiv = $('<div id="' + this._mainDivId + '" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible"></div>'); @@ -206,9 +207,36 @@ $.extend(Datepicker.prototype, { }).bind("getData.datepicker", function(event, key) { return this._get(inst, key); }); + this._autoSize(inst); $.data(target, PROP_NAME, inst); }, + /* Apply the maximum length for the date format. */ + _autoSize: function(inst) { + if (this._get(inst, 'autoSize') && !inst.inline) { + var date = new Date(2009, 12 - 1, 20); // Ensure double digits + var dateFormat = this._get(inst, 'dateFormat'); + if (dateFormat.match(/[DM]/)) { + var findMax = function(names) { + var max = 0; + var maxI = 0; + for (var i = 0; i < names.length; i++) { + if (names[i].length > max) { + max = names[i].length; + maxI = i; + } + } + return maxI; + }; + date.setMonth(findMax(this._get(inst, (dateFormat.match(/MM/) ? + 'monthNames' : 'monthNamesShort')))); + date.setDate(findMax(this._get(inst, (dateFormat.match(/DD/) ? + 'dayNames' : 'dayNamesShort'))) + 20 - date.getDay()); + } + inst.input.attr('size', this._formatDate(inst, date).length); + } + }, + /* Attach an inline date picker to a div. */ _inlineDatepicker: function(target, inst) { var divSpan = $(target); @@ -395,6 +423,7 @@ $.extend(Datepicker.prototype, { } var date = this._getDateDatepicker(target); extendRemove(inst.settings, settings); + this._autoSize(inst); this._setDateDatepicker(target, date); this._updateDatepicker(inst); } |