Show More
@@ -1,70 +1,71 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | "widgets/js/widget", |
|
6 | 6 | "jquery", |
|
7 | 7 | "bootstrap", |
|
8 | 8 | ], function(widget, $){ |
|
9 | 9 | |
|
10 | 10 | var ButtonView = widget.DOMWidgetView.extend({ |
|
11 | 11 | render : function(){ |
|
12 | 12 | // Called when view is rendered. |
|
13 | 13 | this.setElement($("<button />") |
|
14 | 14 | .addClass('btn btn-default')); |
|
15 | ||
|
15 | this.$el.attr("data-toggle", "tooltip"); | |
|
16 | 16 | this.model.on('change:button_style', function(model, value) { |
|
17 | 17 | this.update_button_style(); |
|
18 | 18 | }, this); |
|
19 | 19 | this.update_button_style(''); |
|
20 | 20 | |
|
21 | 21 | this.update(); // Set defaults. |
|
22 | 22 | }, |
|
23 | 23 | |
|
24 | 24 | update : function(){ |
|
25 | 25 | // Update the contents of this view |
|
26 | 26 | // |
|
27 | 27 | // Called when the model is changed. The model may have been |
|
28 | 28 | // changed by another view or by a state update from the back-end. |
|
29 | 29 | var description = this.model.get('description'); |
|
30 | this.$el.attr("title", this.model.get("tooltip")); | |
|
30 | 31 | if (description.length === 0) { |
|
31 | 32 | this.$el.html(" "); // Preserve button height |
|
32 | 33 | } else { |
|
33 | 34 | this.$el.text(description); |
|
34 | 35 | } |
|
35 | 36 | |
|
36 | 37 | if (this.model.get('disabled')) { |
|
37 | 38 | this.$el.attr('disabled','disabled'); |
|
38 | 39 | } else { |
|
39 | 40 | this.$el.removeAttr('disabled'); |
|
40 | 41 | } |
|
41 | 42 | |
|
42 | 43 | return ButtonView.__super__.update.apply(this); |
|
43 | 44 | }, |
|
44 | 45 | |
|
45 | 46 | update_button_style: function(previous_trait_value) { |
|
46 | 47 | var class_map = { |
|
47 | 48 | primary: ['btn-primary'], |
|
48 | 49 | success: ['btn-success'], |
|
49 | 50 | info: ['btn-info'], |
|
50 | 51 | warning: ['btn-warning'], |
|
51 | 52 | danger: ['btn-danger'] |
|
52 | 53 | }; |
|
53 | 54 | this.update_mapped_classes(class_map, 'button_style', previous_trait_value); |
|
54 | 55 | }, |
|
55 | 56 | |
|
56 | 57 | events: { |
|
57 | 58 | // Dictionary of events and their handlers. |
|
58 | 59 | 'click': '_handle_click', |
|
59 | 60 | }, |
|
60 | 61 | |
|
61 | 62 | _handle_click: function(){ |
|
62 | 63 | // Handles when the button is clicked. |
|
63 | 64 | this.send({event: 'click'}); |
|
64 | 65 | }, |
|
65 | 66 | }); |
|
66 | 67 | |
|
67 | 68 | return { |
|
68 | 69 | 'ButtonView': ButtonView, |
|
69 | 70 | }; |
|
70 | 71 | }); |
@@ -1,70 +1,71 b'' | |||
|
1 | 1 | """Button class. |
|
2 | 2 | |
|
3 | 3 | Represents a button in the frontend using a widget. Allows user to listen for |
|
4 | 4 | click events on the button and trigger backend code when the clicks are fired. |
|
5 | 5 | """ |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (c) 2013, the IPython Development Team. |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the Modified BSD License. |
|
10 | 10 | # |
|
11 | 11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | from .widget import DOMWidget, CallbackDispatcher |
|
18 | 18 | from IPython.utils.traitlets import Unicode, Bool, CaselessStrEnum |
|
19 | 19 | from IPython.utils.warn import DeprecatedClass |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Classes |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | class Button(DOMWidget): |
|
25 | 25 | """Button widget. |
|
26 | 26 | |
|
27 | 27 | This widget has an `on_click` method that allows you to listen for the |
|
28 | 28 | user clicking on the button. The click event itself is stateless.""" |
|
29 | 29 | _view_name = Unicode('ButtonView', sync=True) |
|
30 | 30 | |
|
31 | 31 | # Keys |
|
32 |
description = Unicode('', help=" |
|
|
32 | description = Unicode('', help="Button label.", sync=True) | |
|
33 | tooltip = Unicode(help="Tooltip caption of the button.", sync=True) | |
|
33 | 34 | disabled = Bool(False, help="Enable or disable user changes.", sync=True) |
|
34 | 35 | |
|
35 | 36 | button_style = CaselessStrEnum( |
|
36 | 37 | values=['primary', 'success', 'info', 'warning', 'danger', ''], |
|
37 | 38 | default_value='', allow_none=True, sync=True, help="""Use a |
|
38 | 39 | predefined styling for the button.""") |
|
39 | 40 | |
|
40 | 41 | def __init__(self, **kwargs): |
|
41 | 42 | """Constructor""" |
|
42 | 43 | super(Button, self).__init__(**kwargs) |
|
43 | 44 | self._click_handlers = CallbackDispatcher() |
|
44 | 45 | self.on_msg(self._handle_button_msg) |
|
45 | 46 | |
|
46 | 47 | def on_click(self, callback, remove=False): |
|
47 | 48 | """Register a callback to execute when the button is clicked. |
|
48 | 49 | |
|
49 | 50 | The callback will be called with one argument, |
|
50 | 51 | the clicked button widget instance. |
|
51 | 52 | |
|
52 | 53 | Parameters |
|
53 | 54 | ---------- |
|
54 | 55 | remove : bool (optional) |
|
55 | 56 | Set to true to remove the callback from the list of callbacks.""" |
|
56 | 57 | self._click_handlers.register_callback(callback, remove=remove) |
|
57 | 58 | |
|
58 | 59 | def _handle_button_msg(self, _, content): |
|
59 | 60 | """Handle a msg from the front-end. |
|
60 | 61 | |
|
61 | 62 | Parameters |
|
62 | 63 | ---------- |
|
63 | 64 | content: dict |
|
64 | 65 | Content of the msg.""" |
|
65 | 66 | if content.get('event', '') == 'click': |
|
66 | 67 | self._click_handlers(self) |
|
67 | 68 | |
|
68 | 69 | |
|
69 | 70 | # Remove in IPython 4.0 |
|
70 | 71 | ButtonWidget = DeprecatedClass(Button, 'ButtonWidget') |
General Comments 0
You need to be logged in to leave comments.
Login now