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