diff --git a/IPython/html/static/notebook/js/widgets/string.js b/IPython/html/static/notebook/js/widgets/string.js index d7c2482..f221d8d 100644 --- a/IPython/html/static/notebook/js/widgets/string.js +++ b/IPython/html/static/notebook/js/widgets/string.js @@ -132,7 +132,8 @@ define(["notebook/js/widget"], function(widget_manager){ events: {"keyup input" : "handleChanging", "paste input" : "handleChanging", - "cut input" : "handleChanging"}, + "cut input" : "handleChanging", + "keypress input" : "handleKeypress"}, // Handles and validates user input. handleChanging: function(e) { @@ -141,6 +142,16 @@ define(["notebook/js/widget"], function(widget_manager){ this.model.update_other_views(this); this.user_invoked_update = false; }, + + // Handles text submition + handleKeypress: function(e) { + if (e.keyCode == 13) { // Return key + this.user_invoked_update = true; + this.model.set('submits', this.model.get('submits') + 1); + this.model.update_other_views(this); + this.user_invoked_update = false; + } + }, }); widget_manager.register_widget_view('TextBoxView', TextBoxView); diff --git a/IPython/html/widgets/widget_string.py b/IPython/html/widgets/widget_string.py index f603956..2ddad4e 100644 --- a/IPython/html/widgets/widget_string.py +++ b/IPython/html/widgets/widget_string.py @@ -13,8 +13,11 @@ Represents a unicode string using a widget. #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- +import inspect +import types + from .widget import Widget -from IPython.utils.traitlets import Unicode, Bool, List +from IPython.utils.traitlets import Unicode, Bool, List, Int #----------------------------------------------------------------------------- # Classes @@ -24,7 +27,53 @@ class StringWidget(Widget): default_view_name = Unicode('TextBoxView') # Keys - _keys = ['value', 'disabled', 'description'] + _keys = ['value', 'disabled', 'description', 'submits'] value = Unicode(help="String value") disabled = Bool(False, help="Enable or disable user changes") description = Unicode(help="Description of the value this widget represents") + submits = Int(0, help="Used to capture and fire submission ") + + + def __init__(self, **kwargs): + super(StringWidget, self).__init__(**kwargs) + self._submission_callbacks = [] + + + def on_submit(self, callback, remove=False): + """Register a callback to handle text submission (triggered when the + user clicks enter). + + Parameters + callback: Method handle + Function to be called when the text has been submitted. Function + can have two possible signatures: + callback() + callback(sender) + remove: bool (optional) + Whether or not to unregister the callback""" + if remove and callback in self._submission_callbacks: + self._submission_callbacks.remove(callback) + elif not remove and not callback in self._submission_callbacks: + self._submission_callbacks.append(callback) + + + def _submits_changed(self, name, old_value, new_value): + """Handles when a string widget view is submitted.""" + if new_value > old_value: + for handler in self._submission_callbacks: + if callable(handler): + argspec = inspect.getargspec(handler) + nargs = len(argspec[0]) + + # Bound methods have an additional 'self' argument + if isinstance(handler, types.MethodType): + nargs -= 1 + + # Call the callback + if nargs == 0: + handler() + elif nargs == 1: + handler(self) + else: + raise TypeError('StringWidget submit callback must ' \ + 'accept 0 or 1 arguments.')