##// END OF EJS Templates
Remove on_click, doesn't apply
Jonathan Frederic -
Show More
@@ -1,111 +1,93 b''
1 """StringWidget class.
1 """StringWidget class.
2
2
3 Represents a unicode string using a widget.
3 Represents a unicode string using a widget.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 import inspect
16 import inspect
17 import types
17 import types
18
18
19 from .widget import Widget
19 from .widget import Widget
20 from IPython.utils.traitlets import Unicode, Bool, List, Int
20 from IPython.utils.traitlets import Unicode, Bool, List, Int
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Classes
23 # Classes
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 class StringWidget(Widget):
25 class StringWidget(Widget):
26 target_name = Unicode('StringWidgetModel')
26 target_name = Unicode('StringWidgetModel')
27 default_view_name = Unicode('TextBoxView')
27 default_view_name = Unicode('TextBoxView')
28
28
29 # Keys
29 # Keys
30 _keys = ['value', 'disabled', 'description']
30 _keys = ['value', 'disabled', 'description']
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
34
35
35
36 def __init__(self, **kwargs):
36 def __init__(self, **kwargs):
37 super(StringWidget, self).__init__(**kwargs)
37 super(StringWidget, self).__init__(**kwargs)
38 self._submission_callbacks = []
38 self._submission_callbacks = []
39 self.on_msg(self._handle_string_msg)
39 self.on_msg(self._handle_string_msg)
40
40
41
41
42 def scroll_to_bottom(self):
42 def scroll_to_bottom(self):
43 self._comm.send({"method": "scroll_to_bottom"})
43 self._comm.send({"method": "scroll_to_bottom"})
44
44
45
45
46 def on_click(self, callback, remove=False):
47 """Register a callback to execute when the button is clicked. The
48 callback can either accept no parameters or one sender parameter:
49 - callback()
50 - callback(sender)
51 If the callback has a sender parameter, the ButtonWidget instance that
52 called the callback will be passed into the method as the sender.
53
54 Parameters
55 ----------
56 remove : bool (optional)
57 Set to true to remove the callback from the list of callbacks."""
58 if remove:
59 self._click_handlers.remove(callback)
60 elif not callback in self._click_handlers:
61 self._click_handlers.append(callback)
62
63
64 def _handle_string_msg(self, content):
46 def _handle_string_msg(self, content):
65 """Handle a msg from the front-end
47 """Handle a msg from the front-end
66
48
67 Parameters
49 Parameters
68 ----------
50 ----------
69 content: dict
51 content: dict
70 Content of the msg."""
52 Content of the msg."""
71 if 'event' in content and content['event'] == 'submit':
53 if 'event' in content and content['event'] == 'submit':
72 self._handle_submit()
54 self._handle_submit()
73
55
74
56
75 def on_submit(self, callback, remove=False):
57 def on_submit(self, callback, remove=False):
76 """Register a callback to handle text submission (triggered when the
58 """Register a callback to handle text submission (triggered when the
77 user clicks enter).
59 user clicks enter).
78
60
79 Parameters
61 Parameters
80 callback: Method handle
62 callback: Method handle
81 Function to be called when the text has been submitted. Function
63 Function to be called when the text has been submitted. Function
82 can have two possible signatures:
64 can have two possible signatures:
83 callback()
65 callback()
84 callback(sender)
66 callback(sender)
85 remove: bool (optional)
67 remove: bool (optional)
86 Whether or not to unregister the callback"""
68 Whether or not to unregister the callback"""
87 if remove and callback in self._submission_callbacks:
69 if remove and callback in self._submission_callbacks:
88 self._submission_callbacks.remove(callback)
70 self._submission_callbacks.remove(callback)
89 elif not remove and not callback in self._submission_callbacks:
71 elif not remove and not callback in self._submission_callbacks:
90 self._submission_callbacks.append(callback)
72 self._submission_callbacks.append(callback)
91
73
92
74
93 def _handle_submit(self):
75 def _handle_submit(self):
94 """Handles when a string widget view is submitted."""
76 """Handles when a string widget view is submitted."""
95 for handler in self._submission_callbacks:
77 for handler in self._submission_callbacks:
96 if callable(handler):
78 if callable(handler):
97 argspec = inspect.getargspec(handler)
79 argspec = inspect.getargspec(handler)
98 nargs = len(argspec[0])
80 nargs = len(argspec[0])
99
81
100 # Bound methods have an additional 'self' argument
82 # Bound methods have an additional 'self' argument
101 if isinstance(handler, types.MethodType):
83 if isinstance(handler, types.MethodType):
102 nargs -= 1
84 nargs -= 1
103
85
104 # Call the callback
86 # Call the callback
105 if nargs == 0:
87 if nargs == 0:
106 handler()
88 handler()
107 elif nargs == 1:
89 elif nargs == 1:
108 handler(self)
90 handler(self)
109 else:
91 else:
110 raise TypeError('StringWidget submit callback must ' \
92 raise TypeError('StringWidget submit callback must ' \
111 'accept 0 or 1 arguments.')
93 'accept 0 or 1 arguments.')
General Comments 0
You need to be logged in to leave comments. Login now