Index: chrome/browser/resources/shared/js/cr/ui/table.js |
=================================================================== |
--- chrome/browser/resources/shared/js/cr/ui/table.js (revision 86337) |
+++ chrome/browser/resources/shared/js/cr/ui/table.js (working copy) |
@@ -7,7 +7,7 @@ |
*/ |
cr.define('cr.ui', function() { |
- const ListSelectionModel = cr.ui.ListSelectionModel; |
+ const TableSelectionModel = cr.ui.table.TableSelectionModel; |
const ListSelectionController = cr.ui.ListSelectionController; |
const ArrayDataModel = cr.ui.ArrayDataModel; |
const TableColumnModel = cr.ui.table.TableColumnModel; |
@@ -30,19 +30,25 @@ |
/** |
* The table data model. |
* |
- * @type {cr.ui.ArrayDataModel} |
+ * @type {cr.ui.table.TableDataModel} |
*/ |
get dataModel() { |
return this.list_.dataModel; |
}, |
set dataModel(dataModel) { |
if (this.list_.dataModel != dataModel) { |
+ this.list_.dataModel = dataModel; |
if (this.list_.dataModel) { |
+ this.list_.dataModel.removeEventListener('splice', this.boundRedraw_); |
this.list_.dataModel.removeEventListener('sorted', |
this.boundHandleSorted_); |
} |
this.list_.dataModel = dataModel; |
+ this.list_.dataModel.table = this; |
+ |
+ |
if (this.list_.dataModel) { |
+ this.list_.dataModel.addEventListener('splice', this.boundRedraw_); |
this.list_.dataModel.addEventListener('sorted', |
this.boundHandleSorted_); |
} |
@@ -60,13 +66,16 @@ |
}, |
set columnModel(columnModel) { |
if (this.columnModel_ != columnModel) { |
- if (this.columnModel_) |
+ if (this.columnModel_) { |
+ this.columnModel_.removeEventListener('change', this.boundRedraw_); |
this.columnModel_.removeEventListener('resize', this.boundResize_); |
+ } |
this.columnModel_ = columnModel; |
- if (this.columnModel_) |
+ if (this.columnModel_) { |
+ this.columnModel_.addEventListener('change', this.boundRedraw_); |
this.columnModel_.addEventListener('resize', this.boundResize_); |
- this.list_.invalidate(); |
+ } |
this.redraw(); |
} |
}, |
@@ -75,7 +84,7 @@ |
* The table selection model. |
* |
* @type |
- * {cr.ui.ListSelectionModel|cr.ui.table.ListSingleSelectionModel} |
+ * {cr.ui.table.TableSelectionModel|cr.ui.table.TableSingleSelectionModel} |
*/ |
get selectionModel() { |
return this.list_.selectionModel; |
@@ -83,8 +92,9 @@ |
set selectionModel(selectionModel) { |
if (this.list_.selectionModel != selectionModel) { |
if (this.dataModel) |
- selectionModel.adjustLength(this.dataModel.length); |
+ selectionModel.adjust(0, 0, this.dataModel.length); |
this.list_.selectionModel = selectionModel; |
+ this.redraw(); |
} |
}, |
@@ -104,7 +114,7 @@ |
decorate: function() { |
this.list_ = this.ownerDocument.createElement('list'); |
TableList.decorate(this.list_); |
- this.list_.selectionModel = new ListSelectionModel(this); |
+ this.list_.selectionModel = new TableSelectionModel(this); |
this.list_.table = this; |
this.header_ = this.ownerDocument.createElement('div'); |
@@ -117,6 +127,7 @@ |
this.ownerDocument.defaultView.addEventListener( |
'resize', this.header_.updateWidth.bind(this.header_)); |
+ this.boundRedraw_ = this.redraw.bind(this); |
this.boundResize_ = this.resize.bind(this); |
this.boundHandleSorted_ = this.handleSorted_.bind(this); |
@@ -128,14 +139,6 @@ |
}, |
/** |
- * Redraws the table. |
- */ |
- redraw: function(index) { |
- this.list_.redraw(); |
- this.header_.redraw(); |
- }, |
- |
- /** |
* Resize the table columns. |
*/ |
resize: function() { |
@@ -163,12 +166,36 @@ |
}, |
/** |
+ * Redraws the table. |
+ * This forces the list to remove all cached items. |
+ */ |
+ redraw: function() { |
+ this.list_.startBatchUpdates(); |
+ if (this.list_.dataModel) { |
+ for (var i = 0; i < this.list_.dataModel.length; i++) { |
+ this.list_.redrawItem(i); |
+ } |
+ } |
+ this.list_.endBatchUpdates(); |
+ this.list_.redraw(); |
+ this.header_.redraw(); |
+ }, |
+ |
+ /** |
* This handles data model 'sorted' event. |
- * After sorting we need to redraw header |
+ * After sorting we need to |
+ * - adjust selection |
+ * - redraw all the items |
+ * - scroll the list to show selection. |
* @param {Event} e The 'sorted' event. |
*/ |
handleSorted_: function(e) { |
- this.header_.redraw(); |
+ var sm = this.list_.selectionModel; |
+ sm.adjustToReordering(e.sortPermutation); |
+ |
+ this.redraw(); |
+ if (sm.leadIndex != -1) |
+ this.list_.scrollIndexIntoView(sm.leadIndex) |
}, |
/** |