ScoDoc/app/static/libjs/jquery.field.min.js

12 lines
6.7 KiB
JavaScript
Raw Normal View History

2020-09-26 16:19:37 +02:00
/*
* jQuery Field Plug-in
*
* Copyright (c) 2011 Dan G. Switzer, II
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Version: 0.9.6
*/
(function($){var defaults={delimiter:",",checkboxRangeKeyBinding:"shiftKey",useArray:false};$.Field={version:"0.9.6",setDefaults:function(options){$.extend(defaults,options)},setProperty:function(prop,value){defaults[prop]=value},getProperty:function(prop){return defaults[prop]}};$.fn.fieldArray=function(v){var t=$type(v);if(t=="undefined")return getValue(this);if(t=="string"||t=="number"){v=v.toString().split(defaults.delimiter);t="array"}if(t=="array")return setValue(this,v);return this};$.fn.getValue=function(){return getValue(this).join(defaults.delimiter)};var getValue=function(jq){var v=[];jq.each(function(lc){var t=getType(this);switch(t){case"checkbox":case"radio":if(this.checked)v.push(this.value);break;case"select":if(this.type=="select-one"){v.push((this.selectedIndex==-1)?"":getOptionVal(this[this.selectedIndex]))}else{for(var i=0;i<this.length;i++){if(this[i].selected){v.push(getOptionVal(this[i]))}}}break;case"text":v.push(this.value);break}});return v};$.fn.setValue=function(v){return setValue(this,((!v&&(v!==0))?[""]:v.toString().split(defaults.delimiter)))};var setValue=function(jq,v){jq.each(function(lc){var t=getType(this),x;switch(t){case"checkbox":case"radio":if(valueExists(v,this.value))this.checked=true;else this.checked=false;break;case"select":var bSelectOne=(this.type=="select-one");var bKeepLooking=true;for(var i=0;i<this.length;i++){x=getOptionVal(this[i]);bSelectItem=valueExists(v,x);if(bSelectItem){this[i].selected=true;if(bSelectOne){bKeepLooking=false;break}}else if(!bSelectOne)this[i].selected=false}if(bSelectOne&&bKeepLooking&&!!this[0]){this[0].selected=true}break;case"text":this.value=v.join(defaults.delimiter);break}});return jq};$.fn.formHash=function(map,clear){var isGet=(arguments.length==0);var hash={};this.filter("form").each(function(){var els=this.elements,el,n,fields={},$el;for(var i=0,elsMax=els.length;i<elsMax;i++){el=els[i];n=el.name;if(!n||fields[n])continue;var $el=$(el.form[n]);if(isGet){hash[n]=$el[defaults.useArray?"fieldArray":"getValue"]()}else if(n in map){$el[defaults.useArray?"fieldArray":"setValue"](map[n])}else if(clear===true){$el[defaults.useArray?"fieldArray":"setValue"]("")}fields[n]=true}});return(isGet)?hash:this};$.fn.fieldHash=function(map,clear){var isGet=!(map&&typeof map=="object");var hash={},fields={};this.filter(":input").each(function(){var el=this,n=el.name;if(!n||fields[n])return;var $el=$(el.form[n]);if(isGet){hash[n]=$el[defaults.useArray?"fieldArray":"getValue"]()}else if(n in map){$el[defaults.useArray?"fieldArray":"setValue"](map[n])}else if(clear===true){$el[defaults.useArray?"fieldArray":"setValue"]("")}fields[n]=true});return(isGet)?hash:this};$.fn.autoAdvance=function(callback){return this.find(":text,:password,textarea").bind("keyup.autoAdvance",function(e){var $field=$(this),iMaxLength=parseInt($field.attr("maxlength"),10);if(isNaN(iMaxLength)||("|9|16|37|38|39|40|".indexOf("|"+e.keyCode+"|")>-1))return true;if($field.getValue().length>=$field.attr("maxlength")){var $next=$field.moveNext().select();if($.isFunction(callback))callback.apply($field,[$next])}})};$.fn.moveNext=function(){return this.moveIndex("next")};$.fn.movePrev=function(){return this.moveIndex("prev")};$.fn.moveIndex=function(i){var pos=getFieldPosition(this);if(i=="next")i=pos[0]+1;else if(i=="prev")i=pos[0]-1;if(i<0)i=pos[1].length-1;else if(i>=pos[1].length)i=0;return $(pos[1][i]).trigger("focus")};$.fn.getTabIndex=function(){return getFieldPosition(this)[0]};var getFieldPosition=function(jq){var $field=jq.filter("input, select, textarea").get(0),tabIndex=[],posIndex=[];if(!$field)return[-1,[]];$.each($field.form.elements,function(i,o){if(o.tagName!="FIELDSET"&&!o.disabled&&jQuery(o).is(":visible")){if(o.tabIndex>0){tabIndex.push(o)}else{posIndex.push(o)}}});tabIndex.sort(function(a,b){return a.tabIndex-b.tabIndex});tabIndex=$.merge(tabIndex,posIndex);for(var i=0;i<tabIndex.length;i++){if(tabIndex[i]==$field)return[i,tabIndex]}return[-1,tabIndex]};$.fn.limitSelection=function(limit,options){var opt=jQuery.extend((limit&&limit.constructor==Object?limit:{limit:limit,