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 Data Table Card 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 MaterialDataTable = function MaterialDataTable(element) {
30 this.element_ = element;
32 // Initialize instance.
36 window['MaterialDataTable'] = MaterialDataTable;
39 * Store constants in one place so they can be updated easily.
41 * @enum {string | number}
44 MaterialDataTable.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 MaterialDataTable.prototype.CssClasses_ = {
57 DATA_TABLE: 'mdl-data-table',
58 SELECTABLE: 'mdl-data-table--selectable',
59 SELECT_ELEMENT: 'mdl-data-table__select',
60 IS_SELECTED: 'is-selected',
61 IS_UPGRADED: 'is-upgraded'
65 * Generates and returns a function that toggles the selection state of a
66 * single row (or multiple rows).
68 * @param {Element} checkbox Checkbox that toggles the selection state.
69 * @param {Element} row Row to toggle when checkbox changes.
70 * @param {(Array<Object>|NodeList)=} opt_rows Rows to toggle when checkbox changes.
73 MaterialDataTable.prototype.selectRow_ = function(checkbox, row, opt_rows) {
76 if (checkbox.checked) {
77 row.classList.add(this.CssClasses_.IS_SELECTED);
79 row.classList.remove(this.CssClasses_.IS_SELECTED);
88 if (checkbox.checked) {
89 for (i = 0; i < opt_rows.length; i++) {
90 el = opt_rows[i].querySelector('td').querySelector('.mdl-checkbox');
91 el['MaterialCheckbox'].check();
92 opt_rows[i].classList.add(this.CssClasses_.IS_SELECTED);
95 for (i = 0; i < opt_rows.length; i++) {
96 el = opt_rows[i].querySelector('td').querySelector('.mdl-checkbox');
97 el['MaterialCheckbox'].uncheck();
98 opt_rows[i].classList.remove(this.CssClasses_.IS_SELECTED);
106 * Creates a checkbox for a single or or multiple rows and hooks up the
109 * @param {Element} row Row to toggle when checkbox changes.
110 * @param {(Array<Object>|NodeList)=} opt_rows Rows to toggle when checkbox changes.
113 MaterialDataTable.prototype.createCheckbox_ = function(row, opt_rows) {
114 var label = document.createElement('label');
118 'mdl-js-ripple-effect',
119 this.CssClasses_.SELECT_ELEMENT
121 label.className = labelClasses.join(' ');
122 var checkbox = document.createElement('input');
123 checkbox.type = 'checkbox';
124 checkbox.classList.add('mdl-checkbox__input');
127 checkbox.checked = row.classList.contains(this.CssClasses_.IS_SELECTED);
128 checkbox.addEventListener('change', this.selectRow_(checkbox, row));
129 } else if (opt_rows) {
130 checkbox.addEventListener('change', this.selectRow_(checkbox, null, opt_rows));
133 label.appendChild(checkbox);
134 componentHandler.upgradeElement(label, 'MaterialCheckbox');
139 * Initialize element.
141 MaterialDataTable.prototype.init = function() {
143 var firstHeader = this.element_.querySelector('th');
144 var bodyRows = Array.prototype.slice.call(this.element_.querySelectorAll('tbody tr'));
145 var footRows = Array.prototype.slice.call(this.element_.querySelectorAll('tfoot tr'));
146 var rows = bodyRows.concat(footRows);
148 if (this.element_.classList.contains(this.CssClasses_.SELECTABLE)) {
149 var th = document.createElement('th');
150 var headerCheckbox = this.createCheckbox_(null, rows);
151 th.appendChild(headerCheckbox);
152 firstHeader.parentElement.insertBefore(th, firstHeader);
154 for (var i = 0; i < rows.length; i++) {
155 var firstCell = rows[i].querySelector('td');
157 var td = document.createElement('td');
158 if (rows[i].parentNode.nodeName.toUpperCase() === 'TBODY') {
159 var rowCheckbox = this.createCheckbox_(rows[i]);
160 td.appendChild(rowCheckbox);
162 rows[i].insertBefore(td, firstCell);
165 this.element_.classList.add(this.CssClasses_.IS_UPGRADED);
170 // The component registers itself. It can assume componentHandler is available
171 // in the global scope.
172 componentHandler.register({
173 constructor: MaterialDataTable,
174 classAsString: 'MaterialDataTable',
175 cssClass: 'mdl-js-data-table'