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 * @fileoverview This implements a table control.
9 cr.define('cr.ui', function() {
10 /** @const */ var ListSelectionModel = cr.ui.ListSelectionModel;
11 /** @const */ var ListSelectionController = cr.ui.ListSelectionController;
12 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel;
13 /** @const */ var TableColumnModel = cr.ui.table.TableColumnModel;
14 /** @const */ var TableList = cr.ui.table.TableList;
15 /** @const */ var TableHeader = cr.ui.table.TableHeader;
18 * Creates a new table element.
19 * @param {Object=} opt_propertyBag Optional properties.
21 * @extends {HTMLDivElement}
23 var Table = cr.ui.define('div');
26 __proto__: HTMLDivElement.prototype,
28 columnModel_: new TableColumnModel([]),
31 * The table data model.
33 * @type {cr.ui.ArrayDataModel}
36 return this.list_.dataModel;
38 set dataModel(dataModel) {
39 if (this.list_.dataModel != dataModel) {
40 if (this.list_.dataModel) {
41 this.list_.dataModel.removeEventListener('sorted',
42 this.boundHandleSorted_);
43 this.list_.dataModel.removeEventListener('change',
44 this.boundHandleChangeList_);
45 this.list_.dataModel.removeEventListener('splice',
46 this.boundHandleChangeList_);
48 this.list_.dataModel = dataModel;
49 if (this.list_.dataModel) {
50 this.list_.dataModel.addEventListener('sorted',
51 this.boundHandleSorted_);
52 this.list_.dataModel.addEventListener('change',
53 this.boundHandleChangeList_);
54 this.list_.dataModel.addEventListener('splice',
55 this.boundHandleChangeList_);
57 this.header_.redraw();
71 * The table column model.
73 * @type {cr.ui.table.TableColumnModel}
76 return this.columnModel_;
78 set columnModel(columnModel) {
79 if (this.columnModel_ != columnModel) {
80 if (this.columnModel_)
81 this.columnModel_.removeEventListener('resize', this.boundResize_);
82 this.columnModel_ = columnModel;
84 if (this.columnModel_)
85 this.columnModel_.addEventListener('resize', this.boundResize_);
86 this.list_.invalidate();
92 * The table selection model.
95 * {cr.ui.ListSelectionModel|cr.ui.ListSingleSelectionModel}
97 get selectionModel() {
98 return this.list_.selectionModel;
100 set selectionModel(selectionModel) {
101 if (this.list_.selectionModel != selectionModel) {
103 selectionModel.adjustLength(this.dataModel.length);
104 this.list_.selectionModel = selectionModel;
109 * The accessor to "autoExpands" property of the list.
114 return this.list_.autoExpands;
116 set autoExpands(autoExpands) {
117 this.list_.autoExpands = autoExpands;
121 return this.list_.fixedHeight;
123 set fixedHeight(fixedHeight) {
124 this.list_.fixedHeight = fixedHeight;
128 * Returns render function for row.
129 * @return {function(*, cr.ui.Table): HTMLElement} Render function.
131 getRenderFunction: function() {
132 return this.list_.renderFunction_;
136 * Sets render function for row.
137 * @param {function(*, cr.ui.Table): HTMLElement} renderFunction Render
140 setRenderFunction: function(renderFunction) {
141 if (renderFunction === this.list_.renderFunction_)
144 this.list_.renderFunction_ = renderFunction;
145 cr.dispatchSimpleEvent(this, 'change');
149 * The header of the table.
151 * @type {cr.ui.table.TableColumnModel}
158 * Initializes the element.
160 decorate: function() {
161 this.header_ = this.ownerDocument.createElement('div');
162 this.list_ = this.ownerDocument.createElement('list');
164 this.appendChild(this.header_);
165 this.appendChild(this.list_);
167 TableList.decorate(this.list_);
168 this.list_.selectionModel = new ListSelectionModel();
169 this.list_.table = this;
170 this.list_.addEventListener('scroll', this.handleScroll_.bind(this));
172 TableHeader.decorate(this.header_);
173 this.header_.table = this;
175 this.classList.add('table');
177 this.boundResize_ = this.resize.bind(this);
178 this.boundHandleSorted_ = this.handleSorted_.bind(this);
179 this.boundHandleChangeList_ = this.handleChangeList_.bind(this);
181 // The contained list should be focusable, not the table itself.
182 if (this.hasAttribute('tabindex')) {
183 this.list_.setAttribute('tabindex', this.getAttribute('tabindex'));
184 this.removeAttribute('tabindex');
187 this.addEventListener('focus', this.handleElementFocus_, true);
188 this.addEventListener('blur', this.handleElementBlur_, true);
196 this.header_.redraw();
199 startBatchUpdates: function() {
200 this.list_.startBatchUpdates();
201 this.header_.startBatchUpdates();
204 endBatchUpdates: function() {
205 this.list_.endBatchUpdates();
206 this.header_.endBatchUpdates();
210 * Resize the table columns.
213 // We resize columns only instead of full redraw.
215 this.header_.resize();
219 * Ensures that a given index is inside the viewport.
220 * @param {number} i The index of the item to scroll into view.
221 * @return {boolean} Whether any scrolling was needed.
223 scrollIndexIntoView: function(i) {
224 this.list_.scrollIndexIntoView(i);
228 * Find the list item element at the given index.
229 * @param {number} index The index of the list item to get.
230 * @return {cr.ui.ListItem} The found list item or null if not found.
232 getListItemByIndex: function(index) {
233 return this.list_.getListItemByIndex(index);
237 * This handles data model 'sorted' event.
238 * After sorting we need to redraw header
239 * @param {Event} e The 'sorted' event.
241 handleSorted_: function(e) {
242 this.header_.redraw();
246 * This handles data model 'change' and 'splice' events.
247 * Since they may change the visibility of scrollbar, table may need to
248 * re-calculation the width of column headers.
249 * @param {Event} e The 'change' or 'splice' event.
251 handleChangeList_: function(e) {
252 requestAnimationFrame(this.header_.updateWidth.bind(this.header_));
256 * This handles list 'scroll' events. Scrolls the header accordingly.
257 * @param {Event} e Scroll event.
259 handleScroll_: function(e) {
260 this.header_.style.marginLeft = -this.list_.scrollLeft + 'px';
264 * Sort data by the given column.
265 * @param {number} i The index of the column to sort by.
268 var cm = this.columnModel_;
269 var sortStatus = this.list_.dataModel.sortStatus;
270 if (sortStatus.field == cm.getId(i)) {
271 var sortDirection = sortStatus.direction == 'desc' ? 'asc' : 'desc';
272 this.list_.dataModel.sort(sortStatus.field, sortDirection);
274 this.list_.dataModel.sort(cm.getId(i), cm.getDefaultOrder(i));
276 if (this.selectionModel.selectedIndex == -1)
277 this.list_.scrollTop = 0;
281 * Called when an element in the table is focused. Marks the table as having
282 * a focused element, and dispatches an event if it didn't have focus.
283 * @param {Event} e The focus event.
286 handleElementFocus_: function(e) {
287 if (!this.hasElementFocus) {
288 this.hasElementFocus = true;
289 // Force styles based on hasElementFocus to take effect.
295 * Called when an element in the table is blurred. If focus moves outside
296 * the table, marks the table as no longer having focus and dispatches an
298 * @param {Event} e The blur event.
301 handleElementBlur_: function(e) {
302 // When the blur event happens we do not know who is getting focus so we
303 // delay this a bit until we know if the new focus node is outside the
306 var list = this.list_;
307 var doc = e.target.ownerDocument;
308 window.setTimeout(function() {
309 var activeElement = doc.activeElement;
310 if (!table.contains(activeElement)) {
311 table.hasElementFocus = false;
312 // Force styles based on hasElementFocus to take effect.
319 * Adjust column width to fit its content.
320 * @param {number} index Index of the column to adjust width.
322 fitColumn: function(index) {
323 var list = this.list_;
324 var listHeight = list.clientHeight;
326 var cm = this.columnModel_;
327 var dm = this.dataModel;
328 var columnId = cm.getId(index);
329 var doc = this.ownerDocument;
330 var render = cm.getRenderFunction(index);
332 var MAXIMUM_ROWS_TO_MEASURE = 1000;
334 // Create a temporaty list item, put all cells into it and measure its
335 // width. Then remove the item. It fits "list > *" CSS rules.
336 var container = doc.createElement('li');
337 container.style.display = 'inline-block';
338 container.style.textAlign = 'start';
339 // The container will have width of the longest cell.
340 container.style.webkitBoxOrient = 'vertical';
342 // Ensure all needed data available.
343 dm.prepareSort(columnId, function() {
344 // Select at most MAXIMUM_ROWS_TO_MEASURE items around visible area.
345 var items = list.getItemsInViewPort(list.scrollTop, listHeight);
346 var firstIndex = Math.floor(Math.max(0,
347 (items.last + items.first - MAXIMUM_ROWS_TO_MEASURE) / 2));
348 var lastIndex = Math.min(dm.length,
349 firstIndex + MAXIMUM_ROWS_TO_MEASURE);
350 for (var i = firstIndex; i < lastIndex; i++) {
351 var item = dm.item(i);
352 var div = doc.createElement('div');
353 div.className = 'table-row-cell';
354 div.appendChild(render(item, columnId, table));
355 container.appendChild(div);
357 list.appendChild(container);
358 var width = parseFloat(window.getComputedStyle(container).width);
359 list.removeChild(container);
360 cm.setWidth(index, width);
364 normalizeColumns: function() {
365 this.columnModel.normalizeWidths(this.clientWidth);
370 * Whether the table or one of its descendents has focus. This is necessary
371 * because table contents can contain controls that can be focused, and for
372 * some purposes (e.g., styling), the table can still be conceptually focused
373 * at that point even though it doesn't actually have the page focus.
375 cr.defineProperty(Table, 'hasElementFocus', cr.PropertyKind.BOOL_ATTR);