From d6c978584acd1a7c879ce0e7a368f4db7e5aace5 2014-09-26 18:32:59 From: Jason Grout Date: 2014-09-26 18:32:59 Subject: [PATCH] Adding Link widget --- diff --git a/IPython/html/static/widgets/js/widget_link.js b/IPython/html/static/widgets/js/widget_link.js new file mode 100644 index 0000000..6c28427 --- /dev/null +++ b/IPython/html/static/widgets/js/widget_link.js @@ -0,0 +1,48 @@ +// 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.on("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; + }, + }); + return { + "LinkModel": LinkModel, + } +}); diff --git a/IPython/html/widgets/__init__.py b/IPython/html/widgets/__init__.py index 2e93522..552f32e 100644 --- a/IPython/html/widgets/__init__.py +++ b/IPython/html/widgets/__init__.py @@ -10,6 +10,7 @@ from .widget_selection import RadioButtons, ToggleButtons, Dropdown, Select from .widget_selectioncontainer import Tab, Accordion from .widget_string import HTML, Latex, Text, Textarea from .interaction import interact, interactive, fixed, interact_manual +from .widget_link import Link, link # Deprecated classes from .widget_bool import CheckboxWidget, ToggleButtonWidget diff --git a/IPython/html/widgets/widget_link.py b/IPython/html/widgets/widget_link.py new file mode 100644 index 0000000..1753973 --- /dev/null +++ b/IPython/html/widgets/widget_link.py @@ -0,0 +1,35 @@ +"""Link and DirectionalLink classes. + +Represents a button in the frontend using a widget. Allows user to listen for +click events on the button and trigger backend code when the clicks are fired. +""" +#----------------------------------------------------------------------------- +# Copyright (c) 2013, 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 + +#----------------------------------------------------------------------------- +# 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) + +def link(*args): + return Link(widgets=args)