Show More
@@ -1,48 +1,87 | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | "widgets/js/widget", |
|
5 | "widgets/js/widget", | |
6 | "jquery", |
|
6 | "jquery", | |
7 | ], function(widget, $){ |
|
7 | ], function(widget, $){ | |
8 | var LinkModel = widget.WidgetModel.extend({ |
|
8 | var LinkModel = widget.WidgetModel.extend({ | |
9 | initialize: function() { |
|
9 | initialize: function() { | |
10 | this.on("change:widgets", function(model, value, options) { |
|
10 | this.on("change:widgets", function(model, value, options) { | |
11 | this.update_bindings(model.previous("widgets") || [], value); |
|
11 | this.update_bindings(model.previous("widgets") || [], value); | |
12 | this.update_value(this.get("widgets")[0]); |
|
12 | this.update_value(this.get("widgets")[0]); | |
13 | }, this); |
|
13 | }, this); | |
14 | this.on("destroy", function(model, collection, options) { |
|
14 | this.on("destroy", function(model, collection, options) { | |
15 | this.update_bindings(this.get("widgets"), []); |
|
15 | this.update_bindings(this.get("widgets"), []); | |
16 | }, this); |
|
16 | }, this); | |
17 | }, |
|
17 | }, | |
18 | update_bindings: function(oldlist, newlist) { |
|
18 | update_bindings: function(oldlist, newlist) { | |
19 | var that = this; |
|
19 | var that = this; | |
20 | _.each(oldlist, function(elt) {elt[0].off("change:" + elt[1], null, that);}); |
|
20 | _.each(oldlist, function(elt) {elt[0].off("change:" + elt[1], null, that);}); | |
21 | _.each(newlist, function(elt) {elt[0].on("change:" + elt[1], |
|
21 | _.each(newlist, function(elt) {elt[0].on("change:" + elt[1], | |
22 | function(model, value, options) { |
|
22 | function(model, value, options) { | |
23 | that.update_value(elt); |
|
23 | that.update_value(elt); | |
24 | }, that); |
|
24 | }, that); | |
25 | // TODO: register for any destruction handlers |
|
25 | // TODO: register for any destruction handlers | |
26 | // to take an item out of the list |
|
26 | // to take an item out of the list | |
27 | }); |
|
27 | }); | |
28 | }, |
|
28 | }, | |
29 | update_value: function(elt) { |
|
29 | update_value: function(elt) { | |
30 | if (this.updating) {return;} |
|
30 | if (this.updating) {return;} | |
31 | var model = elt[0]; |
|
31 | var model = elt[0]; | |
32 | var attr = elt[1]; |
|
32 | var attr = elt[1]; | |
33 | var new_value = model.get(attr); |
|
33 | var new_value = model.get(attr); | |
34 | this.updating = true; |
|
34 | this.updating = true; | |
35 | _.each(_.without(this.get("widgets"), elt), |
|
35 | _.each(_.without(this.get("widgets"), elt), | |
36 | function(element, index, list) { |
|
36 | function(element, index, list) { | |
37 | if (element[0]) { |
|
37 | if (element[0]) { | |
38 | element[0].set(element[1], new_value); |
|
38 | element[0].set(element[1], new_value); | |
39 | element[0].save_changes(); |
|
39 | element[0].save_changes(); | |
40 | } |
|
40 | } | |
41 | }, this); |
|
41 | }, this); | |
42 | this.updating = false; |
|
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 | return { |
|
83 | return { | |
46 | "LinkModel": LinkModel, |
|
84 | "LinkModel": LinkModel, | |
|
85 | "DirectionalLinkModel": DirectionalLinkModel, | |||
47 | } |
|
86 | } | |
48 | }); |
|
87 | }); |
@@ -1,24 +1,24 | |||||
1 | from .widget import Widget, DOMWidget, CallbackDispatcher |
|
1 | from .widget import Widget, DOMWidget, CallbackDispatcher | |
2 |
|
2 | |||
3 | from .widget_bool import Checkbox, ToggleButton |
|
3 | from .widget_bool import Checkbox, ToggleButton | |
4 | from .widget_button import Button |
|
4 | from .widget_button import Button | |
5 | from .widget_box import Box, Popup, FlexBox, HBox, VBox |
|
5 | from .widget_box import Box, Popup, FlexBox, HBox, VBox | |
6 | from .widget_float import FloatText, BoundedFloatText, FloatSlider, FloatProgress, FloatRangeSlider |
|
6 | from .widget_float import FloatText, BoundedFloatText, FloatSlider, FloatProgress, FloatRangeSlider | |
7 | from .widget_image import Image |
|
7 | from .widget_image import Image | |
8 | from .widget_int import IntText, BoundedIntText, IntSlider, IntProgress, IntRangeSlider |
|
8 | from .widget_int import IntText, BoundedIntText, IntSlider, IntProgress, IntRangeSlider | |
9 | from .widget_selection import RadioButtons, ToggleButtons, Dropdown, Select |
|
9 | from .widget_selection import RadioButtons, ToggleButtons, Dropdown, Select | |
10 | from .widget_selectioncontainer import Tab, Accordion |
|
10 | from .widget_selectioncontainer import Tab, Accordion | |
11 | from .widget_string import HTML, Latex, Text, Textarea |
|
11 | from .widget_string import HTML, Latex, Text, Textarea | |
12 | from .interaction import interact, interactive, fixed, interact_manual |
|
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 | # Deprecated classes |
|
15 | # Deprecated classes | |
16 | from .widget_bool import CheckboxWidget, ToggleButtonWidget |
|
16 | from .widget_bool import CheckboxWidget, ToggleButtonWidget | |
17 | from .widget_button import ButtonWidget |
|
17 | from .widget_button import ButtonWidget | |
18 | from .widget_box import ContainerWidget, PopupWidget |
|
18 | from .widget_box import ContainerWidget, PopupWidget | |
19 | from .widget_float import FloatTextWidget, BoundedFloatTextWidget, FloatSliderWidget, FloatProgressWidget |
|
19 | from .widget_float import FloatTextWidget, BoundedFloatTextWidget, FloatSliderWidget, FloatProgressWidget | |
20 | from .widget_image import ImageWidget |
|
20 | from .widget_image import ImageWidget | |
21 | from .widget_int import IntTextWidget, BoundedIntTextWidget, IntSliderWidget, IntProgressWidget |
|
21 | from .widget_int import IntTextWidget, BoundedIntTextWidget, IntSliderWidget, IntProgressWidget | |
22 | from .widget_selection import RadioButtonsWidget, ToggleButtonsWidget, DropdownWidget, SelectWidget |
|
22 | from .widget_selection import RadioButtonsWidget, ToggleButtonsWidget, DropdownWidget, SelectWidget | |
23 | from .widget_selectioncontainer import TabWidget, AccordionWidget |
|
23 | from .widget_selectioncontainer import TabWidget, AccordionWidget | |
24 | from .widget_string import HTMLWidget, LatexWidget, TextWidget, TextareaWidget |
|
24 | from .widget_string import HTMLWidget, LatexWidget, TextWidget, TextareaWidget |
@@ -1,35 +1,54 | |||||
1 | """Link and DirectionalLink classes. |
|
1 | """Link and DirectionalLink classes. | |
2 |
|
2 | |||
3 | Represents a button in the frontend using a widget. Allows user to listen for |
|
3 | Represents a button in the frontend using a widget. Allows user to listen for | |
4 | click events on the button and trigger backend code when the clicks are fired. |
|
4 | click events on the button and trigger backend code when the clicks are fired. | |
5 | """ |
|
5 | """ | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | # Copyright (c) 2013, the IPython Development Team. | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the Modified BSD License. |
|
9 | # Distributed under the terms of the Modified BSD License. | |
10 | # |
|
10 | # | |
11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | from .widget import Widget |
|
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 | # Classes |
|
21 | # Classes | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | class Link(Widget): |
|
25 | class Link(Widget): | |
26 | """Link Widget""" |
|
26 | """Link Widget""" | |
27 | _model_name = Unicode('LinkModel', sync=True) |
|
27 | _model_name = Unicode('LinkModel', sync=True) | |
28 | widgets = Tuple(sync=True, allow_none=False) |
|
28 | widgets = Tuple(sync=True, allow_none=False) | |
29 |
|
29 | |||
30 | def __init__(self, widgets=(), **kwargs): |
|
30 | def __init__(self, widgets=(), **kwargs): | |
31 | kwargs['widgets'] = widgets |
|
31 | kwargs['widgets'] = widgets | |
32 | super(Link, self).__init__(**kwargs) |
|
32 | super(Link, self).__init__(**kwargs) | |
33 |
|
33 | |||
|
34 | ||||
34 | def link(*args): |
|
35 | def link(*args): | |
35 | return Link(widgets=args) |
|
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