| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview This is a table column model | 6 * @fileoverview This is a table column model |
| 7 */ | 7 */ |
| 8 cr.define('cr.ui.table', function() { | 8 cr.define('cr.ui.table', function() { |
| 9 /** @const */ var EventTarget = cr.EventTarget; | 9 /** @const */ var EventTarget = cr.EventTarget; |
| 10 /** @const */ var Event = cr.Event; | 10 /** @const */ var Event = cr.Event; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * A table column model that wraps table columns array | 13 * A table column model that wraps table columns array |
| 14 * This implementation supports widths in percents. | 14 * This implementation supports widths in percents. |
| 15 * @param {!Array<cr.ui.table.TableColumn>} columnIds Array of table columns. | 15 * @param {!Array<cr.ui.table.TableColumn>} columnIds Array of table columns. |
| 16 * @constructor | 16 * @constructor |
| 17 * @extends {EventTarget} | 17 * @extends {EventTarget} |
| 18 */ | 18 */ |
| 19 function TableColumnModel(tableColumns) { | 19 function TableColumnModel(tableColumns) { |
| 20 this.columns_ = []; | 20 this.columns_ = []; |
| 21 for (var i = 0; i < tableColumns.length; i++) { | 21 for (var i = 0; i < tableColumns.length; i++) { |
| 22 this.columns_.push(tableColumns[i].clone()); | 22 this.columns_.push(tableColumns[i].clone()); |
| 23 } | 23 } |
| 24 this.normalizeWidths_(); | |
| 25 } | 24 } |
| 26 | 25 |
| 26 var MIMIMAL_WIDTH = 10; |
| 27 |
| 27 TableColumnModel.prototype = { | 28 TableColumnModel.prototype = { |
| 28 __proto__: EventTarget.prototype, | 29 __proto__: EventTarget.prototype, |
| 29 | 30 |
| 30 /** | 31 /** |
| 31 * The number of the columns. | 32 * The number of the columns. |
| 32 * @type {number} | 33 * @type {number} |
| 33 */ | 34 */ |
| 34 get size() { | 35 get size() { |
| 35 return this.columns_.length; | 36 return this.columns_.length; |
| 36 }, | 37 }, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 65 if (name != this.columns_[index].name) | 66 if (name != this.columns_[index].name) |
| 66 return; | 67 return; |
| 67 | 68 |
| 68 this.columns_[index].name = name; | 69 this.columns_[index].name = name; |
| 69 cr.dispatchSimpleEvent('change'); | 70 cr.dispatchSimpleEvent('change'); |
| 70 }, | 71 }, |
| 71 | 72 |
| 72 /** | 73 /** |
| 73 * Returns width (in percent) of column at the given index. | 74 * Returns width (in percent) of column at the given index. |
| 74 * @param {number} index The index of the column. | 75 * @param {number} index The index of the column. |
| 75 * @return {string} Column width. | 76 * @return {string} Column width in pixels. |
| 76 */ | 77 */ |
| 77 getWidth: function(index) { | 78 getWidth: function(index) { |
| 78 return this.columns_[index].width; | 79 return this.columns_[index].width; |
| 79 }, | 80 }, |
| 80 | 81 |
| 81 /** | 82 /** |
| 82 * Check if the column at the given index should align to the end. | 83 * Check if the column at the given index should align to the end. |
| 83 * @param {number} index The index of the column. | 84 * @param {number} index The index of the column. |
| 84 * @return {boolean} True if the column is aligned to end. | 85 * @return {boolean} True if the column is aligned to end. |
| 85 */ | 86 */ |
| 86 isEndAlign: function(index) { | 87 isEndAlign: function(index) { |
| 87 return this.columns_[index].endAlign; | 88 return this.columns_[index].endAlign; |
| 88 }, | 89 }, |
| 89 | 90 |
| 90 /** | 91 /** |
| 91 * Sets width of column at the given index. | 92 * Sets width of column at the given index. |
| 92 * @param {number} index The index of the column. | 93 * @param {number} index The index of the column. |
| 93 * @param {number} Column width. | 94 * @param {number} Column width. |
| 94 */ | 95 */ |
| 95 setWidth: function(index, width) { | 96 setWidth: function(index, width) { |
| 96 if (index < 0 || index >= this.columns_.size - 1) | 97 if (index < 0 || index >= this.columns_.size - 1) |
| 97 return; | 98 return; |
| 98 | 99 |
| 99 var minWidth = 3; | 100 width = Math.max(width, MIMIMAL_WIDTH); |
| 100 var currentPlusNextWidth = this.columns_[index + 1].width + | |
| 101 this.columns_[index].width; | |
| 102 var nextWidth = currentPlusNextWidth - width; | |
| 103 if (width < minWidth) { | |
| 104 width = minWidth; | |
| 105 nextWidth = currentPlusNextWidth - minWidth; | |
| 106 } | |
| 107 if (nextWidth < minWidth) { | |
| 108 width = currentPlusNextWidth - minWidth; | |
| 109 nextWidth = minWidth; | |
| 110 } | |
| 111 if (width == this.columns_[index].width) | 101 if (width == this.columns_[index].width) |
| 112 return; | 102 return; |
| 113 | 103 |
| 114 this.columns_[index].width = width; | 104 this.columns_[index].width = width; |
| 115 this.columns_[index + 1].width = nextWidth; | |
| 116 cr.dispatchSimpleEvent(this, 'resize'); | 105 cr.dispatchSimpleEvent(this, 'resize'); |
| 117 }, | 106 }, |
| 118 | 107 |
| 119 /** | 108 /** |
| 120 * Returns render function for the column at the given index. | 109 * Returns render function for the column at the given index. |
| 121 * @param {number} index The index of the column. | 110 * @param {number} index The index of the column. |
| 122 * @return {Function(*, string, cr.ui.Table): HTMLElement} Render function. | 111 * @return {Function(*, string, cr.ui.Table): HTMLElement} Render function. |
| 123 */ | 112 */ |
| 124 getRenderFunction: function(index) { | 113 getRenderFunction: function(index) { |
| 125 return this.columns_[index].renderFunction; | 114 return this.columns_[index].renderFunction; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 148 renderHeader: function(index, table) { | 137 renderHeader: function(index, table) { |
| 149 var c = this.columns_[index]; | 138 var c = this.columns_[index]; |
| 150 return c.headerRenderFunction.call(c, table); | 139 return c.headerRenderFunction.call(c, table); |
| 151 }, | 140 }, |
| 152 | 141 |
| 153 /** | 142 /** |
| 154 * The total width of the columns. | 143 * The total width of the columns. |
| 155 * @type {number} | 144 * @type {number} |
| 156 */ | 145 */ |
| 157 get totalWidth() { | 146 get totalWidth() { |
| 158 return this.totalWidth_; | 147 var total = 0; |
| 148 for (var i = 0; i < this.size; i++) { |
| 149 total += this.columns_[i].width; |
| 150 } |
| 151 return total; |
| 159 }, | 152 }, |
| 160 | 153 |
| 161 /** | 154 /** |
| 162 * Normalizes widths to make their sum 100%. | 155 * Normalizes widths to make their sum 100%. |
| 163 */ | 156 */ |
| 164 normalizeWidths_: function() { | 157 normalizeWidths: function(contentWidth) { |
| 165 var total = 0; | 158 if (this.size == 0) |
| 166 for (var i = 0; i < this.size; i++) { | 159 return; |
| 167 total += this.columns_[i].width; | 160 var c = this.columns_[0]; |
| 168 } | 161 c.width = Math.max(10, c.width - this.totalWidth + contentWidth); |
| 169 for (var i = 0; i < this.size; i++) { | |
| 170 this.columns_[i].width = this.columns_[i].width * 100 / total; | |
| 171 } | |
| 172 }, | 162 }, |
| 173 | 163 |
| 174 /** | 164 /** |
| 175 * Returns default sorting order for the column at the given index. | 165 * Returns default sorting order for the column at the given index. |
| 176 * @param {number} index The index of the column. | 166 * @param {number} index The index of the column. |
| 177 * @return {string} 'asc' or 'desc'. | 167 * @return {string} 'asc' or 'desc'. |
| 178 */ | 168 */ |
| 179 getDefaultOrder: function(index) { | 169 getDefaultOrder: function(index) { |
| 180 return this.columns_[index].defaultOrder; | 170 return this.columns_[index].defaultOrder; |
| 181 }, | 171 }, |
| 182 | 172 |
| 183 /** | 173 /** |
| 184 * Returns index of the column with given id. | 174 * Returns index of the column with given id. |
| 185 * @param {string} id The id to find. | 175 * @param {string} id The id to find. |
| 186 * @return {number} The index of column with given id or -1 if not found. | 176 * @return {number} The index of column with given id or -1 if not found. |
| 187 */ | 177 */ |
| 188 indexOf: function(id) { | 178 indexOf: function(id) { |
| 189 for (var i = 0; i < this.size; i++) { | 179 for (var i = 0; i < this.size; i++) { |
| 190 if (element.id == id) | 180 if (element.id == id) |
| 191 return i; | 181 return i; |
| 192 } | 182 } |
| 193 return -1; | 183 return -1; |
| 194 }, | 184 }, |
| 195 }; | 185 }; |
| 196 | 186 |
| 197 return { | 187 return { |
| 198 TableColumnModel: TableColumnModel | 188 TableColumnModel: TableColumnModel |
| 199 }; | 189 }; |
| 200 }); | 190 }); |
| OLD | NEW |