Merge pull request #6454 from jasongrout/links
Javascript link
commit
3ea8929b82
@ -0,0 +1,86 @@
|
||||
// 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);
|
||||
this.update_value(this.get("widgets")[0]);
|
||||
}, this);
|
||||
this.once("destroy", function(model, collection, options) {
|
||||
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],
|
||||
function(model, value, options) {
|
||||
that.update_value(elt);
|
||||
}, that);
|
||||
// TODO: register for any destruction handlers
|
||||
// to take an item out of the list
|
||||
});
|
||||
},
|
||||
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) {
|
||||
if (element[0]) {
|
||||
element[0].set(element[1], new_value);
|
||||
element[0].save_changes();
|
||||
}
|
||||
}, this);
|
||||
this.updating = false;
|
||||
},
|
||||
});
|
||||
|
||||
var DirectionalLinkModel = widget.WidgetModel.extend({
|
||||
initialize: function() {
|
||||
this.on("change", this.update_bindings, this);
|
||||
this.once("destroy", function() {
|
||||
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");
|
||||
if (this.source) {
|
||||
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) {
|
||||
if (element[0]) {
|
||||
element[0].set(element[1], new_value);
|
||||
element[0].save_changes();
|
||||
}
|
||||
}, this);
|
||||
this.updating = false;
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
"LinkModel": LinkModel,
|
||||
"DirectionalLinkModel": DirectionalLinkModel,
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,61 @@
|
||||
"""Link and DirectionalLink classes.
|
||||
|
||||
Propagate changes between widgets on the javascript side
|
||||
"""
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (c) 2014, the IPython Development Team.
|
||||
#
|
||||
# Distributed under the terms of the Modified BSD License.
|
||||
#
|
||||
# The full license is in the file COPYING.txt, distributed with this software.
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Imports
|
||||
#-----------------------------------------------------------------------------
|
||||
from .widget import Widget
|
||||
from IPython.utils.traitlets import Unicode, Tuple, Any
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Classes
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
class Link(Widget):
|
||||
"""Link Widget"""
|
||||
_model_name = Unicode('LinkModel', sync=True)
|
||||
widgets = Tuple(sync=True, allow_none=False)
|
||||
|
||||
def __init__(self, widgets=(), **kwargs):
|
||||
kwargs['widgets'] = widgets
|
||||
super(Link, self).__init__(**kwargs)
|
||||
|
||||
# for compatibility with traitlet links
|
||||
def unlink(self):
|
||||
self.close()
|
||||
|
||||
|
||||
def link(*args):
|
||||
return Link(widgets=args)
|
||||
|
||||
|
||||
class DirectionalLink(Widget):
|
||||
"""Directional Link Widget"""
|
||||
_model_name = Unicode('DirectionalLinkModel', sync=True)
|
||||
targets = Any(sync=True)
|
||||
source = Tuple(sync=True)
|
||||
|
||||
# Does not quite behave like other widgets but reproduces
|
||||
# the behavior of IPython.utils.traitlets.directional_link
|
||||
def __init__(self, source, targets=(), **kwargs):
|
||||
kwargs['source'] = source
|
||||
kwargs['targets'] = targets
|
||||
super(DirectionalLink, self).__init__(**kwargs)
|
||||
|
||||
# for compatibility with traitlet links
|
||||
def unlink(self):
|
||||
self.close()
|
||||
|
||||
|
||||
def dlink(source, *targets):
|
||||
return DirectionalLink(source, targets)
|
||||
Loading…
Reference in new issue