Debian Codegraph @

jupyter-widgets-base/lib/viewlist.js

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /**
4 * - create_view and remove_view are default functions called when adding or removing views
5 * - create_view takes a model and an index and returns a view or a promise for a view for that model
6 * - remove_view takes a view and destroys it (including calling `view.remove()`)
7 * - each time the update() function is called with a new list, the create and remove
8 * callbacks will be called in an order so that if you append the views created in the
9 * create callback and remove the views in the remove callback, you will duplicate
10 * the order of the list.
11 * - the remove callback defaults to just removing the view (e.g., pass in null for the second parameter)
12 * - the context defaults to the created ViewList. If you pass another context, the create and remove
13 * will be called in that context.
14 */
15 export class ViewList {
16 constructor(create_view, remove_view, context) {
17 this.initialize(create_view, remove_view, context);
18 }
19 initialize(create_view, remove_view, context) {
20 this._handler_context = context || this;
21 this._models = [];
22 this.views = []; // list of promises for views
23 this._create_view = create_view;
24 this._remove_view =
25 remove_view ||
26 function (view) {
27 view.remove();
28 };
29 }
30 /**
31 * the create_view, remove_view, and context arguments override the defaults
32 * specified when the list is created.
33 * after this function, the .views attribute is a list of promises for views
34 * if you want to perform some action on the list of views, do something like
35 * `Promise.all(myviewlist.views).then(function(views) {...});`
36 */
37 update(new_models, create_view, remove_view, context) {
38 const remove = remove_view || this._remove_view;
39 const create = create_view || this._create_view;
40 context = context || this._handler_context;
41 let i = 0;
42 // first, skip past the beginning of the lists if they are identical
43 for (; i < new_models.length; i++) {
44 if (i >= this._models.length || new_models[i] !== this._models[i]) {
45 break;
46 }
47 }
48 const first_removed = i;
49 // Remove the non-matching items from the old list.
50 const removed = this.views.splice(first_removed, this.views.length - first_removed);
51 for (let j = 0; j < removed.length; j++) {
52 removed[j].then(function (view) {
53 remove.call(context, view);
54 });
55 }
56 // Add the rest of the new list items.
57 for (; i < new_models.length; i++) {
58 this.views.push(Promise.resolve(create.call(context, new_models[i], i)));
59 }
60 // make a copy of the input array
61 this._models = new_models.slice();
62 // return a promise that resolves to all of the resolved views
63 return Promise.all(this.views);
64 }
65 /**
66 * removes every view in the list; convenience function for `.update([])`
67 * that should be faster
68 * returns a promise that resolves after this removal is done
69 */
70 remove() {
71 return Promise.all(this.views).then((views) => {
72 views.forEach((value) => this._remove_view.call(this._handler_context, value));
73 this.views = [];
74 this._models = [];
75 });
76 }
77 /**
78 * Dispose this viewlist.
79 *
80 * A synchronous function which just deletes references to child views. This
81 * function does not call .remove() on child views because that is
82 * asynchronous. Use this in cases where child views will be removed in
83 * another way.
84 */
85 dispose() {
86 this.views = null;
87 this._models = null;
88 }
89 }
90 //# sourceMappingURL=viewlist.js.map