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. 4 5/** 6 * @fileoverview This is a table column model 7 */ 8cr.define('cr.ui.table', function() { 9 /** @const */ var EventTarget = cr.EventTarget; 10 11 /** 12 * A table column model that wraps table columns array 13 * This implementation supports widths in percents. 14 * @param {!Array<cr.ui.table.TableColumn>} columnIds Array of table columns. 15 * @constructor 16 * @extends {EventTarget} 17 */ 18 function TableColumnModel(tableColumns) { 19 this.columns_ = []; 20 for (var i = 0; i < tableColumns.length; i++) { 21 this.columns_.push(tableColumns[i].clone()); 22 } 23 } 24 25 var MIMIMAL_WIDTH = 10; 26 27 TableColumnModel.prototype = { 28 __proto__: EventTarget.prototype, 29 30 /** 31 * The number of the columns. 32 * @type {number} 33 */ 34 get size() { 35 return this.columns_.length; 36 }, 37 38 /** 39 * Returns id of column at the given index. 40 * @param {number} index The index of the column. 41 * @return {string} Column id. 42 */ 43 getId: function(index) { 44 return this.columns_[index].id; 45 }, 46 47 /** 48 * Returns name of column at the given index. Name is used as column header 49 * label. 50 * @param {number} index The index of the column. 51 * @return {string} Column name. 52 */ 53 getName: function(index) { 54 return this.columns_[index].name; 55 }, 56 57 /** 58 * Sets name of column at the given index. 59 * @param {number} index The index of the column. 60 * @param {string} name Column name. 61 */ 62 setName: function(index, name) { 63 if (index < 0 || index >= this.columns_.size - 1) 64 return; 65 if (name != this.columns_[index].name) 66 return; 67 68 this.columns_[index].name = name; 69 cr.dispatchSimpleEvent(this, 'change'); 70 }, 71 72 /** 73 * Returns width (in percent) of column at the given index. 74 * @param {number} index The index of the column. 75 * @return {string} Column width in pixels. 76 */ 77 getWidth: function(index) { 78 return this.columns_[index].width; 79 }, 80 81 /** 82 * Check if the column at the given index should align to the end. 83 * @param {number} index The index of the column. 84 * @return {boolean} True if the column is aligned to end. 85 */ 86 isEndAlign: function(index) { 87 return this.columns_[index].endAlign; 88 }, 89 90 /** 91 * Sets width of column at the given index. 92 * @param {number} index The index of the column. 93 * @param {number} width Column width. 94 */ 95 setWidth: function(index, width) { 96 if (index < 0 || index >= this.columns_.size - 1) 97 return; 98 99 width = Math.max(width, MIMIMAL_WIDTH); 100 if (width == this.columns_[index].width) 101 return; 102 103 this.columns_[index].width = width; 104 cr.dispatchSimpleEvent(this, 'resize'); 105 }, 106 107 /** 108 * Returns render function for the column at the given index. 109 * @param {number} index The index of the column. 110 * @return {Function(*, string, cr.ui.Table): HTMLElement} Render function. 111 */ 112 getRenderFunction: function(index) { 113 return this.columns_[index].renderFunction; 114 }, 115 116 /** 117 * Sets render function for the column at the given index. 118 * @param {number} index The index of the column. 119 * @param {Function(*, string, cr.ui.Table): HTMLElement} Render function. 120 */ 121 setRenderFunction: function(index, renderFunction) { 122 if (index < 0 || index >= this.columns_.size - 1) 123 return; 124 if (renderFunction !== this.columns_[index].renderFunction) 125 return; 126 127 this.columns_[index].renderFunction = renderFunction; 128 cr.dispatchSimpleEvent(this, 'change'); 129 }, 130 131 /** 132 * Render the column header. 133 * @param {number} index The index of the column. 134 * @param {cr.ui.Table} table Owner table. 135 */ 136 renderHeader: function(index, table) { 137 var c = this.columns_[index]; 138 return c.headerRenderFunction.call(c, table); 139 }, 140 141 /** 142 * The total width of the columns. 143 * @type {number} 144 */ 145 get totalWidth() { 146 var total = 0; 147 for (var i = 0; i < this.size; i++) { 148 total += this.columns_[i].width; 149 } 150 return total; 151 }, 152 153 /** 154 * Normalizes widths to make their sum 100%. 155 */ 156 normalizeWidths: function(contentWidth) { 157 if (this.size == 0) 158 return; 159 var c = this.columns_[0]; 160 c.width = Math.max(10, c.width - this.totalWidth + contentWidth); 161 }, 162 163 /** 164 * Returns default sorting order for the column at the given index. 165 * @param {number} index The index of the column. 166 * @return {string} 'asc' or 'desc'. 167 */ 168 getDefaultOrder: function(index) { 169 return this.columns_[index].defaultOrder; 170 }, 171 172 /** 173 * Returns index of the column with given id. 174 * @param {string} id The id to find. 175 * @return {number} The index of column with given id or -1 if not found. 176 */ 177 indexOf: function(id) { 178 for (var i = 0; i < this.size; i++) { 179 if (this.getId(i) == id) 180 return i; 181 } 182 return -1; 183 }, 184 }; 185 186 return { 187 TableColumnModel: TableColumnModel 188 }; 189}); 190