1// SpryTabbedPanels.js - version 0.6 - Spry Pre-Release 1.6.1 2// 3// Copyright (c) 2006. Adobe Systems Incorporated. 4// All rights reserved. 5// 6// Redistribution and use in source and binary forms, with or without 7// modification, are permitted provided that the following conditions are met: 8// 9// * Redistributions of source code must retain the above copyright notice, 10// this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above copyright notice, 12// this list of conditions and the following disclaimer in the documentation 13// and/or other materials provided with the distribution. 14// * Neither the name of Adobe Systems Incorporated nor the names of its 15// contributors may be used to endorse or promote products derived from this 16// software without specific prior written permission. 17// 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28// POSSIBILITY OF SUCH DAMAGE. 29 30var Spry; 31if (!Spry) Spry = {}; 32if (!Spry.Widget) Spry.Widget = {}; 33 34Spry.Widget.TabbedPanels = function(element, opts) 35{ 36 this.element = this.getElement(element); 37 this.defaultTab = 0; // Show the first panel by default. 38 this.tabSelectedClass = "TabbedPanelsTabSelected"; 39 this.tabHoverClass = "TabbedPanelsTabHover"; 40 this.tabFocusedClass = "TabbedPanelsTabFocused"; 41 this.panelVisibleClass = "TabbedPanelsContentVisible"; 42 this.focusElement = null; 43 this.hasFocus = false; 44 this.currentTabIndex = 0; 45 this.enableKeyboardNavigation = true; 46 this.nextPanelKeyCode = Spry.Widget.TabbedPanels.KEY_RIGHT; 47 this.previousPanelKeyCode = Spry.Widget.TabbedPanels.KEY_LEFT; 48 49 Spry.Widget.TabbedPanels.setOptions(this, opts); 50 51 // If the defaultTab is expressed as a number/index, convert 52 // it to an element. 53 54 if (typeof (this.defaultTab) == "number") 55 { 56 if (this.defaultTab < 0) 57 this.defaultTab = 0; 58 else 59 { 60 var count = this.getTabbedPanelCount(); 61 if (this.defaultTab >= count) 62 this.defaultTab = (count > 1) ? (count - 1) : 0; 63 } 64 65 this.defaultTab = this.getTabs()[this.defaultTab]; 66 } 67 68 // The defaultTab property is supposed to be the tab element for the tab content 69 // to show by default. The caller is allowed to pass in the element itself or the 70 // element's id, so we need to convert the current value to an element if necessary. 71 72 if (this.defaultTab) 73 this.defaultTab = this.getElement(this.defaultTab); 74 75 this.attachBehaviors(); 76}; 77 78Spry.Widget.TabbedPanels.prototype.getElement = function(ele) 79{ 80 if (ele && typeof ele == "string") 81 return document.getElementById(ele); 82 return ele; 83}; 84 85Spry.Widget.TabbedPanels.prototype.getElementChildren = function(element) 86{ 87 var children = []; 88 var child = element.firstChild; 89 while (child) 90 { 91 if (child.nodeType == 1 /* Node.ELEMENT_NODE */) 92 children.push(child); 93 child = child.nextSibling; 94 } 95 return children; 96}; 97 98Spry.Widget.TabbedPanels.prototype.addClassName = function(ele, className) 99{ 100 if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1)) 101 return; 102 ele.className += (ele.className ? " " : "") + className; 103}; 104 105Spry.Widget.TabbedPanels.prototype.removeClassName = function(ele, className) 106{ 107 if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1)) 108 return; 109 ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), ""); 110}; 111 112Spry.Widget.TabbedPanels.setOptions = function(obj, optionsObj, ignoreUndefinedProps) 113{ 114 if (!optionsObj) 115 return; 116 for (var optionName in optionsObj) 117 { 118 if (ignoreUndefinedProps && optionsObj[optionName] == undefined) 119 continue; 120 obj[optionName] = optionsObj[optionName]; 121 } 122}; 123 124Spry.Widget.TabbedPanels.prototype.getTabGroup = function() 125{ 126 if (this.element) 127 { 128 var children = this.getElementChildren(this.element); 129 if (children.length) 130 return children[0]; 131 } 132 return null; 133}; 134 135Spry.Widget.TabbedPanels.prototype.getTabs = function() 136{ 137 var tabs = []; 138 var tg = this.getTabGroup(); 139 if (tg) 140 tabs = this.getElementChildren(tg); 141 return tabs; 142}; 143 144Spry.Widget.TabbedPanels.prototype.getContentPanelGroup = function() 145{ 146 if (this.element) 147 { 148 var children = this.getElementChildren(this.element); 149 if (children.length > 1) 150 return children[1]; 151 } 152 return null; 153}; 154 155Spry.Widget.TabbedPanels.prototype.getContentPanels = function() 156{ 157 var panels = []; 158 var pg = this.getContentPanelGroup(); 159 if (pg) 160 panels = this.getElementChildren(pg); 161 return panels; 162}; 163 164Spry.Widget.TabbedPanels.prototype.getIndex = function(ele, arr) 165{ 166 ele = this.getElement(ele); 167 if (ele && arr && arr.length) 168 { 169 for (var i = 0; i < arr.length; i++) 170 { 171 if (ele == arr[i]) 172 return i; 173 } 174 } 175 return -1; 176}; 177 178Spry.Widget.TabbedPanels.prototype.getTabIndex = function(ele) 179{ 180 var i = this.getIndex(ele, this.getTabs()); 181 if (i < 0) 182 i = this.getIndex(ele, this.getContentPanels()); 183 return i; 184}; 185 186Spry.Widget.TabbedPanels.prototype.getCurrentTabIndex = function() 187{ 188 return this.currentTabIndex; 189}; 190 191Spry.Widget.TabbedPanels.prototype.getTabbedPanelCount = function(ele) 192{ 193 return Math.min(this.getTabs().length, this.getContentPanels().length); 194}; 195 196Spry.Widget.TabbedPanels.addEventListener = function(element, eventType, handler, capture) 197{ 198 try 199 { 200 if (element.addEventListener) 201 element.addEventListener(eventType, handler, capture); 202 else if (element.attachEvent) 203 element.attachEvent("on" + eventType, handler); 204 } 205 catch (e) {} 206}; 207 208Spry.Widget.TabbedPanels.prototype.cancelEvent = function(e) 209{ 210 if (e.preventDefault) e.preventDefault(); 211 else e.returnValue = false;
212 if (e.stopPropagation) e.stopPropagation(); 213 else e.cancelBubble = true; 214 215 return false; 216}; 217 218Spry.Widget.TabbedPanels.prototype.onTabClick = function(e, tab) 219{ 220 this.showPanel(tab); 221 return this.cancelEvent(e); 222}; 223 224Spry.Widget.TabbedPanels.prototype.onTabMouseOver = function(e, tab) 225{ 226 this.addClassName(tab, this.tabHoverClass); 227 return false; 228}; 229 230Spry.Widget.TabbedPanels.prototype.onTabMouseOut = function(e, tab) 231{ 232 this.removeClassName(tab, this.tabHoverClass); 233 return false; 234}; 235 236Spry.Widget.TabbedPanels.prototype.onTabFocus = function(e, tab) 237{ 238 this.hasFocus = true; 239 this.addClassName(tab, this.tabFocusedClass); 240 return false; 241}; 242 243Spry.Widget.TabbedPanels.prototype.onTabBlur = function(e, tab) 244{ 245 this.hasFocus = false; 246 this.removeClassName(tab, this.tabFocusedClass); 247 return false; 248}; 249 250Spry.Widget.TabbedPanels.KEY_UP = 38; 251Spry.Widget.TabbedPanels.KEY_DOWN = 40; 252Spry.Widget.TabbedPanels.KEY_LEFT = 37; 253Spry.Widget.TabbedPanels.KEY_RIGHT = 39; 254 255 256 257Spry.Widget.TabbedPanels.prototype.onTabKeyDown = function(e, tab) 258{ 259 var key = e.keyCode; 260 if (!this.hasFocus || (key != this.previousPanelKeyCode && key != this.nextPanelKeyCode)) 261 return true; 262 263 var tabs = this.getTabs(); 264 for (var i =0; i < tabs.length; i++) 265 if (tabs[i] == tab) 266 { 267 var el = false; 268 if (key == this.previousPanelKeyCode && i > 0) 269 el = tabs[i-1]; 270 else if (key == this.nextPanelKeyCode && i < tabs.length-1) 271 el = tabs[i+1]; 272 273 if (el) 274 { 275 this.showPanel(el); 276 el.focus(); 277 break; 278 } 279 } 280 281 return this.cancelEvent(e); 282}; 283 284Spry.Widget.TabbedPanels.prototype.preorderTraversal = function(root, func) 285{ 286 var stopTraversal = false; 287 if (root) 288 { 289 stopTraversal = func(root); 290 if (root.hasChildNodes()) 291 { 292 var child = root.firstChild; 293 while (!stopTraversal && child) 294 { 295 stopTraversal = this.preorderTraversal(child, func); 296 try { child = child.nextSibling; } catch (e) { child = null; } 297 } 298 } 299 } 300 return stopTraversal; 301}; 302 303Spry.Widget.TabbedPanels.prototype.addPanelEventListeners = function(tab, panel) 304{ 305 var self = this; 306 Spry.Widget.TabbedPanels.addEventListener(tab, "click", function(e) { return self.onTabClick(e, tab); }, false); 307 Spry.Widget.TabbedPanels.addEventListener(tab, "mouseover", function(e) { return self.onTabMouseOver(e, tab); }, false); 308 Spry.Widget.TabbedPanels.addEventListener(tab, "mouseout", function(e) { return self.onTabMouseOut(e, tab); }, false); 309 310 if (this.enableKeyboardNavigation) 311 { 312 // XXX: IE doesn't allow the setting of tabindex dynamically. This means we can't 313 // rely on adding the tabindex attribute if it is missing to enable keyboard navigation 314 // by default. 315 316 // Find the first element within the tab container that has a tabindex or the first 317 // anchor tag. 318 319 var tabIndexEle = null; 320 var tabAnchorEle = null; 321 322 this.preorderTraversal(tab, function(node) { 323 if (node.nodeType == 1 /* NODE.ELEMENT_NODE */) 324 { 325 var tabIndexAttr = tab.attributes.getNamedItem("tabindex"); 326 if (tabIndexAttr) 327 { 328 tabIndexEle = node; 329 return true; 330 } 331 if (!tabAnchorEle && node.nodeName.toLowerCase() == "a") 332 tabAnchorEle = node; 333 } 334 return false; 335 }); 336 337 if (tabIndexEle) 338 this.focusElement = tabIndexEle; 339 else if (tabAnchorEle) 340 this.focusElement = tabAnchorEle; 341 342 if (this.focusElement) 343 { 344 Spry.Widget.TabbedPanels.addEventListener(this.focusElement, "focus", function(e) { return self.onTabFocus(e, tab); }, false); 345 Spry.Widget.TabbedPanels.addEventListener(this.focusElement, "blur", function(e) { return self.onTabBlur(e, tab); }, false); 346 Spry.Widget.TabbedPanels.addEventListener(this.focusElement, "keydown", function(e) { return self.onTabKeyDown(e, tab); }, false); 347 } 348 } 349}; 350 351Spry.Widget.TabbedPanels.prototype.showPanel = function(elementOrIndex) 352{ 353 var tpIndex = -1; 354 355 if (typeof elementOrIndex == "number") 356 tpIndex = elementOrIndex; 357 else // Must be the element for the tab or content panel. 358 tpIndex = this.getTabIndex(elementOrIndex); 359 360 if (!tpIndex < 0 || tpIndex >= this.getTabbedPanelCount()) 361 return; 362 363 var tabs = this.getTabs(); 364 var panels = this.getContentPanels(); 365 366 var numTabbedPanels = Math.max(tabs.length, panels.length); 367 368 for (var i = 0; i < numTabbedPanels; i++) 369 { 370 if (i != tpIndex) 371 { 372 if (tabs[i]) 373 this.removeClassName(tabs[i], this.tabSelectedClass); 374 if (panels[i]) 375 { 376 this.removeClassName(panels[i], this.panelVisibleClass); 377 panels[i].style.display = "none"; 378 } 379 } 380 } 381 382 this.addClassName(tabs[tpIndex], this.tabSelectedClass); 383 this.addClassName(panels[tpIndex], this.panelVisibleClass); 384 panels[tpIndex].style.display = "block"; 385 386 this.currentTabIndex = tpIndex; 387}; 388 389Spry.Widget.TabbedPanels.prototype.attachBehaviors = function(element) 390{ 391 var tabs = this.getTabs(); 392 var panels = this.getContentPanels(); 393 var panelCount = this.getTabbedPanelCount(); 394 395 for (var i = 0; i < panelCount; i++) 396 this.addPanelEventListeners(tabs[i], panels[i]); 397 398 this.showPanel(this.defaultTab); 399};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.