• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 representation
7 */
8
9cr.define('cr.ui.table', function() {
10  /** @const */ var EventTarget = cr.EventTarget;
11
12  /**
13   * A table column that wraps column ids and settings.
14   * @param {!Array} columnIds Array of column ids.
15   * @constructor
16   * @extends {EventTarget}
17   */
18  function TableColumn(id, name, width, endAlign) {
19    this.id_ = id;
20    this.name_ = name;
21    this.width_ = width;
22    this.endAlign_ = endAlign;
23  }
24
25  TableColumn.prototype = {
26    __proto__: EventTarget.prototype,
27
28    id_: null,
29
30    name_: null,
31
32    width_: null,
33
34    endAlign_: false,
35
36    defaultOrder_: 'asc',
37
38    /**
39     * Clones column.
40     * @return {cr.ui.table.TableColumn} Clone of the given column.
41     */
42    clone: function() {
43      var tableColumn = new TableColumn(this.id_, this.name_, this.width_,
44                                        this.endAlign_);
45      tableColumn.renderFunction = this.renderFunction_;
46      tableColumn.headerRenderFunction = this.headerRenderFunction_;
47      tableColumn.defaultOrder = this.defaultOrder_;
48      return tableColumn;
49    },
50
51    /**
52     * Renders table cell. This is the default render function.
53     * @param {*} dataItem The data item to be rendered.
54     * @param {string} columnId The column id.
55     * @param {cr.ui.Table} table The table.
56     * @return {HTMLElement} Rendered element.
57     */
58    renderFunction_: function(dataItem, columnId, table) {
59      var div = table.ownerDocument.createElement('div');
60      div.textContent = dataItem[columnId];
61      return div;
62    },
63
64    /**
65     * Renders table header. This is the default render function.
66     * @param {cr.ui.Table} table The table.
67     * @return {HTMLElement} Rendered element.
68     */
69    headerRenderFunction_: function(table) {
70      return table.ownerDocument.createTextNode(this.name);
71    },
72  };
73
74  /**
75   * The column id.
76   * @type {string}
77   */
78  cr.defineProperty(TableColumn, 'id');
79
80  /**
81   * The column name
82   * @type {string}
83   */
84  cr.defineProperty(TableColumn, 'name');
85
86  /**
87   * The column width.
88   * @type {number}
89   */
90  cr.defineProperty(TableColumn, 'width');
91
92  /**
93   * True if the column is aligned to end.
94   * @type {boolean}
95   */
96  cr.defineProperty(TableColumn, 'endAlign');
97
98  /**
99   * The column render function.
100   * @type {Function(*, string, cr.ui.Table): HTMLElement}
101   */
102  cr.defineProperty(TableColumn, 'renderFunction');
103
104  /**
105   * The column header render function.
106   * @type {Function(cr.ui.Table): HTMLElement}
107   */
108  cr.defineProperty(TableColumn, 'headerRenderFunction');
109
110  /**
111   * Default sorting order for the column ('asc' or 'desc').
112   * @type {string}
113   */
114  cr.defineProperty(TableColumn, 'defaultOrder');
115
116  return {
117    TableColumn: TableColumn
118  };
119});
120