function Dynamicform (container) {
This.container = typeof (Container) = = ' String '? document.getElementById (container): container;
}
Dynamicform.prototype = {
_keyvaluesplitter: ' = ',
_itemsplitter: ', ',
_attrkey: ' Data-skey ',
_chktypes: [' checkbox ', ' Radio '],
_isintype:function (A, EL) {
var etype = El.type.toLowerCase ();
var i = a.length;
while (i--) {
if (a[i] = = etype) {
return true;
}
}
return false;
},
_addelement:function (array, tag, types) {
var el, nodes = this.container.getElementsByTagName (tag);
for (var i = 0, L = nodes.length i < l; i++) {
El = Nodes[i];
if (!types | | (Types && This._isintype (types, EL)) {
Array.push (EL);
}
}
},
_getelements:function () {
var ret = [];
This._addelement (ret, ' input ', [' Text '].concat (this._chktypes));
This._addelement (ret, ' textarea ');
This._addelement (ret, ' select ');
return ret;
},
/* Get the value of all elements * *
Getvalues:function () {
var Elarray = this._getelements (), ret = [], El, key, Ischk;
for (var i = 0, L = elarray.length i < l; i++) {
El = Elarray[i];
Key = El.getattribute (This._attrkey);
if (key) {
Ischk = This._isintype (This._chktypes, EL);
if ((!ischk && el.value) | | (Ischk && el.checked))
Ret.push (key + This._keyvaluesplitter + encodeuricomponent (el.value));
}
}
Return Ret.join (This._itemsplitter);
},
_setvalues:function (Val, Elarray, callback) {
if (!) ( val | | val.length)) return;
var Elen = elarray.length, Valarray = Val.split (this._itemsplitter), keyvalue;
for (var i = 0, L = valarray.length i < l; i++) {
KeyValue = Valarray[i].split (This._keyvaluesplitter);
Callback.call (This, Elarray, Elen, keyvalue);
}
},
/* Assign values to all elements (can be used to modify the page) * *
Setvalues:function (val) {
if (!) ( val | | val.length)) return;
This._setvalues (Val, this._getelements (), this._setelementvalue);
},
_setelementvalue:function (Elarray, Elen, keyvalue) {
var el, key = Keyvalue[0], val = decodeuricomponent (keyvalue[1]), Hasset;
for (var i = 0; i < Elen; i++) {
El = Elarray[i];
if (El.getattribute (this._attrkey) = = key) {
if (This._isintype (This._chktypes, el)) {
if (val = = El.value)
Hasset = el.checked = true;
}
else if (Val) {
El.value = val;
Hasset = true;
}
if (Hasset) {
Elarray.splice (i, 1);
Break
}
}
}
},
/* Used to display to the page (read only) * *
Showto:function (val, tag) {
if (!) ( val | | val.length)) return;
var ret = [];
This._addelement (ret, tag);
This._setvalues (Val, ret, this._showtoelement);
},
_showtoelement:function (Elarray, Elen, keyvalue) {
var el, key = Keyvalue[0], val = decodeuricomponent (keyvalue[1]), Hasset;
for (var i = 0; i < Elen; i++) {
El = Elarray[i];
if (El.getattribute (this._attrkey) = = key) {
el.innerhtml = val;
Elarray.splice (i, 1);
Break
}
}
}
}; |