##// END OF EJS Templates
font awesome icon
Sylvain Corlay -
Show More
@@ -1,158 +1,159 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 CheckboxView = widget.DOMWidgetView.extend({
11 11 render : function(){
12 12 /**
13 13 * Called when view is rendered.
14 14 */
15 15 this.$el
16 16 .addClass('widget-hbox widget-checkbox');
17 17 this.$label = $('<div />')
18 18 .addClass('widget-label')
19 19 .appendTo(this.$el)
20 20 .hide();
21 21 this.$checkbox = $('<input />')
22 22 .attr('type', 'checkbox')
23 23 .appendTo(this.$el)
24 24 .click($.proxy(this.handle_click, this));
25 25
26 26 this.update(); // Set defaults.
27 27 },
28 28
29 29 update_attr: function(name, value) {
30 30 /**
31 31 * Set a css attr of the widget view.
32 32 */
33 33 if (name == 'padding' || name == 'margin') {
34 34 this.$el.css(name, value);
35 35 } else {
36 36 this.$checkbox.css(name, value);
37 37 }
38 38 },
39 39
40 40 handle_click: function() {
41 41 /**
42 42 * Handles when the checkbox is clicked.
43 43 *
44 44 * Calling model.set will trigger all of the other views of the
45 45 * model to update.
46 46 */
47 47 var value = this.model.get('value');
48 48 this.model.set('value', ! value, {updated_view: this});
49 49 this.touch();
50 50 },
51 51
52 52 update : function(options){
53 53 /**
54 54 * Update the contents of this view
55 55 *
56 56 * Called when the model is changed. The model may have been
57 57 * changed by another view or by a state update from the back-end.
58 58 */
59 59 this.$checkbox.prop('checked', this.model.get('value'));
60 60
61 61 if (options === undefined || options.updated_view != this) {
62 62 var disabled = this.model.get('disabled');
63 63 this.$checkbox.prop('disabled', disabled);
64 64
65 65 var description = this.model.get('description');
66 66 if (description.trim().length === 0) {
67 67 this.$label.hide();
68 68 } else {
69 69 this.typeset(this.$label, description);
70 70 this.$label.show();
71 71 }
72 72 }
73 73 return CheckboxView.__super__.update.apply(this);
74 74 },
75 75
76 76 });
77 77
78 78
79 79 var ToggleButtonView = widget.DOMWidgetView.extend({
80 80 render : function() {
81 81 /**
82 82 * Called when view is rendered.
83 83 */
84 84 var that = this;
85 85 this.setElement($('<button />')
86 86 .addClass('btn btn-default')
87 87 .attr('type', 'button')
88 88 .on('click', function (e) {
89 89 e.preventDefault();
90 90 that.handle_click();
91 91 }));
92 92 this.$el.attr("data-toggle", "tooltip");
93 93 this.model.on('change:button_style', function(model, value) {
94 94 this.update_button_style();
95 95 }, this);
96 96 this.update_button_style('');
97 97
98 98 this.update(); // Set defaults.
99 99 },
100 100
101 101 update_button_style: function(previous_trait_value) {
102 102 var class_map = {
103 103 primary: ['btn-primary'],
104 104 success: ['btn-success'],
105 105 info: ['btn-info'],
106 106 warning: ['btn-warning'],
107 107 danger: ['btn-danger']
108 108 };
109 109 this.update_mapped_classes(class_map, 'button_style', previous_trait_value);
110 110 },
111 111
112 112 update : function(options){
113 113 /**
114 114 * Update the contents of this view
115 115 *
116 116 * Called when the model is changed. The model may have been
117 117 * changed by another view or by a state update from the back-end.
118 118 */
119 119 if (this.model.get('value')) {
120 120 this.$el.addClass('active');
121 121 } else {
122 122 this.$el.removeClass('active');
123 123 }
124 124
125 125 if (options === undefined || options.updated_view != this) {
126 126
127 127 var disabled = this.model.get('disabled');
128 128 this.$el.prop('disabled', disabled);
129 129
130 130 var description = this.model.get('description');
131 131 this.$el.attr("title", this.model.get("tooltip"));
132 if (description.trim().length === 0) {
132 this.$el.text(description);
133 var icon = this.model.get("icon");
134 $('<i class="fa"></i>').prependTo(this.$el).addClass(icon);
135 if (description.trim().length === 0 && icon.trim().length ===0) {
133 136 this.$el.html("&nbsp;"); // Preserve button height
134 } else {
135 this.$el.text(description);
136 137 }
137 138 }
138 139 return ToggleButtonView.__super__.update.apply(this);
139 140 },
140 141
141 142 handle_click: function(e) {
142 143 /**
143 144 * Handles and validates user input.
144 145 *
145 146 * Calling model.set will trigger all of the other views of the
146 147 * model to update.
147 148 */
148 149 var value = this.model.get('value');
149 150 this.model.set('value', ! value, {updated_view: this});
150 151 this.touch();
151 152 },
152 153 });
153 154
154 155 return {
155 156 'CheckboxView': CheckboxView,
156 157 'ToggleButtonView': ToggleButtonView,
157 158 };
158 159 });
@@ -1,77 +1,77 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 /**
13 13 * Called when view is rendered.
14 14 */
15 15 this.setElement($("<button />")
16 16 .addClass('btn btn-default'));
17 17 this.$el.attr("data-toggle", "tooltip");
18 18 this.model.on('change:button_style', function(model, value) {
19 19 this.update_button_style();
20 20 }, this);
21 21 this.update_button_style('');
22 22
23 23 this.update(); // Set defaults.
24 24 },
25 25
26 26 update : function(){
27 27 /**
28 28 * Update the contents of this view
29 29 *
30 30 * Called when the model is changed. The model may have been
31 31 * changed by another view or by a state update from the back-end.
32 32 */
33 33 var description = this.model.get('description');
34 34 this.$el.attr("title", this.model.get("tooltip"));
35 if (description.length === 0) {
35 this.$el.text(description);
36 var icon = this.model.get("icon");
37 $('<i class="fa"></i>').prependTo(this.$el).addClass(icon);
38 if (description.trim().length === 0 && icon.trim().length ===0) {
36 39 this.$el.html("&nbsp;"); // Preserve button height
37 } else {
38 this.$el.text(description);
39 40 }
40
41 41 if (this.model.get('disabled')) {
42 42 this.$el.attr('disabled','disabled');
43 43 } else {
44 44 this.$el.removeAttr('disabled');
45 45 }
46 46
47 47 return ButtonView.__super__.update.apply(this);
48 48 },
49 49
50 50 update_button_style: function(previous_trait_value) {
51 51 var class_map = {
52 52 primary: ['btn-primary'],
53 53 success: ['btn-success'],
54 54 info: ['btn-info'],
55 55 warning: ['btn-warning'],
56 56 danger: ['btn-danger']
57 57 };
58 58 this.update_mapped_classes(class_map, 'button_style', previous_trait_value);
59 59 },
60 60
61 61 events: {
62 62 // Dictionary of events and their handlers.
63 63 'click': '_handle_click',
64 64 },
65 65
66 66 _handle_click: function(){
67 67 /**
68 68 * Handles when the button is clicked.
69 69 */
70 70 this.send({event: 'click'});
71 71 },
72 72 });
73 73
74 74 return {
75 75 'ButtonView': ButtonView,
76 76 };
77 77 });
@@ -1,92 +1,92 b''
1 1 // Test widget bool class
2 2 casper.notebook_test(function () {
3 3 "use strict";
4 4
5 5 // Create a checkbox and togglebutton.
6 6 var bool_index = this.append_cell(
7 7 'from IPython.html import widgets\n' +
8 8 'from IPython.display import display, clear_output\n' +
9 9 'bool_widgets = [widgets.Checkbox(description="Title", value=True),\n' +
10 10 ' widgets.ToggleButton(description="Title", value=True)]\n' +
11 11 'display(bool_widgets[0])\n' +
12 12 'display(bool_widgets[1])\n' +
13 13 'print("Success")');
14 14 this.execute_cell_then(bool_index, function(index){
15 15 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
16 16 'Create bool widget cell executed with correct output.');
17 17 });
18 18
19 19 // Wait for the widgets to actually display.
20 20 var widget_checkbox_selector = '.widget-area .widget-subarea .widget-hbox input';
21 21 var widget_togglebutton_selector = '.widget-area .widget-subarea button';
22 22 this.wait_for_element(bool_index, widget_checkbox_selector);
23 23 this.wait_for_element(bool_index, widget_togglebutton_selector);
24 24
25 25 // Continue the tests.
26 26 this.then(function() {
27 27 this.test.assert(this.cell_element_exists(bool_index,
28 28 '.widget-area .widget-subarea'),
29 29 'Widget subarea exists.');
30 30
31 31 this.test.assert(this.cell_element_exists(bool_index,
32 32 widget_checkbox_selector),
33 33 'Checkbox exists.');
34 34
35 35 this.test.assert(this.cell_element_function(bool_index,
36 36 widget_checkbox_selector, 'prop', ['checked']),
37 37 'Checkbox is checked.');
38 38
39 39 this.test.assert(this.cell_element_exists(bool_index,
40 40 '.widget-area .widget-subarea .widget-hbox .widget-label'),
41 41 'Checkbox label exists.');
42 42
43 43 this.test.assert(this.cell_element_function(bool_index,
44 44 '.widget-area .widget-subarea .widget-hbox .widget-label', 'html')=="Title",
45 45 'Checkbox labeled correctly.');
46 46
47 47 this.test.assert(this.cell_element_exists(bool_index,
48 48 widget_togglebutton_selector),
49 49 'Toggle button exists.');
50 50
51 51 this.test.assert(this.cell_element_function(bool_index,
52 widget_togglebutton_selector, 'html')=="Title",
52 widget_togglebutton_selector, 'html')=='<i class="fa"></i>Title',
53 53 'Toggle button labeled correctly.');
54 54
55 55 this.test.assert(this.cell_element_function(bool_index,
56 56 widget_togglebutton_selector, 'hasClass', ['active']),
57 57 'Toggle button is toggled.');
58 58 });
59 59
60 60 // Try changing the state of the widgets programatically.
61 61 var index = this.append_cell(
62 62 'bool_widgets[0].value = False\n' +
63 63 'bool_widgets[1].value = False\n' +
64 64 'print("Success")');
65 65 this.execute_cell_then(index, function(index){
66 66 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
67 67 'Change bool widget value cell executed with correct output.');
68 68
69 69 this.test.assert(! this.cell_element_function(bool_index,
70 70 widget_checkbox_selector, 'prop', ['checked']),
71 71 'Checkbox is not checked. (1)');
72 72
73 73 this.test.assert(! this.cell_element_function(bool_index,
74 74 widget_togglebutton_selector, 'hasClass', ['active']),
75 75 'Toggle button is not toggled. (1)');
76 76
77 77 // Try toggling the bool by clicking on the checkbox.
78 78 this.cell_element_function(bool_index, widget_checkbox_selector, 'click');
79 79
80 80 this.test.assert(this.cell_element_function(bool_index,
81 81 widget_checkbox_selector, 'prop', ['checked']),
82 82 'Checkbox is checked. (2)');
83 83
84 84 // Try toggling the bool by clicking on the toggle button.
85 85 this.cell_element_function(bool_index, widget_togglebutton_selector, 'click');
86 86
87 87 this.test.assert(this.cell_element_function(bool_index,
88 88 widget_togglebutton_selector, 'hasClass', ['active']),
89 89 'Toggle button is toggled. (3)');
90 90
91 91 });
92 92 });
@@ -1,48 +1,48 b''
1 1 // Test widget button class
2 2 casper.notebook_test(function () {
3 3 var button_index = this.append_cell(
4 4 'from IPython.html import widgets\n' +
5 5 'from IPython.display import display, clear_output\n' +
6 6 'button = widgets.Button(description="Title")\n' +
7 7 'display(button)\n' +
8 8 'print("Success")\n' +
9 9 'def handle_click(sender):\n' +
10 10 ' display("Clicked")\n' +
11 11 'button.on_click(handle_click)');
12 12 this.execute_cell_then(button_index, function(index){
13 13 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
14 14 'Create button cell executed with correct output.');
15 15 });
16 16
17 17 // Wait for the widgets to actually display.
18 18 var widget_button_selector = '.widget-area .widget-subarea button';
19 19 this.wait_for_element(button_index, widget_button_selector);
20 20
21 21 // Continue with the tests.
22 22 this.then(function() {
23 23 this.test.assert(this.cell_element_exists(button_index,
24 24 '.widget-area .widget-subarea'),
25 25 'Widget subarea exists.');
26 26
27 27 this.test.assert(this.cell_element_exists(button_index,
28 28 widget_button_selector),
29 29 'Widget button exists.');
30 30
31 31 this.test.assert(this.cell_element_function(button_index,
32 widget_button_selector, 'html')=='Title',
32 widget_button_selector, 'html')=='<i class="fa"></i>Title',
33 33 'Set button description.');
34 34
35 35 this.cell_element_function(button_index,
36 36 widget_button_selector, 'click');
37 37 });
38 38
39 39 this.wait_for_output(button_index, 1);
40 40
41 41 this.then(function () {
42 42 var warning_text = this.get_output_cell(button_index, 1).text;
43 43 this.test.assertNotEquals(warning_text.indexOf('Warning'), -1,
44 44 'Importing widgets show a warning');
45 45 this.test.assertEquals(this.get_output_cell(button_index, 2).data['text/plain'], "'Clicked'",
46 46 'Button click event fires.');
47 47 });
48 48 });
@@ -1,71 +1,76 b''
1 1 """Bool class.
2 2
3 3 Represents a boolean using a widget.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from .widget import DOMWidget, register
17 17 from IPython.utils.traitlets import Unicode, Bool, CaselessStrEnum
18 18 from IPython.utils.warn import DeprecatedClass
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Classes
22 22 #-----------------------------------------------------------------------------
23 23 class _Bool(DOMWidget):
24 24 """A base class for creating widgets that represent booleans."""
25 25 value = Bool(False, help="Bool value", sync=True)
26 26 description = Unicode('', help="Description of the boolean (label).", sync=True)
27 27 disabled = Bool(False, help="Enable or disable user changes.", sync=True)
28 28
29 29 def __init__(self, value=None, **kwargs):
30 30 if value is not None:
31 31 kwargs['value'] = value
32 32 super(_Bool, self).__init__(**kwargs)
33 33
34 34 @register('IPython.Checkbox')
35 35 class Checkbox(_Bool):
36 36 """Displays a boolean `value` in the form of a checkbox.
37 37
38 38 Parameters
39 39 ----------
40 40 value : {True,False}
41 41 value of the checkbox: True-checked, False-unchecked
42 42 description : str
43 43 description displayed next to the checkbox
44 44 """
45 45 _view_name = Unicode('CheckboxView', sync=True)
46 46
47 47
48 48 @register('IPython.ToggleButton')
49 49 class ToggleButton(_Bool):
50 50 """Displays a boolean `value` in the form of a toggle button.
51 51
52 52 Parameters
53 53 ----------
54 54 value : {True,False}
55 55 value of the toggle button: True-pressed, False-unpressed
56 56 description : str
57 57 description displayed next to the button
58 tooltip: str
59 tooltip caption of the toggle button
60 icon: str
61 font-awesome icon name
58 62 """
59 63
60 64 _view_name = Unicode('ToggleButtonView', sync=True)
61 65 tooltip = Unicode(help="Tooltip caption of the toggle button.", sync=True)
66 icon = Unicode('', help= "Font-awesome icon.", sync=True)
62 67
63 68 button_style = CaselessStrEnum(
64 69 values=['primary', 'success', 'info', 'warning', 'danger', ''],
65 70 default_value='', allow_none=True, sync=True, help="""Use a
66 71 predefined styling for the button.""")
67 72
68 73
69 74 # Remove in IPython 4.0
70 75 CheckboxWidget = DeprecatedClass(Checkbox, 'CheckboxWidget')
71 76 ToggleButtonWidget = DeprecatedClass(ToggleButton, 'ToggleButtonWidget')
@@ -1,72 +1,82 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, register
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 @register('IPython.Button')
25 25 class Button(DOMWidget):
26 26 """Button widget.
27 This widget has an `on_click` method that allows you to listen for the
28 user clicking on the button. The click event itself is stateless.
27 29
28 This widget has an `on_click` method that allows you to listen for the
29 user clicking on the button. The click event itself is stateless."""
30 Parameters
31 ----------
32 description : str
33 description displayed next to the button
34 tooltip: str
35 tooltip caption of the toggle button
36 icon: str
37 font-awesome icon name
38 """
30 39 _view_name = Unicode('ButtonView', sync=True)
31 40
32 41 # Keys
33 42 description = Unicode('', help="Button label.", sync=True)
34 43 tooltip = Unicode(help="Tooltip caption of the button.", sync=True)
35 44 disabled = Bool(False, help="Enable or disable user changes.", sync=True)
45 icon = Unicode('', help= "Font-awesome icon.", sync=True)
36 46
37 47 button_style = CaselessStrEnum(
38 48 values=['primary', 'success', 'info', 'warning', 'danger', ''],
39 49 default_value='', allow_none=True, sync=True, help="""Use a
40 50 predefined styling for the button.""")
41 51
42 52 def __init__(self, **kwargs):
43 53 """Constructor"""
44 54 super(Button, self).__init__(**kwargs)
45 55 self._click_handlers = CallbackDispatcher()
46 56 self.on_msg(self._handle_button_msg)
47 57
48 58 def on_click(self, callback, remove=False):
49 59 """Register a callback to execute when the button is clicked.
50 60
51 61 The callback will be called with one argument,
52 62 the clicked button widget instance.
53 63
54 64 Parameters
55 65 ----------
56 66 remove : bool (optional)
57 67 Set to true to remove the callback from the list of callbacks."""
58 68 self._click_handlers.register_callback(callback, remove=remove)
59 69
60 70 def _handle_button_msg(self, _, content):
61 71 """Handle a msg from the front-end.
62 72
63 73 Parameters
64 74 ----------
65 75 content: dict
66 76 Content of the msg."""
67 77 if content.get('event', '') == 'click':
68 78 self._click_handlers(self)
69 79
70 80
71 81 # Remove in IPython 4.0
72 82 ButtonWidget = DeprecatedClass(Button, 'ButtonWidget')
General Comments 0
You need to be logged in to leave comments. Login now