##// END OF EJS Templates
Changed on_submit to use custom messages instead of stateful communcation
Jonathan Frederic -
Show More
@@ -151,8 +151,8 b' define(["notebook/js/widget"], function(widget_manager){'
151 // Handles text submition
151 // Handles text submition
152 handleKeypress: function(e) {
152 handleKeypress: function(e) {
153 if (e.keyCode == 13) { // Return key
153 if (e.keyCode == 13) { // Return key
154 this.model.set('submits', this.model.get('submits') + 1);
154 this.model.last_modified_view = this; // For callbacks.
155 this.model.update_other_views(this);
155 this.model.send({event: 'submit'});
156 }
156 }
157 },
157 },
158 });
158 });
@@ -27,23 +27,52 b' class StringWidget(Widget):'
27 default_view_name = Unicode('TextBoxView')
27 default_view_name = Unicode('TextBoxView')
28
28
29 # Keys
29 # Keys
30 _keys = ['value', 'disabled', 'description', 'submits', 'scroll_to_bottoms']
30 _keys = ['value', 'disabled', 'description', 'scroll_to_bottoms']
31 value = Unicode(help="String value")
31 value = Unicode(help="String value")
32 disabled = Bool(False, help="Enable or disable user changes")
32 disabled = Bool(False, help="Enable or disable user changes")
33 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 scroll_to_bottoms = Int(0, help="Used to scroll a TextAreaView to the bottom")
34 scroll_to_bottoms = Int(0, help="Used to scroll a TextAreaView to the bottom")
36
35
37
36
38 def __init__(self, **kwargs):
37 def __init__(self, **kwargs):
39 super(StringWidget, self).__init__(**kwargs)
38 super(StringWidget, self).__init__(**kwargs)
40 self._submission_callbacks = []
39 self._submission_callbacks = []
40 self.on_msg(self._handle_string_msg)
41
41
42
42
43 def scroll_to_bottom(self):
43 def scroll_to_bottom(self):
44 self.scroll_to_bottoms += 1
44 self.scroll_to_bottoms += 1
45
45
46
46
47 def on_click(self, callback, remove=False):
48 """Register a callback to execute when the button is clicked. The
49 callback can either accept no parameters or one sender parameter:
50 - callback()
51 - callback(sender)
52 If the callback has a sender parameter, the ButtonWidget instance that
53 called the callback will be passed into the method as the sender.
54
55 Parameters
56 ----------
57 remove : bool (optional)
58 Set to true to remove the callback from the list of callbacks."""
59 if remove:
60 self._click_handlers.remove(callback)
61 elif not callback in self._click_handlers:
62 self._click_handlers.append(callback)
63
64
65 def _handle_string_msg(self, content):
66 """Handle a msg from the front-end
67
68 Parameters
69 ----------
70 content: dict
71 Content of the msg."""
72 if 'event' in content and content['event'] == 'submit':
73 self._handle_submit()
74
75
47 def on_submit(self, callback, remove=False):
76 def on_submit(self, callback, remove=False):
48 """Register a callback to handle text submission (triggered when the
77 """Register a callback to handle text submission (triggered when the
49 user clicks enter).
78 user clicks enter).
@@ -62,23 +91,22 b' class StringWidget(Widget):'
62 self._submission_callbacks.append(callback)
91 self._submission_callbacks.append(callback)
63
92
64
93
65 def _submits_changed(self, name, old_value, new_value):
94 def _handle_submit(self):
66 """Handles when a string widget view is submitted."""
95 """Handles when a string widget view is submitted."""
67 if new_value > old_value:
96 for handler in self._submission_callbacks:
68 for handler in self._submission_callbacks:
97 if callable(handler):
69 if callable(handler):
98 argspec = inspect.getargspec(handler)
70 argspec = inspect.getargspec(handler)
99 nargs = len(argspec[0])
71 nargs = len(argspec[0])
100
72
101 # Bound methods have an additional 'self' argument
73 # Bound methods have an additional 'self' argument
102 if isinstance(handler, types.MethodType):
74 if isinstance(handler, types.MethodType):
103 nargs -= 1
75 nargs -= 1
104
76
105 # Call the callback
77 # Call the callback
106 if nargs == 0:
78 if nargs == 0:
107 handler()
79 handler()
108 elif nargs == 1:
80 elif nargs == 1:
109 handler(self)
81 handler(self)
110 else:
82 else:
111 raise TypeError('StringWidget submit callback must ' \
83 raise TypeError('StringWidget submit callback must ' \
112 'accept 0 or 1 arguments.')
84 'accept 0 or 1 arguments.')
General Comments 0
You need to be logged in to leave comments. Login now