Commit 9495ca25 authored by dpapad's avatar dpapad Committed by Commit Bot

WebUI: Convert table_column.js and table_column_model.js to ES6 syntax.

This is in preparation of removing the custom cr.EventTarget
implementation in favor of native EventTarget.

Bug: 854268
Change-Id: Idb59b729ec3a3026ff08c9a87aed02714b11fc22
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1639644
Auto-Submit: Demetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#665712}
parent 1406fedf
...@@ -7,35 +7,32 @@ ...@@ -7,35 +7,32 @@
*/ */
cr.define('cr.ui.table', function() { cr.define('cr.ui.table', function() {
/** @const */ const EventTarget = cr.EventTarget;
/** /**
* A table column that wraps column ids and settings. * A table column that wraps column ids and settings.
*/
class TableColumn extends cr.EventTarget {
/**
* @param {string} id * @param {string} id
* @param {string} name * @param {string} name
* @param {number} width * @param {number} width
* @param {boolean=} opt_endAlign * @param {boolean=} opt_endAlign
* @constructor
* @extends {cr.EventTarget}
*/ */
function TableColumn(id, name, width, opt_endAlign) { constructor(id, name, width, opt_endAlign) {
super();
this.id_ = id; this.id_ = id;
this.name_ = name; this.name_ = name;
this.width_ = width; this.width_ = width;
this.endAlign_ = !!opt_endAlign; this.endAlign_ = !!opt_endAlign;
this.visible_ = true; this.visible_ = true;
this.defaultOrder_ = 'asc';
} }
TableColumn.prototype = {
__proto__: EventTarget.prototype,
defaultOrder_: 'asc',
/** /**
* Clones column. * Clones column.
* @return {cr.ui.table.TableColumn} Clone of the given column. * @return {cr.ui.table.TableColumn} Clone of the given column.
*/ */
clone: function() { clone() {
const tableColumn = const tableColumn =
new TableColumn(this.id_, this.name_, this.width_, this.endAlign_); new TableColumn(this.id_, this.name_, this.width_, this.endAlign_);
tableColumn.renderFunction = this.renderFunction_; tableColumn.renderFunction = this.renderFunction_;
...@@ -45,7 +42,7 @@ cr.define('cr.ui.table', function() { ...@@ -45,7 +42,7 @@ cr.define('cr.ui.table', function() {
tableColumn.visible_ = this.visible_; tableColumn.visible_ = this.visible_;
return tableColumn; return tableColumn;
}, }
/** /**
* Renders table cell. This is the default render function. * Renders table cell. This is the default render function.
...@@ -54,22 +51,22 @@ cr.define('cr.ui.table', function() { ...@@ -54,22 +51,22 @@ cr.define('cr.ui.table', function() {
* @param {Element} table The table. * @param {Element} table The table.
* @return {HTMLElement} Rendered element. * @return {HTMLElement} Rendered element.
*/ */
renderFunction_: function(dataItem, columnId, table) { renderFunction_(dataItem, columnId, table) {
const div = /** @type {HTMLElement} */ const div = /** @type {HTMLElement} */
(table.ownerDocument.createElement('div')); (table.ownerDocument.createElement('div'));
div.textContent = dataItem[columnId]; div.textContent = dataItem[columnId];
div.hidden = !this.visible; div.hidden = !this.visible;
return div; return div;
}, }
/** /**
* Renders table header. This is the default render function. * Renders table header. This is the default render function.
* @param {Element} table The table. * @param {Element} table The table.
* @return {Text} Rendered text node. * @return {Text} Rendered text node.
*/ */
headerRenderFunction_: function(table) { headerRenderFunction_(table) {
return table.ownerDocument.createTextNode(this.name); return table.ownerDocument.createTextNode(this.name);
}, }
/** /**
* The width of the column. Hidden columns have zero width. * The width of the column. Hidden columns have zero width.
...@@ -77,7 +74,7 @@ cr.define('cr.ui.table', function() { ...@@ -77,7 +74,7 @@ cr.define('cr.ui.table', function() {
*/ */
get width() { get width() {
return this.visible_ ? this.width_ : 0; return this.visible_ ? this.width_ : 0;
}, }
/** /**
* The width of the column, disregarding visibility. For hidden columns, * The width of the column, disregarding visibility. For hidden columns,
...@@ -86,8 +83,8 @@ cr.define('cr.ui.table', function() { ...@@ -86,8 +83,8 @@ cr.define('cr.ui.table', function() {
*/ */
get absoluteWidth() { get absoluteWidth() {
return this.width_; return this.width_;
}, }
}; }
/** /**
* The column id. * The column id.
......
...@@ -6,17 +6,21 @@ ...@@ -6,17 +6,21 @@
* @fileoverview This is a table column model * @fileoverview This is a table column model
*/ */
cr.define('cr.ui.table', function() { cr.define('cr.ui.table', function() {
/** @const */ const EventTarget = cr.EventTarget; /** @type {number} */
const MIMIMAL_WIDTH = 10;
/** /**
* A table column model that wraps table columns array * A table column model that wraps table columns array
* This implementation supports widths in percents. * This implementation supports widths in percents.
*/
class TableColumnModel extends cr.EventTarget {
/**
* @param {!Array<cr.ui.table.TableColumn>} tableColumns Array of table * @param {!Array<cr.ui.table.TableColumn>} tableColumns Array of table
* columns. * columns.
* @constructor
* @extends {cr.EventTarget}
*/ */
function TableColumnModel(tableColumns) { constructor(tableColumns) {
super();
/** @type {!Array<cr.ui.table.TableColumn>} */ /** @type {!Array<cr.ui.table.TableColumn>} */
this.columns_ = []; this.columns_ = [];
for (let i = 0; i < tableColumns.length; i++) { for (let i = 0; i < tableColumns.length; i++) {
...@@ -24,27 +28,22 @@ cr.define('cr.ui.table', function() { ...@@ -24,27 +28,22 @@ cr.define('cr.ui.table', function() {
} }
} }
const MIMIMAL_WIDTH = 10;
TableColumnModel.prototype = {
__proto__: EventTarget.prototype,
/** /**
* The number of the columns. * The number of the columns.
* @type {number} * @type {number}
*/ */
get size() { get size() {
return this.columns_.length; return this.columns_.length;
}, }
/** /**
* Returns id of column at the given index. * Returns id of column at the given index.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @return {string} Column id. * @return {string} Column id.
*/ */
getId: function(index) { getId(index) {
return this.columns_[index].id; return this.columns_[index].id;
}, }
/** /**
* Returns name of column at the given index. Name is used as column header * Returns name of column at the given index. Name is used as column header
...@@ -52,16 +51,16 @@ cr.define('cr.ui.table', function() { ...@@ -52,16 +51,16 @@ cr.define('cr.ui.table', function() {
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @return {string} Column name. * @return {string} Column name.
*/ */
getName: function(index) { getName(index) {
return this.columns_[index].name; return this.columns_[index].name;
}, }
/** /**
* Sets name of column at the given index. * Sets name of column at the given index.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @param {string} name Column name. * @param {string} name Column name.
*/ */
setName: function(index, name) { setName(index, name) {
if (index < 0 || index >= this.columns_.length) { if (index < 0 || index >= this.columns_.length) {
return; return;
} }
...@@ -71,32 +70,32 @@ cr.define('cr.ui.table', function() { ...@@ -71,32 +70,32 @@ cr.define('cr.ui.table', function() {
this.columns_[index].name = name; this.columns_[index].name = name;
cr.dispatchSimpleEvent(this, 'change'); cr.dispatchSimpleEvent(this, 'change');
}, }
/** /**
* Returns width (in percent) of column at the given index. * Returns width (in percent) of column at the given index.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @return {number} Column width in pixels. * @return {number} Column width in pixels.
*/ */
getWidth: function(index) { getWidth(index) {
return this.columns_[index].width; return this.columns_[index].width;
}, }
/** /**
* Check if the column at the given index should align to the end. * Check if the column at the given index should align to the end.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @return {boolean} True if the column is aligned to end. * @return {boolean} True if the column is aligned to end.
*/ */
isEndAlign: function(index) { isEndAlign(index) {
return this.columns_[index].endAlign; return this.columns_[index].endAlign;
}, }
/** /**
* Sets width of column at the given index. * Sets width of column at the given index.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @param {number} width Column width. * @param {number} width Column width.
*/ */
setWidth: function(index, width) { setWidth(index, width) {
if (index < 0 || index >= this.columns_.length) { if (index < 0 || index >= this.columns_.length) {
return; return;
} }
...@@ -113,16 +112,16 @@ cr.define('cr.ui.table', function() { ...@@ -113,16 +112,16 @@ cr.define('cr.ui.table', function() {
if (column.visible) { if (column.visible) {
cr.dispatchSimpleEvent(this, 'resize'); cr.dispatchSimpleEvent(this, 'resize');
} }
}, }
/** /**
* Returns render function for the column at the given index. * Returns render function for the column at the given index.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @return {function(*, string, Element): HTMLElement} Render function. * @return {function(*, string, Element): HTMLElement} Render function.
*/ */
getRenderFunction: function(index) { getRenderFunction(index) {
return this.columns_[index].renderFunction; return this.columns_[index].renderFunction;
}, }
/** /**
* Sets render function for the column at the given index. * Sets render function for the column at the given index.
...@@ -130,7 +129,7 @@ cr.define('cr.ui.table', function() { ...@@ -130,7 +129,7 @@ cr.define('cr.ui.table', function() {
* @param {function(*, string, Element): HTMLElement} renderFunction * @param {function(*, string, Element): HTMLElement} renderFunction
* Render function. * Render function.
*/ */
setRenderFunction: function(index, renderFunction) { setRenderFunction(index, renderFunction) {
if (index < 0 || index >= this.columns_.length) { if (index < 0 || index >= this.columns_.length) {
return; return;
} }
...@@ -140,17 +139,17 @@ cr.define('cr.ui.table', function() { ...@@ -140,17 +139,17 @@ cr.define('cr.ui.table', function() {
this.columns_[index].renderFunction = renderFunction; this.columns_[index].renderFunction = renderFunction;
cr.dispatchSimpleEvent(this, 'change'); cr.dispatchSimpleEvent(this, 'change');
}, }
/** /**
* Render the column header. * Render the column header.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @param {Element} table Owner table. * @param {Element} table Owner table.
*/ */
renderHeader: function(index, table) { renderHeader(index, table) {
const c = this.columns_[index]; const c = this.columns_[index];
return c.headerRenderFunction.call(c, table); return c.headerRenderFunction.call(c, table);
}, }
/** /**
* The total width of the columns. * The total width of the columns.
...@@ -162,48 +161,48 @@ cr.define('cr.ui.table', function() { ...@@ -162,48 +161,48 @@ cr.define('cr.ui.table', function() {
total += this.columns_[i].width; total += this.columns_[i].width;
} }
return total; return total;
}, }
/** /**
* Normalizes widths to make their sum 100%. * Normalizes widths to make their sum 100%.
*/ */
normalizeWidths: function(contentWidth) { normalizeWidths(contentWidth) {
if (this.size == 0) { if (this.size == 0) {
return; return;
} }
const c = this.columns_[0]; const c = this.columns_[0];
c.width = Math.max(10, c.width - this.totalWidth + contentWidth); c.width = Math.max(10, c.width - this.totalWidth + contentWidth);
}, }
/** /**
* Returns default sorting order for the column at the given index. * Returns default sorting order for the column at the given index.
* @param {number} index The index of the column. * @param {number} index The index of the column.
* @return {string} 'asc' or 'desc'. * @return {string} 'asc' or 'desc'.
*/ */
getDefaultOrder: function(index) { getDefaultOrder(index) {
return this.columns_[index].defaultOrder; return this.columns_[index].defaultOrder;
}, }
/** /**
* Returns index of the column with given id. * Returns index of the column with given id.
* @param {string} id The id to find. * @param {string} id The id to find.
* @return {number} The index of column with given id or -1 if not found. * @return {number} The index of column with given id or -1 if not found.
*/ */
indexOf: function(id) { indexOf(id) {
for (let i = 0; i < this.size; i++) { for (let i = 0; i < this.size; i++) {
if (this.getId(i) == id) { if (this.getId(i) == id) {
return i; return i;
} }
} }
return -1; return -1;
}, }
/** /**
* Show/hide a column. * Show/hide a column.
* @param {number} index The column index. * @param {number} index The column index.
* @param {boolean} visible The column visibility. * @param {boolean} visible The column visibility.
*/ */
setVisible: function(index, visible) { setVisible(index, visible) {
if (index < 0 || index >= this.columns_.length) { if (index < 0 || index >= this.columns_.length) {
return; return;
} }
...@@ -218,17 +217,17 @@ cr.define('cr.ui.table', function() { ...@@ -218,17 +217,17 @@ cr.define('cr.ui.table', function() {
const contentWidth = this.totalWidth; const contentWidth = this.totalWidth;
column.visible = visible; column.visible = visible;
this.normalizeWidths(contentWidth); this.normalizeWidths(contentWidth);
}, }
/** /**
* Returns a column's visibility. * Returns a column's visibility.
* @param {number} index The column index. * @param {number} index The column index.
* @return {boolean} Whether the column is visible. * @return {boolean} Whether the column is visible.
*/ */
isVisible: function(index) { isVisible(index) {
return this.columns_[index].visible; return this.columns_[index].visible;
} }
}; }
return {TableColumnModel: TableColumnModel}; return {TableColumnModel: TableColumnModel};
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment