Show More
@@ -1,24 +1,24 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // Basic Widgets |
|
9 | // Basic Widgets | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | define([ |
|
12 | define([ | |
13 | "notebook/js/widgets/bool", |
|
13 | "notebook/js/widgets/widget_bool", | |
14 | "notebook/js/widgets/button", |
|
14 | "notebook/js/widgets/widget_button", | |
15 | "notebook/js/widgets/container", |
|
15 | "notebook/js/widgets/widget_container", | |
16 | "notebook/js/widgets/float", |
|
16 | "notebook/js/widgets/widget_float", | |
17 | "notebook/js/widgets/float_range", |
|
17 | "notebook/js/widgets/widget_float_range", | |
18 | "notebook/js/widgets/image", |
|
18 | "notebook/js/widgets/widget_image", | |
19 | "notebook/js/widgets/int", |
|
19 | "notebook/js/widgets/widget_int", | |
20 | "notebook/js/widgets/int_range", |
|
20 | "notebook/js/widgets/widget_int_range", | |
21 | "notebook/js/widgets/multicontainer", |
|
21 | "notebook/js/widgets/widget_multicontainer", | |
22 | "notebook/js/widgets/selection", |
|
22 | "notebook/js/widgets/widget_selection", | |
23 | "notebook/js/widgets/string", |
|
23 | "notebook/js/widgets/widget_string", | |
24 | ], function(){ return true; }); |
|
24 | ], function(){ return true; }); |
@@ -1,123 +1,123 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // BoolWidget |
|
9 | // BoolWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 |
|
18 | |||
19 | var BoolWidgetModel = IPython.WidgetModel.extend({}); |
|
19 | var BoolWidgetModel = IPython.WidgetModel.extend({}); | |
20 | widget_manager.register_widget_model('BoolWidgetModel', BoolWidgetModel); |
|
20 | widget_manager.register_widget_model('BoolWidgetModel', BoolWidgetModel); | |
21 |
|
21 | |||
22 | var CheckboxView = IPython.WidgetView.extend({ |
|
22 | var CheckboxView = IPython.WidgetView.extend({ | |
23 |
|
23 | |||
24 | // Called when view is rendered. |
|
24 | // Called when view is rendered. | |
25 | render : function(){ |
|
25 | render : function(){ | |
26 | this.$el |
|
26 | this.$el | |
27 | .addClass('widget-hbox-single'); |
|
27 | .addClass('widget-hbox-single'); | |
28 | this.$label = $('<div />') |
|
28 | this.$label = $('<div />') | |
29 | .addClass('widget-hlabel') |
|
29 | .addClass('widget-hlabel') | |
30 | .appendTo(this.$el) |
|
30 | .appendTo(this.$el) | |
31 | .hide(); |
|
31 | .hide(); | |
32 | var that = this; |
|
32 | var that = this; | |
33 | this.$checkbox = $('<input />') |
|
33 | this.$checkbox = $('<input />') | |
34 | .attr('type', 'checkbox') |
|
34 | .attr('type', 'checkbox') | |
35 | .click(function(el) { |
|
35 | .click(function(el) { | |
36 | that.user_invoked_update = true; |
|
36 | that.user_invoked_update = true; | |
37 | that.model.set('value', that.$checkbox.prop('checked')); |
|
37 | that.model.set('value', that.$checkbox.prop('checked')); | |
38 | that.touch(); |
|
38 | that.touch(); | |
39 | that.user_invoked_update = false; |
|
39 | that.user_invoked_update = false; | |
40 | }) |
|
40 | }) | |
41 | .appendTo(this.$el); |
|
41 | .appendTo(this.$el); | |
42 |
|
42 | |||
43 | this.$el_to_style = this.$checkbox; // Set default element to style |
|
43 | this.$el_to_style = this.$checkbox; // Set default element to style | |
44 | this.update(); // Set defaults. |
|
44 | this.update(); // Set defaults. | |
45 | }, |
|
45 | }, | |
46 |
|
46 | |||
47 | // Handles: Backend -> Frontend Sync |
|
47 | // Handles: Backend -> Frontend Sync | |
48 | // Frontent -> Frontend Sync |
|
48 | // Frontent -> Frontend Sync | |
49 | update : function(){ |
|
49 | update : function(){ | |
50 | if (!this.user_invoked_update) { |
|
50 | if (!this.user_invoked_update) { | |
51 | this.$checkbox.prop('checked', this.model.get('value')); |
|
51 | this.$checkbox.prop('checked', this.model.get('value')); | |
52 |
|
52 | |||
53 | var disabled = this.model.get('disabled'); |
|
53 | var disabled = this.model.get('disabled'); | |
54 | this.$checkbox.prop('disabled', disabled); |
|
54 | this.$checkbox.prop('disabled', disabled); | |
55 |
|
55 | |||
56 | var description = this.model.get('description'); |
|
56 | var description = this.model.get('description'); | |
57 | if (description.length === 0) { |
|
57 | if (description.length === 0) { | |
58 | this.$label.hide(); |
|
58 | this.$label.hide(); | |
59 | } else { |
|
59 | } else { | |
60 | this.$label.html(description); |
|
60 | this.$label.html(description); | |
61 | this.$label.show(); |
|
61 | this.$label.show(); | |
62 | } |
|
62 | } | |
63 | } |
|
63 | } | |
64 | return IPython.WidgetView.prototype.update.call(this); |
|
64 | return IPython.WidgetView.prototype.update.call(this); | |
65 | }, |
|
65 | }, | |
66 |
|
66 | |||
67 | }); |
|
67 | }); | |
68 |
|
68 | |||
69 | widget_manager.register_widget_view('CheckboxView', CheckboxView); |
|
69 | widget_manager.register_widget_view('CheckboxView', CheckboxView); | |
70 |
|
70 | |||
71 | var ToggleButtonView = IPython.WidgetView.extend({ |
|
71 | var ToggleButtonView = IPython.WidgetView.extend({ | |
72 |
|
72 | |||
73 | // Called when view is rendered. |
|
73 | // Called when view is rendered. | |
74 | render : function(){ |
|
74 | render : function(){ | |
75 | this.$el |
|
75 | this.$el | |
76 | .html(''); |
|
76 | .html(''); | |
77 | this.$button = $('<button />') |
|
77 | this.$button = $('<button />') | |
78 | .addClass('btn') |
|
78 | .addClass('btn') | |
79 | .attr('type', 'button') |
|
79 | .attr('type', 'button') | |
80 | .attr('data-toggle', 'button') |
|
80 | .attr('data-toggle', 'button') | |
81 | .appendTo(this.$el); |
|
81 | .appendTo(this.$el); | |
82 | this.$el_to_style = this.$button; // Set default element to style |
|
82 | this.$el_to_style = this.$button; // Set default element to style | |
83 |
|
83 | |||
84 | this.update(); // Set defaults. |
|
84 | this.update(); // Set defaults. | |
85 | }, |
|
85 | }, | |
86 |
|
86 | |||
87 | // Handles: Backend -> Frontend Sync |
|
87 | // Handles: Backend -> Frontend Sync | |
88 | // Frontent -> Frontend Sync |
|
88 | // Frontent -> Frontend Sync | |
89 | update : function(){ |
|
89 | update : function(){ | |
90 | if (!this.user_invoked_update) { |
|
90 | if (!this.user_invoked_update) { | |
91 | if (this.model.get('value')) { |
|
91 | if (this.model.get('value')) { | |
92 | this.$button.addClass('active'); |
|
92 | this.$button.addClass('active'); | |
93 | } else { |
|
93 | } else { | |
94 | this.$button.removeClass('active'); |
|
94 | this.$button.removeClass('active'); | |
95 | } |
|
95 | } | |
96 |
|
96 | |||
97 | var disabled = this.model.get('disabled'); |
|
97 | var disabled = this.model.get('disabled'); | |
98 | this.$button.prop('disabled', disabled); |
|
98 | this.$button.prop('disabled', disabled); | |
99 |
|
99 | |||
100 | var description = this.model.get('description'); |
|
100 | var description = this.model.get('description'); | |
101 | if (description.length === 0) { |
|
101 | if (description.length === 0) { | |
102 | this.$button.html(' '); // Preserve button height |
|
102 | this.$button.html(' '); // Preserve button height | |
103 | } else { |
|
103 | } else { | |
104 | this.$button.html(description); |
|
104 | this.$button.html(description); | |
105 | } |
|
105 | } | |
106 | } |
|
106 | } | |
107 | return IPython.WidgetView.prototype.update.call(this); |
|
107 | return IPython.WidgetView.prototype.update.call(this); | |
108 | }, |
|
108 | }, | |
109 |
|
109 | |||
110 | events: {"click button" : "handleClick"}, |
|
110 | events: {"click button" : "handleClick"}, | |
111 |
|
111 | |||
112 | // Handles and validates user input. |
|
112 | // Handles and validates user input. | |
113 | handleClick: function(e) { |
|
113 | handleClick: function(e) { | |
114 | this.user_invoked_update = true; |
|
114 | this.user_invoked_update = true; | |
115 | this.model.set('value', ! $(e.target).hasClass('active')); |
|
115 | this.model.set('value', ! $(e.target).hasClass('active')); | |
116 | this.touch(); |
|
116 | this.touch(); | |
117 | this.user_invoked_update = false; |
|
117 | this.user_invoked_update = false; | |
118 | }, |
|
118 | }, | |
119 | }); |
|
119 | }); | |
120 |
|
120 | |||
121 | widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView); |
|
121 | widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView); | |
122 |
|
122 | |||
123 | }); |
|
123 | }); |
@@ -1,65 +1,65 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // ButtonWidget |
|
9 | // ButtonWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 |
|
18 | |||
19 | var ButtonWidgetModel = IPython.WidgetModel.extend({}); |
|
19 | var ButtonWidgetModel = IPython.WidgetModel.extend({}); | |
20 | widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel); |
|
20 | widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel); | |
21 |
|
21 | |||
22 | var ButtonView = IPython.WidgetView.extend({ |
|
22 | var ButtonView = IPython.WidgetView.extend({ | |
23 |
|
23 | |||
24 | // Called when view is rendered. |
|
24 | // Called when view is rendered. | |
25 | render : function(){ |
|
25 | render : function(){ | |
26 | var that = this; |
|
26 | var that = this; | |
27 | this.setElement($("<button />") |
|
27 | this.setElement($("<button />") | |
28 | .addClass('btn')); |
|
28 | .addClass('btn')); | |
29 |
|
29 | |||
30 | this.update(); // Set defaults. |
|
30 | this.update(); // Set defaults. | |
31 | }, |
|
31 | }, | |
32 |
|
32 | |||
33 | // Handles: Backend -> Frontend Sync |
|
33 | // Handles: Backend -> Frontend Sync | |
34 | // Frontent -> Frontend Sync |
|
34 | // Frontent -> Frontend Sync | |
35 | update : function(){ |
|
35 | update : function(){ | |
36 | var description = this.model.get('description'); |
|
36 | var description = this.model.get('description'); | |
37 | description = description.replace(/ /g, ' ', 'm'); |
|
37 | description = description.replace(/ /g, ' ', 'm'); | |
38 | description = description.replace(/\n/g, '<br>\n', 'm'); |
|
38 | description = description.replace(/\n/g, '<br>\n', 'm'); | |
39 | if (description.length === 0) { |
|
39 | if (description.length === 0) { | |
40 | this.$el.html(' '); // Preserve button height |
|
40 | this.$el.html(' '); // Preserve button height | |
41 | } else { |
|
41 | } else { | |
42 | this.$el.html(description); |
|
42 | this.$el.html(description); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | if (this.model.get('disabled')) { |
|
45 | if (this.model.get('disabled')) { | |
46 | this.$el.attr('disabled','disabled'); |
|
46 | this.$el.attr('disabled','disabled'); | |
47 | } else { |
|
47 | } else { | |
48 | this.$el.removeAttr('disabled'); |
|
48 | this.$el.removeAttr('disabled'); | |
49 | } |
|
49 | } | |
50 |
|
50 | |||
51 | return IPython.WidgetView.prototype.update.call(this); |
|
51 | return IPython.WidgetView.prototype.update.call(this); | |
52 | }, |
|
52 | }, | |
53 |
|
53 | |||
54 | events: { |
|
54 | events: { | |
55 | 'click': '_handle_click', |
|
55 | 'click': '_handle_click', | |
56 | }, |
|
56 | }, | |
57 |
|
57 | |||
58 | _handle_click: function(){ |
|
58 | _handle_click: function(){ | |
59 | this.send({event: 'click'}); |
|
59 | this.send({event: 'click'}); | |
60 | }, |
|
60 | }, | |
61 | }); |
|
61 | }); | |
62 |
|
62 | |||
63 | widget_manager.register_widget_view('ButtonView', ButtonView); |
|
63 | widget_manager.register_widget_view('ButtonView', ButtonView); | |
64 |
|
64 | |||
65 | }); |
|
65 | }); |
@@ -1,279 +1,279 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // ContainerWidget |
|
9 | // ContainerWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager) { | |
18 |
|
18 | |||
19 | var set_flex_property = function(element, property_name, enabled) { |
|
19 | var set_flex_property = function(element, property_name, enabled) { | |
20 | if (enabled) { |
|
20 | if (enabled) { | |
21 | element.addClass(property_name); |
|
21 | element.addClass(property_name); | |
22 | } else { |
|
22 | } else { | |
23 | element.removeClass(property_name); |
|
23 | element.removeClass(property_name); | |
24 | } |
|
24 | } | |
25 | }; |
|
25 | }; | |
26 |
|
26 | |||
27 | var set_flex_properties = function(context, element) { |
|
27 | var set_flex_properties = function(context, element) { | |
28 |
|
28 | |||
29 | // Apply flexible box model properties by adding and removing |
|
29 | // Apply flexible box model properties by adding and removing | |
30 | // corrosponding CSS classes. |
|
30 | // corrosponding CSS classes. | |
31 | // Defined in IPython/html/static/base/less/flexbox.less |
|
31 | // Defined in IPython/html/static/base/less/flexbox.less | |
32 | set_flex_property(element, 'vbox', context.model.get('_vbox')); |
|
32 | set_flex_property(element, 'vbox', context.model.get('_vbox')); | |
33 | set_flex_property(element, 'hbox', context.model.get('_hbox')); |
|
33 | set_flex_property(element, 'hbox', context.model.get('_hbox')); | |
34 | set_flex_property(element, 'start', context.model.get('_pack_start')); |
|
34 | set_flex_property(element, 'start', context.model.get('_pack_start')); | |
35 | set_flex_property(element, 'center', context.model.get('_pack_center')); |
|
35 | set_flex_property(element, 'center', context.model.get('_pack_center')); | |
36 | set_flex_property(element, 'end', context.model.get('_pack_end')); |
|
36 | set_flex_property(element, 'end', context.model.get('_pack_end')); | |
37 | set_flex_property(element, 'align-start', context.model.get('_align_start')); |
|
37 | set_flex_property(element, 'align-start', context.model.get('_align_start')); | |
38 | set_flex_property(element, 'align-center', context.model.get('_align_center')); |
|
38 | set_flex_property(element, 'align-center', context.model.get('_align_center')); | |
39 | set_flex_property(element, 'align-end', context.model.get('_align_end')); |
|
39 | set_flex_property(element, 'align-end', context.model.get('_align_end')); | |
40 | set_flex_property(element, 'box-flex0', context.model.get('_flex0')); |
|
40 | set_flex_property(element, 'box-flex0', context.model.get('_flex0')); | |
41 | set_flex_property(element, 'box-flex1', context.model.get('_flex1')); |
|
41 | set_flex_property(element, 'box-flex1', context.model.get('_flex1')); | |
42 | set_flex_property(element, 'box-flex2', context.model.get('_flex2')); |
|
42 | set_flex_property(element, 'box-flex2', context.model.get('_flex2')); | |
43 | }; |
|
43 | }; | |
44 |
|
44 | |||
45 |
|
45 | |||
46 |
|
46 | |||
47 | var ContainerModel = IPython.WidgetModel.extend({}); |
|
47 | var ContainerModel = IPython.WidgetModel.extend({}); | |
48 | widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel); |
|
48 | widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel); | |
49 |
|
49 | |||
50 | var ContainerView = IPython.WidgetView.extend({ |
|
50 | var ContainerView = IPython.WidgetView.extend({ | |
51 |
|
51 | |||
52 | render: function(){ |
|
52 | render: function(){ | |
53 | this.$el |
|
53 | this.$el | |
54 | .addClass('widget-container'); |
|
54 | .addClass('widget-container'); | |
55 | this.children={}; |
|
55 | this.children={}; | |
56 | this.update_children([], this.model.get('children')); |
|
56 | this.update_children([], this.model.get('children')); | |
57 | this.model.on('change:children', function(model, value, options) { |
|
57 | this.model.on('change:children', function(model, value, options) { | |
58 | this.update_children(model.previous('children'), value); |
|
58 | this.update_children(model.previous('children'), value); | |
59 | }, this); |
|
59 | }, this); | |
60 | this.update() |
|
60 | this.update() | |
61 | }, |
|
61 | }, | |
62 |
|
62 | |||
63 | update_children: function(old_list, new_list) { |
|
63 | update_children: function(old_list, new_list) { | |
64 | this.$el.empty(); |
|
64 | this.$el.empty(); | |
65 | this.update_child_views(old_list, new_list); |
|
65 | this.update_child_views(old_list, new_list); | |
66 | _.each(new_list, function(element, index, list) { |
|
66 | _.each(new_list, function(element, index, list) { | |
67 | this.$el.append(this.child_views[element].$el); |
|
67 | this.$el.append(this.child_views[element].$el); | |
68 | }, this) |
|
68 | }, this) | |
69 | }, |
|
69 | }, | |
70 |
|
70 | |||
71 | update: function(){ |
|
71 | update: function(){ | |
72 | set_flex_properties(this, this.$el); |
|
72 | set_flex_properties(this, this.$el); | |
73 | return IPython.WidgetView.prototype.update.call(this); |
|
73 | return IPython.WidgetView.prototype.update.call(this); | |
74 | }, |
|
74 | }, | |
75 | }); |
|
75 | }); | |
76 |
|
76 | |||
77 | widget_manager.register_widget_view('ContainerView', ContainerView); |
|
77 | widget_manager.register_widget_view('ContainerView', ContainerView); | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | var ModalView = IPython.WidgetView.extend({ |
|
80 | var ModalView = IPython.WidgetView.extend({ | |
81 |
|
81 | |||
82 | render: function(){ |
|
82 | render: function(){ | |
83 | var that = this; |
|
83 | var that = this; | |
84 | this.children={}; |
|
84 | this.children={}; | |
85 | this.update_children([], this.model.get('children')); |
|
85 | this.update_children([], this.model.get('children')); | |
86 | this.model.on('change:children', function(model, value, options) { |
|
86 | this.model.on('change:children', function(model, value, options) { | |
87 | this.update_children(model.previous('children'), value); |
|
87 | this.update_children(model.previous('children'), value); | |
88 | }, this); |
|
88 | }, this); | |
89 |
|
89 | |||
90 | this.$el |
|
90 | this.$el | |
91 | .html('') |
|
91 | .html('') | |
92 | .on("remove", function(){ |
|
92 | .on("remove", function(){ | |
93 | that.$window.remove(); |
|
93 | that.$window.remove(); | |
94 | }); |
|
94 | }); | |
95 | this.$window = $('<div />') |
|
95 | this.$window = $('<div />') | |
96 | .addClass('modal widget-modal') |
|
96 | .addClass('modal widget-modal') | |
97 | .appendTo($('#notebook-container')) |
|
97 | .appendTo($('#notebook-container')) | |
98 | .mousedown(function(){ |
|
98 | .mousedown(function(){ | |
99 | that.bring_to_front(); |
|
99 | that.bring_to_front(); | |
100 | }); |
|
100 | }); | |
101 | this.$title_bar = $('<div />') |
|
101 | this.$title_bar = $('<div />') | |
102 | .addClass('popover-title') |
|
102 | .addClass('popover-title') | |
103 | .appendTo(this.$window) |
|
103 | .appendTo(this.$window) | |
104 | .mousedown(function(){ |
|
104 | .mousedown(function(){ | |
105 | that.bring_to_front(); |
|
105 | that.bring_to_front(); | |
106 | }); |
|
106 | }); | |
107 | this.$close = $('<button />') |
|
107 | this.$close = $('<button />') | |
108 | .addClass('close icon-remove') |
|
108 | .addClass('close icon-remove') | |
109 | .css('margin-left', '5px') |
|
109 | .css('margin-left', '5px') | |
110 | .appendTo(this.$title_bar) |
|
110 | .appendTo(this.$title_bar) | |
111 | .click(function(){ |
|
111 | .click(function(){ | |
112 | that.hide(); |
|
112 | that.hide(); | |
113 | event.stopPropagation(); |
|
113 | event.stopPropagation(); | |
114 | }); |
|
114 | }); | |
115 | this.$minimize = $('<button />') |
|
115 | this.$minimize = $('<button />') | |
116 | .addClass('close icon-arrow-down') |
|
116 | .addClass('close icon-arrow-down') | |
117 | .appendTo(this.$title_bar) |
|
117 | .appendTo(this.$title_bar) | |
118 | .click(function(){ |
|
118 | .click(function(){ | |
119 | that.popped_out = !that.popped_out; |
|
119 | that.popped_out = !that.popped_out; | |
120 | if (!that.popped_out) { |
|
120 | if (!that.popped_out) { | |
121 | that.$minimize |
|
121 | that.$minimize | |
122 | .removeClass('icon-arrow-down') |
|
122 | .removeClass('icon-arrow-down') | |
123 | .addClass('icon-arrow-up'); |
|
123 | .addClass('icon-arrow-up'); | |
124 |
|
124 | |||
125 | that.$window |
|
125 | that.$window | |
126 | .draggable('destroy') |
|
126 | .draggable('destroy') | |
127 | .resizable('destroy') |
|
127 | .resizable('destroy') | |
128 | .removeClass('widget-modal modal') |
|
128 | .removeClass('widget-modal modal') | |
129 | .addClass('docked-widget-modal') |
|
129 | .addClass('docked-widget-modal') | |
130 | .detach() |
|
130 | .detach() | |
131 | .insertBefore(that.$show_button); |
|
131 | .insertBefore(that.$show_button); | |
132 | that.$show_button.hide(); |
|
132 | that.$show_button.hide(); | |
133 | that.$close.hide(); |
|
133 | that.$close.hide(); | |
134 | } else { |
|
134 | } else { | |
135 | that.$minimize |
|
135 | that.$minimize | |
136 | .addClass('icon-arrow-down') |
|
136 | .addClass('icon-arrow-down') | |
137 | .removeClass('icon-arrow-up'); |
|
137 | .removeClass('icon-arrow-up'); | |
138 |
|
138 | |||
139 | that.$window |
|
139 | that.$window | |
140 | .removeClass('docked-widget-modal') |
|
140 | .removeClass('docked-widget-modal') | |
141 | .addClass('widget-modal modal') |
|
141 | .addClass('widget-modal modal') | |
142 | .detach() |
|
142 | .detach() | |
143 | .appendTo($('#notebook-container')) |
|
143 | .appendTo($('#notebook-container')) | |
144 | .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}) |
|
144 | .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}) | |
145 | .resizable() |
|
145 | .resizable() | |
146 | .children('.ui-resizable-handle').show(); |
|
146 | .children('.ui-resizable-handle').show(); | |
147 | that.show(); |
|
147 | that.show(); | |
148 | that.$show_button.show(); |
|
148 | that.$show_button.show(); | |
149 | that.$close.show(); |
|
149 | that.$close.show(); | |
150 | } |
|
150 | } | |
151 | event.stopPropagation(); |
|
151 | event.stopPropagation(); | |
152 | }); |
|
152 | }); | |
153 | this.$title = $('<div />') |
|
153 | this.$title = $('<div />') | |
154 | .addClass('widget-modal-title') |
|
154 | .addClass('widget-modal-title') | |
155 | .html(' ') |
|
155 | .html(' ') | |
156 | .appendTo(this.$title_bar); |
|
156 | .appendTo(this.$title_bar); | |
157 | this.$body = $('<div />') |
|
157 | this.$body = $('<div />') | |
158 | .addClass('modal-body') |
|
158 | .addClass('modal-body') | |
159 | .addClass('widget-modal-body') |
|
159 | .addClass('widget-modal-body') | |
160 | .addClass('widget-container') |
|
160 | .addClass('widget-container') | |
161 | .appendTo(this.$window); |
|
161 | .appendTo(this.$window); | |
162 |
|
162 | |||
163 | this.$show_button = $('<button />') |
|
163 | this.$show_button = $('<button />') | |
164 | .html(' ') |
|
164 | .html(' ') | |
165 | .addClass('btn btn-info widget-modal-show') |
|
165 | .addClass('btn btn-info widget-modal-show') | |
166 | .appendTo(this.$el) |
|
166 | .appendTo(this.$el) | |
167 | .click(function(){ |
|
167 | .click(function(){ | |
168 | that.show(); |
|
168 | that.show(); | |
169 | }); |
|
169 | }); | |
170 |
|
170 | |||
171 | this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}); |
|
171 | this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'}); | |
172 | this.$window.resizable(); |
|
172 | this.$window.resizable(); | |
173 | this.$window.on('resize', function(){ |
|
173 | this.$window.on('resize', function(){ | |
174 | that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight()); |
|
174 | that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight()); | |
175 | }); |
|
175 | }); | |
176 |
|
176 | |||
177 | this.$el_to_style = this.$body; |
|
177 | this.$el_to_style = this.$body; | |
178 | this._shown_once = false; |
|
178 | this._shown_once = false; | |
179 | this.popped_out = true; |
|
179 | this.popped_out = true; | |
180 | }, |
|
180 | }, | |
181 |
|
181 | |||
182 | hide: function() { |
|
182 | hide: function() { | |
183 | this.$window.hide(); |
|
183 | this.$window.hide(); | |
184 | this.$show_button.removeClass('btn-info'); |
|
184 | this.$show_button.removeClass('btn-info'); | |
185 | }, |
|
185 | }, | |
186 |
|
186 | |||
187 | show: function() { |
|
187 | show: function() { | |
188 | this.$show_button.addClass('btn-info'); |
|
188 | this.$show_button.addClass('btn-info'); | |
189 |
|
189 | |||
190 | this.$window.show(); |
|
190 | this.$window.show(); | |
191 | if (this.popped_out) { |
|
191 | if (this.popped_out) { | |
192 | this.$window.css("positon", "absolute"); |
|
192 | this.$window.css("positon", "absolute"); | |
193 | this.$window.css("top", "0px"); |
|
193 | this.$window.css("top", "0px"); | |
194 | this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) + |
|
194 | this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) + | |
195 | $(window).scrollLeft()) + "px"); |
|
195 | $(window).scrollLeft()) + "px"); | |
196 | this.bring_to_front(); |
|
196 | this.bring_to_front(); | |
197 | } |
|
197 | } | |
198 | }, |
|
198 | }, | |
199 |
|
199 | |||
200 | bring_to_front: function() { |
|
200 | bring_to_front: function() { | |
201 | var $widget_modals = $(".widget-modal"); |
|
201 | var $widget_modals = $(".widget-modal"); | |
202 | var max_zindex = 0; |
|
202 | var max_zindex = 0; | |
203 | $widget_modals.each(function (index, el){ |
|
203 | $widget_modals.each(function (index, el){ | |
204 | max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index'))); |
|
204 | max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index'))); | |
205 | }); |
|
205 | }); | |
206 |
|
206 | |||
207 | // Start z-index of widget modals at 2000 |
|
207 | // Start z-index of widget modals at 2000 | |
208 | max_zindex = Math.max(max_zindex, 2000); |
|
208 | max_zindex = Math.max(max_zindex, 2000); | |
209 |
|
209 | |||
210 | $widget_modals.each(function (index, el){ |
|
210 | $widget_modals.each(function (index, el){ | |
211 | $el = $(el); |
|
211 | $el = $(el); | |
212 | if (max_zindex == parseInt($el.css('z-index'))) { |
|
212 | if (max_zindex == parseInt($el.css('z-index'))) { | |
213 | $el.css('z-index', max_zindex - 1); |
|
213 | $el.css('z-index', max_zindex - 1); | |
214 | } |
|
214 | } | |
215 | }); |
|
215 | }); | |
216 | this.$window.css('z-index', max_zindex); |
|
216 | this.$window.css('z-index', max_zindex); | |
217 | }, |
|
217 | }, | |
218 |
|
218 | |||
219 | update_children: function(old_list, new_list) { |
|
219 | update_children: function(old_list, new_list) { | |
220 | this.$el.empty(); |
|
220 | this.$el.empty(); | |
221 | this.update_child_views(old_list, new_list); |
|
221 | this.update_child_views(old_list, new_list); | |
222 | _.each(new_list, function(element, index, list) { |
|
222 | _.each(new_list, function(element, index, list) { | |
223 | this.$body.append(this.child_views[element].$el); |
|
223 | this.$body.append(this.child_views[element].$el); | |
224 | }, this) |
|
224 | }, this) | |
225 | }, |
|
225 | }, | |
226 |
|
226 | |||
227 | update: function(){ |
|
227 | update: function(){ | |
228 | set_flex_properties(this, this.$body); |
|
228 | set_flex_properties(this, this.$body); | |
229 |
|
229 | |||
230 | var description = this.model.get('description'); |
|
230 | var description = this.model.get('description'); | |
231 | description = description.replace(/ /g, ' ', 'm'); |
|
231 | description = description.replace(/ /g, ' ', 'm'); | |
232 | description = description.replace(/\n/g, '<br>\n', 'm'); |
|
232 | description = description.replace(/\n/g, '<br>\n', 'm'); | |
233 | if (description.length === 0) { |
|
233 | if (description.length === 0) { | |
234 | this.$title.html(' '); // Preserve title height |
|
234 | this.$title.html(' '); // Preserve title height | |
235 | } else { |
|
235 | } else { | |
236 | this.$title.html(description); |
|
236 | this.$title.html(description); | |
237 | } |
|
237 | } | |
238 |
|
238 | |||
239 | var button_text = this.model.get('button_text'); |
|
239 | var button_text = this.model.get('button_text'); | |
240 | button_text = button_text.replace(/ /g, ' ', 'm'); |
|
240 | button_text = button_text.replace(/ /g, ' ', 'm'); | |
241 | button_text = button_text.replace(/\n/g, '<br>\n', 'm'); |
|
241 | button_text = button_text.replace(/\n/g, '<br>\n', 'm'); | |
242 | if (button_text.length === 0) { |
|
242 | if (button_text.length === 0) { | |
243 | this.$show_button.html(' '); // Preserve button height |
|
243 | this.$show_button.html(' '); // Preserve button height | |
244 | } else { |
|
244 | } else { | |
245 | this.$show_button.html(button_text); |
|
245 | this.$show_button.html(button_text); | |
246 | } |
|
246 | } | |
247 |
|
247 | |||
248 | if (!this._shown_once) { |
|
248 | if (!this._shown_once) { | |
249 | this._shown_once = true; |
|
249 | this._shown_once = true; | |
250 | this.show(); |
|
250 | this.show(); | |
251 | } |
|
251 | } | |
252 |
|
252 | |||
253 | return IPython.WidgetView.prototype.update.call(this); |
|
253 | return IPython.WidgetView.prototype.update.call(this); | |
254 | }, |
|
254 | }, | |
255 |
|
255 | |||
256 | _get_selector_element: function(selector) { |
|
256 | _get_selector_element: function(selector) { | |
257 |
|
257 | |||
258 | // Since the modal actually isn't within the $el in the DOM, we need to extend |
|
258 | // Since the modal actually isn't within the $el in the DOM, we need to extend | |
259 | // the selector logic to allow the user to set css on the modal if need be. |
|
259 | // the selector logic to allow the user to set css on the modal if need be. | |
260 | // The convention used is: |
|
260 | // The convention used is: | |
261 | // "modal" - select the modal div |
|
261 | // "modal" - select the modal div | |
262 | // "modal [selector]" - select element(s) within the modal div. |
|
262 | // "modal [selector]" - select element(s) within the modal div. | |
263 | // "[selector]" - select elements within $el |
|
263 | // "[selector]" - select elements within $el | |
264 | // "" - select the $el_to_style |
|
264 | // "" - select the $el_to_style | |
265 | if (selector.substring(0, 5) == 'modal') { |
|
265 | if (selector.substring(0, 5) == 'modal') { | |
266 | if (selector == 'modal') { |
|
266 | if (selector == 'modal') { | |
267 | return this.$window; |
|
267 | return this.$window; | |
268 | } else { |
|
268 | } else { | |
269 | return this.$window.find(selector.substring(6)); |
|
269 | return this.$window.find(selector.substring(6)); | |
270 | } |
|
270 | } | |
271 | } else { |
|
271 | } else { | |
272 | return IPython.WidgetView.prototype._get_selector_element.call(this, selector); |
|
272 | return IPython.WidgetView.prototype._get_selector_element.call(this, selector); | |
273 | } |
|
273 | } | |
274 | }, |
|
274 | }, | |
275 |
|
275 | |||
276 | }); |
|
276 | }); | |
277 |
|
277 | |||
278 | widget_manager.register_widget_view('ModalView', ModalView); |
|
278 | widget_manager.register_widget_view('ModalView', ModalView); | |
279 | }); |
|
279 | }); |
@@ -1,20 +1,20 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // FloatWidget |
|
9 | // FloatWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var FloatWidgetModel = IPython.WidgetModel.extend({}); |
|
18 | var FloatWidgetModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('FloatWidgetModel', FloatWidgetModel); |
|
19 | widget_manager.register_widget_model('FloatWidgetModel', FloatWidgetModel); | |
20 | }); No newline at end of file |
|
20 | }); |
@@ -1,255 +1,255 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // FloatRangeWidget |
|
9 | // FloatRangeWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var FloatRangeWidgetModel = IPython.WidgetModel.extend({}); |
|
18 | var FloatRangeWidgetModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel); |
|
19 | widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel); | |
20 |
|
20 | |||
21 | var FloatSliderView = IPython.WidgetView.extend({ |
|
21 | var FloatSliderView = IPython.WidgetView.extend({ | |
22 |
|
22 | |||
23 | // Called when view is rendered. |
|
23 | // Called when view is rendered. | |
24 | render : function(){ |
|
24 | render : function(){ | |
25 | this.$el |
|
25 | this.$el | |
26 | .addClass('widget-hbox-single') |
|
26 | .addClass('widget-hbox-single') | |
27 | .html(''); |
|
27 | .html(''); | |
28 | this.$label = $('<div />') |
|
28 | this.$label = $('<div />') | |
29 | .appendTo(this.$el) |
|
29 | .appendTo(this.$el) | |
30 | .addClass('widget-hlabel') |
|
30 | .addClass('widget-hlabel') | |
31 | .hide(); |
|
31 | .hide(); | |
32 | this.$slider = $('<div />') |
|
32 | this.$slider = $('<div />') | |
33 | .slider({}) |
|
33 | .slider({}) | |
34 | .addClass('slider'); |
|
34 | .addClass('slider'); | |
35 |
|
35 | |||
36 | // Put the slider in a container |
|
36 | // Put the slider in a container | |
37 | this.$slider_container = $('<div />') |
|
37 | this.$slider_container = $('<div />') | |
38 | .addClass('widget-hslider') |
|
38 | .addClass('widget-hslider') | |
39 | .append(this.$slider); |
|
39 | .append(this.$slider); | |
40 | this.$el_to_style = this.$slider_container; // Set default element to style |
|
40 | this.$el_to_style = this.$slider_container; // Set default element to style | |
41 | this.$el.append(this.$slider_container); |
|
41 | this.$el.append(this.$slider_container); | |
42 |
|
42 | |||
43 | // Set defaults. |
|
43 | // Set defaults. | |
44 | this.update(); |
|
44 | this.update(); | |
45 | }, |
|
45 | }, | |
46 |
|
46 | |||
47 | // Handles: Backend -> Frontend Sync |
|
47 | // Handles: Backend -> Frontend Sync | |
48 | // Frontent -> Frontend Sync |
|
48 | // Frontent -> Frontend Sync | |
49 | update : function(){ |
|
49 | update : function(){ | |
50 | // Slider related keys. |
|
50 | // Slider related keys. | |
51 | var _keys = ['step', 'max', 'min', 'disabled']; |
|
51 | var _keys = ['step', 'max', 'min', 'disabled']; | |
52 | for (var index in _keys) { |
|
52 | for (var index in _keys) { | |
53 | var key = _keys[index]; |
|
53 | var key = _keys[index]; | |
54 | if (this.model.get(key) !== undefined) { |
|
54 | if (this.model.get(key) !== undefined) { | |
55 | this.$slider.slider("option", key, this.model.get(key)); |
|
55 | this.$slider.slider("option", key, this.model.get(key)); | |
56 | } |
|
56 | } | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | // WORKAROUND FOR JQUERY SLIDER BUG. |
|
59 | // WORKAROUND FOR JQUERY SLIDER BUG. | |
60 | // The horizontal position of the slider handle |
|
60 | // The horizontal position of the slider handle | |
61 | // depends on the value of the slider at the time |
|
61 | // depends on the value of the slider at the time | |
62 | // of orientation change. Before applying the new |
|
62 | // of orientation change. Before applying the new | |
63 | // workaround, we set the value to the minimum to |
|
63 | // workaround, we set the value to the minimum to | |
64 | // make sure that the horizontal placement of the |
|
64 | // make sure that the horizontal placement of the | |
65 | // handle in the vertical slider is always |
|
65 | // handle in the vertical slider is always | |
66 | // consistent. |
|
66 | // consistent. | |
67 | var orientation = this.model.get('orientation'); |
|
67 | var orientation = this.model.get('orientation'); | |
68 | var value = this.model.get('min'); |
|
68 | var value = this.model.get('min'); | |
69 | this.$slider.slider('option', 'value', value); |
|
69 | this.$slider.slider('option', 'value', value); | |
70 | this.$slider.slider('option', 'orientation', orientation); |
|
70 | this.$slider.slider('option', 'orientation', orientation); | |
71 | value = this.model.get('value'); |
|
71 | value = this.model.get('value'); | |
72 | this.$slider.slider('option', 'value', value); |
|
72 | this.$slider.slider('option', 'value', value); | |
73 |
|
73 | |||
74 | // Use the right CSS classes for vertical & horizontal sliders |
|
74 | // Use the right CSS classes for vertical & horizontal sliders | |
75 | if (orientation=='vertical') { |
|
75 | if (orientation=='vertical') { | |
76 | this.$slider_container |
|
76 | this.$slider_container | |
77 | .removeClass('widget-hslider') |
|
77 | .removeClass('widget-hslider') | |
78 | .addClass('widget-vslider'); |
|
78 | .addClass('widget-vslider'); | |
79 | this.$el |
|
79 | this.$el | |
80 | .removeClass('widget-hbox-single') |
|
80 | .removeClass('widget-hbox-single') | |
81 | .addClass('widget-vbox-single'); |
|
81 | .addClass('widget-vbox-single'); | |
82 | this.$label |
|
82 | this.$label | |
83 | .removeClass('widget-hlabel') |
|
83 | .removeClass('widget-hlabel') | |
84 | .addClass('widget-vlabel'); |
|
84 | .addClass('widget-vlabel'); | |
85 |
|
85 | |||
86 | } else { |
|
86 | } else { | |
87 | this.$slider_container |
|
87 | this.$slider_container | |
88 | .removeClass('widget-vslider') |
|
88 | .removeClass('widget-vslider') | |
89 | .addClass('widget-hslider'); |
|
89 | .addClass('widget-hslider'); | |
90 | this.$el |
|
90 | this.$el | |
91 | .removeClass('widget-vbox-single') |
|
91 | .removeClass('widget-vbox-single') | |
92 | .addClass('widget-hbox-single'); |
|
92 | .addClass('widget-hbox-single'); | |
93 | this.$label |
|
93 | this.$label | |
94 | .removeClass('widget-vlabel') |
|
94 | .removeClass('widget-vlabel') | |
95 | .addClass('widget-hlabel'); |
|
95 | .addClass('widget-hlabel'); | |
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 | var description = this.model.get('description'); |
|
98 | var description = this.model.get('description'); | |
99 | if (description.length === 0) { |
|
99 | if (description.length === 0) { | |
100 | this.$label.hide(); |
|
100 | this.$label.hide(); | |
101 | } else { |
|
101 | } else { | |
102 | this.$label.html(description); |
|
102 | this.$label.html(description); | |
103 | this.$label.show(); |
|
103 | this.$label.show(); | |
104 | } |
|
104 | } | |
105 | return IPython.WidgetView.prototype.update.call(this); |
|
105 | return IPython.WidgetView.prototype.update.call(this); | |
106 | }, |
|
106 | }, | |
107 |
|
107 | |||
108 | // Handles: User input |
|
108 | // Handles: User input | |
109 | events: { "slide" : "handleSliderChange" }, |
|
109 | events: { "slide" : "handleSliderChange" }, | |
110 | handleSliderChange: function(e, ui) { |
|
110 | handleSliderChange: function(e, ui) { | |
111 | this.model.set('value', ui.value); |
|
111 | this.model.set('value', ui.value); | |
112 | this.touch(); |
|
112 | this.touch(); | |
113 | }, |
|
113 | }, | |
114 | }); |
|
114 | }); | |
115 |
|
115 | |||
116 | widget_manager.register_widget_view('FloatSliderView', FloatSliderView); |
|
116 | widget_manager.register_widget_view('FloatSliderView', FloatSliderView); | |
117 |
|
117 | |||
118 |
|
118 | |||
119 | var FloatTextView = IPython.WidgetView.extend({ |
|
119 | var FloatTextView = IPython.WidgetView.extend({ | |
120 |
|
120 | |||
121 | // Called when view is rendered. |
|
121 | // Called when view is rendered. | |
122 | render : function(){ |
|
122 | render : function(){ | |
123 | this.$el |
|
123 | this.$el | |
124 | .addClass('widget-hbox-single') |
|
124 | .addClass('widget-hbox-single') | |
125 | .html(''); |
|
125 | .html(''); | |
126 | this.$label = $('<div />') |
|
126 | this.$label = $('<div />') | |
127 | .appendTo(this.$el) |
|
127 | .appendTo(this.$el) | |
128 | .addClass('widget-hlabel') |
|
128 | .addClass('widget-hlabel') | |
129 | .hide(); |
|
129 | .hide(); | |
130 | this.$textbox = $('<input type="text" />') |
|
130 | this.$textbox = $('<input type="text" />') | |
131 | .addClass('input') |
|
131 | .addClass('input') | |
132 | .addClass('widget-numeric-text') |
|
132 | .addClass('widget-numeric-text') | |
133 | .appendTo(this.$el); |
|
133 | .appendTo(this.$el); | |
134 | this.$el_to_style = this.$textbox; // Set default element to style |
|
134 | this.$el_to_style = this.$textbox; // Set default element to style | |
135 | this.update(); // Set defaults. |
|
135 | this.update(); // Set defaults. | |
136 | }, |
|
136 | }, | |
137 |
|
137 | |||
138 | // Handles: Backend -> Frontend Sync |
|
138 | // Handles: Backend -> Frontend Sync | |
139 | // Frontent -> Frontend Sync |
|
139 | // Frontent -> Frontend Sync | |
140 | update : function(){ |
|
140 | update : function(){ | |
141 | var value = this.model.get('value'); |
|
141 | var value = this.model.get('value'); | |
142 | if (!this.changing && parseFloat(this.$textbox.val()) != value) { |
|
142 | if (!this.changing && parseFloat(this.$textbox.val()) != value) { | |
143 | this.$textbox.val(value); |
|
143 | this.$textbox.val(value); | |
144 | } |
|
144 | } | |
145 |
|
145 | |||
146 | if (this.model.get('disabled')) { |
|
146 | if (this.model.get('disabled')) { | |
147 | this.$textbox.attr('disabled','disabled'); |
|
147 | this.$textbox.attr('disabled','disabled'); | |
148 | } else { |
|
148 | } else { | |
149 | this.$textbox.removeAttr('disabled'); |
|
149 | this.$textbox.removeAttr('disabled'); | |
150 | } |
|
150 | } | |
151 |
|
151 | |||
152 | var description = this.model.get('description'); |
|
152 | var description = this.model.get('description'); | |
153 | if (description.length === 0) { |
|
153 | if (description.length === 0) { | |
154 | this.$label.hide(); |
|
154 | this.$label.hide(); | |
155 | } else { |
|
155 | } else { | |
156 | this.$label.html(description); |
|
156 | this.$label.html(description); | |
157 | this.$label.show(); |
|
157 | this.$label.show(); | |
158 | } |
|
158 | } | |
159 | return IPython.WidgetView.prototype.update.call(this); |
|
159 | return IPython.WidgetView.prototype.update.call(this); | |
160 | }, |
|
160 | }, | |
161 |
|
161 | |||
162 |
|
162 | |||
163 | events: {"keyup input" : "handleChanging", |
|
163 | events: {"keyup input" : "handleChanging", | |
164 | "paste input" : "handleChanging", |
|
164 | "paste input" : "handleChanging", | |
165 | "cut input" : "handleChanging", |
|
165 | "cut input" : "handleChanging", | |
166 | "change input" : "handleChanged"}, // Fires only when control is validated or looses focus. |
|
166 | "change input" : "handleChanged"}, // Fires only when control is validated or looses focus. | |
167 |
|
167 | |||
168 | // Handles and validates user input. |
|
168 | // Handles and validates user input. | |
169 | handleChanging: function(e) { |
|
169 | handleChanging: function(e) { | |
170 |
|
170 | |||
171 | // Try to parse value as a float. |
|
171 | // Try to parse value as a float. | |
172 | var numericalValue = 0.0; |
|
172 | var numericalValue = 0.0; | |
173 | if (e.target.value !== '') { |
|
173 | if (e.target.value !== '') { | |
174 | numericalValue = parseFloat(e.target.value); |
|
174 | numericalValue = parseFloat(e.target.value); | |
175 | } |
|
175 | } | |
176 |
|
176 | |||
177 | // If parse failed, reset value to value stored in model. |
|
177 | // If parse failed, reset value to value stored in model. | |
178 | if (isNaN(numericalValue)) { |
|
178 | if (isNaN(numericalValue)) { | |
179 | e.target.value = this.model.get('value'); |
|
179 | e.target.value = this.model.get('value'); | |
180 | } else if (!isNaN(numericalValue)) { |
|
180 | } else if (!isNaN(numericalValue)) { | |
181 | if (this.model.get('max') !== undefined) { |
|
181 | if (this.model.get('max') !== undefined) { | |
182 | numericalValue = Math.min(this.model.get('max'), numericalValue); |
|
182 | numericalValue = Math.min(this.model.get('max'), numericalValue); | |
183 | } |
|
183 | } | |
184 | if (this.model.get('min') !== undefined) { |
|
184 | if (this.model.get('min') !== undefined) { | |
185 | numericalValue = Math.max(this.model.get('min'), numericalValue); |
|
185 | numericalValue = Math.max(this.model.get('min'), numericalValue); | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | // Apply the value if it has changed. |
|
188 | // Apply the value if it has changed. | |
189 | if (numericalValue != this.model.get('value')) { |
|
189 | if (numericalValue != this.model.get('value')) { | |
190 | this.changing = true; |
|
190 | this.changing = true; | |
191 | this.model.set('value', numericalValue); |
|
191 | this.model.set('value', numericalValue); | |
192 | this.touch(); |
|
192 | this.touch(); | |
193 | this.changing = false; |
|
193 | this.changing = false; | |
194 | } |
|
194 | } | |
195 | } |
|
195 | } | |
196 | }, |
|
196 | }, | |
197 |
|
197 | |||
198 | // Applies validated input. |
|
198 | // Applies validated input. | |
199 | handleChanged: function(e) { |
|
199 | handleChanged: function(e) { | |
200 | // Update the textbox |
|
200 | // Update the textbox | |
201 | if (this.model.get('value') != e.target.value) { |
|
201 | if (this.model.get('value') != e.target.value) { | |
202 | e.target.value = this.model.get('value'); |
|
202 | e.target.value = this.model.get('value'); | |
203 | } |
|
203 | } | |
204 | } |
|
204 | } | |
205 | }); |
|
205 | }); | |
206 |
|
206 | |||
207 | widget_manager.register_widget_view('FloatTextView', FloatTextView); |
|
207 | widget_manager.register_widget_view('FloatTextView', FloatTextView); | |
208 |
|
208 | |||
209 |
|
209 | |||
210 | var ProgressView = IPython.WidgetView.extend({ |
|
210 | var ProgressView = IPython.WidgetView.extend({ | |
211 |
|
211 | |||
212 | // Called when view is rendered. |
|
212 | // Called when view is rendered. | |
213 | render : function(){ |
|
213 | render : function(){ | |
214 | this.$el |
|
214 | this.$el | |
215 | .addClass('widget-hbox-single') |
|
215 | .addClass('widget-hbox-single') | |
216 | .html(''); |
|
216 | .html(''); | |
217 | this.$label = $('<div />') |
|
217 | this.$label = $('<div />') | |
218 | .appendTo(this.$el) |
|
218 | .appendTo(this.$el) | |
219 | .addClass('widget-hlabel') |
|
219 | .addClass('widget-hlabel') | |
220 | .hide(); |
|
220 | .hide(); | |
221 | this.$progress = $('<div />') |
|
221 | this.$progress = $('<div />') | |
222 | .addClass('progress') |
|
222 | .addClass('progress') | |
223 | .addClass('widget-progress') |
|
223 | .addClass('widget-progress') | |
224 | .appendTo(this.$el); |
|
224 | .appendTo(this.$el); | |
225 | this.$el_to_style = this.$progress; // Set default element to style |
|
225 | this.$el_to_style = this.$progress; // Set default element to style | |
226 | this.$bar = $('<div />') |
|
226 | this.$bar = $('<div />') | |
227 | .addClass('bar') |
|
227 | .addClass('bar') | |
228 | .css('width', '50%') |
|
228 | .css('width', '50%') | |
229 | .appendTo(this.$progress); |
|
229 | .appendTo(this.$progress); | |
230 | this.update(); // Set defaults. |
|
230 | this.update(); // Set defaults. | |
231 | }, |
|
231 | }, | |
232 |
|
232 | |||
233 | // Handles: Backend -> Frontend Sync |
|
233 | // Handles: Backend -> Frontend Sync | |
234 | // Frontent -> Frontend Sync |
|
234 | // Frontent -> Frontend Sync | |
235 | update : function(){ |
|
235 | update : function(){ | |
236 | var value = this.model.get('value'); |
|
236 | var value = this.model.get('value'); | |
237 | var max = this.model.get('max'); |
|
237 | var max = this.model.get('max'); | |
238 | var min = this.model.get('min'); |
|
238 | var min = this.model.get('min'); | |
239 | var percent = 100.0 * (value - min) / (max - min); |
|
239 | var percent = 100.0 * (value - min) / (max - min); | |
240 | this.$bar.css('width', percent + '%'); |
|
240 | this.$bar.css('width', percent + '%'); | |
241 |
|
241 | |||
242 | var description = this.model.get('description'); |
|
242 | var description = this.model.get('description'); | |
243 | if (description.length === 0) { |
|
243 | if (description.length === 0) { | |
244 | this.$label.hide(); |
|
244 | this.$label.hide(); | |
245 | } else { |
|
245 | } else { | |
246 | this.$label.html(description); |
|
246 | this.$label.html(description); | |
247 | this.$label.show(); |
|
247 | this.$label.show(); | |
248 | } |
|
248 | } | |
249 | return IPython.WidgetView.prototype.update.call(this); |
|
249 | return IPython.WidgetView.prototype.update.call(this); | |
250 | }, |
|
250 | }, | |
251 |
|
251 | |||
252 | }); |
|
252 | }); | |
253 |
|
253 | |||
254 | widget_manager.register_widget_view('ProgressView', ProgressView); |
|
254 | widget_manager.register_widget_view('ProgressView', ProgressView); | |
255 | }); |
|
255 | }); |
@@ -1,55 +1,55 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // ImageWidget |
|
9 | // ImageWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var ImageWidgetModel = IPython.WidgetModel.extend({}); |
|
18 | var ImageWidgetModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('ImageWidgetModel', ImageWidgetModel); |
|
19 | widget_manager.register_widget_model('ImageWidgetModel', ImageWidgetModel); | |
20 |
|
20 | |||
21 | var ImageView = IPython.WidgetView.extend({ |
|
21 | var ImageView = IPython.WidgetView.extend({ | |
22 |
|
22 | |||
23 | // Called when view is rendered. |
|
23 | // Called when view is rendered. | |
24 | render : function(){ |
|
24 | render : function(){ | |
25 | this.setElement($("<img />")); |
|
25 | this.setElement($("<img />")); | |
26 | this.update(); // Set defaults. |
|
26 | this.update(); // Set defaults. | |
27 | }, |
|
27 | }, | |
28 |
|
28 | |||
29 | // Handles: Backend -> Frontend Sync |
|
29 | // Handles: Backend -> Frontend Sync | |
30 | // Frontent -> Frontend Sync |
|
30 | // Frontent -> Frontend Sync | |
31 | update : function(){ |
|
31 | update : function(){ | |
32 | var image_src = 'data:image/' + this.model.get('image_format') + ';base64,' + this.model.get('_b64value'); |
|
32 | var image_src = 'data:image/' + this.model.get('image_format') + ';base64,' + this.model.get('_b64value'); | |
33 | this.$el.attr('src', image_src); |
|
33 | this.$el.attr('src', image_src); | |
34 |
|
34 | |||
35 | var width = this.model.get('width'); |
|
35 | var width = this.model.get('width'); | |
36 | if (width !== undefined && width.length > 0) { |
|
36 | if (width !== undefined && width.length > 0) { | |
37 | this.$el.attr('width', width); |
|
37 | this.$el.attr('width', width); | |
38 | } else { |
|
38 | } else { | |
39 | this.$el.removeAttr('width'); |
|
39 | this.$el.removeAttr('width'); | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | var height = this.model.get('height'); |
|
42 | var height = this.model.get('height'); | |
43 | if (height !== undefined && height.length > 0) { |
|
43 | if (height !== undefined && height.length > 0) { | |
44 | this.$el.attr('height', height); |
|
44 | this.$el.attr('height', height); | |
45 | } else { |
|
45 | } else { | |
46 | this.$el.removeAttr('height'); |
|
46 | this.$el.removeAttr('height'); | |
47 | } |
|
47 | } | |
48 | return IPython.WidgetView.prototype.update.call(this); |
|
48 | return IPython.WidgetView.prototype.update.call(this); | |
49 | }, |
|
49 | }, | |
50 |
|
50 | |||
51 | }); |
|
51 | }); | |
52 |
|
52 | |||
53 | widget_manager.register_widget_view('ImageView', ImageView); |
|
53 | widget_manager.register_widget_view('ImageView', ImageView); | |
54 |
|
54 | |||
55 | }); |
|
55 | }); |
@@ -1,20 +1,20 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // IntWidget |
|
9 | // IntWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var IntWidgetModel = IPython.WidgetModel.extend({}); |
|
18 | var IntWidgetModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('IntWidgetModel', IntWidgetModel); |
|
19 | widget_manager.register_widget_model('IntWidgetModel', IntWidgetModel); | |
20 | }); No newline at end of file |
|
20 | }); |
@@ -1,207 +1,207 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // IntRangeWidget |
|
9 | // IntRangeWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var IntRangeWidgetModel = IPython.WidgetModel.extend({}); |
|
18 | var IntRangeWidgetModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel); |
|
19 | widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel); | |
20 |
|
20 | |||
21 | var IntSliderView = IPython.WidgetView.extend({ |
|
21 | var IntSliderView = IPython.WidgetView.extend({ | |
22 |
|
22 | |||
23 | // Called when view is rendered. |
|
23 | // Called when view is rendered. | |
24 | render : function(){ |
|
24 | render : function(){ | |
25 | this.$el |
|
25 | this.$el | |
26 | .addClass('widget-hbox-single') |
|
26 | .addClass('widget-hbox-single') | |
27 | .html(''); |
|
27 | .html(''); | |
28 | this.$label = $('<div />') |
|
28 | this.$label = $('<div />') | |
29 | .appendTo(this.$el) |
|
29 | .appendTo(this.$el) | |
30 | .addClass('widget-hlabel') |
|
30 | .addClass('widget-hlabel') | |
31 | .hide(); |
|
31 | .hide(); | |
32 | this.$slider = $('<div />') |
|
32 | this.$slider = $('<div />') | |
33 | .slider({}) |
|
33 | .slider({}) | |
34 | .addClass('slider'); |
|
34 | .addClass('slider'); | |
35 |
|
35 | |||
36 | // Put the slider in a container |
|
36 | // Put the slider in a container | |
37 | this.$slider_container = $('<div />') |
|
37 | this.$slider_container = $('<div />') | |
38 | .addClass('widget-hslider') |
|
38 | .addClass('widget-hslider') | |
39 | .append(this.$slider); |
|
39 | .append(this.$slider); | |
40 | this.$el_to_style = this.$slider_container; // Set default element to style |
|
40 | this.$el_to_style = this.$slider_container; // Set default element to style | |
41 | this.$el.append(this.$slider_container); |
|
41 | this.$el.append(this.$slider_container); | |
42 |
|
42 | |||
43 | // Set defaults. |
|
43 | // Set defaults. | |
44 | this.update(); |
|
44 | this.update(); | |
45 | }, |
|
45 | }, | |
46 |
|
46 | |||
47 | // Handles: Backend -> Frontend Sync |
|
47 | // Handles: Backend -> Frontend Sync | |
48 | // Frontent -> Frontend Sync |
|
48 | // Frontent -> Frontend Sync | |
49 | update : function(){ |
|
49 | update : function(){ | |
50 | // Slider related keys. |
|
50 | // Slider related keys. | |
51 | var _keys = ['step', 'max', 'min', 'disabled']; |
|
51 | var _keys = ['step', 'max', 'min', 'disabled']; | |
52 | for (var index in _keys) { |
|
52 | for (var index in _keys) { | |
53 | var key = _keys[index]; |
|
53 | var key = _keys[index]; | |
54 | if (this.model.get(key) !== undefined) { |
|
54 | if (this.model.get(key) !== undefined) { | |
55 | this.$slider.slider("option", key, this.model.get(key)); |
|
55 | this.$slider.slider("option", key, this.model.get(key)); | |
56 | } |
|
56 | } | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | // WORKAROUND FOR JQUERY SLIDER BUG. |
|
59 | // WORKAROUND FOR JQUERY SLIDER BUG. | |
60 | // The horizontal position of the slider handle |
|
60 | // The horizontal position of the slider handle | |
61 | // depends on the value of the slider at the time |
|
61 | // depends on the value of the slider at the time | |
62 | // of orientation change. Before applying the new |
|
62 | // of orientation change. Before applying the new | |
63 | // workaround, we set the value to the minimum to |
|
63 | // workaround, we set the value to the minimum to | |
64 | // make sure that the horizontal placement of the |
|
64 | // make sure that the horizontal placement of the | |
65 | // handle in the vertical slider is always |
|
65 | // handle in the vertical slider is always | |
66 | // consistent. |
|
66 | // consistent. | |
67 | var orientation = this.model.get('orientation'); |
|
67 | var orientation = this.model.get('orientation'); | |
68 | var value = this.model.get('min'); |
|
68 | var value = this.model.get('min'); | |
69 | this.$slider.slider('option', 'value', value); |
|
69 | this.$slider.slider('option', 'value', value); | |
70 | this.$slider.slider('option', 'orientation', orientation); |
|
70 | this.$slider.slider('option', 'orientation', orientation); | |
71 | value = this.model.get('value'); |
|
71 | value = this.model.get('value'); | |
72 | this.$slider.slider('option', 'value', value); |
|
72 | this.$slider.slider('option', 'value', value); | |
73 |
|
73 | |||
74 | // Use the right CSS classes for vertical & horizontal sliders |
|
74 | // Use the right CSS classes for vertical & horizontal sliders | |
75 | if (orientation=='vertical') { |
|
75 | if (orientation=='vertical') { | |
76 | this.$slider_container |
|
76 | this.$slider_container | |
77 | .removeClass('widget-hslider') |
|
77 | .removeClass('widget-hslider') | |
78 | .addClass('widget-vslider'); |
|
78 | .addClass('widget-vslider'); | |
79 | this.$el |
|
79 | this.$el | |
80 | .removeClass('widget-hbox-single') |
|
80 | .removeClass('widget-hbox-single') | |
81 | .addClass('widget-vbox-single'); |
|
81 | .addClass('widget-vbox-single'); | |
82 | this.$label |
|
82 | this.$label | |
83 | .removeClass('widget-hlabel') |
|
83 | .removeClass('widget-hlabel') | |
84 | .addClass('widget-vlabel'); |
|
84 | .addClass('widget-vlabel'); | |
85 |
|
85 | |||
86 | } else { |
|
86 | } else { | |
87 | this.$slider_container |
|
87 | this.$slider_container | |
88 | .removeClass('widget-vslider') |
|
88 | .removeClass('widget-vslider') | |
89 | .addClass('widget-hslider'); |
|
89 | .addClass('widget-hslider'); | |
90 | this.$el |
|
90 | this.$el | |
91 | .removeClass('widget-vbox-single') |
|
91 | .removeClass('widget-vbox-single') | |
92 | .addClass('widget-hbox-single'); |
|
92 | .addClass('widget-hbox-single'); | |
93 | this.$label |
|
93 | this.$label | |
94 | .removeClass('widget-vlabel') |
|
94 | .removeClass('widget-vlabel') | |
95 | .addClass('widget-hlabel'); |
|
95 | .addClass('widget-hlabel'); | |
96 | } |
|
96 | } | |
97 |
|
97 | |||
98 | var description = this.model.get('description'); |
|
98 | var description = this.model.get('description'); | |
99 | if (description.length === 0) { |
|
99 | if (description.length === 0) { | |
100 | this.$label.hide(); |
|
100 | this.$label.hide(); | |
101 | } else { |
|
101 | } else { | |
102 | this.$label.html(description); |
|
102 | this.$label.html(description); | |
103 | this.$label.show(); |
|
103 | this.$label.show(); | |
104 | } |
|
104 | } | |
105 | return IPython.WidgetView.prototype.update.call(this); |
|
105 | return IPython.WidgetView.prototype.update.call(this); | |
106 | }, |
|
106 | }, | |
107 |
|
107 | |||
108 | // Handles: User input |
|
108 | // Handles: User input | |
109 | events: { "slide" : "handleSliderChange" }, |
|
109 | events: { "slide" : "handleSliderChange" }, | |
110 | handleSliderChange: function(e, ui) { |
|
110 | handleSliderChange: function(e, ui) { | |
111 | this.model.set('value', ~~ui.value); // Double bit-wise not to truncate decimel |
|
111 | this.model.set('value', ~~ui.value); // Double bit-wise not to truncate decimel | |
112 | this.touch(); |
|
112 | this.touch(); | |
113 | }, |
|
113 | }, | |
114 | }); |
|
114 | }); | |
115 |
|
115 | |||
116 | widget_manager.register_widget_view('IntSliderView', IntSliderView); |
|
116 | widget_manager.register_widget_view('IntSliderView', IntSliderView); | |
117 |
|
117 | |||
118 | var IntTextView = IPython.WidgetView.extend({ |
|
118 | var IntTextView = IPython.WidgetView.extend({ | |
119 |
|
119 | |||
120 | // Called when view is rendered. |
|
120 | // Called when view is rendered. | |
121 | render : function(){ |
|
121 | render : function(){ | |
122 | this.$el |
|
122 | this.$el | |
123 | .addClass('widget-hbox-single') |
|
123 | .addClass('widget-hbox-single') | |
124 | .html(''); |
|
124 | .html(''); | |
125 | this.$label = $('<div />') |
|
125 | this.$label = $('<div />') | |
126 | .appendTo(this.$el) |
|
126 | .appendTo(this.$el) | |
127 | .addClass('widget-hlabel') |
|
127 | .addClass('widget-hlabel') | |
128 | .hide(); |
|
128 | .hide(); | |
129 | this.$textbox = $('<input type="text" />') |
|
129 | this.$textbox = $('<input type="text" />') | |
130 | .addClass('input') |
|
130 | .addClass('input') | |
131 | .addClass('widget-numeric-text') |
|
131 | .addClass('widget-numeric-text') | |
132 | .appendTo(this.$el); |
|
132 | .appendTo(this.$el); | |
133 | this.$el_to_style = this.$textbox; // Set default element to style |
|
133 | this.$el_to_style = this.$textbox; // Set default element to style | |
134 | this.update(); // Set defaults. |
|
134 | this.update(); // Set defaults. | |
135 | }, |
|
135 | }, | |
136 |
|
136 | |||
137 | // Handles: Backend -> Frontend Sync |
|
137 | // Handles: Backend -> Frontend Sync | |
138 | // Frontent -> Frontend Sync |
|
138 | // Frontent -> Frontend Sync | |
139 | update : function(){ |
|
139 | update : function(){ | |
140 | var value = this.model.get('value'); |
|
140 | var value = this.model.get('value'); | |
141 | if (!this.changing && parseInt(this.$textbox.val()) != value) { |
|
141 | if (!this.changing && parseInt(this.$textbox.val()) != value) { | |
142 | this.$textbox.val(value); |
|
142 | this.$textbox.val(value); | |
143 | } |
|
143 | } | |
144 |
|
144 | |||
145 | if (this.model.get('disabled')) { |
|
145 | if (this.model.get('disabled')) { | |
146 | this.$textbox.attr('disabled','disabled'); |
|
146 | this.$textbox.attr('disabled','disabled'); | |
147 | } else { |
|
147 | } else { | |
148 | this.$textbox.removeAttr('disabled'); |
|
148 | this.$textbox.removeAttr('disabled'); | |
149 | } |
|
149 | } | |
150 |
|
150 | |||
151 | var description = this.model.get('description'); |
|
151 | var description = this.model.get('description'); | |
152 | if (description.length === 0) { |
|
152 | if (description.length === 0) { | |
153 | this.$label.hide(); |
|
153 | this.$label.hide(); | |
154 | } else { |
|
154 | } else { | |
155 | this.$label.html(description); |
|
155 | this.$label.html(description); | |
156 | this.$label.show(); |
|
156 | this.$label.show(); | |
157 | } |
|
157 | } | |
158 | return IPython.WidgetView.prototype.update.call(this); |
|
158 | return IPython.WidgetView.prototype.update.call(this); | |
159 | }, |
|
159 | }, | |
160 |
|
160 | |||
161 |
|
161 | |||
162 | events: {"keyup input" : "handleChanging", |
|
162 | events: {"keyup input" : "handleChanging", | |
163 | "paste input" : "handleChanging", |
|
163 | "paste input" : "handleChanging", | |
164 | "cut input" : "handleChanging", |
|
164 | "cut input" : "handleChanging", | |
165 | "change input" : "handleChanged"}, // Fires only when control is validated or looses focus. |
|
165 | "change input" : "handleChanged"}, // Fires only when control is validated or looses focus. | |
166 |
|
166 | |||
167 | // Handles and validates user input. |
|
167 | // Handles and validates user input. | |
168 | handleChanging: function(e) { |
|
168 | handleChanging: function(e) { | |
169 |
|
169 | |||
170 | // Try to parse value as a float. |
|
170 | // Try to parse value as a float. | |
171 | var numericalValue = 0; |
|
171 | var numericalValue = 0; | |
172 | if (e.target.value !== '') { |
|
172 | if (e.target.value !== '') { | |
173 | numericalValue = parseInt(e.target.value); |
|
173 | numericalValue = parseInt(e.target.value); | |
174 | } |
|
174 | } | |
175 |
|
175 | |||
176 | // If parse failed, reset value to value stored in model. |
|
176 | // If parse failed, reset value to value stored in model. | |
177 | if (isNaN(numericalValue)) { |
|
177 | if (isNaN(numericalValue)) { | |
178 | e.target.value = this.model.get('value'); |
|
178 | e.target.value = this.model.get('value'); | |
179 | } else if (!isNaN(numericalValue)) { |
|
179 | } else if (!isNaN(numericalValue)) { | |
180 | if (this.model.get('max') !== undefined) { |
|
180 | if (this.model.get('max') !== undefined) { | |
181 | numericalValue = Math.min(this.model.get('max'), numericalValue); |
|
181 | numericalValue = Math.min(this.model.get('max'), numericalValue); | |
182 | } |
|
182 | } | |
183 | if (this.model.get('min') !== undefined) { |
|
183 | if (this.model.get('min') !== undefined) { | |
184 | numericalValue = Math.max(this.model.get('min'), numericalValue); |
|
184 | numericalValue = Math.max(this.model.get('min'), numericalValue); | |
185 | } |
|
185 | } | |
186 |
|
186 | |||
187 | // Apply the value if it has changed. |
|
187 | // Apply the value if it has changed. | |
188 | if (numericalValue != this.model.get('value')) { |
|
188 | if (numericalValue != this.model.get('value')) { | |
189 | this.changing = true; |
|
189 | this.changing = true; | |
190 | this.model.set('value', numericalValue); |
|
190 | this.model.set('value', numericalValue); | |
191 | this.touch(); |
|
191 | this.touch(); | |
192 | this.changing = false; |
|
192 | this.changing = false; | |
193 | } |
|
193 | } | |
194 | } |
|
194 | } | |
195 | }, |
|
195 | }, | |
196 |
|
196 | |||
197 | // Applies validated input. |
|
197 | // Applies validated input. | |
198 | handleChanged: function(e) { |
|
198 | handleChanged: function(e) { | |
199 | // Update the textbox |
|
199 | // Update the textbox | |
200 | if (this.model.get('value') != e.target.value) { |
|
200 | if (this.model.get('value') != e.target.value) { | |
201 | e.target.value = this.model.get('value'); |
|
201 | e.target.value = this.model.get('value'); | |
202 | } |
|
202 | } | |
203 | } |
|
203 | } | |
204 | }); |
|
204 | }); | |
205 |
|
205 | |||
206 | widget_manager.register_widget_view('IntTextView', IntTextView); |
|
206 | widget_manager.register_widget_view('IntTextView', IntTextView); | |
207 | }); |
|
207 | }); |
@@ -1,208 +1,208 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // MultiContainerWidget |
|
9 | // MultiContainerWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var MulticontainerModel = IPython.WidgetModel.extend({}); |
|
18 | var MulticontainerModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('MulticontainerWidgetModel', MulticontainerModel); |
|
19 | widget_manager.register_widget_model('MulticontainerWidgetModel', MulticontainerModel); | |
20 |
|
20 | |||
21 | var AccordionView = IPython.WidgetView.extend({ |
|
21 | var AccordionView = IPython.WidgetView.extend({ | |
22 |
|
22 | |||
23 | render: function(){ |
|
23 | render: function(){ | |
24 | var guid = 'accordion' + IPython.utils.uuid(); |
|
24 | var guid = 'accordion' + IPython.utils.uuid(); | |
25 | this.$el |
|
25 | this.$el | |
26 | .attr('id', guid) |
|
26 | .attr('id', guid) | |
27 | .addClass('accordion'); |
|
27 | .addClass('accordion'); | |
28 | this.containers = []; |
|
28 | this.containers = []; | |
29 | this.update_children([], this.model.get('children')); |
|
29 | this.update_children([], this.model.get('children')); | |
30 | this.model.on('change:children', function(model, value, options) { |
|
30 | this.model.on('change:children', function(model, value, options) { | |
31 | this.update_children(model.previous('children'), value); |
|
31 | this.update_children(model.previous('children'), value); | |
32 | }, this); |
|
32 | }, this); | |
33 | }, |
|
33 | }, | |
34 |
|
34 | |||
35 | update_children: function(old_list, new_list) { |
|
35 | update_children: function(old_list, new_list) { | |
36 | _.each(this.containers, function(element, index, list) { |
|
36 | _.each(this.containers, function(element, index, list) { | |
37 | element.remove(); |
|
37 | element.remove(); | |
38 | }, this); |
|
38 | }, this); | |
39 | this.containers = []; |
|
39 | this.containers = []; | |
40 | this.update_child_views(old_list, new_list); |
|
40 | this.update_child_views(old_list, new_list); | |
41 | _.each(new_list, function(element, index, list) { |
|
41 | _.each(new_list, function(element, index, list) { | |
42 | this.add_child_view(this.child_views[element]); |
|
42 | this.add_child_view(this.child_views[element]); | |
43 | }, this) |
|
43 | }, this) | |
44 | }, |
|
44 | }, | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | update: function() { |
|
47 | update: function() { | |
48 | // Set tab titles |
|
48 | // Set tab titles | |
49 | var titles = this.model.get('_titles'); |
|
49 | var titles = this.model.get('_titles'); | |
50 | for (var page_index in titles) { |
|
50 | for (var page_index in titles) { | |
51 |
|
51 | |||
52 | var accordian = this.containers[page_index]; |
|
52 | var accordian = this.containers[page_index]; | |
53 | if (accordian !== undefined) { |
|
53 | if (accordian !== undefined) { | |
54 | accordian |
|
54 | accordian | |
55 | .find('.accordion-heading') |
|
55 | .find('.accordion-heading') | |
56 | .find('.accordion-toggle') |
|
56 | .find('.accordion-toggle') | |
57 | .html(titles[page_index]); |
|
57 | .html(titles[page_index]); | |
58 | } |
|
58 | } | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | // Set selected page |
|
61 | // Set selected page | |
62 | var selected_index = this.model.get("selected_index"); |
|
62 | var selected_index = this.model.get("selected_index"); | |
63 | if (0 <= selected_index && selected_index < this.containers.length) { |
|
63 | if (0 <= selected_index && selected_index < this.containers.length) { | |
64 | for (var index in this.containers) { |
|
64 | for (var index in this.containers) { | |
65 | if (index==selected_index) { |
|
65 | if (index==selected_index) { | |
66 | this.containers[index].find('.accordion-body').collapse('show'); |
|
66 | this.containers[index].find('.accordion-body').collapse('show'); | |
67 | } else { |
|
67 | } else { | |
68 | this.containers[index].find('.accordion-body').collapse('hide'); |
|
68 | this.containers[index].find('.accordion-body').collapse('hide'); | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | } |
|
71 | } | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | return IPython.WidgetView.prototype.update.call(this); |
|
74 | return IPython.WidgetView.prototype.update.call(this); | |
75 | }, |
|
75 | }, | |
76 |
|
76 | |||
77 | add_child_view: function(view) { |
|
77 | add_child_view: function(view) { | |
78 |
|
78 | |||
79 | var index = this.containers.length; |
|
79 | var index = this.containers.length; | |
80 | var uuid = IPython.utils.uuid(); |
|
80 | var uuid = IPython.utils.uuid(); | |
81 | var accordion_group = $('<div />') |
|
81 | var accordion_group = $('<div />') | |
82 | .addClass('accordion-group') |
|
82 | .addClass('accordion-group') | |
83 | .appendTo(this.$el); |
|
83 | .appendTo(this.$el); | |
84 | var accordion_heading = $('<div />') |
|
84 | var accordion_heading = $('<div />') | |
85 | .addClass('accordion-heading') |
|
85 | .addClass('accordion-heading') | |
86 | .appendTo(accordion_group); |
|
86 | .appendTo(accordion_group); | |
87 | var that = this; |
|
87 | var that = this; | |
88 | var accordion_toggle = $('<a />') |
|
88 | var accordion_toggle = $('<a />') | |
89 | .addClass('accordion-toggle') |
|
89 | .addClass('accordion-toggle') | |
90 | .attr('data-toggle', 'collapse') |
|
90 | .attr('data-toggle', 'collapse') | |
91 | .attr('data-parent', '#' + this.$el.attr('id')) |
|
91 | .attr('data-parent', '#' + this.$el.attr('id')) | |
92 | .attr('href', '#' + uuid) |
|
92 | .attr('href', '#' + uuid) | |
93 | .click(function(evt){ |
|
93 | .click(function(evt){ | |
94 | that.model.set("selected_index", index); |
|
94 | that.model.set("selected_index", index); | |
95 | that.touch(); |
|
95 | that.touch(); | |
96 | }) |
|
96 | }) | |
97 | .html('Page ' + index) |
|
97 | .html('Page ' + index) | |
98 | .appendTo(accordion_heading); |
|
98 | .appendTo(accordion_heading); | |
99 | var accordion_body = $('<div />', {id: uuid}) |
|
99 | var accordion_body = $('<div />', {id: uuid}) | |
100 | .addClass('accordion-body collapse') |
|
100 | .addClass('accordion-body collapse') | |
101 | .appendTo(accordion_group); |
|
101 | .appendTo(accordion_group); | |
102 | var accordion_inner = $('<div />') |
|
102 | var accordion_inner = $('<div />') | |
103 | .addClass('accordion-inner') |
|
103 | .addClass('accordion-inner') | |
104 | .appendTo(accordion_body); |
|
104 | .appendTo(accordion_body); | |
105 | this.containers.push(accordion_group); |
|
105 | this.containers.push(accordion_group); | |
106 | accordion_inner.append(view.$el); |
|
106 | accordion_inner.append(view.$el); | |
107 |
|
107 | |||
108 | this.update(); |
|
108 | this.update(); | |
109 |
|
109 | |||
110 | // Stupid workaround to close the bootstrap accordion tabs which |
|
110 | // Stupid workaround to close the bootstrap accordion tabs which | |
111 | // open by default even though they don't have the `in` class |
|
111 | // open by default even though they don't have the `in` class | |
112 | // attached to them. For some reason a delay is required. |
|
112 | // attached to them. For some reason a delay is required. | |
113 | // TODO: Better fix. |
|
113 | // TODO: Better fix. | |
114 | setTimeout(function(){ that.update(); }, 500); |
|
114 | setTimeout(function(){ that.update(); }, 500); | |
115 | }, |
|
115 | }, | |
116 | }); |
|
116 | }); | |
117 |
|
117 | |||
118 | widget_manager.register_widget_view('AccordionView', AccordionView); |
|
118 | widget_manager.register_widget_view('AccordionView', AccordionView); | |
119 |
|
119 | |||
120 | var TabView = IPython.WidgetView.extend({ |
|
120 | var TabView = IPython.WidgetView.extend({ | |
121 |
|
121 | |||
122 | initialize: function() { |
|
122 | initialize: function() { | |
123 | this.containers = []; |
|
123 | this.containers = []; | |
124 | IPython.WidgetView.prototype.initialize.apply(this, arguments); |
|
124 | IPython.WidgetView.prototype.initialize.apply(this, arguments); | |
125 | }, |
|
125 | }, | |
126 |
|
126 | |||
127 | render: function(){ |
|
127 | render: function(){ | |
128 | var uuid = 'tabs'+IPython.utils.uuid(); |
|
128 | var uuid = 'tabs'+IPython.utils.uuid(); | |
129 | var that = this; |
|
129 | var that = this; | |
130 | this.$tabs = $('<div />', {id: uuid}) |
|
130 | this.$tabs = $('<div />', {id: uuid}) | |
131 | .addClass('nav') |
|
131 | .addClass('nav') | |
132 | .addClass('nav-tabs') |
|
132 | .addClass('nav-tabs') | |
133 | .appendTo(this.$el); |
|
133 | .appendTo(this.$el); | |
134 | this.$tab_contents = $('<div />', {id: uuid + 'Content'}) |
|
134 | this.$tab_contents = $('<div />', {id: uuid + 'Content'}) | |
135 | .addClass('tab-content') |
|
135 | .addClass('tab-content') | |
136 | .appendTo(this.$el); |
|
136 | .appendTo(this.$el); | |
137 | this.containers = []; |
|
137 | this.containers = []; | |
138 | this.update_children([], this.model.get('children')); |
|
138 | this.update_children([], this.model.get('children')); | |
139 | this.model.on('change:children', function(model, value, options) { |
|
139 | this.model.on('change:children', function(model, value, options) { | |
140 | this.update_children(model.previous('children'), value); |
|
140 | this.update_children(model.previous('children'), value); | |
141 | }, this); |
|
141 | }, this); | |
142 | }, |
|
142 | }, | |
143 |
|
143 | |||
144 | update_children: function(old_list, new_list) { |
|
144 | update_children: function(old_list, new_list) { | |
145 | _.each(this.containers, function(element, index, list) { |
|
145 | _.each(this.containers, function(element, index, list) { | |
146 | element.remove(); |
|
146 | element.remove(); | |
147 | }, this); |
|
147 | }, this); | |
148 | this.containers = []; |
|
148 | this.containers = []; | |
149 | this.update_child_views(old_list, new_list); |
|
149 | this.update_child_views(old_list, new_list); | |
150 | _.each(new_list, function(element, index, list) { |
|
150 | _.each(new_list, function(element, index, list) { | |
151 | this.add_child_view(this.child_views[element]); |
|
151 | this.add_child_view(this.child_views[element]); | |
152 | }, this) |
|
152 | }, this) | |
153 | }, |
|
153 | }, | |
154 |
|
154 | |||
155 | update: function() { |
|
155 | update: function() { | |
156 | // Set tab titles |
|
156 | // Set tab titles | |
157 | var titles = this.model.get('_titles'); |
|
157 | var titles = this.model.get('_titles'); | |
158 | for (var page_index in titles) { |
|
158 | for (var page_index in titles) { | |
159 | var tab_text = this.containers[page_index]; |
|
159 | var tab_text = this.containers[page_index]; | |
160 | if (tab_text !== undefined) { |
|
160 | if (tab_text !== undefined) { | |
161 | tab_text.html(titles[page_index]); |
|
161 | tab_text.html(titles[page_index]); | |
162 | } |
|
162 | } | |
163 | } |
|
163 | } | |
164 |
|
164 | |||
165 | var selected_index = this.model.get('selected_index'); |
|
165 | var selected_index = this.model.get('selected_index'); | |
166 | if (0 <= selected_index && selected_index < this.containers.length) { |
|
166 | if (0 <= selected_index && selected_index < this.containers.length) { | |
167 | this.select_page(selected_index); |
|
167 | this.select_page(selected_index); | |
168 | } |
|
168 | } | |
169 |
|
169 | |||
170 | return IPython.WidgetView.prototype.update.call(this); |
|
170 | return IPython.WidgetView.prototype.update.call(this); | |
171 | }, |
|
171 | }, | |
172 |
|
172 | |||
173 | add_child_view: function(view) { |
|
173 | add_child_view: function(view) { | |
174 | var index = this.containers.length; |
|
174 | var index = this.containers.length; | |
175 | var uuid = IPython.utils.uuid(); |
|
175 | var uuid = IPython.utils.uuid(); | |
176 |
|
176 | |||
177 | var that = this; |
|
177 | var that = this; | |
178 | var tab = $('<li />') |
|
178 | var tab = $('<li />') | |
179 | .css('list-style-type', 'none') |
|
179 | .css('list-style-type', 'none') | |
180 | .appendTo(this.$tabs); |
|
180 | .appendTo(this.$tabs); | |
181 | var tab_text = $('<a />') |
|
181 | var tab_text = $('<a />') | |
182 | .attr('href', '#' + uuid) |
|
182 | .attr('href', '#' + uuid) | |
183 | .attr('data-toggle', 'tab') |
|
183 | .attr('data-toggle', 'tab') | |
184 | .html('Page ' + index) |
|
184 | .html('Page ' + index) | |
185 | .appendTo(tab) |
|
185 | .appendTo(tab) | |
186 | .click(function (e) { |
|
186 | .click(function (e) { | |
187 | that.model.set("selected_index", index); |
|
187 | that.model.set("selected_index", index); | |
188 | that.touch(); |
|
188 | that.touch(); | |
189 | that.select_page(index); |
|
189 | that.select_page(index); | |
190 | }); |
|
190 | }); | |
191 | this.containers.push(tab_text); |
|
191 | this.containers.push(tab_text); | |
192 |
|
192 | |||
193 | var contents_div = $('<div />', {id: uuid}) |
|
193 | var contents_div = $('<div />', {id: uuid}) | |
194 | .addClass('tab-pane') |
|
194 | .addClass('tab-pane') | |
195 | .addClass('fade') |
|
195 | .addClass('fade') | |
196 | .append(view.$el) |
|
196 | .append(view.$el) | |
197 | .appendTo(this.$tab_contents); |
|
197 | .appendTo(this.$tab_contents); | |
198 | }, |
|
198 | }, | |
199 |
|
199 | |||
200 | select_page: function(index) { |
|
200 | select_page: function(index) { | |
201 | this.$tabs.find('li') |
|
201 | this.$tabs.find('li') | |
202 | .removeClass('active'); |
|
202 | .removeClass('active'); | |
203 | this.containers[index].tab('show'); |
|
203 | this.containers[index].tab('show'); | |
204 | }, |
|
204 | }, | |
205 | }); |
|
205 | }); | |
206 |
|
206 | |||
207 | widget_manager.register_widget_view('TabView', TabView); |
|
207 | widget_manager.register_widget_view('TabView', TabView); | |
208 | }); |
|
208 | }); |
@@ -1,360 +1,360 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // SelectionWidget |
|
9 | // SelectionWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); |
|
18 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); |
|
19 | widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); | |
20 |
|
20 | |||
21 | var DropdownView = IPython.WidgetView.extend({ |
|
21 | var DropdownView = IPython.WidgetView.extend({ | |
22 |
|
22 | |||
23 | // Called when view is rendered. |
|
23 | // Called when view is rendered. | |
24 | render : function(){ |
|
24 | render : function(){ | |
25 |
|
25 | |||
26 | this.$el |
|
26 | this.$el | |
27 | .addClass('widget-hbox-single') |
|
27 | .addClass('widget-hbox-single') | |
28 | .html(''); |
|
28 | .html(''); | |
29 | this.$label = $('<div />') |
|
29 | this.$label = $('<div />') | |
30 | .appendTo(this.$el) |
|
30 | .appendTo(this.$el) | |
31 | .addClass('widget-hlabel') |
|
31 | .addClass('widget-hlabel') | |
32 | .hide(); |
|
32 | .hide(); | |
33 | this.$buttongroup = $('<div />') |
|
33 | this.$buttongroup = $('<div />') | |
34 | .addClass('widget_item') |
|
34 | .addClass('widget_item') | |
35 | .addClass('btn-group') |
|
35 | .addClass('btn-group') | |
36 | .appendTo(this.$el); |
|
36 | .appendTo(this.$el); | |
37 | this.$el_to_style = this.$buttongroup; // Set default element to style |
|
37 | this.$el_to_style = this.$buttongroup; // Set default element to style | |
38 | this.$droplabel = $('<button />') |
|
38 | this.$droplabel = $('<button />') | |
39 | .addClass('btn') |
|
39 | .addClass('btn') | |
40 | .addClass('widget-combo-btn') |
|
40 | .addClass('widget-combo-btn') | |
41 | .html(' ') |
|
41 | .html(' ') | |
42 | .appendTo(this.$buttongroup); |
|
42 | .appendTo(this.$buttongroup); | |
43 | this.$dropbutton = $('<button />') |
|
43 | this.$dropbutton = $('<button />') | |
44 | .addClass('btn') |
|
44 | .addClass('btn') | |
45 | .addClass('dropdown-toggle') |
|
45 | .addClass('dropdown-toggle') | |
46 | .addClass('widget-combo-carrot-btn') |
|
46 | .addClass('widget-combo-carrot-btn') | |
47 | .attr('data-toggle', 'dropdown') |
|
47 | .attr('data-toggle', 'dropdown') | |
48 | .html('<span class="caret"></span>') |
|
48 | .html('<span class="caret"></span>') | |
49 | .appendTo(this.$buttongroup); |
|
49 | .appendTo(this.$buttongroup); | |
50 | this.$droplist = $('<ul />') |
|
50 | this.$droplist = $('<ul />') | |
51 | .addClass('dropdown-menu') |
|
51 | .addClass('dropdown-menu') | |
52 | .appendTo(this.$buttongroup); |
|
52 | .appendTo(this.$buttongroup); | |
53 |
|
53 | |||
54 | // Set defaults. |
|
54 | // Set defaults. | |
55 | this.update(); |
|
55 | this.update(); | |
56 | }, |
|
56 | }, | |
57 |
|
57 | |||
58 | // Handles: Backend -> Frontend Sync |
|
58 | // Handles: Backend -> Frontend Sync | |
59 | // Frontent -> Frontend Sync |
|
59 | // Frontent -> Frontend Sync | |
60 | update : function(){ |
|
60 | update : function(){ | |
61 |
|
61 | |||
62 | var selected_item_text = this.model.get('value'); |
|
62 | var selected_item_text = this.model.get('value'); | |
63 | selected_item_text = selected_item_text.replace(/ /g, ' '); |
|
63 | selected_item_text = selected_item_text.replace(/ /g, ' '); | |
64 | selected_item_text = selected_item_text.replace(/\n/g, '<br>\n'); |
|
64 | selected_item_text = selected_item_text.replace(/\n/g, '<br>\n'); | |
65 | if (selected_item_text.length === 0) { |
|
65 | if (selected_item_text.length === 0) { | |
66 | this.$droplabel.html(' '); |
|
66 | this.$droplabel.html(' '); | |
67 | } else { |
|
67 | } else { | |
68 | this.$droplabel.html(selected_item_text); |
|
68 | this.$droplabel.html(selected_item_text); | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | var items = this.model.get('values'); |
|
71 | var items = this.model.get('values'); | |
72 | this.$droplist.html(''); |
|
72 | this.$droplist.html(''); | |
73 | for (var index in items) { |
|
73 | for (var index in items) { | |
74 | var that = this; |
|
74 | var that = this; | |
75 | var item_button = $('<a href="#"/>') |
|
75 | var item_button = $('<a href="#"/>') | |
76 | .html(items[index]) |
|
76 | .html(items[index]) | |
77 | .on('click', $.proxy(this.handle_click, this)); |
|
77 | .on('click', $.proxy(this.handle_click, this)); | |
78 | this.$droplist.append($('<li />').append(item_button)); |
|
78 | this.$droplist.append($('<li />').append(item_button)); | |
79 | } |
|
79 | } | |
80 |
|
80 | |||
81 | if (this.model.get('disabled')) { |
|
81 | if (this.model.get('disabled')) { | |
82 | this.$buttongroup.attr('disabled','disabled'); |
|
82 | this.$buttongroup.attr('disabled','disabled'); | |
83 | this.$droplabel.attr('disabled','disabled'); |
|
83 | this.$droplabel.attr('disabled','disabled'); | |
84 | this.$dropbutton.attr('disabled','disabled'); |
|
84 | this.$dropbutton.attr('disabled','disabled'); | |
85 | this.$droplist.attr('disabled','disabled'); |
|
85 | this.$droplist.attr('disabled','disabled'); | |
86 | } else { |
|
86 | } else { | |
87 | this.$buttongroup.removeAttr('disabled'); |
|
87 | this.$buttongroup.removeAttr('disabled'); | |
88 | this.$droplabel.removeAttr('disabled'); |
|
88 | this.$droplabel.removeAttr('disabled'); | |
89 | this.$dropbutton.removeAttr('disabled'); |
|
89 | this.$dropbutton.removeAttr('disabled'); | |
90 | this.$droplist.removeAttr('disabled'); |
|
90 | this.$droplist.removeAttr('disabled'); | |
91 | } |
|
91 | } | |
92 |
|
92 | |||
93 | var description = this.model.get('description'); |
|
93 | var description = this.model.get('description'); | |
94 | if (description.length === 0) { |
|
94 | if (description.length === 0) { | |
95 | this.$label.hide(); |
|
95 | this.$label.hide(); | |
96 | } else { |
|
96 | } else { | |
97 | this.$label.html(description); |
|
97 | this.$label.html(description); | |
98 | this.$label.show(); |
|
98 | this.$label.show(); | |
99 | } |
|
99 | } | |
100 | return IPython.WidgetView.prototype.update.call(this); |
|
100 | return IPython.WidgetView.prototype.update.call(this); | |
101 | }, |
|
101 | }, | |
102 |
|
102 | |||
103 | // Handle when a value is clicked. |
|
103 | // Handle when a value is clicked. | |
104 | handle_click: function (e) { |
|
104 | handle_click: function (e) { | |
105 | this.model.set('value', $(e.target).html(), this); |
|
105 | this.model.set('value', $(e.target).html(), this); | |
106 | this.touch(); |
|
106 | this.touch(); | |
107 | }, |
|
107 | }, | |
108 |
|
108 | |||
109 | }); |
|
109 | }); | |
110 |
|
110 | |||
111 | widget_manager.register_widget_view('DropdownView', DropdownView); |
|
111 | widget_manager.register_widget_view('DropdownView', DropdownView); | |
112 |
|
112 | |||
113 | var RadioButtonsView = IPython.WidgetView.extend({ |
|
113 | var RadioButtonsView = IPython.WidgetView.extend({ | |
114 |
|
114 | |||
115 | // Called when view is rendered. |
|
115 | // Called when view is rendered. | |
116 | render : function(){ |
|
116 | render : function(){ | |
117 | this.$el |
|
117 | this.$el | |
118 | .addClass('widget-hbox') |
|
118 | .addClass('widget-hbox') | |
119 | .html(''); |
|
119 | .html(''); | |
120 | this.$label = $('<div />') |
|
120 | this.$label = $('<div />') | |
121 | .appendTo(this.$el) |
|
121 | .appendTo(this.$el) | |
122 | .addClass('widget-hlabel') |
|
122 | .addClass('widget-hlabel') | |
123 | .hide(); |
|
123 | .hide(); | |
124 | this.$container = $('<div />') |
|
124 | this.$container = $('<div />') | |
125 | .appendTo(this.$el) |
|
125 | .appendTo(this.$el) | |
126 | .addClass('widget-container') |
|
126 | .addClass('widget-container') | |
127 | .addClass('vbox'); |
|
127 | .addClass('vbox'); | |
128 | this.$el_to_style = this.$container; // Set default element to style |
|
128 | this.$el_to_style = this.$container; // Set default element to style | |
129 | this.update(); |
|
129 | this.update(); | |
130 | }, |
|
130 | }, | |
131 |
|
131 | |||
132 | // Handles: Backend -> Frontend Sync |
|
132 | // Handles: Backend -> Frontend Sync | |
133 | // Frontent -> Frontend Sync |
|
133 | // Frontent -> Frontend Sync | |
134 | update : function(){ |
|
134 | update : function(){ | |
135 |
|
135 | |||
136 | // Add missing items to the DOM. |
|
136 | // Add missing items to the DOM. | |
137 | var items = this.model.get('values'); |
|
137 | var items = this.model.get('values'); | |
138 | var disabled = this.model.get('disabled'); |
|
138 | var disabled = this.model.get('disabled'); | |
139 | for (var index in items) { |
|
139 | for (var index in items) { | |
140 | var item_query = ' :input[value="' + items[index] + '"]'; |
|
140 | var item_query = ' :input[value="' + items[index] + '"]'; | |
141 | if (this.$el.find(item_query).length === 0) { |
|
141 | if (this.$el.find(item_query).length === 0) { | |
142 | var $label = $('<label />') |
|
142 | var $label = $('<label />') | |
143 | .addClass('radio') |
|
143 | .addClass('radio') | |
144 | .html(items[index]) |
|
144 | .html(items[index]) | |
145 | .appendTo(this.$container); |
|
145 | .appendTo(this.$container); | |
146 |
|
146 | |||
147 | $('<input />') |
|
147 | $('<input />') | |
148 | .attr('type', 'radio') |
|
148 | .attr('type', 'radio') | |
149 | .addClass(this.model) |
|
149 | .addClass(this.model) | |
150 | .val(items[index]) |
|
150 | .val(items[index]) | |
151 | .prependTo($label) |
|
151 | .prependTo($label) | |
152 | .on('click', $.proxy(this.handle_click, this)); |
|
152 | .on('click', $.proxy(this.handle_click, this)); | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | var $item_element = this.$container.find(item_query); |
|
155 | var $item_element = this.$container.find(item_query); | |
156 | if (this.model.get('value') == items[index]) { |
|
156 | if (this.model.get('value') == items[index]) { | |
157 | $item_element.prop('checked', true); |
|
157 | $item_element.prop('checked', true); | |
158 | } else { |
|
158 | } else { | |
159 | $item_element.prop('checked', false); |
|
159 | $item_element.prop('checked', false); | |
160 | } |
|
160 | } | |
161 | $item_element.prop('disabled', disabled); |
|
161 | $item_element.prop('disabled', disabled); | |
162 | } |
|
162 | } | |
163 |
|
163 | |||
164 | // Remove items that no longer exist. |
|
164 | // Remove items that no longer exist. | |
165 | this.$container.find('input').each(function(i, obj) { |
|
165 | this.$container.find('input').each(function(i, obj) { | |
166 | var value = $(obj).val(); |
|
166 | var value = $(obj).val(); | |
167 | var found = false; |
|
167 | var found = false; | |
168 | for (var index in items) { |
|
168 | for (var index in items) { | |
169 | if (items[index] == value) { |
|
169 | if (items[index] == value) { | |
170 | found = true; |
|
170 | found = true; | |
171 | break; |
|
171 | break; | |
172 | } |
|
172 | } | |
173 | } |
|
173 | } | |
174 |
|
174 | |||
175 | if (!found) { |
|
175 | if (!found) { | |
176 | $(obj).parent().remove(); |
|
176 | $(obj).parent().remove(); | |
177 | } |
|
177 | } | |
178 | }); |
|
178 | }); | |
179 |
|
179 | |||
180 | var description = this.model.get('description'); |
|
180 | var description = this.model.get('description'); | |
181 | if (description.length === 0) { |
|
181 | if (description.length === 0) { | |
182 | this.$label.hide(); |
|
182 | this.$label.hide(); | |
183 | } else { |
|
183 | } else { | |
184 | this.$label.html(description); |
|
184 | this.$label.html(description); | |
185 | this.$label.show(); |
|
185 | this.$label.show(); | |
186 | } |
|
186 | } | |
187 | return IPython.WidgetView.prototype.update.call(this); |
|
187 | return IPython.WidgetView.prototype.update.call(this); | |
188 | }, |
|
188 | }, | |
189 |
|
189 | |||
190 | // Handle when a value is clicked. |
|
190 | // Handle when a value is clicked. | |
191 | handle_click: function (e) { |
|
191 | handle_click: function (e) { | |
192 | this.model.set('value', $(e.target).val(), this); |
|
192 | this.model.set('value', $(e.target).val(), this); | |
193 | this.touch(); |
|
193 | this.touch(); | |
194 | }, |
|
194 | }, | |
195 | }); |
|
195 | }); | |
196 |
|
196 | |||
197 | widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView); |
|
197 | widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView); | |
198 |
|
198 | |||
199 |
|
199 | |||
200 | var ToggleButtonsView = IPython.WidgetView.extend({ |
|
200 | var ToggleButtonsView = IPython.WidgetView.extend({ | |
201 |
|
201 | |||
202 | // Called when view is rendered. |
|
202 | // Called when view is rendered. | |
203 | render : function(){ |
|
203 | render : function(){ | |
204 | this.$el |
|
204 | this.$el | |
205 | .addClass('widget-hbox-single') |
|
205 | .addClass('widget-hbox-single') | |
206 | .html(''); |
|
206 | .html(''); | |
207 | this.$label = $('<div />') |
|
207 | this.$label = $('<div />') | |
208 | .appendTo(this.$el) |
|
208 | .appendTo(this.$el) | |
209 | .addClass('widget-hlabel') |
|
209 | .addClass('widget-hlabel') | |
210 | .hide(); |
|
210 | .hide(); | |
211 | this.$buttongroup = $('<div />') |
|
211 | this.$buttongroup = $('<div />') | |
212 | .addClass('btn-group') |
|
212 | .addClass('btn-group') | |
213 | .attr('data-toggle', 'buttons-radio') |
|
213 | .attr('data-toggle', 'buttons-radio') | |
214 | .appendTo(this.$el); |
|
214 | .appendTo(this.$el); | |
215 | this.$el_to_style = this.$buttongroup; // Set default element to style |
|
215 | this.$el_to_style = this.$buttongroup; // Set default element to style | |
216 | this.update(); |
|
216 | this.update(); | |
217 | }, |
|
217 | }, | |
218 |
|
218 | |||
219 | // Handles: Backend -> Frontend Sync |
|
219 | // Handles: Backend -> Frontend Sync | |
220 | // Frontent -> Frontend Sync |
|
220 | // Frontent -> Frontend Sync | |
221 | update : function(){ |
|
221 | update : function(){ | |
222 |
|
222 | |||
223 | // Add missing items to the DOM. |
|
223 | // Add missing items to the DOM. | |
224 | var items = this.model.get('values'); |
|
224 | var items = this.model.get('values'); | |
225 | var disabled = this.model.get('disabled'); |
|
225 | var disabled = this.model.get('disabled'); | |
226 | for (var index in items) { |
|
226 | for (var index in items) { | |
227 | var item_query = ' :contains("' + items[index] + '")'; |
|
227 | var item_query = ' :contains("' + items[index] + '")'; | |
228 | if (this.$buttongroup.find(item_query).length === 0) { |
|
228 | if (this.$buttongroup.find(item_query).length === 0) { | |
229 | $('<button />') |
|
229 | $('<button />') | |
230 | .attr('type', 'button') |
|
230 | .attr('type', 'button') | |
231 | .addClass('btn') |
|
231 | .addClass('btn') | |
232 | .html(items[index]) |
|
232 | .html(items[index]) | |
233 | .appendTo(this.$buttongroup) |
|
233 | .appendTo(this.$buttongroup) | |
234 | .on('click', $.proxy(this.handle_click, this)); |
|
234 | .on('click', $.proxy(this.handle_click, this)); | |
235 | } |
|
235 | } | |
236 |
|
236 | |||
237 | var $item_element = this.$buttongroup.find(item_query); |
|
237 | var $item_element = this.$buttongroup.find(item_query); | |
238 | if (this.model.get('value') == items[index]) { |
|
238 | if (this.model.get('value') == items[index]) { | |
239 | $item_element.addClass('active'); |
|
239 | $item_element.addClass('active'); | |
240 | } else { |
|
240 | } else { | |
241 | $item_element.removeClass('active'); |
|
241 | $item_element.removeClass('active'); | |
242 | } |
|
242 | } | |
243 | $item_element.prop('disabled', disabled); |
|
243 | $item_element.prop('disabled', disabled); | |
244 | } |
|
244 | } | |
245 |
|
245 | |||
246 | // Remove items that no longer exist. |
|
246 | // Remove items that no longer exist. | |
247 | this.$buttongroup.find('button').each(function(i, obj) { |
|
247 | this.$buttongroup.find('button').each(function(i, obj) { | |
248 | var value = $(obj).html(); |
|
248 | var value = $(obj).html(); | |
249 | var found = false; |
|
249 | var found = false; | |
250 | for (var index in items) { |
|
250 | for (var index in items) { | |
251 | if (items[index] == value) { |
|
251 | if (items[index] == value) { | |
252 | found = true; |
|
252 | found = true; | |
253 | break; |
|
253 | break; | |
254 | } |
|
254 | } | |
255 | } |
|
255 | } | |
256 |
|
256 | |||
257 | if (!found) { |
|
257 | if (!found) { | |
258 | $(obj).remove(); |
|
258 | $(obj).remove(); | |
259 | } |
|
259 | } | |
260 | }); |
|
260 | }); | |
261 |
|
261 | |||
262 | var description = this.model.get('description'); |
|
262 | var description = this.model.get('description'); | |
263 | if (description.length === 0) { |
|
263 | if (description.length === 0) { | |
264 | this.$label.hide(); |
|
264 | this.$label.hide(); | |
265 | } else { |
|
265 | } else { | |
266 | this.$label.html(description); |
|
266 | this.$label.html(description); | |
267 | this.$label.show(); |
|
267 | this.$label.show(); | |
268 | } |
|
268 | } | |
269 | return IPython.WidgetView.prototype.update.call(this); |
|
269 | return IPython.WidgetView.prototype.update.call(this); | |
270 | }, |
|
270 | }, | |
271 |
|
271 | |||
272 | // Handle when a value is clicked. |
|
272 | // Handle when a value is clicked. | |
273 | handle_click: function (e) { |
|
273 | handle_click: function (e) { | |
274 | this.model.set('value', $(e.target).html(), this); |
|
274 | this.model.set('value', $(e.target).html(), this); | |
275 | this.touch(); |
|
275 | this.touch(); | |
276 | }, |
|
276 | }, | |
277 |
|
277 | |||
278 | }); |
|
278 | }); | |
279 |
|
279 | |||
280 | widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView); |
|
280 | widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView); | |
281 |
|
281 | |||
282 | var ListBoxView = IPython.WidgetView.extend({ |
|
282 | var ListBoxView = IPython.WidgetView.extend({ | |
283 |
|
283 | |||
284 | // Called when view is rendered. |
|
284 | // Called when view is rendered. | |
285 | render : function(){ |
|
285 | render : function(){ | |
286 | this.$el |
|
286 | this.$el | |
287 | .addClass('widget-hbox') |
|
287 | .addClass('widget-hbox') | |
288 | .html(''); |
|
288 | .html(''); | |
289 | this.$label = $('<div />') |
|
289 | this.$label = $('<div />') | |
290 | .appendTo(this.$el) |
|
290 | .appendTo(this.$el) | |
291 | .addClass('widget-hlabel') |
|
291 | .addClass('widget-hlabel') | |
292 | .hide(); |
|
292 | .hide(); | |
293 | this.$listbox = $('<select />') |
|
293 | this.$listbox = $('<select />') | |
294 | .addClass('widget-listbox') |
|
294 | .addClass('widget-listbox') | |
295 | .attr('size', 6) |
|
295 | .attr('size', 6) | |
296 | .appendTo(this.$el); |
|
296 | .appendTo(this.$el); | |
297 | this.$el_to_style = this.$listbox; // Set default element to style |
|
297 | this.$el_to_style = this.$listbox; // Set default element to style | |
298 | this.update(); |
|
298 | this.update(); | |
299 | }, |
|
299 | }, | |
300 |
|
300 | |||
301 | // Handles: Backend -> Frontend Sync |
|
301 | // Handles: Backend -> Frontend Sync | |
302 | // Frontent -> Frontend Sync |
|
302 | // Frontent -> Frontend Sync | |
303 | update : function(){ |
|
303 | update : function(){ | |
304 |
|
304 | |||
305 | // Add missing items to the DOM. |
|
305 | // Add missing items to the DOM. | |
306 | var items = this.model.get('values'); |
|
306 | var items = this.model.get('values'); | |
307 | for (var index in items) { |
|
307 | for (var index in items) { | |
308 | var item_query = ' :contains("' + items[index] + '")'; |
|
308 | var item_query = ' :contains("' + items[index] + '")'; | |
309 | if (this.$listbox.find(item_query).length === 0) { |
|
309 | if (this.$listbox.find(item_query).length === 0) { | |
310 | $('<option />') |
|
310 | $('<option />') | |
311 | .html(items[index]) |
|
311 | .html(items[index]) | |
312 | .attr('value', items[index]) |
|
312 | .attr('value', items[index]) | |
313 | .appendTo(this.$listbox) |
|
313 | .appendTo(this.$listbox) | |
314 | .on('click', $.proxy(this.handle_click, this)); |
|
314 | .on('click', $.proxy(this.handle_click, this)); | |
315 | } |
|
315 | } | |
316 | } |
|
316 | } | |
317 |
|
317 | |||
318 | // Select the correct element |
|
318 | // Select the correct element | |
319 | this.$listbox.val(this.model.get('value')); |
|
319 | this.$listbox.val(this.model.get('value')); | |
320 |
|
320 | |||
321 | // Disable listbox if needed |
|
321 | // Disable listbox if needed | |
322 | var disabled = this.model.get('disabled'); |
|
322 | var disabled = this.model.get('disabled'); | |
323 | this.$listbox.prop('disabled', disabled); |
|
323 | this.$listbox.prop('disabled', disabled); | |
324 |
|
324 | |||
325 | // Remove items that no longer exist. |
|
325 | // Remove items that no longer exist. | |
326 | this.$listbox.find('option').each(function(i, obj) { |
|
326 | this.$listbox.find('option').each(function(i, obj) { | |
327 | var value = $(obj).html(); |
|
327 | var value = $(obj).html(); | |
328 | var found = false; |
|
328 | var found = false; | |
329 | for (var index in items) { |
|
329 | for (var index in items) { | |
330 | if (items[index] == value) { |
|
330 | if (items[index] == value) { | |
331 | found = true; |
|
331 | found = true; | |
332 | break; |
|
332 | break; | |
333 | } |
|
333 | } | |
334 | } |
|
334 | } | |
335 |
|
335 | |||
336 | if (!found) { |
|
336 | if (!found) { | |
337 | $(obj).remove(); |
|
337 | $(obj).remove(); | |
338 | } |
|
338 | } | |
339 | }); |
|
339 | }); | |
340 |
|
340 | |||
341 | var description = this.model.get('description'); |
|
341 | var description = this.model.get('description'); | |
342 | if (description.length === 0) { |
|
342 | if (description.length === 0) { | |
343 | this.$label.hide(); |
|
343 | this.$label.hide(); | |
344 | } else { |
|
344 | } else { | |
345 | this.$label.html(description); |
|
345 | this.$label.html(description); | |
346 | this.$label.show(); |
|
346 | this.$label.show(); | |
347 | } |
|
347 | } | |
348 | return IPython.WidgetView.prototype.update.call(this); |
|
348 | return IPython.WidgetView.prototype.update.call(this); | |
349 | }, |
|
349 | }, | |
350 |
|
350 | |||
351 | // Handle when a value is clicked. |
|
351 | // Handle when a value is clicked. | |
352 | handle_click: function (e) { |
|
352 | handle_click: function (e) { | |
353 | this.model.set('value', $(e.target).html(), this); |
|
353 | this.model.set('value', $(e.target).html(), this); | |
354 | this.touch(); |
|
354 | this.touch(); | |
355 | }, |
|
355 | }, | |
356 |
|
356 | |||
357 | }); |
|
357 | }); | |
358 |
|
358 | |||
359 | widget_manager.register_widget_view('ListBoxView', ListBoxView); |
|
359 | widget_manager.register_widget_view('ListBoxView', ListBoxView); | |
360 | }); |
|
360 | }); |
@@ -1,188 +1,188 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2013 The IPython Development Team |
|
2 | // Copyright (C) 2013 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // StringWidget |
|
9 | // StringWidget | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | **/ |
|
15 | **/ | |
16 |
|
16 | |||
17 |
define(["notebook/js/widgets/ |
|
17 | define(["notebook/js/widgets/widget"], function(widget_manager){ | |
18 | var StringWidgetModel = IPython.WidgetModel.extend({}); |
|
18 | var StringWidgetModel = IPython.WidgetModel.extend({}); | |
19 | widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel); |
|
19 | widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel); | |
20 |
|
20 | |||
21 | var HTMLView = IPython.WidgetView.extend({ |
|
21 | var HTMLView = IPython.WidgetView.extend({ | |
22 |
|
22 | |||
23 | // Called when view is rendered. |
|
23 | // Called when view is rendered. | |
24 | render : function(){ |
|
24 | render : function(){ | |
25 | this.update(); // Set defaults. |
|
25 | this.update(); // Set defaults. | |
26 | }, |
|
26 | }, | |
27 |
|
27 | |||
28 | // Handles: Backend -> Frontend Sync |
|
28 | // Handles: Backend -> Frontend Sync | |
29 | // Frontent -> Frontend Sync |
|
29 | // Frontent -> Frontend Sync | |
30 | update : function(){ |
|
30 | update : function(){ | |
31 | this.$el.html(this.model.get('value')); |
|
31 | this.$el.html(this.model.get('value')); | |
32 | return IPython.WidgetView.prototype.update.call(this); |
|
32 | return IPython.WidgetView.prototype.update.call(this); | |
33 | }, |
|
33 | }, | |
34 |
|
34 | |||
35 | }); |
|
35 | }); | |
36 |
|
36 | |||
37 | widget_manager.register_widget_view('HTMLView', HTMLView); |
|
37 | widget_manager.register_widget_view('HTMLView', HTMLView); | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | var LatexView = IPython.WidgetView.extend({ |
|
40 | var LatexView = IPython.WidgetView.extend({ | |
41 |
|
41 | |||
42 | // Called when view is rendered. |
|
42 | // Called when view is rendered. | |
43 | render : function(){ |
|
43 | render : function(){ | |
44 | this.update(); // Set defaults. |
|
44 | this.update(); // Set defaults. | |
45 | }, |
|
45 | }, | |
46 |
|
46 | |||
47 | // Handles: Backend -> Frontend Sync |
|
47 | // Handles: Backend -> Frontend Sync | |
48 | // Frontent -> Frontend Sync |
|
48 | // Frontent -> Frontend Sync | |
49 | update : function(){ |
|
49 | update : function(){ | |
50 | this.$el.html(this.model.get('value')); |
|
50 | this.$el.html(this.model.get('value')); | |
51 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]); |
|
51 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]); | |
52 |
|
52 | |||
53 | return IPython.WidgetView.prototype.update.call(this); |
|
53 | return IPython.WidgetView.prototype.update.call(this); | |
54 | }, |
|
54 | }, | |
55 |
|
55 | |||
56 | }); |
|
56 | }); | |
57 |
|
57 | |||
58 | widget_manager.register_widget_view('LatexView', LatexView); |
|
58 | widget_manager.register_widget_view('LatexView', LatexView); | |
59 |
|
59 | |||
60 | var TextAreaView = IPython.WidgetView.extend({ |
|
60 | var TextAreaView = IPython.WidgetView.extend({ | |
61 |
|
61 | |||
62 | // Called when view is rendered. |
|
62 | // Called when view is rendered. | |
63 | render: function(){ |
|
63 | render: function(){ | |
64 | this.$el |
|
64 | this.$el | |
65 | .addClass('widget-hbox') |
|
65 | .addClass('widget-hbox') | |
66 | .html(''); |
|
66 | .html(''); | |
67 | this.$label = $('<div />') |
|
67 | this.$label = $('<div />') | |
68 | .appendTo(this.$el) |
|
68 | .appendTo(this.$el) | |
69 | .addClass('widget-hlabel') |
|
69 | .addClass('widget-hlabel') | |
70 | .hide(); |
|
70 | .hide(); | |
71 | this.$textbox = $('<textarea />') |
|
71 | this.$textbox = $('<textarea />') | |
72 | .attr('rows', 5) |
|
72 | .attr('rows', 5) | |
73 | .addClass('widget-text') |
|
73 | .addClass('widget-text') | |
74 | .appendTo(this.$el); |
|
74 | .appendTo(this.$el); | |
75 | this.$el_to_style = this.$textbox; // Set default element to style |
|
75 | this.$el_to_style = this.$textbox; // Set default element to style | |
76 | this.update(); // Set defaults. |
|
76 | this.update(); // Set defaults. | |
77 |
|
77 | |||
78 | this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this)); |
|
78 | this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this)); | |
79 | }, |
|
79 | }, | |
80 |
|
80 | |||
81 |
|
81 | |||
82 | _handle_textarea_msg: function (content){ |
|
82 | _handle_textarea_msg: function (content){ | |
83 | if (content.method == "scroll_to_bottom") { |
|
83 | if (content.method == "scroll_to_bottom") { | |
84 | this.scroll_to_bottom(); |
|
84 | this.scroll_to_bottom(); | |
85 | } |
|
85 | } | |
86 | }, |
|
86 | }, | |
87 |
|
87 | |||
88 |
|
88 | |||
89 | scroll_to_bottom: function (){ |
|
89 | scroll_to_bottom: function (){ | |
90 | this.$textbox.scrollTop(this.$textbox[0].scrollHeight); |
|
90 | this.$textbox.scrollTop(this.$textbox[0].scrollHeight); | |
91 | }, |
|
91 | }, | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | // Handles: Backend -> Frontend Sync |
|
94 | // Handles: Backend -> Frontend Sync | |
95 | // Frontent -> Frontend Sync |
|
95 | // Frontent -> Frontend Sync | |
96 | update: function(){ |
|
96 | update: function(){ | |
97 | if (!this.user_invoked_update) { |
|
97 | if (!this.user_invoked_update) { | |
98 | this.$textbox.val(this.model.get('value')); |
|
98 | this.$textbox.val(this.model.get('value')); | |
99 | } |
|
99 | } | |
100 |
|
100 | |||
101 | var disabled = this.model.get('disabled'); |
|
101 | var disabled = this.model.get('disabled'); | |
102 | this.$textbox.prop('disabled', disabled); |
|
102 | this.$textbox.prop('disabled', disabled); | |
103 |
|
103 | |||
104 | var description = this.model.get('description'); |
|
104 | var description = this.model.get('description'); | |
105 | if (description.length === 0) { |
|
105 | if (description.length === 0) { | |
106 | this.$label.hide(); |
|
106 | this.$label.hide(); | |
107 | } else { |
|
107 | } else { | |
108 | this.$label.html(description); |
|
108 | this.$label.html(description); | |
109 | this.$label.show(); |
|
109 | this.$label.show(); | |
110 | } |
|
110 | } | |
111 | return IPython.WidgetView.prototype.update.call(this); |
|
111 | return IPython.WidgetView.prototype.update.call(this); | |
112 | }, |
|
112 | }, | |
113 |
|
113 | |||
114 | events: {"keyup textarea": "handleChanging", |
|
114 | events: {"keyup textarea": "handleChanging", | |
115 | "paste textarea": "handleChanging", |
|
115 | "paste textarea": "handleChanging", | |
116 | "cut textarea": "handleChanging"}, |
|
116 | "cut textarea": "handleChanging"}, | |
117 |
|
117 | |||
118 | // Handles and validates user input. |
|
118 | // Handles and validates user input. | |
119 | handleChanging: function(e) { |
|
119 | handleChanging: function(e) { | |
120 | this.user_invoked_update = true; |
|
120 | this.user_invoked_update = true; | |
121 | this.model.set('value', e.target.value); |
|
121 | this.model.set('value', e.target.value); | |
122 | this.touch(); |
|
122 | this.touch(); | |
123 | this.user_invoked_update = false; |
|
123 | this.user_invoked_update = false; | |
124 | }, |
|
124 | }, | |
125 | }); |
|
125 | }); | |
126 |
|
126 | |||
127 | widget_manager.register_widget_view('TextAreaView', TextAreaView); |
|
127 | widget_manager.register_widget_view('TextAreaView', TextAreaView); | |
128 |
|
128 | |||
129 | var TextBoxView = IPython.WidgetView.extend({ |
|
129 | var TextBoxView = IPython.WidgetView.extend({ | |
130 |
|
130 | |||
131 | // Called when view is rendered. |
|
131 | // Called when view is rendered. | |
132 | render: function(){ |
|
132 | render: function(){ | |
133 | this.$el |
|
133 | this.$el | |
134 | .addClass('widget-hbox-single') |
|
134 | .addClass('widget-hbox-single') | |
135 | .html(''); |
|
135 | .html(''); | |
136 | this.$label = $('<div />') |
|
136 | this.$label = $('<div />') | |
137 | .addClass('widget-hlabel') |
|
137 | .addClass('widget-hlabel') | |
138 | .appendTo(this.$el) |
|
138 | .appendTo(this.$el) | |
139 | .hide(); |
|
139 | .hide(); | |
140 | this.$textbox = $('<input type="text" />') |
|
140 | this.$textbox = $('<input type="text" />') | |
141 | .addClass('input') |
|
141 | .addClass('input') | |
142 | .addClass('widget-text') |
|
142 | .addClass('widget-text') | |
143 | .appendTo(this.$el); |
|
143 | .appendTo(this.$el); | |
144 | this.$el_to_style = this.$textbox; // Set default element to style |
|
144 | this.$el_to_style = this.$textbox; // Set default element to style | |
145 | this.update(); // Set defaults. |
|
145 | this.update(); // Set defaults. | |
146 | }, |
|
146 | }, | |
147 |
|
147 | |||
148 | // Handles: Backend -> Frontend Sync |
|
148 | // Handles: Backend -> Frontend Sync | |
149 | // Frontent -> Frontend Sync |
|
149 | // Frontent -> Frontend Sync | |
150 | update: function(){ |
|
150 | update: function(){ | |
151 | if (this.$textbox.val() != this.model.get('value')) { |
|
151 | if (this.$textbox.val() != this.model.get('value')) { | |
152 | this.$textbox.val(this.model.get('value')); |
|
152 | this.$textbox.val(this.model.get('value')); | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | var disabled = this.model.get('disabled'); |
|
155 | var disabled = this.model.get('disabled'); | |
156 | this.$textbox.prop('disabled', disabled); |
|
156 | this.$textbox.prop('disabled', disabled); | |
157 |
|
157 | |||
158 | var description = this.model.get('description'); |
|
158 | var description = this.model.get('description'); | |
159 | if (description.length === 0) { |
|
159 | if (description.length === 0) { | |
160 | this.$label.hide(); |
|
160 | this.$label.hide(); | |
161 | } else { |
|
161 | } else { | |
162 | this.$label.html(description); |
|
162 | this.$label.html(description); | |
163 | this.$label.show(); |
|
163 | this.$label.show(); | |
164 | } |
|
164 | } | |
165 | return IPython.WidgetView.prototype.update.call(this); |
|
165 | return IPython.WidgetView.prototype.update.call(this); | |
166 | }, |
|
166 | }, | |
167 |
|
167 | |||
168 | events: {"keyup input": "handleChanging", |
|
168 | events: {"keyup input": "handleChanging", | |
169 | "paste input": "handleChanging", |
|
169 | "paste input": "handleChanging", | |
170 | "cut input": "handleChanging", |
|
170 | "cut input": "handleChanging", | |
171 | "keypress input": "handleKeypress"}, |
|
171 | "keypress input": "handleKeypress"}, | |
172 |
|
172 | |||
173 | // Handles and validates user input. |
|
173 | // Handles and validates user input. | |
174 | handleChanging: function(e) { |
|
174 | handleChanging: function(e) { | |
175 | this.model.set('value', e.target.value); |
|
175 | this.model.set('value', e.target.value); | |
176 | this.touch(); |
|
176 | this.touch(); | |
177 | }, |
|
177 | }, | |
178 |
|
178 | |||
179 | // Handles text submition |
|
179 | // Handles text submition | |
180 | handleKeypress: function(e) { |
|
180 | handleKeypress: function(e) { | |
181 | if (e.keyCode == 13) { // Return key |
|
181 | if (e.keyCode == 13) { // Return key | |
182 | this.send({event: 'submit'}); |
|
182 | this.send({event: 'submit'}); | |
183 | } |
|
183 | } | |
184 | }, |
|
184 | }, | |
185 | }); |
|
185 | }); | |
186 |
|
186 | |||
187 | widget_manager.register_widget_view('TextBoxView', TextBoxView); |
|
187 | widget_manager.register_widget_view('TextBoxView', TextBoxView); | |
188 | }); |
|
188 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now