Show More
@@ -132,7 +132,8 b' define(["notebook/js/widget"], function(widget_manager){' | |||||
132 |
|
132 | |||
133 | events: {"keyup input" : "handleChanging", |
|
133 | events: {"keyup input" : "handleChanging", | |
134 | "paste input" : "handleChanging", |
|
134 | "paste input" : "handleChanging", | |
135 |
"cut input" : "handleChanging" |
|
135 | "cut input" : "handleChanging", | |
|
136 | "keypress input" : "handleKeypress"}, | |||
136 |
|
137 | |||
137 | // Handles and validates user input. |
|
138 | // Handles and validates user input. | |
138 | handleChanging: function(e) { |
|
139 | handleChanging: function(e) { | |
@@ -141,6 +142,16 b' define(["notebook/js/widget"], function(widget_manager){' | |||||
141 | this.model.update_other_views(this); |
|
142 | this.model.update_other_views(this); | |
142 | this.user_invoked_update = false; |
|
143 | this.user_invoked_update = false; | |
143 | }, |
|
144 | }, | |
|
145 | ||||
|
146 | // Handles text submition | |||
|
147 | handleKeypress: function(e) { | |||
|
148 | if (e.keyCode == 13) { // Return key | |||
|
149 | this.user_invoked_update = true; | |||
|
150 | this.model.set('submits', this.model.get('submits') + 1); | |||
|
151 | this.model.update_other_views(this); | |||
|
152 | this.user_invoked_update = false; | |||
|
153 | } | |||
|
154 | }, | |||
144 | }); |
|
155 | }); | |
145 |
|
156 | |||
146 | widget_manager.register_widget_view('TextBoxView', TextBoxView); |
|
157 | widget_manager.register_widget_view('TextBoxView', TextBoxView); |
@@ -13,8 +13,11 b' Represents a unicode string using a widget.' | |||||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | import inspect | |||
|
17 | import types | |||
|
18 | ||||
16 | from .widget import Widget |
|
19 | from .widget import Widget | |
17 | from IPython.utils.traitlets import Unicode, Bool, List |
|
20 | from IPython.utils.traitlets import Unicode, Bool, List, Int | |
18 |
|
21 | |||
19 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
23 | # Classes | |
@@ -24,7 +27,53 b' class StringWidget(Widget):' | |||||
24 | default_view_name = Unicode('TextBoxView') |
|
27 | default_view_name = Unicode('TextBoxView') | |
25 |
|
28 | |||
26 | # Keys |
|
29 | # Keys | |
27 | _keys = ['value', 'disabled', 'description'] |
|
30 | _keys = ['value', 'disabled', 'description', 'submits'] | |
28 | value = Unicode(help="String value") |
|
31 | value = Unicode(help="String value") | |
29 | disabled = Bool(False, help="Enable or disable user changes") |
|
32 | disabled = Bool(False, help="Enable or disable user changes") | |
30 | description = Unicode(help="Description of the value this widget represents") |
|
33 | description = Unicode(help="Description of the value this widget represents") | |
|
34 | submits = Int(0, help="Used to capture and fire submission ") | |||
|
35 | ||||
|
36 | ||||
|
37 | def __init__(self, **kwargs): | |||
|
38 | super(StringWidget, self).__init__(**kwargs) | |||
|
39 | self._submission_callbacks = [] | |||
|
40 | ||||
|
41 | ||||
|
42 | def on_submit(self, callback, remove=False): | |||
|
43 | """Register a callback to handle text submission (triggered when the | |||
|
44 | user clicks enter). | |||
|
45 | ||||
|
46 | Parameters | |||
|
47 | callback: Method handle | |||
|
48 | Function to be called when the text has been submitted. Function | |||
|
49 | can have two possible signatures: | |||
|
50 | callback() | |||
|
51 | callback(sender) | |||
|
52 | remove: bool (optional) | |||
|
53 | Whether or not to unregister the callback""" | |||
|
54 | if remove and callback in self._submission_callbacks: | |||
|
55 | self._submission_callbacks.remove(callback) | |||
|
56 | elif not remove and not callback in self._submission_callbacks: | |||
|
57 | self._submission_callbacks.append(callback) | |||
|
58 | ||||
|
59 | ||||
|
60 | def _submits_changed(self, name, old_value, new_value): | |||
|
61 | """Handles when a string widget view is submitted.""" | |||
|
62 | if new_value > old_value: | |||
|
63 | for handler in self._submission_callbacks: | |||
|
64 | if callable(handler): | |||
|
65 | argspec = inspect.getargspec(handler) | |||
|
66 | nargs = len(argspec[0]) | |||
|
67 | ||||
|
68 | # Bound methods have an additional 'self' argument | |||
|
69 | if isinstance(handler, types.MethodType): | |||
|
70 | nargs -= 1 | |||
|
71 | ||||
|
72 | # Call the callback | |||
|
73 | if nargs == 0: | |||
|
74 | handler() | |||
|
75 | elif nargs == 1: | |||
|
76 | handler(self) | |||
|
77 | else: | |||
|
78 | raise TypeError('StringWidget submit callback must ' \ | |||
|
79 | 'accept 0 or 1 arguments.') |
General Comments 0
You need to be logged in to leave comments.
Login now