Show More
@@ -42,7 +42,46 b' define([' | |||
|
42 | 42 | this.updating = false; |
|
43 | 43 | }, |
|
44 | 44 | }); |
|
45 | ||
|
46 | var DirectionalLinkModel = widget.WidgetModel.extend({ | |
|
47 | initialize: function() { | |
|
48 | this.on("change", this.update_bindings, this); | |
|
49 | this.on("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 | ||
|
45 | 83 | return { |
|
46 | 84 | "LinkModel": LinkModel, |
|
85 | "DirectionalLinkModel": DirectionalLinkModel, | |
|
47 | 86 | } |
|
48 | 87 | }); |
@@ -10,7 +10,7 b' from .widget_selection import RadioButtons, ToggleButtons, Dropdown, Select' | |||
|
10 | 10 | from .widget_selectioncontainer import Tab, Accordion |
|
11 | 11 | from .widget_string import HTML, Latex, Text, Textarea |
|
12 | 12 | from .interaction import interact, interactive, fixed, interact_manual |
|
13 | from .widget_link import Link, link | |
|
13 | from .widget_link import Link, link, DirectionalLink, dlink | |
|
14 | 14 | |
|
15 | 15 | # Deprecated classes |
|
16 | 16 | from .widget_bool import CheckboxWidget, ToggleButtonWidget |
@@ -15,7 +15,7 b' click events on the button and trigger backend code when the clicks are fired.' | |||
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | from .widget import Widget |
|
18 | from IPython.utils.traitlets import Unicode, Tuple | |
|
18 | from IPython.utils.traitlets import Unicode, Tuple, Any | |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Classes |
@@ -31,5 +31,24 b' class Link(Widget):' | |||
|
31 | 31 | kwargs['widgets'] = widgets |
|
32 | 32 | super(Link, self).__init__(**kwargs) |
|
33 | 33 | |
|
34 | ||
|
34 | 35 | def link(*args): |
|
35 | 36 | return Link(widgets=args) |
|
37 | ||
|
38 | ||
|
39 | class DirectionalLink(Widget): | |
|
40 | """Directional Link Widget""" | |
|
41 | _model_name = Unicode('DirectionalLinkModel', sync=True) | |
|
42 | targets = Any(sync=True) | |
|
43 | source = Tuple(sync=True) | |
|
44 | ||
|
45 | # Does not quite behave like other widgets but reproduces | |
|
46 | # the behavior of IPython.utils.traitlets.directional_link | |
|
47 | def __init__(self, source, targets=(), **kwargs): | |
|
48 | kwargs['source'] = source | |
|
49 | kwargs['targets'] = targets | |
|
50 | super(DirectionalLink, self).__init__(**kwargs) | |
|
51 | ||
|
52 | ||
|
53 | def dlink(source, *targets): | |
|
54 | return DirectionalLink(source, targets) |
General Comments 0
You need to be logged in to leave comments.
Login now