6 lines
16 KiB
JavaScript
6 lines
16 KiB
JavaScript
|
/* http://keith-wood.name/timeEntry.html
|
||
|
Time entry for jQuery v2.0.1.
|
||
|
Written by Keith Wood (kbwood{at}iinet.com.au) June 2007.
|
||
|
Available under the MIT (https://github.com/jquery/jquery/blob/master/MIT-LICENSE.txt) license.
|
||
|
Please attribute the author if you use it. */
|
||
|
(function($){var n='timeEntry';$.JQPlugin.createPlugin({name:n,defaultOptions:{appendText:'',showSeconds:false,unlimitedHours:false,timeSteps:[1,1,1],initialField:null,noSeparatorEntry:false,tabToExit:false,useMouseWheel:true,defaultTime:null,minTime:null,maxTime:null,spinnerImage:'spinnerDefault.png',spinnerSize:[20,20,8],spinnerBigImage:'',spinnerBigSize:[40,40,16],spinnerIncDecOnly:false,spinnerRepeat:[500,250],beforeShow:null,beforeSetTime:null},regionalOptions:{'':{show24Hours:false,separator:':',ampmPrefix:'',ampmNames:['AM','PM'],spinnerTexts:['Now','Previous field','Next field','Increment','Decrement']}},_getters:['getOffset','getTime','isDisabled'],_appendClass:n+'-append',_controlClass:n+'-control',_expandClass:n+'-expand',_disabledInputs:[],_instSettings:function(a,b){return{_field:0,_selectedHour:0,_selectedMinute:0,_selectedSecond:0}},_postAttach:function(b,c){b.on('focus.'+c.name,this._doFocus).on('blur.'+c.name,this._doBlur).on('click.'+c.name,this._doClick).on('keydown.'+c.name,this._doKeyDown).on('keypress.'+c.name,this._doKeyPress).on('paste.'+c.name,function(a){setTimeout(function(){o._parseTime(c)},1)})},_optionsChanged:function(a,b,c){var d=this._extractTime(b);$.extend(b.options,c);b.options.show24Hours=b.options.show24Hours||b.options.unlimitedHours;b._field=0;if(d){this._setTime(b,new Date(0,0,0,d[0],d[1],d[2]))}a.next('span.'+this._appendClass).remove();a.parent().find('span.'+this._controlClass).remove();if($.fn.mousewheel){a.unmousewheel()}var e=(!b.options.spinnerImage?null:$('<span class="'+this._controlClass+'" style="display: inline-block; '+'background: url(\''+b.options.spinnerImage+'\') 0 0 no-repeat; width: '+b.options.spinnerSize[0]+'px; height: '+b.options.spinnerSize[1]+'px;"></span>'));a.after(b.options.appendText?'<span class="'+this._appendClass+'">'+b.options.appendText+'</span>':'').after(e||'');if(b.options.useMouseWheel&&$.fn.mousewheel){a.mousewheel(this._doMouseWheel)}if(e){e.mousedown(this._handleSpinner).mouseup(this._endSpinner).mouseover(this._expandSpinner).mouseout(this._endSpinner).mousemove(this._describeSpinner)}},enable:function(a){this._enableDisable(a,false)},disable:function(a){this._enableDisable(a,true)},_enableDisable:function(b,c){var d=this._getInst(b);if(!d){return}b.disabled=c;if(b.nextSibling&&b.nextSibling.nodeName.toLowerCase()==='span'){this._changeSpinner(d,b.nextSibling,(c?5:-1))}this._disabledInputs=$.map(this._disabledInputs,function(a){return(a===b?null:a)});if(c){this._disabledInputs.push(b)}},isDisabled:function(a){return $.inArray(a,this._disabledInputs)>-1},_preDestroy:function(b,c){b=$(b).off('.'+n);if($.fn.mousewheel){b.unmousewheel()}this._disabledInputs=$.map(this._disabledInputs,function(a){return(a===b[0]?null:a)});b.siblings('.'+this._appendClass+',.'+this._controlClass).remove()},setTime:function(a,b){var c=this._getInst(a);if(c){if(b===null||b===''){$(a).val('')}else{this._setTime(c,b?($.isArray(b)?b:(typeof b==='object'?new Date(b.getTime()):b)):null)}}},getTime:function(a){var b=this._getInst(a);var c=(b?this._extractTime(b):null);return(!c?null:new Date(0,0,0,c[0],c[1],c[2]))},getOffset:function(a){var b=this._getInst(a);var c=(b?this._extractTime(b):null);return(!c?0:(c[0]*3600+c[1]*60+c[2])*1000)},_doFocus:function(a){var b=(a.nodeName&&a.nodeName.toLowerCase()==='input'?a:this);if(o._lastInput===b||o.isDisabled(b)){o._focussed=false;return}var c=o._getInst(b);o._focussed=true;o._lastInput=b;o._blurredInput=null;$.extend(c.options,($.isFunction(c.options.beforeShow)?c.options.beforeShow.apply(b,[b]):{}));o._parseTime(c,a.nodeName?null:a);setTimeout(function(){o._showField(c)},10)},_doBlur:function(a){o._blurredInput=o._lastInput;o._lastInput=null},_doClick:function(a){var b=a.target;var c=o._getInst(b);var d=c._field;if(!o._focussed){c._field=o._getSelection(c,b,a)}if(d!==c._field){c._lastChr=''}o._showField(c);o._focussed=false},_getSelection:function(b,c,d){var e=0;var f=[b.elem.val().split(b.options.separator)[0].length,2,2];if(c.selectionStart!==null){var g=0;for(var h=0;h<=Math.max(1,b._secondField,b._ampmField);h++){g+=(h
|