📄 tabs.js
字号:
// First, make sure all browsers have necessary ECMAScript v3 // and DOM Level 1 properties/** @type undefined */var undefined;/** @type Object */var Node = Node ? Node : {};/** @type number */Node.ELEMENT_NODE = 1;/** @type number */Node.ATTRIBUTE_NODE = 2;/** @type number */Node.TEXT_NODE = 3;/** @type number */Node.CDATA_SECTION_NODE = 4;/** @type number */Node.ENTITY_REFERENCE_NODE = 5;/** @type number */Node.ENTITY_NODE = 6;/** @type number */Node.PROCESSING_INSTRUCTION_NODE = 7;/** @type number */Node.COMMENT_NODE = 8;/** @type number */Node.DOCUMENT_NODE = 9;/** @type number */Node.DOCUMENT_TYPE_NODE = 10;/** @type number */Node.DOCUMENT_FRAGMENT_NODE = 11;/** @type number */Node.NOTATION_NODE = 12;/** * String convenience method to trim leading and trailing whitespace. * * @returns String */String.prototype.trim = function () { return this.replace(/^\s*(.+)/gi,"$1").replace(/\s*$/gi,"");};/** * String convenience method for checking if the end of this string equals * a given string. * * @returns String */String.prototype.endsWith = function (s) { if ("string" != typeof s) { throw("IllegalArgumentException: Must pass a string to " + "String.endsWith()"); } var start = this.length - s.length; return this.substring(start) == s;};/** * Array convenience method to check for membership. * * @param object element * @returns boolean */Array.prototype.contains = function (element) { for (var i = 0; i < this.length; i++) { if (this[i] == element) { return true; } } return false;};/** * Array convenience method to remove element. * * @param object element * @returns boolean */Array.prototype.remove = function (element) { var result = false; var array = []; for (var i = 0; i < this.length; i++) { if (this[i] == element) { result = true; } else { array.push(this[i]); } } this.clear(); for (var i = 0; i < array.length; i++) { this.push(array[i]); } array = null; return result;};/** * Array convenience method to clear membership. * * @param object element * @returns void */Array.prototype.clear = function () { this.length = 0;};/** * Array convenience method to add stack functionality to <code>Array</code>s * in browsers that do not support ECMAScript v3. * * @param object element * @returns number */Array.prototype.push = function (element) { this[this.length] = element; return this.length;};/** * Array convenience method to add set functionality to <code>Array</code>s * ... if the element is already a member of this array, return false. * Otherwise, add it and return true. * * @param object element * @returns boolean */Array.prototype.add = function (element) { if (this.contains(element)) { return false; } this.push(element); return false;};/** * Array convenience method to add set functionality to <code>Array</code>s * ... Uniquely adds all elements in the array parameter to this array. * Returns true if any new elements were added to this array. False otherwise. * * @param Array that * @returns boolean */Array.prototype.addAll = function (that) { var result = false; for (var i = 0; i < that.length; i++) { if (this.add(that[i])) { result = true; } } return true;};// Initialize org.ditchnet namespacevar org = org ? org : {}; org.ditchnet = org.ditchnet ? org.ditchnet : {}; org.ditchnet.dom = org.ditchnet.dom ? org.ditchnet.dom : {}; org.ditchnet.jsp = org.ditchnet.jsp ? org.ditchnet.jsp : {}; org.ditchnet.util = org.ditchnet.util ? org.ditchnet.util : {}; org.ditchnet.event = org.ditchnet.event ? org.ditchnet.event : {};/** * A utility class that exists to combine functionality for the Tabs * into a single class as static methods and constants. This class is not * meant to be subclassed. * * @constructor */org.ditchnet.jsp.TabUtils = function () {};/** @type string */org.ditchnet.jsp.TabUtils.TAB_CONTAINER_CLASS_NAME = "ditch-tab-container";/** @type string */org.ditchnet.jsp.TabUtils.TAB_WRAP_CLASS_NAME = "ditch-tab-wrap";/** @type string */org.ditchnet.jsp.TabUtils.TAB_CLASS_NAME = "ditch-tab";/** @type string */org.ditchnet.jsp.TabUtils.TAB_BG_LEFT_CLASS_NAME = "ditch-tab-bg-left";/** @type string */org.ditchnet.jsp.TabUtils.TAB_PANE_WRAP_CLASS_NAME = "ditch-tab-pane-wrap";/** @type string */org.ditchnet.jsp.TabUtils.TAB_PANE_CLASS_NAME = "ditch-tab-pane";/** @type HTMLElement */org.ditchnet.jsp.TabUtils.tabContainer;/** @type HTMLElement */org.ditchnet.jsp.TabUtils.tabWrap;/** @type HTMLElement */org.ditchnet.jsp.TabUtils.tab;/** @type Array */org.ditchnet.jsp.TabUtils.tabs;/** @type Array */org.ditchnet.jsp.TabUtils.tabPanes;/** @type number */org.ditchnet.jsp.TabUtils.selectedIndex;/** * A utility class that exists to combine common DOM algorithms and utilities * into a single class as static methods and constants. This class is not * meant to be subclassed. * * @constructor */org.ditchnet.dom.DomUtils = function () {};/** @type string */org.ditchnet.dom.DomUtils.FOCUSED_CLASS_NAME = "ditch-focused";/** @type string */org.ditchnet.dom.DomUtils.UNFOCUSED_CLASS_NAME = "ditch-unfocused";/** * @param Element target * @returns void */org.ditchnet.dom.DomUtils.show = function (target) { target.style.display = "";};/** * @param Element target * @returns void */org.ditchnet.dom.DomUtils.hide = function (target) { target.style.display = "none";};/** * @param Element target * @returns void */org.ditchnet.dom.DomUtils.setClassNameAsFocused = function (target) { with (org.ditchnet.dom.DomUtils) { removeClassName( target,UNFOCUSED_CLASS_NAME ); addClassName( target,FOCUSED_CLASS_NAME ); }};/** * @param Element target * @returns void */org.ditchnet.dom.DomUtils.setClassNameAsUnFocused = function (target) { with (org.ditchnet.dom.DomUtils) { removeClassName( target,FOCUSED_CLASS_NAME ); addClassName( target,UNFOCUSED_CLASS_NAME ); }};/** * @param Node target * @param string k * @returns void */org.ditchnet.dom.DomUtils.addClassName = function (target,k) { with (org.ditchnet.dom.DomUtils) { if(!isElementNode(target)) { throw new Error("Attempting to add a className to a non-Element" + " Node"); } var classNames = target.className.split(/\s+/g); if (classNames.contains(k)) { return; } else { classNames.push(k); } target.className = classNames.join(" "); target.className = target.className.trim(); }}/** * @param Node target * @param string k * @returns void */org.ditchnet.dom.DomUtils.removeClassName = function (target,k) { with (org.ditchnet.dom.DomUtils) { if(!isElementNode(target)) { throw new Error("Attempting to remove a className to a " + "non-Element Node"); } var classNames = target.className.split(/\s+/g); if (!classNames.contains(k)) { return; } else { classNames.remove(k); } target.className = classNames.join(" "); target.className = target.className.trim(); }}/** * Tests to see if <code>target</code>'s <code>getNodeType()</code> * method returns <code>Node.ELEMENT_NODE</code>. * @param Element target * @returns boolean */org.ditchnet.dom.DomUtils.isElementNode = function (target) { return Node.ELEMENT_NODE == target.nodeType;};/** * @param Element target * @param string id * @returns boolean */org.ditchnet.dom.DomUtils.hasId = function (target,id) { return target.id == id;};/** * @param Element target * @param string className * @returns boolean */org.ditchnet.dom.DomUtils.hasClassName = function (target,className) { function _isLastOfMultpleClassNames(all,className) { var spaceBefore = all.lastIndexOf(className)-1; return all.endsWith(className) && all.substring(spaceBefore,spaceBefore+1) == " "; } className = className.trim(); var cn = target.className; if (!cn) { return false; } cn = cn.trim(); if (cn == className) { return true; } if (cn.indexOf(className + " ") > -1) { return true; } if (_isLastOfMultpleClassNames(cn,className)) { return true; } return false;};/** * @param Node target * @param string className * @returns Element */org.ditchnet.dom.DomUtils.getFirstAncestorOrSelfByClassName = function (target, className) { with (org.ditchnet.dom.DomUtils) { var parent = target; do { if (isElementNode(parent) && hasClassName(parent,className)) { return parent; } } while (parent = parent.parentNode); } return null;};/** * @param Node target * @param string className * @returns Element */org.ditchnet.dom.DomUtils.getFirstAncestorByClassName = function (target,className) { with (org.ditchnet.dom.DomUtils) { var parent = target; while (parent = parent.parentNode) { if (isElementNode(parent) && hasClassName(parent,className)) { return parent; } } } return null;};/** * @param Node target * @param string className * @returns Element */org.ditchnet.dom.DomUtils.getFirstChildByClassName = function (target,className) { with (org.ditchnet.dom.DomUtils) { var kids = target.childNodes; for (var i = 0; i < kids.length; i++) { var kid = kids[i]; if (isElementNode(kid) && hasClassName(kid,className)) { return kid; } } } return null;};/** * @param Node target * @param string className * @returns Array */org.ditchnet.dom.DomUtils.getChildrenByClassName = function (target,className) { var result = []; with (org.ditchnet.dom.DomUtils) { var kids = target.childNodes; for (var i = 0; i < kids.length; i++) { var kid = kids[i]; if (isElementNode(kid) && hasClassName(kid,className)) { result.push(kid); } } } return result;};/** * Retreives <code>target</code>'s first descendant element with an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -