/* * episerverscriptmanager.js - JavaScript support routines for EPiServer * Copyright (c) 2007 EPiServer AB */ var EPi; if (!EPi) { EPi = {}; } EPi._loadEvents = new Array(); // ------------------------ // Traversing DOM // ------------------------ // Returns the ASP.NET form EPi.GetForm = function() { if (window.theForm) { // theForm is a global reference to the main form specified by asp.net. return theForm; } else { return document.forms[0]; } } // GetDocument returns the document for the supplied window object. // Useful to get the document object for an iframe. EPi.GetDocument = function(windowObject) { var doc = null; if (windowObject) { try{ if (windowObject.contentDocument) { // For Gecko doc = windowObject.contentDocument; try{ // Gecko fails with permission denied when you try to access document // properties and methods, but the error object is just a string. var tmp = doc.location; } catch(e) { return null; } } else if (windowObject.contentWindow) { // For >= IE5.5 doc = windowObject.contentWindow.document; } else if (windowObject.document) { // For IE5 doc = windowObject.document; } } catch(exception) { if ((exception.number & 0xFFFF) == 5) // Access denied { // If we get access denied for some frame we ignore it, since we're not allowed to script against that window. doc = null; } else { throw exception; } } } return doc; } // GetDocumentWindow returns the window object for the supplied DOM node object. EPi.GetDocumentWindow = function(node) { if (node && node.nodeType != 9) // nodeType: 9 = document { node = node.ownerDocument; } if (node.defaultView) { // Gecko return node.defaultView; } else if (node.parentWindow) { // IE return node.parentWindow; } } // GetWindowSize returns the innerWidth and innerHeight as an array (innerWidth, innerHeight) // of the supplied window (or top window if no window as argument). EPi.GetWindowSize = function(win) { if (!win) { var win = window.top; } var innerWidth; var innerHeight; if (win.innerWidth) { innerWidth = win.innerWidth; innerHeight = win.innerHeight; } else if (win.document) { if (win.document.documentElement.clientWidth) { innerWidth = win.document.documentElement.clientWidth; innerHeight = win.document.documentElement.clientHeight; } else { innerWidth = win.document.body.clientWidth; innerHeight = win.document.body.clientHeight; } } return [innerWidth, innerHeight]; } // Returns an array of elements containing the specified className EPi.GetElementsByClassName = function(tagName, className, parentNode) { return EPi.GetElementsByAttribute(tagName, "className", className, parentNode, true); } // Returns an array of elements with the certain attributeName (and if attributeValue is specified with the certain attributeValue). // Optional tagName. If not specified searches all tagNames. // Optional parentNode. Start search from here. If not specified searches complete page. // Optional doSearchInAttribute. // Example: // var nodeArray = EPi.GetElementsByAttribute("input", "type", "text") returns all in page. // var nodeArray = EPi.GetElementsByClassName(null, "hidden", document.getElementById("elementContainer")); // returns an array of elements of any type with one of its classNames set to "hidden". EPi.GetElementsByAttribute = function(tagName, attributeName, attributeValue, parentNode, doSearchInAttribute) { if (!tagName) {tagName = "*";} if (!parentNode) {parentNode = document;} var childNodes = parentNode.getElementsByTagName(tagName); var i; var matchingNodesArray = new Array(); if (doSearchInAttribute) { var pattern = new RegExp(attributeValue); } for (i=0; i< childNodes.length; i++) { if (doSearchInAttribute && pattern.test(childNodes[i][attributeName])) { matchingNodesArray.push(childNodes[i]); } else if (attributeValue == null || childNodes[i][attributeName] == attributeValue) { matchingNodesArray.push(childNodes[i]); } } return matchingNodesArray; } EPi.GetCurrentStyle = function(element, styleProperty) { if (element.currentStyle) { return element.currentStyle[styleProperty]; } else if (document.defaultView && document.defaultView.getComputedStyle) { // Due to bug in mozilla we cant get Computed Style if the window containing // document is display: none. try { var computedStyles = document.defaultView.getComputedStyle(element, null); if (!computedStyles) { return null; } return computedStyles[styleProperty]; } catch (e) { return null; } } } // Used to make overloaded Add and Remove Event methods. Value is either a domObject or a domId. Returns a domObject. EPi._GetDomObject = function(value) { if (typeof(value) != "object" && document.getElementById(value)) { value = document.getElementById(value); } return value; } // Translates the IE mouse button information to comply with DOM Level 2 standard EPi.TranslateIEMouseButton = function(button) { // IE Left Mouse Button if (button == 1) { return 0; } // IE Right Mouse Button if (button == 2) { return 2; } // IE Middle Mouse button if (button == 4) { return 1; } return void(0); //undefined } // Event Handling // - Adding and removing eventListeners. // In DOM compliant browsers (uses addEventListener, removeEventListener) it works as normal // while in IE (uses attachEvent, detachEvent) we pass a custom event object as argument to the eventHandler. // This custom event object has the most commnly used properties as the DOM event object and also includes // the IE event object in a property _event. // In IE we use the call method with the DOM object as context in order to make "this" in the eventHandler // refer to the domObject. (Like in the DOM event model). // In IE eventhandlers are added to a custom array property on the DOM node and called via a custom event // handler attached to the DOM object. // This makes it necessary to clean up the added events and eventHandlers in IE // on unloading page which is done automatically with EPi._EventCleaner EPi.AddEventListener = function(domRef, eventType, eventHandler) { var domObject = EPi._GetDomObject(domRef); // Special handling of window.load events. // window load events is run by EPi._InitManagerObjects which also has the doOnload property if (domObject == window && eventType == "load" && !eventHandler.doOnload) { // Check if this event is already added. for (var i=0; i 0) { toggleImage[0].src = imgSrc; } } else { toggleNode.style.display = ""; } } EPi.ToggleDisplay.EventHandler = function(e) { if (this.EPiObject) { // If clicked node is part of a toggleGroup switch which node to show. EPi.ToggleDisplay.SwitchVisibleToggleGroupNode(this); // If possible to show AND hide current toggleNode toggle the node EPi.ToggleDisplay.ToggleViewStyle(this); // If clicked node is an A element we don't want to follow the href if (EPi.GetProperty(this, "PreventDefault") == "true") { e.preventDefault(); } } }