Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1419)

Side by Side Diff: chrome/browser/resources/shared/js/cr/ui/list.js

Issue 7038024: Move sorting logic from table to list. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged with http://codereview.chromium.org/7062001/ to fix this bug. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // require: array_data_model.js 5 // require: array_data_model.js
6 // require: list_selection_model.js 6 // require: list_selection_model.js
7 // require: list_selection_controller.js 7 // require: list_selection_controller.js
8 // require: list_item.js 8 // require: list_item.js
9 9
10 /** 10 /**
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 this.itemConstructor_ = func; 153 this.itemConstructor_ = func;
154 this.cachedItems_ = {}; 154 this.cachedItems_ = {};
155 this.redraw(); 155 this.redraw();
156 } 156 }
157 }, 157 },
158 158
159 dataModel_: null, 159 dataModel_: null,
160 160
161 /** 161 /**
162 * The data model driving the list. 162 * The data model driving the list.
163 * @type {ListDataModel} 163 * @type {ArrayDataModel}
164 */ 164 */
165 set dataModel(dataModel) { 165 set dataModel(dataModel) {
166 if (this.dataModel_ != dataModel) { 166 if (this.dataModel_ != dataModel) {
167 if (!this.boundHandleDataModelSplice_) { 167 if (!this.boundHandleDataModelPermuted_) {
168 this.boundHandleDataModelSplice_ = 168 this.boundHandleDataModelPermuted_ =
169 this.handleDataModelSplice_.bind(this); 169 this.handleDataModelPermuted_.bind(this);
170 this.boundHandleDataModelChange_ = 170 this.boundHandleDataModelChange_ =
171 this.handleDataModelChange_.bind(this); 171 this.handleDataModelChange_.bind(this);
172 this.boundHandleSorted_ =
173 this.handleSorted_.bind(this);
174 } 172 }
175 173
176 if (this.dataModel_) { 174 if (this.dataModel_) {
177 this.dataModel_.removeEventListener('splice', 175 this.dataModel_.removeEventListener(
178 this.boundHandleDataModelSplice_); 176 'permuted',
177 this.boundHandleDataModelPermuted_);
179 this.dataModel_.removeEventListener('change', 178 this.dataModel_.removeEventListener('change',
180 this.boundHandleDataModelChange_); 179 this.boundHandleDataModelChange_);
181 this.dataModel_.removeEventListener('sorted',
182 this.boundHandleSorted_);
183 } 180 }
184 181
185 this.dataModel_ = dataModel; 182 this.dataModel_ = dataModel;
186 183
187 this.cachedItems_ = {}; 184 this.cachedItems_ = {};
188 this.selectionModel.clear(); 185 this.selectionModel.clear();
189 if (dataModel) 186 if (dataModel)
190 this.selectionModel.adjust(0, 0, dataModel.length); 187 this.selectionModel.adjustLength(dataModel.length);
191 188
192 if (this.dataModel_) { 189 if (this.dataModel_) {
193 this.dataModel_.addEventListener('splice', 190 this.dataModel_.addEventListener(
194 this.boundHandleDataModelSplice_); 191 'permuted',
192 this.boundHandleDataModelPermuted_);
195 this.dataModel_.addEventListener('change', 193 this.dataModel_.addEventListener('change',
196 this.boundHandleDataModelChange_); 194 this.boundHandleDataModelChange_);
197 this.dataModel_.addEventListener('sorted',
198 this.boundHandleSorted_);
199 } 195 }
200 196
201 this.redraw(); 197 this.redraw();
202 } 198 }
203 }, 199 },
204 200
205 get dataModel() { 201 get dataModel() {
206 return this.dataModel_; 202 return this.dataModel_;
207 }, 203 },
208 204
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 // the bottom of the list is not "sticky.") So, we set a timeout to 543 // the bottom of the list is not "sticky.") So, we set a timeout to
548 // rescroll the list after this all gets sorted out. This is perhaps 544 // rescroll the list after this all gets sorted out. This is perhaps
549 // not the most elegant solution, but no others seem obvious. 545 // not the most elegant solution, but no others seem obvious.
550 var self = this; 546 var self = this;
551 window.setTimeout(function() { 547 window.setTimeout(function() {
552 self.scrollIndexIntoView(pe.newValue); 548 self.scrollIndexIntoView(pe.newValue);
553 }); 549 });
554 } 550 }
555 }, 551 },
556 552
557 handleDataModelSplice_: function(e) { 553 /**
558 this.selectionModel.adjust(e.index, e.removed.length, e.added.length); 554 * This handles data model 'permuted' event.
559 // Remove the cache of everything above index. 555 * this event is dispatched as a part of sort or splice.
556 * We need to
557 * - adjust the cache.
558 * - adjust selection.
559 * - redraw.
560 * - scroll the list to show selection.
561 * It is important that the cache adjustment happens before selection model
562 * adjustments.
563 * @param {Event} e The 'permuted' event.
564 */
565 handleDataModelPermuted_: function(e) {
566 var newCachedItems = {};
560 for (var index in this.cachedItems_) { 567 for (var index in this.cachedItems_) {
561 if (index >= e.index) 568 if (e.permutation[index] != -1)
569 newCachedItems[e.permutation[index]] = this.cachedItems_[index];
570 else
562 delete this.cachedItems_[index]; 571 delete this.cachedItems_[index];
563 } 572 }
573 this.cachedItems_ = newCachedItems;
574
575 var sm = this.selectionModel;
576 sm.adjustLength(e.newLength);
577 sm.adjustToReordering(e.permutation);
578
564 this.redraw(); 579 this.redraw();
580
581 if (sm.leadIndex != -1)
582 this.scrollIndexIntoView(sm.leadIndex);
565 }, 583 },
566 584
567 handleDataModelChange_: function(e) { 585 handleDataModelChange_: function(e) {
568 if (e.index >= this.firstIndex_ && e.index < this.lastIndex_) { 586 if (e.index >= this.firstIndex_ && e.index < this.lastIndex_) {
569 this.cachedItems_ = null; 587 if (this.cachedItems_[index])
588 delete this.cachedItems_[index];
570 this.redraw(); 589 this.redraw();
571 } 590 }
572 }, 591 },
573 592
574 /** 593 /**
575 * This handles data model 'sorted' event.
576 * After sorting we need to
577 * - adjust selection.
578 * - delete the cache.
579 * - redraw all the items.
580 * - scroll the list to show selection.
581 * @param {Event} e The 'sorted' event.
582 */
583 handleSorted_: function(e) {
584 var sm = this.selectionModel;
585 sm.adjustToReordering(e.sortPermutation);
586
587 this.cachedItems_ = null;
588 this.redraw();
589 if (sm.leadIndex != -1)
590 this.scrollIndexIntoView(sm.leadIndex);
591 },
592
593 /**
594 * @param {number} index The index of the item. 594 * @param {number} index The index of the item.
595 * @return {number} The top position of the item inside the list, not taking 595 * @return {number} The top position of the item inside the list, not taking
596 * into account lead item. May vary in the case of multiple columns. 596 * into account lead item. May vary in the case of multiple columns.
597 */ 597 */
598 getItemTop: function(index) { 598 getItemTop: function(index) {
599 return index * this.getItemHeight_(); 599 return index * this.getItemHeight_();
600 }, 600 },
601 601
602 /** 602 /**
603 * @param {number} index The index of the item. 603 * @param {number} index The index of the item.
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 var list = this; 924 var list = this;
925 window.setTimeout(function() { 925 window.setTimeout(function() {
926 if (listItem.parentNode == list) { 926 if (listItem.parentNode == list) {
927 list.measured_ = measureItem(list, listItem); 927 list.measured_ = measureItem(list, listItem);
928 } 928 }
929 }); 929 });
930 } 930 }
931 }, 931 },
932 932
933 /** 933 /**
934 * Invalidates list by removing cached items.
935 */
936 invalidate: function() {
937 this.cachedItems_ = {};
938 },
939
940 /**
934 * Redraws a single item. 941 * Redraws a single item.
935 * @param {number} index The row index to redraw. 942 * @param {number} index The row index to redraw.
936 */ 943 */
937 redrawItem: function(index) { 944 redrawItem: function(index) {
938 if (index >= this.firstIndex_ && index < this.lastIndex_) { 945 if (index >= this.firstIndex_ && index < this.lastIndex_) {
939 delete this.cachedItems_[index]; 946 delete this.cachedItems_[index];
940 this.redraw(); 947 this.redraw();
941 } 948 }
942 }, 949 },
943 950
(...skipping 13 matching lines...) Expand all
957 * because list items can contain controls that can be focused, and for some 964 * because list items can contain controls that can be focused, and for some
958 * purposes (e.g., styling), the list can still be conceptually focused at 965 * purposes (e.g., styling), the list can still be conceptually focused at
959 * that point even though it doesn't actually have the page focus. 966 * that point even though it doesn't actually have the page focus.
960 */ 967 */
961 cr.defineProperty(List, 'hasElementFocus', cr.PropertyKind.BOOL_ATTR); 968 cr.defineProperty(List, 'hasElementFocus', cr.PropertyKind.BOOL_ATTR);
962 969
963 return { 970 return {
964 List: List 971 List: List
965 } 972 }
966 }); 973 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698