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
|
/* ===============================
| DOMSCRIPTER.JS
| Copyright, Andy Croxall (mitya@mitya.co.uk)
| For documentation and demo see http://www.mitya.co.uk/scripts/Domscripter---reduce-your-DOM-scripting-code-98
|
| USAGE
| This script may be used, distributed and modified freely but this header must remain in tact.
| For usage info and demo, including info on args and params, see www.mitya.co.uk/scripts
=============================== */
domscripter = function(params, debug) {
//utility event registration method - mitya's clever method whereby even in IE 'this' inside the callback represents the element the event was bound to, not window
arguments.callee.registerEvent = function(on, ev, func) {
if (window.addEventListener)
on.addEventListener(ev, func, false);
else {
if (!on.funcsCounter) on.funcsCounter = 0;
on['func'+on.funcsCounter] = func;
on.funcsCounter++;
on.attachEvent("on"+ev, function() { for(var i=0; i<on.funcsCounter; i++) on['func'+i](window.event); });
}
}
//die if insufficient args passed
if (typeof params != "object" || !params.element || !params.parent) return false;
//if we get this far, params OK. Build.
var node = document.createElement(params.element);
//add attributes
if (typeof params.attrs == "object") {
for (var w in params.attrs) {
var attrName = w != 'classs' ? w : 'className';
node[attrName] = params.attrs[w];
}
}
//add options if element == 'select'
if (params.element.toLowerCase() == 'select' && typeof params.options == 'object') {
var counter = 0;
for(var r in params.options) {
var selected = !params.indexSelected ? (counter == 0 ? 'selected' : null) : (params.indexSelected == counter ? 'selected' : null);
var opt = arguments.callee({element: 'option', parent: node, textNode: r, attrs: {selected: selected, value: params.options[r]}});
node.appendChild(opt);
counter++;
}
}
//populate with text?
if (params.textNode)
node.appendChild(document.createTextNode(params.textNode));
//if element is field or select and label passed, create label too
if ((" "+['input', 'textarea', 'select'].join(" ")+" ").match(eval("/\\b"+params.element+"\\b/")) && params.label)
arguments.callee({element: 'label', fadeIn: params.fadeIn, parent: params.parent, html: params.label, styles: params.labelStyles});
//register events
if (typeof params.evts == "object") {
for (var w in params.evts) arguments.callee.registerEvent(node, w, params.evts[w]);
}
//inner HTML?
if (params.html) node.innerHTML = params.html;
//add styles
if (typeof params.styles == "object") {
for (var y in params.styles) node.style[y] = params.styles[y];
}
//publish
!params.insertBefore ? params.parent.appendChild(node) : params.parent.insertBefore(node, params.insertBefore);
//if fadeIn, start as hidden
if (params.fadeIn) {
node.style.display = 'none';
$(node).fadeIn('slow');
}
//add clear afterwards?
if (params.clear)
arguments.callee({element: 'div', parent: params.parent, styles: {clear: params.clear}});
//add br afterwards?
if (params.br)
arguments.callee({element: 'br', parent: params.parent});
//assign new element to var, if called in an assignment
return node;
} |
Partager