/** * Class : JSL.dom * Handles all the DOM functions. * Arguments: The argument can be the ID of an element, a DOM node reference or a CSS query. * Example: * JSL.dom("element-id") * JSL.dom(document.getElementById("element-id")) * JSL.dom("span a.external-links") */ (function() { function _dom_init(arguments) { var selected_elements = []; for(var i=0, args_length = arguments.length; i{"left":leftx,"x":leftx,"top":topy,"y":topy} * Example: var position = JSL.dom("element-id").getPosition(); */ "getPosition": function() { var ele = this.nodes.array[0]; //You can have the position for only 1 element. var leftx = topy = 0; if (ele.offsetParent) { leftx = ele.offsetLeft; topy = ele.offsetTop; while (ele = ele.offsetParent) { leftx += ele.offsetLeft; topy += ele.offsetTop; } } return {"left":leftx,"x":leftx,"top":topy,"y":topy}; }, /** * Get the specified style of the active element. * Inspired by http://www.quirksmode.org/dom/getstyles.html * Argument: property - The name of the property that must be fetched. * Example: JSL.dom("element-id").getStyle("width"); */ "getStyle": function(property) { var ele = this.nodes.array[0]; if (ele.currentStyle) { var alt_property_name = property.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});//background-color becomes backgroundColor var value = ele.currentStyle[property]||ele.currentStyle[alt_property_name]; } else if (window.getComputedStyle) { property = property.replace(/([A-Z])/g,"-$1").toLowerCase();//backgroundColor becomes background-color var value = document.defaultView.getComputedStyle(ele,null).getPropertyValue(property); } //Some properties are special cases if(property == "opacity" && ele.filter) value = (parseFloat( ele.filter.match(/opacity\=([^)]*)/)[1] ) / 100); else if(property == "width" && isNaN(value)) value = ele.clientWidth || ele.offsetWidth; else if(property == "height" && isNaN(value)) value = ele.clientHeight || ele.offsetHeight; //Remove the 'px' from the end of values if(typeof value == "string" && value.match(/^\d+px$/)) { value = Number(value.replace(/px/, "")); } return value; }, /** * Set the style of the element. * Example: * JSL.dom("element").setStyle("position", "absolute"); * OR * JSL.dom("element").setStyle({ * "position":"absolute", * "top":"50px", * "left":"100px" * }); */ "setStyle": function(property, value) { var all_styles = {}; if(typeof property === "string") all_styles[property] = value; else all_styles = property; this.nodes.each(function(ele) { JSL.array(all_styles).each(function(value, property, all, ele) { property = property.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});//background-color becomes backgroundColor //Append a 'px' at the end of all numbers. if(value && value.constructor == Number) { var non_px = JSL.array(['zIndex','fontWeight','opacity','zoom','lineHeight']); //...except for these ones if( non_px.indexOf(property) == -1 && value.toString().indexOf("px") == -1 ) value += 'px'; } if(property == "opacity") { ele.style.opacity = value; ele.style.filter = 'alpha(opacity='+value+')'; //IE } else { ele.style[property] = value; } },ele); }); return this; }, /** * Shows all currently selected elements. * Arguments: * display[OPTIONAL] - could be 'visible', 'inline' or 'block'. If you chose 'visible', the function changes the 'visibility' property instead of the 'display' property. Defaults to 'block'. * Example: * JSL.dom("example").show() * JSL.dom("example").show("visible") */ "show" : function(display) { this.nodes.each(function(ele) { if(display === "visible") ele.style.visibility = "visible"; else if(display === "inline") ele.style.display = "inline"; else ele.style.display = "block"; }); return this; }, /** * Hides all the currently selected elements * Arguments: * display[OPTIONAL] - could be 'hidden', 'none'. If you chose 'hidden', the function changes the 'visibility' property instead of the 'display' property. Defaults to 'none'. * Example: * JSL.dom("example").hide() */ "hide" : function(display) { this.nodes.each(function(ele) { if(display === "hidden") ele.style.visibility = "hidden"; else ele.style.display = "none"; }); return this; }, /** * Toggles the selected elements between hidden and displayed state. It the element is shown, * one toggle call will make it hidden - and if its hidden, a toggle call will show it. * Example: * JSL.dom("example").toggle() */ "toggle": function() { this.nodes.each(function(ele) { if(ele.style.display != "block" ) ele.style.display = "block"; else ele.style.display = "none"; }); // :TODO: Write tests return this; }, ///Alias of toggle - I often confuse the two. [IGNORE] "toogle":function(){this.toggle.apply(this, arguments);return this;}, ///////////////////////////////// Events ///////////////////////// /** * Attach an event to a function. * Arguments: event - The event to watch for. * function - The function that shoulb be called on that event. * Example: * JSL.dom(window).on("unload", goodbye_func); */ "on" : function(event, func) { this.nodes.each(function(ele) { JSL.event().add(ele, event, func); }); return this; }, /** * Attach the click event to the specified function for all the selected elements * Argument: func - The function that should be called on the 'onclick' event. * Example: * JSL.dom("a.delete-links").click(confirm_delete); */ "click":function(func){return this.on("click", func);}, /** * Attach the load event to the specified function. Usually its only used with the window element * Argument: func - The function that should be called on document 'onload' event. * Example: * JSL.dom(window).load(init); */ "load":function(func){return this.on("load", func);}, //////////////// The Privates ////////////////// /// CSS Selectors - Taken from http://www.openjs.com/scripts/dom/css_selector/css_selector.js "_select" : function(all_selectors) { //Find what the selector is... var type = this._getType(all_selectors); if(type === "id") { //Superfast processing for IDs var ele = document.getElementById(all_selectors.replace("#","")); //To make sure we get the non existant elements if(ele) return [ele]; else return []; } var selected = new Array(); if(!document.getElementsByTagName) return selected; //It must be a very old browser all_selectors = all_selectors.replace(/\s*([^\w\.\#])\s*/g,"$1");//Remove the 'beautification' spaces var selectors = all_selectors.split(","); var getElementsByTagName = function(context, tag) { ///[IGNORE] if (tag == '*') return (context.all) ? context.all : context.getElementsByTagName("*"); // For IE. return context.getElementsByTagName(tag); } // Grab all of the tagName elements within current context var getElements = function(context,tag) { ///[IGNORE] if (!tag) tag = '*'; // Get elements matching tag, filter them for class selector var found = []; for (var i=0,len=context.length; con=context[i],ileft_bracket && pos");//Child selectors if(pos+1 && !(pos>left_bracket && pos"); var tag = parts[0]; var child = parts[1]; var found = getElements(context, tag); //Get the tags in the current context. context = []; for (var l=0,len=found.length; fnd=found[l],lleft_bracket && posleft_bracket && pos 0 || str.indexOf(".")+1 || str.indexOf(" ")+1 || str.indexOf(",")+1 || str.indexOf("[")+1 || str.indexOf("*")+1 || str.indexOf("+")+1 || str.indexOf(">")+1) return "css"; //A CSS Selector else if(JSL.array(this.valid_tags).indexOf(str)+1) return "tag"; //Its a tag. else return "id"; //An id, perhabs? } else { return "node"; } } } window.JSL["dom"] = function() { var return_vaule = new _dom_init(arguments); if(return_vaule.return_null) return null; return return_vaule; } })(); /* :TODO: * */