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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/ui/ListModel.js

Issue 2916743002: [DevTools] Introduce Common.List used as a backend for list controls (Closed)
Patch Set: addressed comments Created 3 years, 6 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
OLDNEW
(Empty)
1 // Copyright 2017 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 * @template T
7 */
8 UI.ListModel = class extends Common.Object {
9 /**
10 * @param {!Array<T>=} items
11 */
12 constructor(items) {
13 super();
14 this._items = items || [];
15 }
16
17 /**
18 * @return {number}
19 */
20 length() {
21 return this._items.length;
22 }
23
24 /**
25 * @param {number} index
26 * @return {T}
27 */
28 itemAtIndex(index) {
29 return this._items[index];
30 }
31
32 /**
33 * @param {T} item
34 */
35 pushItem(item) {
36 this.replaceItemsInRange(this._items.length, this._items.length, [item]);
37 }
38
39 /**
40 * @return {T}
41 */
42 popItem() {
43 return this.removeItemAtIndex(this._items.length - 1);
44 }
45
46 /**
47 * @param {number} index
48 * @param {T} item
49 */
50 insertItemAtIndex(index, item) {
51 this.replaceItemsInRange(index, index, [item]);
52 }
53
54 /**
55 * @param {T} item
56 * @param {function(T, T):number} comparator
57 */
58 insertItemWithComparator(item, comparator) {
59 var index = this._items.lowerBound(item, comparator);
60 this.insertItemAtIndex(index, item);
61 }
62
63 /**
64 * @param {T} item
65 * @return {number}
66 */
67 indexOfItem(item) {
68 return this._items.indexOf(item);
69 }
70
71 /**
72 * @param {number} index
73 * @return {T}
74 */
75 removeItemAtIndex(index) {
76 var result = this._items[index];
77 this.replaceItemsInRange(index, index + 1, []);
78 return result;
79 }
80
81 /**
82 * @param {T} item
83 */
84 removeItem(item) {
85 var index = this._items.indexOf(item);
86 if (index === -1) {
87 console.error('Attempt to remove non-existing item');
88 return;
89 }
90 this.removeItemAtIndex(index);
91 }
92
93 /**
94 * @param {!Array<T>} items
95 */
96 replaceAllItems(items) {
97 this.replaceItemsInRange(0, this._items.length, items);
98 }
99
100 /**
101 * @param {number} index
102 * @param {T} item
103 */
104 replaceItemAtIndex(index, item) {
105 this.replaceItemsInRange(index, index + 1, [item]);
106 }
107
108 /**
109 * @param {function(T):boolean} callback
110 * @return {number}
111 */
112 findIndex(callback) {
113 return this._items.findIndex(callback);
114 }
115
116 /**
117 * @param {number} from
118 * @param {number} to
119 * @param {!Array<T>} items
120 */
121 replaceItemsInRange(from, to, items) {
122 var removed;
123 if (items.length < 10000) {
124 removed = this._items.splice(from, to - from, ...items);
125 } else {
126 removed = this._items.slice(from, to);
127 // Splice may fail with too many arguments.
128 var before = this._items.slice(0, from);
129 var after = this._items.slice(to);
130 this._items = [].concat(before, items, after);
131 }
132 this.dispatchEventToListeners(
133 UI.ListModel.Events.ItemsReplaced, {index: from, removed: removed, inser ted: items.length});
134 }
135 };
136
137 /** @enum {symbol} */
138 UI.ListModel.Events = {
139 ItemsReplaced: Symbol('ItemsReplaced'),
140 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698