##// END OF EJS Templates
Backport parts of ipywidgets#3
Backport parts of ipywidgets#3

File last commit:

r18058:c7253b21
r21475:c42ee63a
Show More
widget_link.js
86 lines | 3.4 KiB | application/javascript | JavascriptLexer
Jason Grout
Adding Link widget
r18051 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
"widgets/js/widget",
"jquery",
], function(widget, $){
var LinkModel = widget.WidgetModel.extend({
initialize: function() {
this.on("change:widgets", function(model, value, options) {
this.update_bindings(model.previous("widgets") || [], value);
Jason Grout
Fix whitespace
r18053 this.update_value(this.get("widgets")[0]);
Jason Grout
Adding Link widget
r18051 }, this);
Jason Grout
on("destroy",...) -> once("destroy",...) so we don't keep a reference to it, preventing gc...
r18058 this.once("destroy", function(model, collection, options) {
Jason Grout
Adding Link widget
r18051 this.update_bindings(this.get("widgets"), []);
}, this);
},
update_bindings: function(oldlist, newlist) {
var that = this;
_.each(oldlist, function(elt) {elt[0].off("change:" + elt[1], null, that);});
_.each(newlist, function(elt) {elt[0].on("change:" + elt[1],
Jason Grout
Fix whitespace
r18053 function(model, value, options) {
that.update_value(elt);
}, that);
// TODO: register for any destruction handlers
// to take an item out of the list
});
Jason Grout
Adding Link widget
r18051 },
update_value: function(elt) {
if (this.updating) {return;}
var model = elt[0];
var attr = elt[1];
var new_value = model.get(attr);
this.updating = true;
_.each(_.without(this.get("widgets"), elt),
function(element, index, list) {
Jason Grout
Fix whitespace
r18053 if (element[0]) {
element[0].set(element[1], new_value);
element[0].save_changes();
}
Jason Grout
Adding Link widget
r18051 }, this);
this.updating = false;
},
});
Sylvain Corlay
Adding directional link widget
r18052
var DirectionalLinkModel = widget.WidgetModel.extend({
initialize: function() {
this.on("change", this.update_bindings, this);
Jason Grout
on("destroy",...) -> once("destroy",...) so we don't keep a reference to it, preventing gc...
r18058 this.once("destroy", function() {
Sylvain Corlay
Adding directional link widget
r18052 if (this.source) {
this.source[0].off("change:" + this.source[1], null, this);
}
}, this);
},
update_bindings: function() {
if (this.source) {
this.source[0].off("change:" + this.source[1], null, this);
}
this.source = this.get("source");
Jason Grout
Fix whitespace
r18053 if (this.source) {
Sylvain Corlay
Adding directional link widget
r18052 this.source[0].on("change:" + this.source[1], function() { this.update_value(this.source); }, this);
this.update_value(this.source);
}
},
update_value: function(elt) {
if (this.updating) {return;}
var model = elt[0];
var attr = elt[1];
var new_value = model.get(attr);
this.updating = true;
_.each(this.get("targets"),
function(element, index, list) {
Jason Grout
Fix whitespace
r18053 if (element[0]) {
element[0].set(element[1], new_value);
element[0].save_changes();
}
Sylvain Corlay
Adding directional link widget
r18052 }, this);
this.updating = false;
},
});
Jason Grout
Adding Link widget
r18051 return {
"LinkModel": LinkModel,
Sylvain Corlay
Adding directional link widget
r18052 "DirectionalLinkModel": DirectionalLinkModel,
Jason Grout
Adding Link widget
r18051 }
});