widget_link.js
86 lines
| 3.4 KiB
| application/javascript
|
JavascriptLexer
Jason Grout
|
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
|
r18053 | this.update_value(this.get("widgets")[0]); | ||
Jason Grout
|
r18051 | }, this); | ||
Jason Grout
|
r18058 | this.once("destroy", function(model, collection, options) { | ||
Jason Grout
|
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
|
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
|
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
|
r18053 | if (element[0]) { | ||
element[0].set(element[1], new_value); | ||||
element[0].save_changes(); | ||||
} | ||||
Jason Grout
|
r18051 | }, this); | ||
this.updating = false; | ||||
}, | ||||
}); | ||||
Sylvain Corlay
|
r18052 | |||
var DirectionalLinkModel = widget.WidgetModel.extend({ | ||||
initialize: function() { | ||||
this.on("change", this.update_bindings, this); | ||||
Jason Grout
|
r18058 | this.once("destroy", function() { | ||
Sylvain Corlay
|
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
|
r18053 | if (this.source) { | ||
Sylvain Corlay
|
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
|
r18053 | if (element[0]) { | ||
element[0].set(element[1], new_value); | ||||
element[0].save_changes(); | ||||
} | ||||
Sylvain Corlay
|
r18052 | }, this); | ||
this.updating = false; | ||||
}, | ||||
}); | ||||
Jason Grout
|
r18051 | return { | ||
"LinkModel": LinkModel, | ||||
Sylvain Corlay
|
r18052 | "DirectionalLinkModel": DirectionalLinkModel, | ||
Jason Grout
|
r18051 | } | ||
}); | ||||