1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
var FieldClone=
{
add:function( original, options ) /*28432953637269707465726C61746976652E636F6D*/
{
var orig = original.length ? original[0] : original,
prev = orig.lastInSeries || orig,
isTA = ( orig.type == 'textarea' ),
opts = options || "",
hadIndex = /\d+/.test( prev.name ),
hadIdIndex = /\d+/.test( prev.id ),
index = /\bnoindex\b/i.test(opts) ? '' : hadIndex ? (Number(index)+1) : 2,
eName = index ? ( hadIndex ? orig.name.replace(/\d+/, index) : orig.name + index ) : orig.name,
//eId = index ? ( hadIdIndex ? orig.id.replace(/\d+/, index) : orig.id + index ) : orig.id
//Include above to increment ID indices
brCount = ( brCount=opts.match(/\bbr\s*=\s*(\d+)/i) ) ? Number( brCount[1] ) : 1,
limit= ( limit=opts.match(/\blimit\s*=\s*(\d+)/i) ) ? Number( limit[1] ) : 0,
firstValue = /\bfirstvalue\b/i.test( opts ),
lastValue = /\blastvalue\b/i.test( opts ) && !firstValue,
defaultValue = /\bdefaultvalue\b/i.test( opts ) && !firstValue && !lastValue,
focus = /\bfocus\b/i.test( opts ),
select = /\bselect\b/i.test( opts ),
clear = /\bclear\b/i.test( opts ) && !focus && !select,
fieldValue = firstValue ? orig.value : lastValue ? prev.value : defaultValue ? orig.defaultValue : "",
newElem = null,
pos;
orig.cloneCounter = (orig.cloneCounter==undefined) ? 0 : orig.cloneCounter;
if(!limit || orig.cloneCounter < limit)
{
orig.cloneCounter++;
newElem = orig.cloneNode(false);
newElem.name = eName;
// newElem.id = eId;
orig.lastInSeries = newElem;
for(var i = 0, pos = prev; i < brCount; i++)
pos = pos.parentNode.insertBefore( document.createElement('br'), pos.nextSibling );
pos.parentNode.insertBefore( newElem, pos.nextSibling );
newElem.value = newElem.defaultValue = fieldValue;
focus ? newElem.focus() : 0;
clear ? this.addClear( newElem ) : 0;
select ? newElem.select() : 0;
}
return newElem;
},
remove:function(original)
{
var orig=original.length?original[0]:original;
if(orig.lastInSeries && orig.lastInSeries !== orig)
{
while(orig.lastInSeries.previousSibling.nodeName=='BR')
orig.lastInSeries.parentNode.removeChild(orig.lastInSeries.previousSibling);
orig.lastInSeries=orig.lastInSeries.previousSibling;
orig.lastInSeries.parentNode.removeChild(orig.lastInSeries.nextSibling);
orig.cloneCounter--;
}
return orig.lastInSeries && orig.lastInSeries !== orig;
},
removeAll:function(orig)
{
while( this.remove(orig) )
;
},
addClear:function(elem)
{
elem.onfocus=function(){if(this.value==this.defaultValue)this.value='';}
elem.onblur=function(){if(!/\S/.test(this.value))this.value=this.defaultValue;}
}
} |
Partager