Show More
@@ -151,8 +151,8 b' define(["notebook/js/widget"], function(widget_manager){' | |||
|
151 | 151 | // Handles text submition |
|
152 | 152 | handleKeypress: function(e) { |
|
153 | 153 | if (e.keyCode == 13) { // Return key |
|
154 | this.model.set('submits', this.model.get('submits') + 1); | |
|
155 |
this.model. |
|
|
154 | this.model.last_modified_view = this; // For callbacks. | |
|
155 | this.model.send({event: 'submit'}); | |
|
156 | 156 | } |
|
157 | 157 | }, |
|
158 | 158 | }); |
@@ -27,23 +27,52 b' class StringWidget(Widget):' | |||
|
27 | 27 | default_view_name = Unicode('TextBoxView') |
|
28 | 28 | |
|
29 | 29 | # Keys |
|
30 |
_keys = ['value', 'disabled', 'description', ' |
|
|
30 | _keys = ['value', 'disabled', 'description', 'scroll_to_bottoms'] | |
|
31 | 31 | value = Unicode(help="String value") |
|
32 | 32 | disabled = Bool(False, help="Enable or disable user changes") |
|
33 | 33 | description = Unicode(help="Description of the value this widget represents") |
|
34 | submits = Int(0, help="Used to capture and fire submission ") | |
|
35 | 34 | scroll_to_bottoms = Int(0, help="Used to scroll a TextAreaView to the bottom") |
|
36 | 35 | |
|
37 | 36 | |
|
38 | 37 | def __init__(self, **kwargs): |
|
39 | 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 | 43 | def scroll_to_bottom(self): |
|
44 | 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 | 76 | def on_submit(self, callback, remove=False): |
|
48 | 77 | """Register a callback to handle text submission (triggered when the |
|
49 | 78 | user clicks enter). |
@@ -62,23 +91,22 b' class StringWidget(Widget):' | |||
|
62 | 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 | 95 | """Handles when a string widget view is submitted.""" |
|
67 | if new_value > old_value: | |
|
68 | for handler in self._submission_callbacks: | |
|
69 |
|
|
|
70 | argspec = inspect.getargspec(handler) | |
|
71 | nargs = len(argspec[0]) | |
|
72 | ||
|
73 | # Bound methods have an additional 'self' argument | |
|
74 | if isinstance(handler, types.MethodType): | |
|
75 | nargs -= 1 | |
|
76 | ||
|
77 | # Call the callback | |
|
78 |
|
|
|
79 | handler() | |
|
80 |
|
|
|
81 |
|
|
|
82 | else: | |
|
83 | raise TypeError('StringWidget submit callback must ' \ | |
|
84 | 'accept 0 or 1 arguments.') | |
|
96 | for handler in self._submission_callbacks: | |
|
97 | if callable(handler): | |
|
98 | argspec = inspect.getargspec(handler) | |
|
99 | nargs = len(argspec[0]) | |
|
100 | ||
|
101 | # Bound methods have an additional 'self' argument | |
|
102 | if isinstance(handler, types.MethodType): | |
|
103 | nargs -= 1 | |
|
104 | ||
|
105 | # Call the callback | |
|
106 | if nargs == 0: | |
|
107 | handler() | |
|
108 | elif nargs == 1: | |
|
109 | handler(self) | |
|
110 | else: | |
|
111 | raise TypeError('StringWidget submit callback must ' \ | |
|
112 | 'accept 0 or 1 arguments.') |
General Comments 0
You need to be logged in to leave comments.
Login now