From 1ead58101f9f6d92367f0d36f896ed4ffcfcd5c1 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Tue, 14 Feb 2012 11:51:45 +0100 Subject: [PATCH] [FIX] Datetime widget buggy since jquery.ui upgrade lp bug: https://launchpad.net/bugs/928918 fixed bzr revid: fme@openerp.com-20120214105145-pat0srumt8m7nq38 --- addons/web/__openerp__.py | 3 +- .../css/jquery-ui-timepicker-addon.css | 6 + .../js/jquery-ui-timepicker-addon.js | 765 ++++++++++++++---- 3 files changed, 598 insertions(+), 176 deletions(-) create mode 100644 addons/web/static/lib/jquery.ui.timepicker/css/jquery-ui-timepicker-addon.css rename addons/web/static/lib/{jquery.ui => jquery.ui.timepicker}/js/jquery-ui-timepicker-addon.js (52%) diff --git a/addons/web/__openerp__.py b/addons/web/__openerp__.py index 2eea9ebe9ae..41a58cd3a07 100644 --- a/addons/web/__openerp__.py +++ b/addons/web/__openerp__.py @@ -25,7 +25,7 @@ "static/lib/jquery.superfish/js/hoverIntent.js", "static/lib/jquery.superfish/js/superfish.js", "static/lib/jquery.ui/js/jquery-ui-1.8.17.custom.min.js", - "static/lib/jquery.ui/js/jquery-ui-timepicker-addon.js", + "static/lib/jquery.ui.timepicker/js/jquery-ui-timepicker-addon.js", "static/lib/jquery.ui.notify/js/jquery.notify.js", "static/lib/jquery.deferred-queue/jquery.deferred-queue.js", "static/lib/jquery.scrollTo/jquery.scrollTo-min.js", @@ -56,6 +56,7 @@ 'css' : [ "static/lib/jquery.superfish/css/superfish.css", "static/lib/jquery.ui/css/smoothness/jquery-ui-1.8.17.custom.css", + "static/lib/jquery.ui.timepicker/css/jquery-ui-timepicker-addon.css", "static/lib/jquery.ui.notify/css/ui.notify.css", "static/lib/jquery.tipsy/tipsy.css", "static/src/css/base.css", diff --git a/addons/web/static/lib/jquery.ui.timepicker/css/jquery-ui-timepicker-addon.css b/addons/web/static/lib/jquery.ui.timepicker/css/jquery-ui-timepicker-addon.css new file mode 100644 index 00000000000..e0d03c290af --- /dev/null +++ b/addons/web/static/lib/jquery.ui.timepicker/css/jquery-ui-timepicker-addon.css @@ -0,0 +1,6 @@ +.ui-timepicker-div .ui-widget-header { margin-bottom: 8px; } +.ui-timepicker-div dl { text-align: left; } +.ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; } +.ui-timepicker-div dl dd { margin: 0 10px 10px 65px; } +.ui-timepicker-div td { font-size: 90%; } +.ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; } diff --git a/addons/web/static/lib/jquery.ui/js/jquery-ui-timepicker-addon.js b/addons/web/static/lib/jquery.ui.timepicker/js/jquery-ui-timepicker-addon.js similarity index 52% rename from addons/web/static/lib/jquery.ui/js/jquery-ui-timepicker-addon.js rename to addons/web/static/lib/jquery.ui.timepicker/js/jquery-ui-timepicker-addon.js index 85c795c2e79..f83839ee488 100644 --- a/addons/web/static/lib/jquery.ui/js/jquery-ui-timepicker-addon.js +++ b/addons/web/static/lib/jquery.ui.timepicker/js/jquery-ui-timepicker-addon.js @@ -1,25 +1,26 @@ /* * jQuery timepicker addon * By: Trent Richardson [http://trentrichardson.com] -* Version 0.9.3 -* Last Modified: 02/05/2011 +* Version 0.9.9 +* Last Modified: 02/05/2012 * -* Copyright 2010 Trent Richardson +* Copyright 2012 Trent Richardson * Dual licensed under the MIT and GPL licenses. * http://trentrichardson.com/Impromptu/GPL-LICENSE.txt * http://trentrichardson.com/Impromptu/MIT-LICENSE.txt * * HERES THE CSS: -* .ui-timepicker-div .ui-widget-header{ margin-bottom: 8px; } -* .ui-timepicker-div dl{ text-align: left; } -* .ui-timepicker-div dl dt{ height: 25px; } -* .ui-timepicker-div dl dd{ margin: -25px 0 10px 65px; } +* .ui-timepicker-div .ui-widget-header { margin-bottom: 8px; } +* .ui-timepicker-div dl { text-align: left; } +* .ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; } +* .ui-timepicker-div dl dd { margin: 0 10px 10px 65px; } * .ui-timepicker-div td { font-size: 90%; } +* .ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; } */ (function($) { -$.extend($.ui, { timepicker: { version: "0.9.3" } }); +$.extend($.ui, { timepicker: { version: "0.9.9" } }); /* Time picker manager. Use the singleton instance of this class, $.timepicker, to interact with the time picker. @@ -32,12 +33,17 @@ function Timepicker() { currentText: 'Now', closeText: 'Done', ampm: false, + amNames: ['AM', 'A'], + pmNames: ['PM', 'P'], timeFormat: 'hh:mm tt', + timeSuffix: '', timeOnlyTitle: 'Choose Time', timeText: 'Time', hourText: 'Hour', minuteText: 'Minute', - secondText: 'Second' + secondText: 'Second', + millisecText: 'Millisecond', + timezoneText: 'Time Zone' }; this._defaults = { // Global defaults for all the datetime picker instances showButtonPanel: true, @@ -45,31 +51,44 @@ function Timepicker() { showHour: true, showMinute: true, showSecond: false, + showMillisec: false, + showTimezone: false, showTime: true, - stepHour: 0.05, - stepMinute: 0.05, - stepSecond: 0.05, + stepHour: 1, + stepMinute: 1, + stepSecond: 1, + stepMillisec: 1, hour: 0, minute: 0, second: 0, + millisec: 0, + timezone: '+0000', hourMin: 0, minuteMin: 0, secondMin: 0, + millisecMin: 0, hourMax: 23, minuteMax: 59, secondMax: 59, + millisecMax: 999, minDateTime: null, - maxDateTime: null, + maxDateTime: null, + onSelect: null, hourGrid: 0, minuteGrid: 0, secondGrid: 0, + millisecGrid: 0, alwaysSetTime: true, separator: ' ', altFieldTimeOnly: true, - showTimepicker: true + showTimepicker: true, + timezoneIso8609: false, + timezoneList: null, + addSliderAccess: false, + sliderAccessArgs: null }; $.extend(this._defaults, this.regional['']); -} +}; $.extend(Timepicker.prototype, { $input: null, @@ -79,19 +98,26 @@ $.extend(Timepicker.prototype, { hour_slider: null, minute_slider: null, second_slider: null, + millisec_slider: null, + timezone_select: null, hour: 0, minute: 0, second: 0, + millisec: 0, + timezone: '+0000', hourMinOriginal: null, minuteMinOriginal: null, secondMinOriginal: null, + millisecMinOriginal: null, hourMaxOriginal: null, minuteMaxOriginal: null, secondMaxOriginal: null, + millisecMaxOriginal: null, ampm: '', formattedDate: '', formattedTime: '', formattedDateTime: '', + timezoneList: null, /* Override the default settings for all instances of the time picker. @param settings object - the new settings to use as defaults (anonymous object) @@ -107,14 +133,7 @@ $.extend(Timepicker.prototype, { _newInst: function($input, o) { var tp_inst = new Timepicker(), inlineSettings = {}; - - tp_inst.hour = tp_inst._defaults.hour; - tp_inst.minute = tp_inst._defaults.minute; - tp_inst.second = tp_inst._defaults.second; - tp_inst.ampm = ''; - tp_inst.$input = $input; - for (var attrName in this._defaults) { var attrValue = $input.attr('time:' + attrName); if (attrValue) { @@ -126,30 +145,59 @@ $.extend(Timepicker.prototype, { } } tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, { - beforeShow: function(input, dp_inst) { + beforeShow: function(input, dp_inst) { if ($.isFunction(o.beforeShow)) - o.beforeShow(input, dp_inst, tp_inst); + return o.beforeShow(input, dp_inst, tp_inst); }, onChangeMonthYear: function(year, month, dp_inst) { // Update the time as well : this prevents the time from disappearing from the $input field. tp_inst._updateDateTime(dp_inst); if ($.isFunction(o.onChangeMonthYear)) - o.onChangeMonthYear(year, month, dp_inst, tp_inst); + o.onChangeMonthYear.call($input[0], year, month, dp_inst, tp_inst); }, onClose: function(dateText, dp_inst) { if (tp_inst.timeDefined === true && $input.val() != '') tp_inst._updateDateTime(dp_inst); if ($.isFunction(o.onClose)) - o.onClose(dateText, dp_inst, tp_inst); + o.onClose.call($input[0], dateText, dp_inst, tp_inst); }, timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker'); }); + tp_inst.amNames = $.map(tp_inst._defaults.amNames, function(val) { return val.toUpperCase() }); + tp_inst.pmNames = $.map(tp_inst._defaults.pmNames, function(val) { return val.toUpperCase() }); + + if (tp_inst._defaults.timezoneList === null) { + var timezoneList = []; + for (var i = -11; i <= 12; i++) + timezoneList.push((i >= 0 ? '+' : '-') + ('0' + Math.abs(i).toString()).slice(-2) + '00'); + if (tp_inst._defaults.timezoneIso8609) + timezoneList = $.map(timezoneList, function(val) { + return val == '+0000' ? 'Z' : (val.substring(0, 3) + ':' + val.substring(3)); + }); + tp_inst._defaults.timezoneList = timezoneList; + } + + tp_inst.hour = tp_inst._defaults.hour; + tp_inst.minute = tp_inst._defaults.minute; + tp_inst.second = tp_inst._defaults.second; + tp_inst.millisec = tp_inst._defaults.millisec; + tp_inst.ampm = ''; + tp_inst.$input = $input; if (o.altField) tp_inst.$altInput = $(o.altField) .css({ cursor: 'pointer' }) .focus(function(){ $input.trigger("focus"); }); - + + if(tp_inst._defaults.minDate==0 || tp_inst._defaults.minDateTime==0) + { + tp_inst._defaults.minDate=new Date(); + } + if(tp_inst._defaults.maxDate==0 || tp_inst._defaults.maxDateTime==0) + { + tp_inst._defaults.maxDate=new Date(); + } + // datepicker needs minDate/maxDate, timepicker needs minDateTime/maxDateTime.. if(tp_inst._defaults.minDate !== undefined && tp_inst._defaults.minDate instanceof Date) tp_inst._defaults.minDateTime = new Date(tp_inst._defaults.minDate.getTime()); @@ -159,7 +207,6 @@ $.extend(Timepicker.prototype, { tp_inst._defaults.maxDateTime = new Date(tp_inst._defaults.maxDate.getTime()); if(tp_inst._defaults.maxDateTime !== undefined && tp_inst._defaults.maxDateTime instanceof Date) tp_inst._defaults.maxDate = new Date(tp_inst._defaults.maxDateTime.getTime()); - return tp_inst; }, @@ -184,9 +231,12 @@ $.extend(Timepicker.prototype, { .replace(/h{1,2}/ig, '(\\d?\\d)') .replace(/m{1,2}/ig, '(\\d?\\d)') .replace(/s{1,2}/ig, '(\\d?\\d)') - .replace(/t{1,2}/ig, '(am|pm|a|p)?') - .replace(/\s/g, '\\s?') + '$', + .replace(/l{1}/ig, '(\\d?\\d?\\d)') + .replace(/t{1,2}/ig, this._getPatternAmpm()) + .replace(/z{1}/ig, '(z|[-+]\\d\\d:?\\d\\d)?') + .replace(/\s/g, '\\s?') + this._defaults.timeSuffix + '$', order = this._getFormatPositions(), + ampm = '', treg; if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]); @@ -195,27 +245,58 @@ $.extend(Timepicker.prototype, { // the time should come after x number of characters and a space. // x = at least the length of text specified by the date format var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat'); - regstr = '.{' + dp_dateFormat.length + ',}' + this._defaults.separator + regstr; + // escape special regex characters in the seperator + var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); + regstr = '^.{' + dp_dateFormat.length + ',}?' + this._defaults.separator.replace(specials, "\\$&") + regstr; } - + treg = timeString.match(new RegExp(regstr, 'i')); if (treg) { - if (order.t !== -1) - this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ? - '' : - (treg[order.t].charAt(0).toUpperCase() == 'A') ? 'AM' : 'PM').toUpperCase(); + if (order.t !== -1) { + if (treg[order.t] === undefined || treg[order.t].length === 0) { + ampm = ''; + this.ampm = ''; + } else { + ampm = $.inArray(treg[order.t].toUpperCase(), this.amNames) !== -1 ? 'AM' : 'PM'; + this.ampm = this._defaults[ampm == 'AM' ? 'amNames' : 'pmNames'][0]; + } + } if (order.h !== -1) { - if (this.ampm == 'AM' && treg[order.h] == '12') + if (ampm == 'AM' && treg[order.h] == '12') this.hour = 0; // 12am = 0 hour - else if (this.ampm == 'PM' && treg[order.h] != '12') + else if (ampm == 'PM' && treg[order.h] != '12') this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12 else this.hour = Number(treg[order.h]); } if (order.m !== -1) this.minute = Number(treg[order.m]); if (order.s !== -1) this.second = Number(treg[order.s]); + if (order.l !== -1) this.millisec = Number(treg[order.l]); + if (order.z !== -1 && treg[order.z] !== undefined) { + var tz = treg[order.z].toUpperCase(); + switch (tz.length) { + case 1: // Z + tz = this._defaults.timezoneIso8609 ? 'Z' : '+0000'; + break; + case 5: // +hhmm + if (this._defaults.timezoneIso8609) + tz = tz.substring(1) == '0000' + ? 'Z' + : tz.substring(0, 3) + ':' + tz.substring(3); + break; + case 6: // +hh:mm + if (!this._defaults.timezoneIso8609) + tz = tz == 'Z' || tz.substring(1) == '00:00' + ? '+0000' + : tz.replace(/:/, ''); + else if (tz.substring(1) == '00:00') + tz = 'Z'; + break; + } + this.timezone = tz; + } return true; @@ -223,12 +304,26 @@ $.extend(Timepicker.prototype, { return false; }, + //######################################################################## + // pattern for standard and localized AM/PM markers + //######################################################################## + _getPatternAmpm: function() { + var markers = []; + o = this._defaults; + if (o.amNames) + $.merge(markers, o.amNames); + if (o.pmNames) + $.merge(markers, o.pmNames); + markers = $.map(markers, function(val) { return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&') }); + return '(' + markers.join('|') + ')?'; + }, + //######################################################################## // figure out position of time elements.. cause js cant do named captures //######################################################################## _getFormatPositions: function() { - var finds = this._defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|t{1,2})/g), - orders = { h: -1, m: -1, s: -1, t: -1 }; + var finds = this._defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|l{1}|t{1,2}|z)/g), + orders = { h: -1, m: -1, s: -1, l: -1, t: -1, z: -1 }; if (finds) for (var i = 0; i < finds.length; i++) @@ -248,9 +343,10 @@ $.extend(Timepicker.prototype, { // Added by Peter Medeiros: // - Figure out what the hour/minute/second max should be based on the step values. // - Example: if stepMinute is 15, then minMax is 45. - hourMax = (o.hourMax - (o.hourMax % o.stepHour)).toFixed(0), - minMax = (o.minuteMax - (o.minuteMax % o.stepMinute)).toFixed(0), - secMax = (o.secondMax - (o.secondMax % o.stepSecond)).toFixed(0), + hourMax = parseInt((o.hourMax - ((o.hourMax - o.hourMin) % o.stepHour)) ,10), + minMax = parseInt((o.minuteMax - ((o.minuteMax - o.minuteMin) % o.stepMinute)) ,10), + secMax = parseInt((o.secondMax - ((o.secondMax - o.secondMin) % o.stepSecond)) ,10), + millisecMax = parseInt((o.millisecMax - ((o.millisecMax - o.millisecMin) % o.stepMillisec)) ,10), dp_id = this.inst.id.toString().replace(/([^A-Za-z0-9_])/g, ''); // Prevent displaying twice @@ -267,14 +363,16 @@ $.extend(Timepicker.prototype, { hourGridSize = 0, minuteGridSize = 0, secondGridSize = 0, + millisecGridSize = 0, size; - - if (o.showHour && o.hourGrid > 0) { - html += '
' + - '
' + - '
'; - for (var h = o.hourMin; h < hourMax; h += o.hourGrid) { + // Hours + html += '
'; + if (o.showHour && o.hourGrid > 0) { + html += '
'; + + for (var h = o.hourMin; h <= hourMax; h += parseInt(o.hourGrid,10)) { hourGridSize++; var tmph = (o.ampm && h > 12) ? h-12 : h; if (tmph < 10) tmph = '0' + tmph; @@ -286,48 +384,69 @@ $.extend(Timepicker.prototype, { html += ''; } - html += '
' + tmph + '
' + - '
'; - } else html += '
'; + html += ''; + } + html += ''; + // Minutes html += '
' + o.minuteText + '
'; + ((o.showMinute) ? '' : noDisplay) + '>' + o.minuteText + ''+ + '
'; if (o.showMinute && o.minuteGrid > 0) { - html += '
' + - '
' + - '
'; + html += '
'; - for (var m = o.minuteMin; m < minMax; m += o.minuteGrid) { + for (var m = o.minuteMin; m <= minMax; m += parseInt(o.minuteGrid,10)) { minuteGridSize++; html += ''; } - html += '
' + ((m < 10) ? '0' : '') + m + '
' + - '
'; - } else html += '
'; + html += ''; + } + html += ''; + // Seconds html += '
' + o.secondText + '
'; + ((o.showSecond) ? '' : noDisplay) + '>' + o.secondText + ''+ + '
'; if (o.showSecond && o.secondGrid > 0) { - html += '
' + - '
' + - '
'; + html += '
'; - for (var s = o.secondMin; s < secMax; s += o.secondGrid) { + for (var s = o.secondMin; s <= secMax; s += parseInt(o.secondGrid,10)) { secondGridSize++; html += ''; } - html += '
' + ((s < 10) ? '0' : '') + s + '
' + - '
'; - } else html += '
'; + html += ''; + } + html += ''; + + // Milliseconds + html += '
' + o.millisecText + '
'+ + '
'; + + if (o.showMillisec && o.millisecGrid > 0) { + html += '
'; + + for (var l = o.millisecMin; l <= millisecMax; l += parseInt(o.millisecGrid,10)) { + millisecGridSize++; + html += ''; + } + + html += '
' + ((l < 10) ? '0' : '') + l + '
'; + } + html += '
'; + + // Timezone + html += '
' + o.timezoneText + '
'; + html += '
'; html += ''; $tp = $(html); @@ -353,6 +472,7 @@ $.extend(Timepicker.prototype, { } }); + // Updated by Peter Medeiros: // - Pass in Event and UI instance into slide function this.minute_slider = $tp.find('#ui_tpicker_minute_'+ dp_id).slider({ @@ -362,7 +482,6 @@ $.extend(Timepicker.prototype, { max: minMax, step: o.stepMinute, slide: function(event, ui) { - // update the global minute slider instance value with the current slider value tp_inst.minute_slider.slider( "option", "value", ui.value); tp_inst._onTimeChange(); } @@ -380,6 +499,31 @@ $.extend(Timepicker.prototype, { } }); + this.millisec_slider = $tp.find('#ui_tpicker_millisec_'+ dp_id).slider({ + orientation: "horizontal", + value: this.millisec, + min: o.millisecMin, + max: millisecMax, + step: o.stepMillisec, + slide: function(event, ui) { + tp_inst.millisec_slider.slider( "option", "value", ui.value); + tp_inst._onTimeChange(); + } + }); + + this.timezone_select = $tp.find('#ui_tpicker_timezone_'+ dp_id).append('').find("select"); + $.fn.append.apply(this.timezone_select, + $.map(o.timezoneList, function(val, idx) { + return $("