Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
"widgets/js/widget",
6
"jquery",
7
], function(widget, $){
8
var LinkModel = widget.WidgetModel.extend({
9
initialize: function() {
10
this.on("change:widgets", function(model, value, options) {
11
this.update_bindings(model.previous("widgets") || [], value);
12
this.update_value(this.get("widgets")[0]);
13
}, this);
14
this.once("destroy", function(model, collection, options) {
15
this.update_bindings(this.get("widgets"), []);
16
}, this);
17
},
18
update_bindings: function(oldlist, newlist) {
19
var that = this;
20
_.each(oldlist, function(elt) {elt[0].off("change:" + elt[1], null, that);});
21
_.each(newlist, function(elt) {elt[0].on("change:" + elt[1],
22
function(model, value, options) {
23
that.update_value(elt);
24
}, that);
25
// TODO: register for any destruction handlers
26
// to take an item out of the list
27
});
28
},
29
update_value: function(elt) {
30
if (this.updating) {return;}
31
var model = elt[0];
32
var attr = elt[1];
33
var new_value = model.get(attr);
34
this.updating = true;
35
_.each(_.without(this.get("widgets"), elt),
36
function(element, index, list) {
37
if (element[0]) {
38
element[0].set(element[1], new_value);
39
element[0].save_changes();
40
}
41
}, this);
42
this.updating = false;
43
},
44
});
45
46
var DirectionalLinkModel = widget.WidgetModel.extend({
47
initialize: function() {
48
this.on("change", this.update_bindings, this);
49
this.once("destroy", function() {
50
if (this.source) {
51
this.source[0].off("change:" + this.source[1], null, this);
52
}
53
}, this);
54
},
55
update_bindings: function() {
56
if (this.source) {
57
this.source[0].off("change:" + this.source[1], null, this);
58
}
59
this.source = this.get("source");
60
if (this.source) {
61
this.source[0].on("change:" + this.source[1], function() { this.update_value(this.source); }, this);
62
this.update_value(this.source);
63
}
64
},
65
update_value: function(elt) {
66
if (this.updating) {return;}
67
var model = elt[0];
68
var attr = elt[1];
69
var new_value = model.get(attr);
70
this.updating = true;
71
_.each(this.get("targets"),
72
function(element, index, list) {
73
if (element[0]) {
74
element[0].set(element[1], new_value);
75
element[0].save_changes();
76
}
77
}, this);
78
this.updating = false;
79
},
80
});
81
82
return {
83
"LinkModel": LinkModel,
84
"DirectionalLinkModel": DirectionalLinkModel,
85
}
86
});
87
88