1// See: http://www.reach1to1.com/sandbox/jquery/testform.html 2// Changes for ActionKit: 3// 4// Tacked a call to actionkit.forms.onPrefillerLoaded() at the end. 5// 6// Also made it check that an argument is *defined* rather than if 7// it's *true*, so passing in {somefield: ''} works 8// 9// Also, made it call onchange() if one is setup. Used by the 10// donation form to keep Total updated, for example. 11 12/** 13 * This function sets the values of form element variables from an array 14 * This is the reverse process of Mark Constable's serialize function 15 * It is expected to be used as a call back for an ajax call that retrieves the form data 16 * @param data : array or hash containing name,value pairs for elements in the form 17 * 18 * Examples 19 * 20 * 1. Deserialize from an array 21 * $('#form-id').deserialize([{'name':'firstname','value':'John'},{'name':'lastname','value':'Resig'}]); 22 * 23 * 2. Deserialize from a hash(object) 24 * $('#form-id').deserialize({'firstname':'John','lastname':'Resig'}); 25 * 26 * 3. Deserialize multiple config for select/radio/checkbox 27 * $('#form-id').deserialize({'toppings':['capsicum','mushroom','extra_cheese'],'size':'medium'}) 28 * which will set the corresponding select/radio/checkbox config for toppings 29 * 30 * 3. Deserialize multiple config for select/radio/checkbox and with isPHPnaming = true 31 * $('#form-id').deserialize({'toppings':['capsicum','mushroom','extra_cheese'],'size':'medium'},{isPHPnaming:true}) 32 * which will set the corresponding select/radio/checkbox config for toppings, but will ignore select names ending with [] 33 * 34 * @return the jQuery Object 35 * @author Ashutosh Bijoor ([email protected]) 36 * @version 0.35 37 */ 38 39$.fn.deserialize = function(d,config) { 40 var data= d; 41 me = this; 42 43 if (d === undefined) { 44 return me; 45 } 46 47 config = $.extend({ isPHPnaming : false, 48 overwrite : true},config); 49 50 // check if data is an array, and convert to hash, converting multiple entries of 51 // same name to an array 52 if (d.constructor == Array) { 53 data={}; 54 for(var i=0; i<d.length; i++) { 55 if (typeof data[d[i].name] != 'undefined') { 56 if (data[d[i].name].constructor!= Array) { 57 data[d[i].name]=[data[d[i].name],d[i].value]; 58 } else { 59 data[d[i].name].push(d[i].value); 60 } 61 } else { 62 data[d[i].name]=d[i].value; 63 } 64 } 65 } 66 67 // now data is a hash. insert each parameter into the form 68 $('input,select,textarea',me) 69 .each(function() { 70 var p=this.name; 71 var v = []; 72 73 // handle wierd PHP names if required 74 if (config.isPHPnaming) { 75 p=p.replace(/\[\]$/,''); 76 } 77 if(p && data[p] != undefined) { 78 v = data[p].constructor == Array ? data[p] : [data[p]]; 79 } 80 // Additional parameter overwrite 81 if (config.overwrite === true || (typeof(data[p]) != 'undefined')) { 82 switch(this.type || this.tagName.toLowerCase()) { 83 case "radio": 84 case "checkbox": 85 this.checked=false; 86 for(var i=0;i<v.length;i++) { 87 this.checked|=(this.value!='' && v[i]==this.value); 88 } 89 break; 90 case "select-multiple" || "select": 91 for( i=0;i<this.options.length;i++) { 92 this.options[i].selected=false; 93 for(var j=0;j<v.length;j++) { 94 this.options[i].selected|=(this.options[i].value!='' && this.options[i].value==v[j]); 95 } 96 } 97 break; 98 case "button": 99 case "submit": 100 this.value=v.length>0?v.join(','):this.value; 101 break; 102 default: 103 this.value=v.join(','); 104 } 105 106 // call the onchange event if one is setup 107 if (this.onchange) 108 this.onchange(); 109 } 110 }); 111 return me; 112}; 113 114actionkit.forms.onPrefillerLoaded();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.