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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/common/List.js

Issue 2916743002: [DevTools] Introduce Common.List used as a backend for list controls (Closed)
Patch Set: 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 Common.List = class extends Common.Object {
pfeldman 2017/05/31 23:00:02 UI.ListModel
dgozman 2017/06/01 01:03:02 Done.
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) {
einbinder 2017/05/31 23:23:08 I don't think this is good to have if we want a so
dgozman 2017/06/01 01:03:02 Yeah, we'll figure it out when the need arises. Fo
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 // TODO(dgozman): move this to sorted list.
60 var index = this._items.lowerBound(item, comparator);
61 this.insertItemAtIndex(index, item);
62 }
63
64 /**
65 * @param {T} item
66 * @return {number}
67 */
68 indexOfItem(item) {
69 return this._items.indexOf(item);
70 }
71
72 /**
73 * @param {number} index
74 * @return {T}
75 */
76 removeItemAtIndex(index) {
77 var result = this._items[index];
78 this.replaceItemsInRange(index, index + 1, []);
79 return result;
80 }
81
82 /**
83 * @param {T} item
84 */
85 removeItem(item) {
86 var index = this._items.indexOf(item);
87 if (index === -1) {
88 console.error('Attempt to remove non-existing item');
89 return;
90 }
91 this.removeItemAtIndex(index);
92 }
93
94 /**
95 * @param {!Array<T>} items
96 */
97 replaceAllItems(items) {
98 this.replaceItemsInRange(0, this._items.length, items);
99 }
100
101 /**
102 * @param {number} index
103 * @param {T} item
104 */
105 replaceItemAtIndex(index, item) {
106 this.replaceItemsInRange(index, index + 1, [item]);
107 }
108
109 /**
110 * @param {function(T):boolean} callback
111 * @return {number}
112 */
113 findIndex(callback) {
114 return this._items.findIndex(callback);
115 }
116
117 /**
118 * @param {number} from
119 * @param {number} to
120 * @param {!Array<T>} items
121 */
122 replaceItemsInRange(from, to, items) {
123 var removed = this._items.slice(from, to);
124 if (items.length < 10000) {
125 this._items.splice.bind(this._items, from, to - from).apply(null, items);
126 } else {
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 Common.List.Events.ItemsReplaced, {index: from, removed: removed, insert ed: items.length});
134 }
135 };
136
137 /** @enum {symbol} */
138 Common.List.Events = {
139 ItemsReplaced: Symbol('ItemsReplaced'),
140 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698