3 * Copyright 2015 Google Inc. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 * Class constructor for Tabs MDL component.
23 * Implements MDL component design pattern defined at:
24 * https://github.com/jasonmayes/mdl-component-design-pattern
27 * @param {Element} element The element that will be upgraded.
29 var MaterialTabs = function MaterialTabs(element) {
30 // Stores the HTML element.
31 this.element_ = element;
33 // Initialize instance.
36 window['MaterialTabs'] = MaterialTabs;
39 * Store constants in one place so they can be updated easily.
44 MaterialTabs.prototype.Constant_ = {
45 // None at the moment.
49 * Store strings for class names defined by this component that are used in
50 * JavaScript. This allows us to simply change it in one place should we
51 * decide to modify at a later date.
56 MaterialTabs.prototype.CssClasses_ = {
57 TAB_CLASS: 'mdl-tabs__tab',
58 PANEL_CLASS: 'mdl-tabs__panel',
59 ACTIVE_CLASS: 'is-active',
60 UPGRADED_CLASS: 'is-upgraded',
62 MDL_JS_RIPPLE_EFFECT: 'mdl-js-ripple-effect',
63 MDL_RIPPLE_CONTAINER: 'mdl-tabs__ripple-container',
64 MDL_RIPPLE: 'mdl-ripple',
65 MDL_JS_RIPPLE_EFFECT_IGNORE_EVENTS: 'mdl-js-ripple-effect--ignore-events'
69 * Handle clicks to a tabs component
73 MaterialTabs.prototype.initTabs_ = function() {
74 if (this.element_.classList.contains(this.CssClasses_.MDL_JS_RIPPLE_EFFECT)) {
75 this.element_.classList.add(
76 this.CssClasses_.MDL_JS_RIPPLE_EFFECT_IGNORE_EVENTS);
79 // Select element tabs, document panels
80 this.tabs_ = this.element_.querySelectorAll('.' + this.CssClasses_.TAB_CLASS);
82 this.element_.querySelectorAll('.' + this.CssClasses_.PANEL_CLASS);
84 // Create new tabs for each tab element
85 for (var i = 0; i < this.tabs_.length; i++) {
86 new MaterialTab(this.tabs_[i], this);
89 this.element_.classList.add(this.CssClasses_.UPGRADED_CLASS);
93 * Reset tab state, dropping active classes
97 MaterialTabs.prototype.resetTabState_ = function() {
98 for (var k = 0; k < this.tabs_.length; k++) {
99 this.tabs_[k].classList.remove(this.CssClasses_.ACTIVE_CLASS);
104 * Reset panel state, droping active classes
108 MaterialTabs.prototype.resetPanelState_ = function() {
109 for (var j = 0; j < this.panels_.length; j++) {
110 this.panels_[j].classList.remove(this.CssClasses_.ACTIVE_CLASS);
115 * Initialize element.
117 MaterialTabs.prototype.init = function() {
124 * Constructor for an individual tab.
127 * @param {Element} tab The HTML element for the tab.
128 * @param {MaterialTabs} ctx The MaterialTabs object that owns the tab.
130 function MaterialTab(tab, ctx) {
132 if (ctx.element_.classList.contains(ctx.CssClasses_.MDL_JS_RIPPLE_EFFECT)) {
133 var rippleContainer = document.createElement('span');
134 rippleContainer.classList.add(ctx.CssClasses_.MDL_RIPPLE_CONTAINER);
135 rippleContainer.classList.add(ctx.CssClasses_.MDL_JS_RIPPLE_EFFECT);
136 var ripple = document.createElement('span');
137 ripple.classList.add(ctx.CssClasses_.MDL_RIPPLE);
138 rippleContainer.appendChild(ripple);
139 tab.appendChild(rippleContainer);
142 tab.addEventListener('click', function(e) {
143 if (tab.getAttribute('href').charAt(0) === '#') {
145 var href = tab.href.split('#')[1];
146 var panel = ctx.element_.querySelector('#' + href);
147 ctx.resetTabState_();
148 ctx.resetPanelState_();
149 tab.classList.add(ctx.CssClasses_.ACTIVE_CLASS);
150 panel.classList.add(ctx.CssClasses_.ACTIVE_CLASS);
157 // The component registers itself. It can assume componentHandler is available
158 // in the global scope.
159 componentHandler.register({
160 constructor: MaterialTabs,
161 classAsString: 'MaterialTabs',
162 cssClass: 'mdl-js-tabs'