Show More
@@ -1,159 +1,157 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | "widgets/js/widget", |
|
5 | "widgets/js/widget", | |
6 | "jquery", |
|
6 | "jquery", | |
7 | "bootstrap", |
|
7 | "bootstrap", | |
8 | ], function(widget, $){ |
|
8 | ], function(widget, $){ | |
9 |
|
9 | |||
10 | var CheckboxView = widget.DOMWidgetView.extend({ |
|
10 | var CheckboxView = widget.DOMWidgetView.extend({ | |
11 | render : function(){ |
|
11 | render : function(){ | |
12 | /** |
|
12 | /** | |
13 | * Called when view is rendered. |
|
13 | * Called when view is rendered. | |
14 | */ |
|
14 | */ | |
15 | this.$el |
|
15 | this.$el | |
16 | .addClass('widget-hbox widget-checkbox'); |
|
16 | .addClass('widget-hbox widget-checkbox'); | |
17 | this.$label = $('<div />') |
|
17 | this.$label = $('<div />') | |
18 | .addClass('widget-label') |
|
18 | .addClass('widget-label') | |
19 | .appendTo(this.$el) |
|
19 | .appendTo(this.$el) | |
20 | .hide(); |
|
20 | .hide(); | |
21 | this.$checkbox = $('<input />') |
|
21 | this.$checkbox = $('<input />') | |
22 | .attr('type', 'checkbox') |
|
22 | .attr('type', 'checkbox') | |
23 | .appendTo(this.$el) |
|
23 | .appendTo(this.$el) | |
24 | .click($.proxy(this.handle_click, this)); |
|
24 | .click($.proxy(this.handle_click, this)); | |
25 |
|
25 | |||
26 | this.update(); // Set defaults. |
|
26 | this.update(); // Set defaults. | |
27 | }, |
|
27 | }, | |
28 |
|
28 | |||
29 | update_attr: function(name, value) { |
|
29 | update_attr: function(name, value) { | |
30 | /** |
|
30 | /** | |
31 | * Set a css attr of the widget view. |
|
31 | * Set a css attr of the widget view. | |
32 | */ |
|
32 | */ | |
33 | if (name == 'padding' || name == 'margin') { |
|
33 | if (name == 'padding' || name == 'margin') { | |
34 | this.$el.css(name, value); |
|
34 | this.$el.css(name, value); | |
35 | } else { |
|
35 | } else { | |
36 | this.$checkbox.css(name, value); |
|
36 | this.$checkbox.css(name, value); | |
37 | } |
|
37 | } | |
38 | }, |
|
38 | }, | |
39 |
|
39 | |||
40 | handle_click: function() { |
|
40 | handle_click: function() { | |
41 | /** |
|
41 | /** | |
42 | * Handles when the checkbox is clicked. |
|
42 | * Handles when the checkbox is clicked. | |
43 | * |
|
43 | * | |
44 | * Calling model.set will trigger all of the other views of the |
|
44 | * Calling model.set will trigger all of the other views of the | |
45 | * model to update. |
|
45 | * model to update. | |
46 | */ |
|
46 | */ | |
47 | var value = this.model.get('value'); |
|
47 | var value = this.model.get('value'); | |
48 | this.model.set('value', ! value, {updated_view: this}); |
|
48 | this.model.set('value', ! value, {updated_view: this}); | |
49 | this.touch(); |
|
49 | this.touch(); | |
50 | }, |
|
50 | }, | |
51 |
|
51 | |||
52 | update : function(options){ |
|
52 | update : function(options){ | |
53 | /** |
|
53 | /** | |
54 | * Update the contents of this view |
|
54 | * Update the contents of this view | |
55 | * |
|
55 | * | |
56 | * Called when the model is changed. The model may have been |
|
56 | * Called when the model is changed. The model may have been | |
57 | * changed by another view or by a state update from the back-end. |
|
57 | * changed by another view or by a state update from the back-end. | |
58 | */ |
|
58 | */ | |
59 | this.$checkbox.prop('checked', this.model.get('value')); |
|
59 | this.$checkbox.prop('checked', this.model.get('value')); | |
60 |
|
60 | |||
61 | if (options === undefined || options.updated_view != this) { |
|
61 | if (options === undefined || options.updated_view != this) { | |
62 |
|
|
62 | this.$checkbox.prop("disabled", this.model.get("disabled")); | |
63 | this.$checkbox.prop('disabled', disabled); |
|
|||
64 |
|
63 | |||
65 |
var description = this.model.get( |
|
64 | var description = this.model.get("description"); | |
66 | if (description.trim().length === 0) { |
|
65 | if (description.trim().length === 0) { | |
67 | this.$label.hide(); |
|
66 | this.$label.hide(); | |
68 | } else { |
|
67 | } else { | |
69 | this.typeset(this.$label, description); |
|
68 | this.typeset(this.$label, description); | |
70 | this.$label.show(); |
|
69 | this.$label.show(); | |
71 | } |
|
70 | } | |
72 | } |
|
71 | } | |
73 | return CheckboxView.__super__.update.apply(this); |
|
72 | return CheckboxView.__super__.update.apply(this); | |
74 | }, |
|
73 | }, | |
75 |
|
74 | |||
76 | }); |
|
75 | }); | |
77 |
|
76 | |||
78 |
|
77 | |||
79 | var ToggleButtonView = widget.DOMWidgetView.extend({ |
|
78 | var ToggleButtonView = widget.DOMWidgetView.extend({ | |
80 | render : function() { |
|
79 | render : function() { | |
81 | /** |
|
80 | /** | |
82 | * Called when view is rendered. |
|
81 | * Called when view is rendered. | |
83 | */ |
|
82 | */ | |
84 | var that = this; |
|
83 | var that = this; | |
85 | this.setElement($('<button />') |
|
84 | this.setElement($('<button />') | |
86 | .addClass('btn btn-default') |
|
85 | .addClass('btn btn-default') | |
87 | .attr('type', 'button') |
|
86 | .attr('type', 'button') | |
88 | .on('click', function (e) { |
|
87 | .on('click', function (e) { | |
89 | e.preventDefault(); |
|
88 | e.preventDefault(); | |
90 | that.handle_click(); |
|
89 | that.handle_click(); | |
91 | })); |
|
90 | })); | |
92 | this.$el.attr("data-toggle", "tooltip"); |
|
91 | this.$el.attr("data-toggle", "tooltip"); | |
93 | this.model.on('change:button_style', function(model, value) { |
|
92 | this.model.on('change:button_style', function(model, value) { | |
94 | this.update_button_style(); |
|
93 | this.update_button_style(); | |
95 | }, this); |
|
94 | }, this); | |
96 | this.update_button_style(''); |
|
95 | this.update_button_style(''); | |
97 |
|
96 | |||
98 | this.update(); // Set defaults. |
|
97 | this.update(); // Set defaults. | |
99 | }, |
|
98 | }, | |
100 |
|
99 | |||
101 | update_button_style: function(previous_trait_value) { |
|
100 | update_button_style: function(previous_trait_value) { | |
102 | var class_map = { |
|
101 | var class_map = { | |
103 | primary: ['btn-primary'], |
|
102 | primary: ['btn-primary'], | |
104 | success: ['btn-success'], |
|
103 | success: ['btn-success'], | |
105 | info: ['btn-info'], |
|
104 | info: ['btn-info'], | |
106 | warning: ['btn-warning'], |
|
105 | warning: ['btn-warning'], | |
107 | danger: ['btn-danger'] |
|
106 | danger: ['btn-danger'] | |
108 | }; |
|
107 | }; | |
109 | this.update_mapped_classes(class_map, 'button_style', previous_trait_value); |
|
108 | this.update_mapped_classes(class_map, 'button_style', previous_trait_value); | |
110 | }, |
|
109 | }, | |
111 |
|
110 | |||
112 | update : function(options){ |
|
111 | update : function(options){ | |
113 | /** |
|
112 | /** | |
114 | * Update the contents of this view |
|
113 | * Update the contents of this view | |
115 | * |
|
114 | * | |
116 |
* Called when the model is changed. |
|
115 | * Called when the model is changed. The model may have been | |
117 | * changed by another view or by a state update from the back-end. |
|
116 | * changed by another view or by a state update from the back-end. | |
118 | */ |
|
117 | */ | |
119 | if (this.model.get('value')) { |
|
118 | if (this.model.get('value')) { | |
120 | this.$el.addClass('active'); |
|
119 | this.$el.addClass('active'); | |
121 | } else { |
|
120 | } else { | |
122 | this.$el.removeClass('active'); |
|
121 | this.$el.removeClass('active'); | |
123 | } |
|
122 | } | |
124 |
|
123 | |||
125 | if (options === undefined || options.updated_view != this) { |
|
124 | if (options === undefined || options.updated_view != this) { | |
126 |
|
125 | this.$el.prop("disabled", this.model.get("disabled")); | ||
127 | var disabled = this.model.get('disabled'); |
|
|||
128 | this.$el.prop('disabled', disabled); |
|
|||
129 |
|
||||
130 | var description = this.model.get('description'); |
|
|||
131 | this.$el.attr("title", this.model.get("tooltip")); |
|
126 | this.$el.attr("title", this.model.get("tooltip")); | |
132 | this.$el.text(description); |
|
127 | ||
|
128 | var description = this.model.get("description"); | |||
133 | var icon = this.model.get("icon"); |
|
129 | 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) { |
|
130 | if (description.trim().length === 0 && icon.trim().length ===0) { | |
136 | this.$el.html(" "); // Preserve button height |
|
131 | this.$el.html(" "); // Preserve button height | |
|
132 | } else { | |||
|
133 | this.$el.text(description); | |||
|
134 | $('<i class="fa"></i>').prependTo(this.$el).addClass(icon); | |||
137 | } |
|
135 | } | |
138 | } |
|
136 | } | |
139 | return ToggleButtonView.__super__.update.apply(this); |
|
137 | return ToggleButtonView.__super__.update.apply(this); | |
140 | }, |
|
138 | }, | |
141 |
|
139 | |||
142 | handle_click: function(e) { |
|
140 | handle_click: function(e) { | |
143 | /** |
|
141 | /** | |
144 | * Handles and validates user input. |
|
142 | * Handles and validates user input. | |
145 | * |
|
143 | * | |
146 | * Calling model.set will trigger all of the other views of the |
|
144 | * Calling model.set will trigger all of the other views of the | |
147 | * model to update. |
|
145 | * model to update. | |
148 | */ |
|
146 | */ | |
149 | var value = this.model.get('value'); |
|
147 | var value = this.model.get('value'); | |
150 | this.model.set('value', ! value, {updated_view: this}); |
|
148 | this.model.set('value', ! value, {updated_view: this}); | |
151 | this.touch(); |
|
149 | this.touch(); | |
152 | }, |
|
150 | }, | |
153 | }); |
|
151 | }); | |
154 |
|
152 | |||
155 | return { |
|
153 | return { | |
156 | 'CheckboxView': CheckboxView, |
|
154 | 'CheckboxView': CheckboxView, | |
157 | 'ToggleButtonView': ToggleButtonView, |
|
155 | 'ToggleButtonView': ToggleButtonView, | |
158 | }; |
|
156 | }; | |
159 | }); |
|
157 | }); |
@@ -1,77 +1,75 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | "widgets/js/widget", |
|
5 | "widgets/js/widget", | |
6 | "jquery", |
|
6 | "jquery", | |
7 | "bootstrap", |
|
7 | "bootstrap", | |
8 | ], function(widget, $){ |
|
8 | ], function(widget, $){ | |
9 |
|
9 | |||
10 | var ButtonView = widget.DOMWidgetView.extend({ |
|
10 | var ButtonView = widget.DOMWidgetView.extend({ | |
11 | render : function(){ |
|
11 | render : function(){ | |
12 | /** |
|
12 | /** | |
13 | * Called when view is rendered. |
|
13 | * Called when view is rendered. | |
14 | */ |
|
14 | */ | |
15 | this.setElement($("<button />") |
|
15 | this.setElement($("<button />") | |
16 | .addClass('btn btn-default')); |
|
16 | .addClass('btn btn-default')); | |
17 | this.$el.attr("data-toggle", "tooltip"); |
|
17 | this.$el.attr("data-toggle", "tooltip"); | |
18 | this.model.on('change:button_style', function(model, value) { |
|
18 | this.model.on('change:button_style', function(model, value) { | |
19 | this.update_button_style(); |
|
19 | this.update_button_style(); | |
20 | }, this); |
|
20 | }, this); | |
21 | this.update_button_style(''); |
|
21 | this.update_button_style(''); | |
22 |
|
22 | |||
23 | this.update(); // Set defaults. |
|
23 | this.update(); // Set defaults. | |
24 | }, |
|
24 | }, | |
25 |
|
25 | |||
26 | update : function(){ |
|
26 | update : function(){ | |
27 | /** |
|
27 | /** | |
28 | * Update the contents of this view |
|
28 | * Update the contents of this view | |
29 | * |
|
29 | * | |
30 |
* Called when the model is changed. |
|
30 | * Called when the model is changed. The model may have been | |
31 | * changed by another view or by a state update from the back-end. |
|
31 | * changed by another view or by a state update from the back-end. | |
32 | */ |
|
32 | */ | |
33 | var description = this.model.get('description'); |
|
33 | this.$el.prop("disabled", this.model.get("disabled")); | |
34 | this.$el.attr("title", this.model.get("tooltip")); |
|
34 | this.$el.attr("title", this.model.get("tooltip")); | |
35 | this.$el.text(description); |
|
35 | ||
|
36 | var description = this.model.get("description"); | |||
36 | var icon = this.model.get("icon"); |
|
37 | 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) { |
|
38 | if (description.trim().length === 0 && icon.trim().length ===0) { | |
39 | this.$el.html(" "); // Preserve button height |
|
39 | this.$el.html(" "); // Preserve button height | |
40 | } |
|
|||
41 | if (this.model.get('disabled')) { |
|
|||
42 | this.$el.attr('disabled','disabled'); |
|
|||
43 | } else { |
|
40 | } else { | |
44 |
this.$el. |
|
41 | this.$el.text(description); | |
|
42 | $('<i class="fa"></i>').prependTo(this.$el).addClass(icon); | |||
45 | } |
|
43 | } | |
46 |
|
44 | |||
47 | return ButtonView.__super__.update.apply(this); |
|
45 | return ButtonView.__super__.update.apply(this); | |
48 | }, |
|
46 | }, | |
49 |
|
47 | |||
50 | update_button_style: function(previous_trait_value) { |
|
48 | update_button_style: function(previous_trait_value) { | |
51 | var class_map = { |
|
49 | var class_map = { | |
52 | primary: ['btn-primary'], |
|
50 | primary: ['btn-primary'], | |
53 | success: ['btn-success'], |
|
51 | success: ['btn-success'], | |
54 | info: ['btn-info'], |
|
52 | info: ['btn-info'], | |
55 | warning: ['btn-warning'], |
|
53 | warning: ['btn-warning'], | |
56 | danger: ['btn-danger'] |
|
54 | danger: ['btn-danger'] | |
57 | }; |
|
55 | }; | |
58 | this.update_mapped_classes(class_map, 'button_style', previous_trait_value); |
|
56 | this.update_mapped_classes(class_map, 'button_style', previous_trait_value); | |
59 | }, |
|
57 | }, | |
60 |
|
58 | |||
61 | events: { |
|
59 | events: { | |
62 | // Dictionary of events and their handlers. |
|
60 | // Dictionary of events and their handlers. | |
63 | 'click': '_handle_click', |
|
61 | 'click': '_handle_click', | |
64 | }, |
|
62 | }, | |
65 |
|
63 | |||
66 | _handle_click: function(){ |
|
64 | _handle_click: function(){ | |
67 | /** |
|
65 | /** | |
68 | * Handles when the button is clicked. |
|
66 | * Handles when the button is clicked. | |
69 | */ |
|
67 | */ | |
70 | this.send({event: 'click'}); |
|
68 | this.send({event: 'click'}); | |
71 | }, |
|
69 | }, | |
72 | }); |
|
70 | }); | |
73 |
|
71 | |||
74 | return { |
|
72 | return { | |
75 | 'ButtonView': ButtonView, |
|
73 | 'ButtonView': ButtonView, | |
76 | }; |
|
74 | }; | |
77 | }); |
|
75 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now