Show More
@@ -1,190 +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/widget"], function(widget_manager){ |
|
17 | define(["notebook/js/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 | var that=this; |
|
|||
51 | this.$el.html(this.model.get('value')); |
|
50 | this.$el.html(this.model.get('value')); | |
52 | var math_el = that.$el.get(0); |
|
51 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]); | |
53 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,math_el]); |
|
|||
54 |
|
52 | |||
55 | return IPython.WidgetView.prototype.update.call(this); |
|
53 | return IPython.WidgetView.prototype.update.call(this); | |
56 | }, |
|
54 | }, | |
57 |
|
55 | |||
58 | }); |
|
56 | }); | |
59 |
|
57 | |||
60 | widget_manager.register_widget_view('LatexView', LatexView); |
|
58 | widget_manager.register_widget_view('LatexView', LatexView); | |
61 |
|
59 | |||
62 | var TextAreaView = IPython.WidgetView.extend({ |
|
60 | var TextAreaView = IPython.WidgetView.extend({ | |
63 |
|
61 | |||
64 | // Called when view is rendered. |
|
62 | // Called when view is rendered. | |
65 | render: function(){ |
|
63 | render: function(){ | |
66 | this.$el |
|
64 | this.$el | |
67 | .addClass('widget-hbox') |
|
65 | .addClass('widget-hbox') | |
68 | .html(''); |
|
66 | .html(''); | |
69 | this.$label = $('<div />') |
|
67 | this.$label = $('<div />') | |
70 | .appendTo(this.$el) |
|
68 | .appendTo(this.$el) | |
71 | .addClass('widget-hlabel') |
|
69 | .addClass('widget-hlabel') | |
72 | .hide(); |
|
70 | .hide(); | |
73 | this.$textbox = $('<textarea />') |
|
71 | this.$textbox = $('<textarea />') | |
74 | .attr('rows', 5) |
|
72 | .attr('rows', 5) | |
75 | .addClass('widget-text') |
|
73 | .addClass('widget-text') | |
76 | .appendTo(this.$el); |
|
74 | .appendTo(this.$el); | |
77 | this.$el_to_style = this.$textbox; // Set default element to style |
|
75 | this.$el_to_style = this.$textbox; // Set default element to style | |
78 | this.update(); // Set defaults. |
|
76 | this.update(); // Set defaults. | |
79 |
|
77 | |||
80 | this.model.on_msg($.proxy(this._handle_textarea_msg, this)); |
|
78 | this.model.on_msg($.proxy(this._handle_textarea_msg, this)); | |
81 | }, |
|
79 | }, | |
82 |
|
80 | |||
83 |
|
81 | |||
84 | _handle_textarea_msg: function (content){ |
|
82 | _handle_textarea_msg: function (content){ | |
85 | if (content.method == "scroll_to_bottom") { |
|
83 | if (content.method == "scroll_to_bottom") { | |
86 | this.scroll_to_bottom(); |
|
84 | this.scroll_to_bottom(); | |
87 | } |
|
85 | } | |
88 | }, |
|
86 | }, | |
89 |
|
87 | |||
90 |
|
88 | |||
91 | scroll_to_bottom: function (){ |
|
89 | scroll_to_bottom: function (){ | |
92 | this.$textbox.scrollTop(this.$textbox[0].scrollHeight); |
|
90 | this.$textbox.scrollTop(this.$textbox[0].scrollHeight); | |
93 | }, |
|
91 | }, | |
94 |
|
92 | |||
95 |
|
93 | |||
96 | // Handles: Backend -> Frontend Sync |
|
94 | // Handles: Backend -> Frontend Sync | |
97 | // Frontent -> Frontend Sync |
|
95 | // Frontent -> Frontend Sync | |
98 | update: function(){ |
|
96 | update: function(){ | |
99 | if (!this.user_invoked_update) { |
|
97 | if (!this.user_invoked_update) { | |
100 | this.$textbox.val(this.model.get('value')); |
|
98 | this.$textbox.val(this.model.get('value')); | |
101 | } |
|
99 | } | |
102 |
|
100 | |||
103 | var disabled = this.model.get('disabled'); |
|
101 | var disabled = this.model.get('disabled'); | |
104 | this.$textbox.prop('disabled', disabled); |
|
102 | this.$textbox.prop('disabled', disabled); | |
105 |
|
103 | |||
106 | var description = this.model.get('description'); |
|
104 | var description = this.model.get('description'); | |
107 | if (description.length == 0) { |
|
105 | if (description.length == 0) { | |
108 | this.$label.hide(); |
|
106 | this.$label.hide(); | |
109 | } else { |
|
107 | } else { | |
110 | this.$label.html(description); |
|
108 | this.$label.html(description); | |
111 | this.$label.show(); |
|
109 | this.$label.show(); | |
112 | } |
|
110 | } | |
113 | return IPython.WidgetView.prototype.update.call(this); |
|
111 | return IPython.WidgetView.prototype.update.call(this); | |
114 | }, |
|
112 | }, | |
115 |
|
113 | |||
116 | events: {"keyup textarea": "handleChanging", |
|
114 | events: {"keyup textarea": "handleChanging", | |
117 | "paste textarea": "handleChanging", |
|
115 | "paste textarea": "handleChanging", | |
118 | "cut textarea": "handleChanging"}, |
|
116 | "cut textarea": "handleChanging"}, | |
119 |
|
117 | |||
120 | // Handles and validates user input. |
|
118 | // Handles and validates user input. | |
121 | handleChanging: function(e) { |
|
119 | handleChanging: function(e) { | |
122 | this.user_invoked_update = true; |
|
120 | this.user_invoked_update = true; | |
123 | this.model.set('value', e.target.value); |
|
121 | this.model.set('value', e.target.value); | |
124 | this.model.update_other_views(this); |
|
122 | this.model.update_other_views(this); | |
125 | this.user_invoked_update = false; |
|
123 | this.user_invoked_update = false; | |
126 | }, |
|
124 | }, | |
127 | }); |
|
125 | }); | |
128 |
|
126 | |||
129 | widget_manager.register_widget_view('TextAreaView', TextAreaView); |
|
127 | widget_manager.register_widget_view('TextAreaView', TextAreaView); | |
130 |
|
128 | |||
131 | var TextBoxView = IPython.WidgetView.extend({ |
|
129 | var TextBoxView = IPython.WidgetView.extend({ | |
132 |
|
130 | |||
133 | // Called when view is rendered. |
|
131 | // Called when view is rendered. | |
134 | render: function(){ |
|
132 | render: function(){ | |
135 | this.$el |
|
133 | this.$el | |
136 | .addClass('widget-hbox-single') |
|
134 | .addClass('widget-hbox-single') | |
137 | .html(''); |
|
135 | .html(''); | |
138 | this.$label = $('<div />') |
|
136 | this.$label = $('<div />') | |
139 | .addClass('widget-hlabel') |
|
137 | .addClass('widget-hlabel') | |
140 | .appendTo(this.$el) |
|
138 | .appendTo(this.$el) | |
141 | .hide(); |
|
139 | .hide(); | |
142 | this.$textbox = $('<input type="text" />') |
|
140 | this.$textbox = $('<input type="text" />') | |
143 | .addClass('input') |
|
141 | .addClass('input') | |
144 | .addClass('widget-text') |
|
142 | .addClass('widget-text') | |
145 | .appendTo(this.$el); |
|
143 | .appendTo(this.$el); | |
146 | this.$el_to_style = this.$textbox; // Set default element to style |
|
144 | this.$el_to_style = this.$textbox; // Set default element to style | |
147 | this.update(); // Set defaults. |
|
145 | this.update(); // Set defaults. | |
148 | }, |
|
146 | }, | |
149 |
|
147 | |||
150 | // Handles: Backend -> Frontend Sync |
|
148 | // Handles: Backend -> Frontend Sync | |
151 | // Frontent -> Frontend Sync |
|
149 | // Frontent -> Frontend Sync | |
152 | update: function(){ |
|
150 | update: function(){ | |
153 | if (this.$textbox.val() != this.model.get('value')) { |
|
151 | if (this.$textbox.val() != this.model.get('value')) { | |
154 | this.$textbox.val(this.model.get('value')); |
|
152 | this.$textbox.val(this.model.get('value')); | |
155 | } |
|
153 | } | |
156 |
|
154 | |||
157 | var disabled = this.model.get('disabled'); |
|
155 | var disabled = this.model.get('disabled'); | |
158 | this.$textbox.prop('disabled', disabled); |
|
156 | this.$textbox.prop('disabled', disabled); | |
159 |
|
157 | |||
160 | var description = this.model.get('description'); |
|
158 | var description = this.model.get('description'); | |
161 | if (description.length == 0) { |
|
159 | if (description.length == 0) { | |
162 | this.$label.hide(); |
|
160 | this.$label.hide(); | |
163 | } else { |
|
161 | } else { | |
164 | this.$label.html(description); |
|
162 | this.$label.html(description); | |
165 | this.$label.show(); |
|
163 | this.$label.show(); | |
166 | } |
|
164 | } | |
167 | return IPython.WidgetView.prototype.update.call(this); |
|
165 | return IPython.WidgetView.prototype.update.call(this); | |
168 | }, |
|
166 | }, | |
169 |
|
167 | |||
170 | events: {"keyup input": "handleChanging", |
|
168 | events: {"keyup input": "handleChanging", | |
171 | "paste input": "handleChanging", |
|
169 | "paste input": "handleChanging", | |
172 | "cut input": "handleChanging", |
|
170 | "cut input": "handleChanging", | |
173 | "keypress input": "handleKeypress"}, |
|
171 | "keypress input": "handleKeypress"}, | |
174 |
|
172 | |||
175 | // Handles and validates user input. |
|
173 | // Handles and validates user input. | |
176 | handleChanging: function(e) { |
|
174 | handleChanging: function(e) { | |
177 | this.model.set('value', e.target.value); |
|
175 | this.model.set('value', e.target.value); | |
178 | this.model.update_other_views(this); |
|
176 | this.model.update_other_views(this); | |
179 | }, |
|
177 | }, | |
180 |
|
178 | |||
181 | // Handles text submition |
|
179 | // Handles text submition | |
182 | handleKeypress: function(e) { |
|
180 | handleKeypress: function(e) { | |
183 | if (e.keyCode == 13) { // Return key |
|
181 | if (e.keyCode == 13) { // Return key | |
184 | this.send({event: 'submit'}); |
|
182 | this.send({event: 'submit'}); | |
185 | } |
|
183 | } | |
186 | }, |
|
184 | }, | |
187 | }); |
|
185 | }); | |
188 |
|
186 | |||
189 | widget_manager.register_widget_view('TextBoxView', TextBoxView); |
|
187 | widget_manager.register_widget_view('TextBoxView', TextBoxView); | |
190 | }); |
|
188 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now