##// END OF EJS Templates
Fixed button on_click handler so it's initiated on instanciation
Jonathan Frederic -
Show More
@@ -1,72 +1,75 b''
1 """ButtonWidget class.
1 """ButtonWidget class.
2
2
3 Represents a button in the frontend using a widget. Allows user to listen for
3 Represents a button in the frontend using a widget. Allows user to listen for
4 click events on the button and trigger backend code when the clicks are fired.
4 click events on the button and trigger backend code when the clicks are fired.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
7 # Copyright (c) 2013, the IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 import inspect
17 import inspect
18 import types
18 import types
19
19
20 from .widget import Widget
20 from .widget import Widget
21 from IPython.utils.traitlets import Unicode, Bool, Int
21 from IPython.utils.traitlets import Unicode, Bool, Int
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Classes
24 # Classes
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 class ButtonWidget(Widget):
26 class ButtonWidget(Widget):
27 target_name = Unicode('ButtonWidgetModel')
27 target_name = Unicode('ButtonWidgetModel')
28 default_view_name = Unicode('ButtonView')
28 default_view_name = Unicode('ButtonView')
29
29
30 # Keys
30 # Keys
31 _keys = ['clicks', 'description', 'disabled']
31 _keys = ['clicks', 'description', 'disabled']
32 clicks = Int(0, help="Number of times the button has been clicked.")
32 clicks = Int(0, help="Number of times the button has been clicked.")
33 description = Unicode('', help="Description of the button (label).")
33 description = Unicode('', help="Description of the button (label).")
34 disabled = Bool(False, help="Enable or disable user changes.")
34 disabled = Bool(False, help="Enable or disable user changes.")
35
35
36 _click_handlers = []
36
37 def __init__(self, **kwargs):
38 self._click_handlers = []
39 super(ButtonWidget, self).__init__(**kwargs)
37
40
38
41
39 def on_click(self, callback, remove=False):
42 def on_click(self, callback, remove=False):
40 """Register a callback to execute when the button is clicked.
43 """Register a callback to execute when the button is clicked.
41
44
42 Parameters
45 Parameters
43 ----------
46 ----------
44 remove : bool (optional)
47 remove : bool (optional)
45 Set to tru to remove the callback from the list of callbacks."""
48 Set to tru to remove the callback from the list of callbacks."""
46 if remove:
49 if remove:
47 self._click_handlers.remove(callback)
50 self._click_handlers.remove(callback)
48 else:
51 else:
49 self._click_handlers.append(callback)
52 self._click_handlers.append(callback)
50
53
51
54
52 def _clicks_changed(self, name, old, new):
55 def _clicks_changed(self, name, old, new):
53 """Handles when the clicks property has been changed. Fires on_click
56 """Handles when the clicks property has been changed. Fires on_click
54 callbacks when appropriate."""
57 callbacks when appropriate."""
55 if new > old:
58 if new > old:
56 for handler in self._click_handlers:
59 for handler in self._click_handlers:
57 if callable(handler):
60 if callable(handler):
58 argspec = inspect.getargspec(handler)
61 argspec = inspect.getargspec(handler)
59 nargs = len(argspec[0])
62 nargs = len(argspec[0])
60
63
61 # Bound methods have an additional 'self' argument
64 # Bound methods have an additional 'self' argument
62 if isinstance(handler, types.MethodType):
65 if isinstance(handler, types.MethodType):
63 nargs -= 1
66 nargs -= 1
64
67
65 # Call the callback
68 # Call the callback
66 if nargs == 0:
69 if nargs == 0:
67 handler()
70 handler()
68 elif nargs == 1:
71 elif nargs == 1:
69 handler(self)
72 handler(self)
70 else:
73 else:
71 raise TypeError('ButtonWidget click callback must ' \
74 raise TypeError('ButtonWidget click callback must ' \
72 'accept 0 or 1 arguments.')
75 'accept 0 or 1 arguments.')
General Comments 0
You need to be logged in to leave comments. Login now