Show More
@@ -0,0 +1,48 b'' | |||||
|
1 | // Copyright (c) IPython Development Team. | |||
|
2 | // Distributed under the terms of the Modified BSD License. | |||
|
3 | ||||
|
4 | define([ | |||
|
5 | "widgets/js/widget", | |||
|
6 | "jquery", | |||
|
7 | ], function(widget, $){ | |||
|
8 | var LinkModel = widget.WidgetModel.extend({ | |||
|
9 | initialize: function() { | |||
|
10 | this.on("change:widgets", function(model, value, options) { | |||
|
11 | this.update_bindings(model.previous("widgets") || [], value); | |||
|
12 | this.update_value(this.get("widgets")[0]); | |||
|
13 | }, this); | |||
|
14 | this.on("destroy", function(model, collection, options) { | |||
|
15 | this.update_bindings(this.get("widgets"), []); | |||
|
16 | }, this); | |||
|
17 | }, | |||
|
18 | update_bindings: function(oldlist, newlist) { | |||
|
19 | var that = this; | |||
|
20 | _.each(oldlist, function(elt) {elt[0].off("change:" + elt[1], null, that);}); | |||
|
21 | _.each(newlist, function(elt) {elt[0].on("change:" + elt[1], | |||
|
22 | function(model, value, options) { | |||
|
23 | that.update_value(elt); | |||
|
24 | }, that); | |||
|
25 | // TODO: register for any destruction handlers | |||
|
26 | // to take an item out of the list | |||
|
27 | }); | |||
|
28 | }, | |||
|
29 | update_value: function(elt) { | |||
|
30 | if (this.updating) {return;} | |||
|
31 | var model = elt[0]; | |||
|
32 | var attr = elt[1]; | |||
|
33 | var new_value = model.get(attr); | |||
|
34 | this.updating = true; | |||
|
35 | _.each(_.without(this.get("widgets"), elt), | |||
|
36 | function(element, index, list) { | |||
|
37 | if (element[0]) { | |||
|
38 | element[0].set(element[1], new_value); | |||
|
39 | element[0].save_changes(); | |||
|
40 | } | |||
|
41 | }, this); | |||
|
42 | this.updating = false; | |||
|
43 | }, | |||
|
44 | }); | |||
|
45 | return { | |||
|
46 | "LinkModel": LinkModel, | |||
|
47 | } | |||
|
48 | }); |
@@ -0,0 +1,35 b'' | |||||
|
1 | """Link and DirectionalLink classes. | |||
|
2 | ||||
|
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. | |||
|
5 | """ | |||
|
6 | #----------------------------------------------------------------------------- | |||
|
7 | # Copyright (c) 2013, the IPython Development Team. | |||
|
8 | # | |||
|
9 | # Distributed under the terms of the Modified BSD License. | |||
|
10 | # | |||
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | ||||
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | # Imports | |||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | from .widget import Widget | |||
|
18 | from IPython.utils.traitlets import Unicode, Tuple | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Classes | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | ||||
|
25 | class Link(Widget): | |||
|
26 | """Link Widget""" | |||
|
27 | _model_name = Unicode('LinkModel', sync=True) | |||
|
28 | widgets = Tuple(sync=True, allow_none=False) | |||
|
29 | ||||
|
30 | def __init__(self, widgets=(), **kwargs): | |||
|
31 | kwargs['widgets'] = widgets | |||
|
32 | super(Link, self).__init__(**kwargs) | |||
|
33 | ||||
|
34 | def link(*args): | |||
|
35 | return Link(widgets=args) |
@@ -10,6 +10,7 b' 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 |
|
14 | |||
14 | # Deprecated classes |
|
15 | # Deprecated classes | |
15 | from .widget_bool import CheckboxWidget, ToggleButtonWidget |
|
16 | from .widget_bool import CheckboxWidget, ToggleButtonWidget |
General Comments 0
You need to be logged in to leave comments.
Login now