1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Add support for tab pannels on custom elements (tabs, header and content)
11 function registerEvent(target, eventType, handler) {
12 if (target.addEventListener) {
13 target.addEventListener(eventType, handler);
15 target.attachEvent(eventType, handler);
19 function getSessionKey(key) {
20 return window.sessionStorage.getItem("__tab_"+key);
23 function setSessionKey(key, value) {
24 window.sessionStorage.setItem("__tab_"+key, value);
27 function onTabHeaderKeyDown(e) {
28 if (e.keyCode == 13) {
34 function onTabClicked(e) {
35 var tabs = e.target.parentNode;
36 if (!tabs || tabs.tagName !== 'TABS')
39 var headers = tabs.getElementsByTagName('header'),
40 contents = tabs.getElementsByTagName('content'),
41 tabGroup = tabs.getAttribute("data-group"),
42 tabValue = e.target.getAttribute("data-value");
44 if (tabGroup && tabValue && window.sessionStorage)
45 setSessionKey(tabGroup, tabValue);
47 for (var i=0; i<headers.length; i++) {
48 if (headers[i] === e.target) {
49 headers[i].classList.remove('unselected');
50 if (contents.length > i)
51 contents[i].classList.remove('unselected');
53 headers[i].classList.add('unselected');
54 if (contents.length > i)
55 contents[i].classList.add('unselected');
60 function initTabPane(tab) {
61 var tabGroup = tab.getAttribute("data-group");
62 if (tabGroup && window.sessionStorage)
63 var tabGroupSelectedValue = getSessionKey(tabGroup);
65 var headers = tab.getElementsByTagName('header');
66 var contents = tab.getElementsByTagName('content');
67 var hasSelected = false;
69 if (headers.length==0 || contents.length==0)
72 for (var j=0; j<headers.length; j++) {
73 var selected = tabGroup && tabGroupSelectedValue
74 && tabGroupSelectedValue===headers[j].getAttribute("data-value");
76 if (!hasSelected && selected) {
77 headers[j].classList.remove("unselected");
78 contents[j].classList.remove("unselected");
81 headers[j].classList.add("unselected");
82 contents[j].classList.add("unselected");
85 headers[j].addEventListener('click', onTabClicked);
86 headers[j].addEventListener('keydown', onTabHeaderKeyDown);
90 headers[0].classList.remove("unselected");
91 contents[0].classList.remove("unselected");
96 var tabs = document.getElementsByTagName('tabs');
97 for (var i=0; i<tabs.length; i++) {
102 window.addEventListener('DOMContentLoaded', onLoad);