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 is a table column model
8 cr.define('cr.ui.table', function() {
9 /** @const */ var EventTarget = cr.EventTarget;
12 * A table column model that wraps table columns array
13 * This implementation supports widths in percents.
14 * @param {!Array<cr.ui.table.TableColumn>} tableColumns Array of table
17 * @extends {cr.EventTarget}
19 function TableColumnModel(tableColumns) {
21 for (var i = 0; i < tableColumns.length; i++) {
22 this.columns_.push(tableColumns[i].clone());
26 var MIMIMAL_WIDTH = 10;
28 TableColumnModel.prototype = {
29 __proto__: EventTarget.prototype,
32 * The number of the columns.
36 return this.columns_.length;
40 * Returns id of column at the given index.
41 * @param {number} index The index of the column.
42 * @return {string} Column id.
44 getId: function(index) {
45 return this.columns_[index].id;
49 * Returns name of column at the given index. Name is used as column header
51 * @param {number} index The index of the column.
52 * @return {string} Column name.
54 getName: function(index) {
55 return this.columns_[index].name;
59 * Sets name of column at the given index.
60 * @param {number} index The index of the column.
61 * @param {string} name Column name.
63 setName: function(index, name) {
64 if (index < 0 || index >= this.columns_.size - 1)
66 if (name != this.columns_[index].name)
69 this.columns_[index].name = name;
70 cr.dispatchSimpleEvent(this, 'change');
74 * Returns width (in percent) of column at the given index.
75 * @param {number} index The index of the column.
76 * @return {string} Column width in pixels.
78 getWidth: function(index) {
79 return this.columns_[index].width;
83 * Check if the column at the given index should align to the end.
84 * @param {number} index The index of the column.
85 * @return {boolean} True if the column is aligned to end.
87 isEndAlign: function(index) {
88 return this.columns_[index].endAlign;
92 * Sets width of column at the given index.
93 * @param {number} index The index of the column.
94 * @param {number} width Column width.
96 setWidth: function(index, width) {
97 if (index < 0 || index >= this.columns_.size - 1)
100 width = Math.max(width, MIMIMAL_WIDTH);
101 if (width == this.columns_[index].width)
104 this.columns_[index].width = width;
105 cr.dispatchSimpleEvent(this, 'resize');
109 * Returns render function for the column at the given index.
110 * @param {number} index The index of the column.
111 * @return {function(*, string, cr.ui.Table): HTMLElement} Render function.
113 getRenderFunction: function(index) {
114 return this.columns_[index].renderFunction;
118 * Sets render function for the column at the given index.
119 * @param {number} index The index of the column.
120 * @param {function(*, string, cr.ui.Table): HTMLElement} renderFunction
123 setRenderFunction: function(index, renderFunction) {
124 if (index < 0 || index >= this.columns_.size - 1)
126 if (renderFunction !== this.columns_[index].renderFunction)
129 this.columns_[index].renderFunction = renderFunction;
130 cr.dispatchSimpleEvent(this, 'change');
134 * Render the column header.
135 * @param {number} index The index of the column.
136 * @param {cr.ui.Table} table Owner table.
138 renderHeader: function(index, table) {
139 var c = this.columns_[index];
140 return c.headerRenderFunction.call(c, table);
144 * The total width of the columns.
149 for (var i = 0; i < this.size; i++) {
150 total += this.columns_[i].width;
156 * Normalizes widths to make their sum 100%.
158 normalizeWidths: function(contentWidth) {
161 var c = this.columns_[0];
162 c.width = Math.max(10, c.width - this.totalWidth + contentWidth);
166 * Returns default sorting order for the column at the given index.
167 * @param {number} index The index of the column.
168 * @return {string} 'asc' or 'desc'.
170 getDefaultOrder: function(index) {
171 return this.columns_[index].defaultOrder;
175 * Returns index of the column with given id.
176 * @param {string} id The id to find.
177 * @return {number} The index of column with given id or -1 if not found.
179 indexOf: function(id) {
180 for (var i = 0; i < this.size; i++) {
181 if (this.getId(i) == id)
189 TableColumnModel: TableColumnModel