UNPKG

4.04 kBJavaScriptView 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 */
15var ViewList = /** @class */ (function () {
16 function ViewList(create_view, remove_view, context) {
17 this.initialize(create_view, remove_view, context);
18 }
19 ViewList.prototype.initialize = function (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 = remove_view || function (view) { view.remove(); };
25 };
26 /**
27 * the create_view, remove_view, and context arguments override the defaults
28 * specified when the list is created.
29 * after this function, the .views attribute is a list of promises for views
30 * if you want to perform some action on the list of views, do something like
31 * `Promise.all(myviewlist.views).then(function(views) {...});`
32 */
33 ViewList.prototype.update = function (new_models, create_view, remove_view, context) {
34 var remove = remove_view || this._remove_view;
35 var create = create_view || this._create_view;
36 context = context || this._handler_context;
37 var i = 0;
38 // first, skip past the beginning of the lists if they are identical
39 for (; i < new_models.length; i++) {
40 if (i >= this._models.length || new_models[i] !== this._models[i]) {
41 break;
42 }
43 }
44 var first_removed = i;
45 // Remove the non-matching items from the old list.
46 var removed = this.views.splice(first_removed, this.views.length - first_removed);
47 for (var j = 0; j < removed.length; j++) {
48 removed[j].then(function (view) {
49 remove.call(context, view);
50 });
51 }
52 // Add the rest of the new list items.
53 for (; i < new_models.length; i++) {
54 this.views.push(Promise.resolve(create.call(context, new_models[i], i)));
55 }
56 // make a copy of the input array
57 this._models = new_models.slice();
58 // return a promise that resolves to all of the resolved views
59 return Promise.all(this.views);
60 };
61 /**
62 * removes every view in the list; convenience function for `.update([])`
63 * that should be faster
64 * returns a promise that resolves after this removal is done
65 */
66 ViewList.prototype.remove = function () {
67 var _this = this;
68 return Promise.all(this.views).then(function (views) {
69 views.forEach(function (value) { return _this._remove_view.call(_this._handler_context, value); });
70 _this.views = [];
71 _this._models = [];
72 });
73 };
74 /**
75 * Dispose this viewlist.
76 *
77 * A synchronous function which just deletes references to child views. This
78 * function does not call .remove() on child views because that is
79 * asynchronous. Use this in cases where child views will be removed in
80 * another way.
81 */
82 ViewList.prototype.dispose = function () {
83 this.views = null;
84 this._models = null;
85 };
86 return ViewList;
87}());
88export { ViewList };