Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / webui / resources / js / cr / ui / table / table_column_model.js
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  */
8 cr.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>} tableColumns Array of table
15    *     columns.
16    * @constructor
17    * @extends {cr.EventTarget}
18    */
19   function TableColumnModel(tableColumns) {
20     this.columns_ = [];
21     for (var i = 0; i < tableColumns.length; i++) {
22       this.columns_.push(tableColumns[i].clone());
23     }
24   }
25
26   var MIMIMAL_WIDTH = 10;
27
28   TableColumnModel.prototype = {
29     __proto__: EventTarget.prototype,
30
31     /**
32      * The number of the columns.
33      * @type {number}
34      */
35     get size() {
36       return this.columns_.length;
37     },
38
39     /**
40      * Returns id of column at the given index.
41      * @param {number} index The index of the column.
42      * @return {string} Column id.
43      */
44     getId: function(index) {
45       return this.columns_[index].id;
46     },
47
48     /**
49      * Returns name of column at the given index. Name is used as column header
50      * label.
51      * @param {number} index The index of the column.
52      * @return {string} Column name.
53      */
54     getName: function(index) {
55       return this.columns_[index].name;
56     },
57
58     /**
59      * Sets name of column at the given index.
60      * @param {number} index The index of the column.
61      * @param {string} name Column name.
62      */
63     setName: function(index, name) {
64       if (index < 0 || index >= this.columns_.size - 1)
65         return;
66       if (name != this.columns_[index].name)
67         return;
68
69       this.columns_[index].name = name;
70       cr.dispatchSimpleEvent(this, 'change');
71     },
72
73     /**
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.
77      */
78     getWidth: function(index) {
79       return this.columns_[index].width;
80     },
81
82     /**
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.
86      */
87     isEndAlign: function(index) {
88       return this.columns_[index].endAlign;
89     },
90
91     /**
92      * Sets width of column at the given index.
93      * @param {number} index The index of the column.
94      * @param {number} width Column width.
95      */
96     setWidth: function(index, width) {
97       if (index < 0 || index >= this.columns_.size - 1)
98         return;
99
100       width = Math.max(width, MIMIMAL_WIDTH);
101       if (width == this.columns_[index].width)
102         return;
103
104       this.columns_[index].width = width;
105       cr.dispatchSimpleEvent(this, 'resize');
106     },
107
108     /**
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.
112      */
113     getRenderFunction: function(index) {
114       return this.columns_[index].renderFunction;
115     },
116
117     /**
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
121      *     Render function.
122      */
123     setRenderFunction: function(index, renderFunction) {
124       if (index < 0 || index >= this.columns_.size - 1)
125         return;
126       if (renderFunction !== this.columns_[index].renderFunction)
127         return;
128
129       this.columns_[index].renderFunction = renderFunction;
130       cr.dispatchSimpleEvent(this, 'change');
131     },
132
133     /**
134      * Render the column header.
135      * @param {number} index The index of the column.
136      * @param {cr.ui.Table} table Owner table.
137      */
138     renderHeader: function(index, table) {
139       var c = this.columns_[index];
140       return c.headerRenderFunction.call(c, table);
141     },
142
143     /**
144      * The total width of the columns.
145      * @type {number}
146      */
147     get totalWidth() {
148       var total = 0;
149       for (var i = 0; i < this.size; i++) {
150         total += this.columns_[i].width;
151       }
152       return total;
153     },
154
155     /**
156      * Normalizes widths to make their sum 100%.
157      */
158     normalizeWidths: function(contentWidth) {
159       if (this.size == 0)
160         return;
161       var c = this.columns_[0];
162       c.width = Math.max(10, c.width - this.totalWidth + contentWidth);
163     },
164
165     /**
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'.
169      */
170     getDefaultOrder: function(index) {
171       return this.columns_[index].defaultOrder;
172     },
173
174     /**
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.
178      */
179     indexOf: function(id) {
180       for (var i = 0; i < this.size; i++) {
181         if (this.getId(i) == id)
182           return i;
183       }
184       return -1;
185     },
186   };
187
188   return {
189     TableColumnModel: TableColumnModel
190   };
191 });