7 lines
15 KiB
JavaScript
7 lines
15 KiB
JavaScript
|
/* http://keith-wood.name/timeEntry.html
|
||
|
Time entry for jQuery v1.3.1.
|
||
|
Written by Keith Wood (kbwood@virginbroadband.com.au) June 2007.
|
||
|
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
|
||
|
MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
|
||
|
Please attribute the author if you use it. */
|
||
|
(function($){function TimeEntry(){this._nextId=0;this._inst=[];this._disabledInputs=[];this.regional=[];this.regional['']={show24Hours:false,separator:':',ampmPrefix:'',ampmNames:['AM','PM'],spinnerTexts:['Now','Previous field','Next field','Increment','Decrement']};this._defaults={appendText:'',showSeconds:false,timeSteps:[1,1,1],initialField:0,useMouseWheel:true,defaultTime:null,minTime:null,maxTime:null,spinnerImage:'timeEntry.png',spinnerSize:[20,20,8],spinnerIncDecOnly:false,spinnerRepeat:[500,250],beforeShow:null};$.extend(this._defaults,this.regional[''])}$.extend(TimeEntry.prototype,{markerClassName:'hasTimeEntry',_register:function(a){var b=this._nextId++;this._inst[b]=a;return b},_getInst:function(a){return this._inst[a]||a},setDefaults:function(a){extendRemove(this._defaults,a||{})},_doFocus:function(a){var b=(a.nodeName&&a.nodeName.toLowerCase()=='input'?a:this);if($.timeEntry._lastInput==b){return}if($.timeEntry._isDisabledTimeEntry(b)){return}var c=$.timeEntry._getInst(b._timeId);c._input=$(b);$.timeEntry._focussed=true;$.timeEntry._lastInput=b;$.timeEntry._blurredInput=null;var d=c._get('beforeShow');extendRemove(c._settings,(d?d(b):{}));c._parseTime()},_doBlur:function(a){$.timeEntry._blurredInput=$.timeEntry._lastInput;$.timeEntry._lastInput=null},_doClick:function(a){var b=a.target;var c=$.timeEntry._getInst(b._timeId);if(!$.timeEntry._focussed){var d=c._get('separator').length+2;c._field=0;if($.browser.msie){var e=b.value;var f=a.clientX+$.timeEntry._findScroll(a.srcElement)[0]-$.timeEntry._findPos(a.srcElement)[0];for(var g=0;g<=Math.max(1,c._secondField,c._ampmField);g++){var h=(g!=c._ampmField?(g*d)+2:(c._ampmField*d)+c._get('ampmPrefix').length+c._get('ampmNames')[0].length);b.value=e.substring(0,h);var i=b.createTextRange();if(f<i.boundingWidth){c._field=g;break}}b.value=e}else{for(var g=0;g<=Math.max(1,c._secondField,c._ampmField);g++){var j=(g!=c._ampmField?(g*d)+2:(c._ampmField*d)+c._get('ampmPrefix').length+c._get('ampmNames')[0].length);if(b.selectionStart<j){c._field=g;break}}}}c._showField();$.timeEntry._focussed=false},_doKeyDown:function(a){if(a.keyCode>=48){return true}var b=$.timeEntry._getInst(this._timeId);switch(a.keyCode){case 9:return(a.shiftKey?b._previousField(true):b._nextField(true));case 35:if(a.ctrlKey){b._setValue('')}else{b._field=Math.max(1,b._secondField,b._ampmField);b._adjustField(0)}break;case 36:if(a.ctrlKey){b._setTime()}else{b._field=0;b._adjustField(0)}break;case 37:b._previousField(false);break;case 38:b._adjustField(+1);break;case 39:b._nextField(false);break;case 40:b._adjustField(-1);break;case 46:b._setValue('');break}return false},_doKeyPress:function(a){var b=String.fromCharCode(a.charCode==undefined?a.keyCode:a.charCode);if(b<' '){return true}var c=$.timeEntry._getInst(this._timeId);c._handleKeyPress(b);return false},_doMouseWheel:function(a,b){b=($.browser.opera?-b/Math.abs(b):b);var c=$.timeEntry._getInst(this._timeId);c._adjustField(b);a.preventDefault()},_connectTimeEntry:function(b,c){var d=$(b);if(d.is('.'+this.markerClassName)){return}var e=c._get('spinnerImage');var f=c._get('spinnerText');var g=c._get('spinnerSize');var h=c._get('appendText');var i=(!e?null:$('<span class="timeEntry_control" _timeid="'+c._id+'" style="display: inline-block; background: url(\''+e+'\') 0 0 no-repeat; '+'width: '+g[0]+'px; height: '+g[1]+'px;'+($.browser.mozilla&&$.browser.version.substr(0,3)!='1.9'?' padding-left: '+g[0]+'px; padding-bottom: '+(g[1]-18)+'px;':'')+'"></span>'));d.wrap('<span class="timeEntry_wrap"></span>').after(h?'<span class="timeEntry_append">'+h+'</span>':'').after(i||'');d.addClass(this.markerClassName).bind('focus.timeEntry',this._doFocus).bind('blur.timeEntry',this._doBlur).bind('click.timeEntry',this._doClick).bind('keydown.timeEntry',this._doKeyDown).bind('keypress.timeEntry',this._doKeyPress);if($.browser.mozilla){d.bind('input.timeEntry',function(a){c._parseTime()})}if($.browser.msie){d.bind('paste.timeEntry',function(a){setTimeout(function(){c._parseTime()},1)})}if(c._get('useMouseWheel')&&$.fn.mousewheel){d.mousewheel(this._doMouseWheel
|