Show More
@@ -1,212 +1,216 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 | // WidgetModel, WidgetView, and WidgetManager |
|
9 | // WidgetModel, WidgetView, and WidgetManager | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * Base Widget classes |
|
12 | * Base Widget classes | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule widget |
|
15 | * @submodule widget | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 | (function () { |
|
18 | (function () { | |
19 | "use strict"; |
|
19 | "use strict"; | |
20 |
|
20 | |||
21 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
21 | // Use require.js 'define' method so that require.js is intelligent enough to | |
22 | // syncronously load everything within this file when it is being 'required' |
|
22 | // syncronously load everything within this file when it is being 'required' | |
23 | // elsewhere. |
|
23 | // elsewhere. | |
24 | define(["underscore", |
|
24 | define(["underscore", | |
25 | "backbone", |
|
25 | "backbone", | |
26 | ], function (Underscore, Backbone) { |
|
26 | ], function (Underscore, Backbone) { | |
27 |
|
27 | |||
28 | //-------------------------------------------------------------------- |
|
28 | //-------------------------------------------------------------------- | |
29 | // WidgetManager class |
|
29 | // WidgetManager class | |
30 | //-------------------------------------------------------------------- |
|
30 | //-------------------------------------------------------------------- | |
31 | var WidgetManager = function (comm_manager) { |
|
31 | var WidgetManager = function (comm_manager) { | |
32 | // Public constructor |
|
32 | // Public constructor | |
33 | WidgetManager._managers.push(this); |
|
33 | WidgetManager._managers.push(this); | |
34 |
|
34 | |||
35 | // Attach a comm manager to the |
|
35 | // Attach a comm manager to the | |
36 | this.comm_manager = comm_manager; |
|
36 | this.comm_manager = comm_manager; | |
37 | this._models = {}; /* Dictionary of model ids and model instances */ |
|
37 | this._models = {}; /* Dictionary of model ids and model instances */ | |
38 |
|
38 | |||
39 | // Register already-registered widget model types with the comm manager. |
|
39 | // Register already-registered widget model types with the comm manager. | |
40 | var that = this; |
|
40 | var that = this; | |
41 | _.each(WidgetManager._model_types, function(model_type, model_name) { |
|
41 | _.each(WidgetManager._model_types, function(model_type, model_name) { | |
42 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); |
|
42 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); | |
43 | }); |
|
43 | }); | |
44 | }; |
|
44 | }; | |
45 |
|
45 | |||
46 | //-------------------------------------------------------------------- |
|
46 | //-------------------------------------------------------------------- | |
47 | // Class level |
|
47 | // Class level | |
48 | //-------------------------------------------------------------------- |
|
48 | //-------------------------------------------------------------------- | |
49 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ |
|
49 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ | |
50 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ |
|
50 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ | |
51 | WidgetManager._managers = []; /* List of widget managers */ |
|
51 | WidgetManager._managers = []; /* List of widget managers */ | |
52 |
|
52 | |||
53 | WidgetManager.register_widget_model = function (model_name, model_type) { |
|
53 | WidgetManager.register_widget_model = function (model_name, model_type) { | |
54 | // Registers a widget model by name. |
|
54 | // Registers a widget model by name. | |
55 | WidgetManager._model_types[model_name] = model_type; |
|
55 | WidgetManager._model_types[model_name] = model_type; | |
56 |
|
56 | |||
57 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
57 | // Register the widget with the comm manager. Make sure to pass this object's context | |
58 | // in so `this` works in the call back. |
|
58 | // in so `this` works in the call back. | |
59 | _.each(WidgetManager._managers, function(instance, i) { |
|
59 | _.each(WidgetManager._managers, function(instance, i) { | |
60 | if (instance.comm_manager !== null) { |
|
60 | if (instance.comm_manager !== null) { | |
61 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); |
|
61 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); | |
62 | } |
|
62 | } | |
63 | }); |
|
63 | }); | |
64 | }; |
|
64 | }; | |
65 |
|
65 | |||
66 | WidgetManager.register_widget_view = function (view_name, view_type) { |
|
66 | WidgetManager.register_widget_view = function (view_name, view_type) { | |
67 | // Registers a widget view by name. |
|
67 | // Registers a widget view by name. | |
68 | WidgetManager._view_types[view_name] = view_type; |
|
68 | WidgetManager._view_types[view_name] = view_type; | |
69 | }; |
|
69 | }; | |
70 |
|
70 | |||
71 | //-------------------------------------------------------------------- |
|
71 | //-------------------------------------------------------------------- | |
72 | // Instance level |
|
72 | // Instance level | |
73 | //-------------------------------------------------------------------- |
|
73 | //-------------------------------------------------------------------- | |
74 | WidgetManager.prototype.display_view = function(msg, model) { |
|
74 | WidgetManager.prototype.display_view = function(msg, model) { | |
75 | // Displays a view for a particular model. |
|
75 | // Displays a view for a particular model. | |
76 | var cell = this.get_msg_cell(msg.parent_header.msg_id); |
|
76 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
77 | if (cell === null) { |
|
77 | if (cell === null) { | |
78 | console.log("Could not determine where the display" + |
|
78 | console.log("Could not determine where the display" + | |
79 | " message was from. Widget will not be displayed"); |
|
79 | " message was from. Widget will not be displayed"); | |
80 | } else { |
|
80 | } else { | |
81 | var view = this.create_view(model, {cell: cell}); |
|
81 | var view = this.create_view(model, {cell: cell}); | |
82 | if (view === null) { |
|
82 | if (view === null) { | |
83 | console.error("View creation failed", model); |
|
83 | console.error("View creation failed", model); | |
84 | } |
|
84 | } | |
85 | if (cell.widget_subarea !== undefined |
|
85 | if (cell.widget_subarea !== undefined | |
86 | && cell.widget_subarea !== null) { |
|
86 | && cell.widget_subarea !== null) { | |
87 |
|
87 | |||
88 | cell.widget_area.show(); |
|
88 | cell.widget_area.show(); | |
89 | cell.widget_subarea.append(view.$el); |
|
89 | cell.widget_subarea.append(view.$el); | |
90 | } |
|
90 | } | |
91 | } |
|
91 | } | |
92 | }; |
|
92 | }; | |
93 |
|
93 | |||
94 | WidgetManager.prototype.create_view = function(model, options, view) { |
|
94 | WidgetManager.prototype.create_view = function(model, options, view) { | |
95 | // Creates a view for a particular model. |
|
95 | // Creates a view for a particular model. | |
96 | var view_name = model.get('_view_name'); |
|
96 | var view_name = model.get('_view_name'); | |
97 | var ViewType = WidgetManager._view_types[view_name]; |
|
97 | var ViewType = WidgetManager._view_types[view_name]; | |
98 | if (ViewType !== undefined && ViewType !== null) { |
|
98 | if (ViewType !== undefined && ViewType !== null) { | |
99 |
|
99 | |||
100 | // If a view is passed into the method, use that view's cell as |
|
100 | // If a view is passed into the method, use that view's cell as | |
101 | // the cell for the view that is created. |
|
101 | // the cell for the view that is created. | |
102 | options = options || {}; |
|
102 | options = options || {}; | |
103 | if (view !== undefined) { |
|
103 | if (view !== undefined) { | |
104 | options.cell = view.options.cell; |
|
104 | options.cell = view.options.cell; | |
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 | // Create and render the view... |
|
107 | // Create and render the view... | |
108 | var parameters = {model: model, options: options}; |
|
108 | var parameters = {model: model, options: options}; | |
109 | var view = new ViewType(parameters); |
|
109 | var view = new ViewType(parameters); | |
110 | view.render(); |
|
110 | view.render(); | |
111 | model.views.push(view); |
|
111 | model.views.push(view); | |
112 | model.on('destroy', view.remove, view); |
|
112 | model.on('destroy', view.remove, view); | |
113 |
|
113 | |||
114 | this._handle_new_view(view); |
|
114 | this._handle_new_view(view); | |
115 | return view; |
|
115 | return view; | |
116 | } |
|
116 | } | |
117 | return null; |
|
117 | return null; | |
118 | }; |
|
118 | }; | |
119 |
|
119 | |||
120 | WidgetManager.prototype._handle_new_view = function (view) { |
|
120 | WidgetManager.prototype._handle_new_view = function (view) { | |
121 | // Called when a view has been created and rendered. |
|
121 | // Called when a view has been created and rendered. | |
122 |
|
122 | |||
123 | // If the view has a well defined element, inform the keyboard |
|
123 | // If the view has a well defined element, inform the keyboard | |
124 | // manager about the view's element, so as the element can |
|
124 | // manager about the view's element, so as the element can | |
125 | // escape the dreaded command mode. |
|
125 | // escape the dreaded command mode. | |
126 | if (view.$el !== undefined && view.$el !== null) { |
|
126 | if (view.$el !== undefined && view.$el !== null) { | |
127 | IPython.keyboard_manager.register_events(view.$el); |
|
127 | IPython.keyboard_manager.register_events(view.$el); | |
128 | } |
|
128 | } | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 | WidgetManager.prototype.get_msg_cell = function (msg_id) { |
|
131 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
132 | var cell = null; |
|
132 | var cell = null; | |
133 | // First, check to see if the msg was triggered by cell execution. |
|
133 | // First, check to see if the msg was triggered by cell execution. | |
134 | if (IPython.notebook !== undefined && IPython.notebook !== null) { |
|
134 | if (IPython.notebook !== undefined && IPython.notebook !== null) { | |
135 | cell = IPython.notebook.get_msg_cell(msg_id); |
|
135 | cell = IPython.notebook.get_msg_cell(msg_id); | |
136 | } |
|
136 | } | |
137 | if (cell !== null) { |
|
137 | if (cell !== null) { | |
138 | return cell |
|
138 | return cell | |
139 | } |
|
139 | } | |
140 | // Second, check to see if a get_cell callback was defined |
|
140 | // Second, check to see if a get_cell callback was defined | |
141 | // for the message. get_cell callbacks are registered for |
|
141 | // for the message. get_cell callbacks are registered for | |
142 | // widget messages, so this block is actually checking to see if the |
|
142 | // widget messages, so this block is actually checking to see if the | |
143 | // message was triggered by a widget. |
|
143 | // message was triggered by a widget. | |
144 | var kernel = this.comm_manager.kernel; |
|
144 | var kernel = this.comm_manager.kernel; | |
145 | if (kernel !== undefined && kernel !== null) { |
|
145 | if (kernel !== undefined && kernel !== null) { | |
146 | var callbacks = kernel.get_callbacks_for_msg(msg_id); |
|
146 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
147 | if (callbacks !== undefined && |
|
147 | if (callbacks !== undefined && | |
148 | callbacks.iopub !== undefined && |
|
148 | callbacks.iopub !== undefined && | |
149 | callbacks.iopub.get_cell !== undefined) { |
|
149 | callbacks.iopub.get_cell !== undefined) { | |
150 |
|
150 | |||
151 | return callbacks.iopub.get_cell(); |
|
151 | return callbacks.iopub.get_cell(); | |
152 | } |
|
152 | } | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | // Not triggered by a cell or widget (no get_cell callback |
|
155 | // Not triggered by a cell or widget (no get_cell callback | |
156 | // exists). |
|
156 | // exists). | |
157 | return null; |
|
157 | return null; | |
158 | }; |
|
158 | }; | |
159 |
|
159 | |||
160 | WidgetManager.prototype.callbacks = function (view) { |
|
160 | WidgetManager.prototype.callbacks = function (view) { | |
161 | // callback handlers specific a view |
|
161 | // callback handlers specific a view | |
162 | var callbacks = {}; |
|
162 | var callbacks = {}; | |
163 | var cell = view.options.cell; |
|
163 | if (view !== undefined && | |
164 |
|
|
164 | view !== null && | |
|
165 | view.options.cell !== undefined && | |||
|
166 | view.options.cell !== null) { | |||
|
167 | ||||
165 | // Try to get output handlers |
|
168 | // Try to get output handlers | |
|
169 | var cell = view.options.cell; | |||
166 | var handle_output = null; |
|
170 | var handle_output = null; | |
167 | var handle_clear_output = null; |
|
171 | var handle_clear_output = null; | |
168 | if (cell.output_area !== undefined && cell.output_area !== null) { |
|
172 | if (cell.output_area !== undefined && cell.output_area !== null) { | |
169 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); |
|
173 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
170 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); |
|
174 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
171 | } |
|
175 | } | |
172 |
|
176 | |||
173 | // Create callback dict using what is known |
|
177 | // Create callback dict using what is known | |
174 | var that = this; |
|
178 | var that = this; | |
175 | callbacks = { |
|
179 | callbacks = { | |
176 | iopub : { |
|
180 | iopub : { | |
177 | output : handle_output, |
|
181 | output : handle_output, | |
178 | clear_output : handle_clear_output, |
|
182 | clear_output : handle_clear_output, | |
179 |
|
183 | |||
180 | // Special function only registered by widget messages. |
|
184 | // Special function only registered by widget messages. | |
181 | // Allows us to get the cell for a message so we know |
|
185 | // Allows us to get the cell for a message so we know | |
182 | // where to add widgets if the code requires it. |
|
186 | // where to add widgets if the code requires it. | |
183 | get_cell : function () { |
|
187 | get_cell : function () { | |
184 | return cell; |
|
188 | return cell; | |
185 | }, |
|
189 | }, | |
186 | }, |
|
190 | }, | |
187 | }; |
|
191 | }; | |
188 | } |
|
192 | } | |
189 | return callbacks; |
|
193 | return callbacks; | |
190 | }; |
|
194 | }; | |
191 |
|
195 | |||
192 | WidgetManager.prototype.get_model = function (model_id) { |
|
196 | WidgetManager.prototype.get_model = function (model_id) { | |
193 | // Look-up a model instance by its id. |
|
197 | // Look-up a model instance by its id. | |
194 | var model = this._models[model_id]; |
|
198 | var model = this._models[model_id]; | |
195 | if (model !== undefined && model.id == model_id) { |
|
199 | if (model !== undefined && model.id == model_id) { | |
196 | return model; |
|
200 | return model; | |
197 | } |
|
201 | } | |
198 | return null; |
|
202 | return null; | |
199 | }; |
|
203 | }; | |
200 |
|
204 | |||
201 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { |
|
205 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
202 | // Handle when a comm is opened. |
|
206 | // Handle when a comm is opened. | |
203 | var model_id = comm.comm_id; |
|
207 | var model_id = comm.comm_id; | |
204 | var widget_type_name = msg.content.target_name; |
|
208 | var widget_type_name = msg.content.target_name; | |
205 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); |
|
209 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); | |
206 | this._models[model_id] = widget_model; |
|
210 | this._models[model_id] = widget_model; | |
207 | }; |
|
211 | }; | |
208 |
|
212 | |||
209 | IPython.WidgetManager = WidgetManager; |
|
213 | IPython.WidgetManager = WidgetManager; | |
210 | return IPython.WidgetManager; |
|
214 | return IPython.WidgetManager; | |
211 | }); |
|
215 | }); | |
212 | }()); |
|
216 | }()); |
@@ -1,413 +1,419 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 | // Base Widget Model and View classes |
|
9 | // Base Widget Model and View classes | |
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/widgetmanager", |
|
17 | define(["notebook/js/widgetmanager", | |
18 | "underscore", |
|
18 | "underscore", | |
19 | "backbone"], |
|
19 | "backbone"], | |
20 | function(WidgetManager, Underscore, Backbone){ |
|
20 | function(WidgetManager, Underscore, Backbone){ | |
21 |
|
21 | |||
22 | var WidgetModel = Backbone.Model.extend({ |
|
22 | var WidgetModel = Backbone.Model.extend({ | |
23 | constructor: function (widget_manager, model_id, comm) { |
|
23 | constructor: function (widget_manager, model_id, comm) { | |
24 | // Construcctor |
|
24 | // Construcctor | |
25 | // |
|
25 | // | |
26 | // Creates a WidgetModel instance. |
|
26 | // Creates a WidgetModel instance. | |
27 | // |
|
27 | // | |
28 | // Parameters |
|
28 | // Parameters | |
29 | // ---------- |
|
29 | // ---------- | |
30 | // widget_manager : WidgetManager instance |
|
30 | // widget_manager : WidgetManager instance | |
31 | // model_id : string |
|
31 | // model_id : string | |
32 | // An ID unique to this model. |
|
32 | // An ID unique to this model. | |
33 | // comm : Comm instance (optional) |
|
33 | // comm : Comm instance (optional) | |
34 | this.widget_manager = widget_manager; |
|
34 | this.widget_manager = widget_manager; | |
35 | this.pending_msgs = 0; |
|
35 | this.pending_msgs = 0; | |
36 | this.msg_throttle = 3; |
|
36 | this.msg_throttle = 3; | |
37 | this.msg_buffer = null; |
|
37 | this.msg_buffer = null; | |
38 | this.key_value_lock = null; |
|
38 | this.key_value_lock = null; | |
39 | this.id = model_id; |
|
39 | this.id = model_id; | |
40 | this.views = []; |
|
40 | this.views = []; | |
41 |
|
41 | |||
42 | if (comm !== undefined) { |
|
42 | if (comm !== undefined) { | |
43 | // Remember comm associated with the model. |
|
43 | // Remember comm associated with the model. | |
44 | this.comm = comm; |
|
44 | this.comm = comm; | |
45 | comm.model = this; |
|
45 | comm.model = this; | |
46 |
|
46 | |||
47 | // Hook comm messages up to model. |
|
47 | // Hook comm messages up to model. | |
48 | comm.on_close($.proxy(this._handle_comm_closed, this)); |
|
48 | comm.on_close($.proxy(this._handle_comm_closed, this)); | |
49 | comm.on_msg($.proxy(this._handle_comm_msg, this)); |
|
49 | comm.on_msg($.proxy(this._handle_comm_msg, this)); | |
50 | } |
|
50 | } | |
51 | return Backbone.Model.apply(this); |
|
51 | return Backbone.Model.apply(this); | |
52 | }, |
|
52 | }, | |
53 |
|
53 | |||
54 | send: function (content, callbacks) { |
|
54 | send: function (content, callbacks) { | |
55 | // Send a custom msg over the comm. |
|
55 | // Send a custom msg over the comm. | |
56 | if (this.comm !== undefined) { |
|
56 | if (this.comm !== undefined) { | |
57 | var data = {method: 'custom', content: content}; |
|
57 | var data = {method: 'custom', content: content}; | |
58 | this.comm.send(data, callbacks); |
|
58 | this.comm.send(data, callbacks); | |
|
59 | this.pending_msgs++; | |||
59 | } |
|
60 | } | |
60 | }, |
|
61 | }, | |
61 |
|
62 | |||
62 | _handle_comm_closed: function (msg) { |
|
63 | _handle_comm_closed: function (msg) { | |
63 | // Handle when a widget is closed. |
|
64 | // Handle when a widget is closed. | |
64 | this.trigger('comm:close'); |
|
65 | this.trigger('comm:close'); | |
65 | delete this.comm.model; // Delete ref so GC will collect widget model. |
|
66 | delete this.comm.model; // Delete ref so GC will collect widget model. | |
66 | delete this.comm; |
|
67 | delete this.comm; | |
67 | delete this.model_id; // Delete id from model so widget manager cleans up. |
|
68 | delete this.model_id; // Delete id from model so widget manager cleans up. | |
68 | _.each(this.views, function(view, i) { |
|
69 | _.each(this.views, function(view, i) { | |
69 | view.remove(); |
|
70 | view.remove(); | |
70 | }); |
|
71 | }); | |
71 | }, |
|
72 | }, | |
72 |
|
73 | |||
73 | _handle_comm_msg: function (msg) { |
|
74 | _handle_comm_msg: function (msg) { | |
74 | // Handle incoming comm msg. |
|
75 | // Handle incoming comm msg. | |
75 | var method = msg.content.data.method; |
|
76 | var method = msg.content.data.method; | |
76 | switch (method) { |
|
77 | switch (method) { | |
77 | case 'update': |
|
78 | case 'update': | |
78 | this.apply_update(msg.content.data.state); |
|
79 | this.apply_update(msg.content.data.state); | |
79 | break; |
|
80 | break; | |
80 | case 'custom': |
|
81 | case 'custom': | |
81 | this.trigger('msg:custom', msg.content.data.content); |
|
82 | this.trigger('msg:custom', msg.content.data.content); | |
82 | break; |
|
83 | break; | |
83 | case 'display': |
|
84 | case 'display': | |
84 | this.widget_manager.display_view(msg, this); |
|
85 | this.widget_manager.display_view(msg, this); | |
85 | break; |
|
86 | break; | |
86 | } |
|
87 | } | |
87 | }, |
|
88 | }, | |
88 |
|
89 | |||
89 | apply_update: function (state) { |
|
90 | apply_update: function (state) { | |
90 | // Handle when a widget is updated via the python side. |
|
91 | // Handle when a widget is updated via the python side. | |
91 | var that = this; |
|
92 | var that = this; | |
92 | _.each(state, function(value, key) { |
|
93 | _.each(state, function(value, key) { | |
93 | that.key_value_lock = [key, value]; |
|
94 | that.key_value_lock = [key, value]; | |
94 | try { |
|
95 | try { | |
95 | that.set(key, that._unpack_models(value)); |
|
96 | that.set(key, that._unpack_models(value)); | |
96 | } finally { |
|
97 | } finally { | |
97 | that.key_value_lock = null; |
|
98 | that.key_value_lock = null; | |
98 | } |
|
99 | } | |
99 | }); |
|
100 | }); | |
100 | }, |
|
101 | }, | |
101 |
|
102 | |||
102 | _handle_status: function (msg, callbacks) { |
|
103 | _handle_status: function (msg, callbacks) { | |
103 | // Handle status msgs. |
|
104 | // Handle status msgs. | |
104 |
|
105 | |||
105 | // execution_state : ('busy', 'idle', 'starting') |
|
106 | // execution_state : ('busy', 'idle', 'starting') | |
106 | if (this.comm !== undefined) { |
|
107 | if (this.comm !== undefined) { | |
107 | if (msg.content.execution_state ==='idle') { |
|
108 | if (msg.content.execution_state ==='idle') { | |
108 | // Send buffer if this message caused another message to be |
|
109 | // Send buffer if this message caused another message to be | |
109 | // throttled. |
|
110 | // throttled. | |
110 | if (this.msg_buffer !== null && |
|
111 | if (this.msg_buffer !== null && | |
111 | this.msg_throttle === this.pending_msgs) { |
|
112 | this.msg_throttle === this.pending_msgs) { | |
112 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; |
|
113 | var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer}; | |
113 | this.comm.send(data, callbacks); |
|
114 | this.comm.send(data, callbacks); | |
114 | this.msg_buffer = null; |
|
115 | this.msg_buffer = null; | |
115 | } else { |
|
116 | } else { | |
116 | --this.pending_msgs; |
|
117 | --this.pending_msgs; | |
117 | } |
|
118 | } | |
118 | } |
|
119 | } | |
119 | } |
|
120 | } | |
120 | }, |
|
121 | }, | |
121 |
|
122 | |||
122 | callbacks: function(view) { |
|
123 | callbacks: function(view) { | |
123 | // Create msg callbacks for a comm msg. |
|
124 | // Create msg callbacks for a comm msg. | |
124 | var callbacks = this.widget_manager.callbacks(view); |
|
125 | var callbacks = this.widget_manager.callbacks(view); | |
125 |
|
126 | |||
126 | if (callbacks.iopub === undefined) { |
|
127 | if (callbacks.iopub === undefined) { | |
127 | callbacks.iopub = {}; |
|
128 | callbacks.iopub = {}; | |
128 | } |
|
129 | } | |
129 |
|
130 | |||
130 | var that = this; |
|
131 | var that = this; | |
131 | callbacks.iopub.status = function (msg) { |
|
132 | callbacks.iopub.status = function (msg) { | |
132 | that._handle_status(msg, callbacks); |
|
133 | that._handle_status(msg, callbacks); | |
133 | } |
|
134 | } | |
134 | return callbacks; |
|
135 | return callbacks; | |
135 | }, |
|
136 | }, | |
136 |
|
137 | |||
137 | sync: function (method, model, options) { |
|
138 | sync: function (method, model, options) { | |
138 | // Handle sync to the back-end. Called when a model.save() is called. |
|
139 | // Handle sync to the back-end. Called when a model.save() is called. | |
139 |
|
140 | |||
140 | // Make sure a comm exists. |
|
141 | // Make sure a comm exists. | |
141 | var error = options.error || function() { |
|
142 | var error = options.error || function() { | |
142 | console.error('Backbone sync error:', arguments); |
|
143 | console.error('Backbone sync error:', arguments); | |
143 | } |
|
144 | } | |
144 | if (this.comm === undefined) { |
|
145 | if (this.comm === undefined) { | |
145 | error(); |
|
146 | error(); | |
146 | return false; |
|
147 | return false; | |
147 | } |
|
148 | } | |
148 |
|
149 | |||
149 | // Delete any key value pairs that the back-end already knows about. |
|
150 | // Delete any key value pairs that the back-end already knows about. | |
150 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); |
|
151 | var attrs = (method === 'patch') ? options.attrs : model.toJSON(options); | |
151 | if (this.key_value_lock !== null) { |
|
152 | if (this.key_value_lock !== null) { | |
152 | var key = this.key_value_lock[0]; |
|
153 | var key = this.key_value_lock[0]; | |
153 | var value = this.key_value_lock[1]; |
|
154 | var value = this.key_value_lock[1]; | |
154 | if (attrs[key] === value) { |
|
155 | if (attrs[key] === value) { | |
155 | delete attrs[key]; |
|
156 | delete attrs[key]; | |
156 | } |
|
157 | } | |
157 | } |
|
158 | } | |
158 |
|
159 | |||
159 | // Only sync if there are attributes to send to the back-end. |
|
160 | // Only sync if there are attributes to send to the back-end. | |
160 | if (_.size(attrs) > 0) { |
|
161 | if (_.size(attrs) > 0) { | |
161 | var callbacks = options.callbacks || {}; |
|
162 | ||
|
163 | // If this message was sent via backbone itself, it will not | |||
|
164 | // have any callbacks. It's important that we create callbacks | |||
|
165 | // so we can listen for status messages, etc... | |||
|
166 | var callbacks = options.callbacks || this.callbacks(); | |||
|
167 | ||||
|
168 | // Check throttle. | |||
162 | if (this.pending_msgs >= this.msg_throttle) { |
|
169 | if (this.pending_msgs >= this.msg_throttle) { | |
163 | // The throttle has been exceeded, buffer the current msg so |
|
170 | // The throttle has been exceeded, buffer the current msg so | |
164 | // it can be sent once the kernel has finished processing |
|
171 | // it can be sent once the kernel has finished processing | |
165 | // some of the existing messages. |
|
172 | // some of the existing messages. | |
166 |
|
173 | |||
167 | // Combine updates if it is a 'patch' sync, otherwise replace updates |
|
174 | // Combine updates if it is a 'patch' sync, otherwise replace updates | |
168 | switch (method) { |
|
175 | switch (method) { | |
169 | case 'patch': |
|
176 | case 'patch': | |
170 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); |
|
177 | this.msg_buffer = $.extend(this.msg_buffer || {}, attrs); | |
171 | break; |
|
178 | break; | |
172 | case 'update': |
|
179 | case 'update': | |
173 | case 'create': |
|
180 | case 'create': | |
174 | this.msg_buffer = attrs; |
|
181 | this.msg_buffer = attrs; | |
175 | break; |
|
182 | break; | |
176 | default: |
|
183 | default: | |
177 | error(); |
|
184 | error(); | |
178 | return false; |
|
185 | return false; | |
179 | } |
|
186 | } | |
180 | this.msg_buffer_callbacks = callbacks; |
|
187 | this.msg_buffer_callbacks = callbacks; | |
181 |
|
188 | |||
182 | } else { |
|
189 | } else { | |
183 | // We haven't exceeded the throttle, send the message like |
|
190 | // We haven't exceeded the throttle, send the message like | |
184 | // normal. If this is a patch operation, just send the |
|
191 | // normal. | |
185 | // changes. |
|
|||
186 | var data = {method: 'backbone', sync_data: attrs}; |
|
192 | var data = {method: 'backbone', sync_data: attrs}; | |
187 | this.comm.send(data, callbacks); |
|
193 | this.comm.send(data, callbacks); | |
188 | this.pending_msgs++; |
|
194 | this.pending_msgs++; | |
189 | } |
|
195 | } | |
190 | } |
|
196 | } | |
191 | // Since the comm is a one-way communication, assume the message |
|
197 | // Since the comm is a one-way communication, assume the message | |
192 | // arrived. Don't call success since we don't have a model back from the server |
|
198 | // arrived. Don't call success since we don't have a model back from the server | |
193 | // this means we miss out on the 'sync' event. |
|
199 | // this means we miss out on the 'sync' event. | |
194 | }, |
|
200 | }, | |
195 |
|
201 | |||
196 | save_changes: function(callbacks) { |
|
202 | save_changes: function(callbacks) { | |
197 | // Push this model's state to the back-end |
|
203 | // Push this model's state to the back-end | |
198 | // |
|
204 | // | |
199 | // This invokes a Backbone.Sync. |
|
205 | // This invokes a Backbone.Sync. | |
200 | this.save(this.changedAttributes(), {patch: true, callbacks: callbacks}); |
|
206 | this.save(this.changedAttributes(), {patch: true, callbacks: callbacks}); | |
201 | }, |
|
207 | }, | |
202 |
|
208 | |||
203 | _pack_models: function(value) { |
|
209 | _pack_models: function(value) { | |
204 | // Replace models with model ids recursively. |
|
210 | // Replace models with model ids recursively. | |
205 | if (value instanceof Backbone.Model) { |
|
211 | if (value instanceof Backbone.Model) { | |
206 | return value.id; |
|
212 | return value.id; | |
207 | } else if (value instanceof Object) { |
|
213 | } else if (value instanceof Object) { | |
208 | var packed = {}; |
|
214 | var packed = {}; | |
209 | var that = this; |
|
215 | var that = this; | |
210 | _.each(value, function(sub_value, key) { |
|
216 | _.each(value, function(sub_value, key) { | |
211 | packed[key] = that._pack_models(sub_value); |
|
217 | packed[key] = that._pack_models(sub_value); | |
212 | }); |
|
218 | }); | |
213 | return packed; |
|
219 | return packed; | |
214 | } else { |
|
220 | } else { | |
215 | return value; |
|
221 | return value; | |
216 | } |
|
222 | } | |
217 | }, |
|
223 | }, | |
218 |
|
224 | |||
219 | _unpack_models: function(value) { |
|
225 | _unpack_models: function(value) { | |
220 | // Replace model ids with models recursively. |
|
226 | // Replace model ids with models recursively. | |
221 | if (value instanceof Object) { |
|
227 | if (value instanceof Object) { | |
222 | var unpacked = {}; |
|
228 | var unpacked = {}; | |
223 | var that = this; |
|
229 | var that = this; | |
224 | _.each(value, function(sub_value, key) { |
|
230 | _.each(value, function(sub_value, key) { | |
225 | unpacked[key] = that._unpack_models(sub_value); |
|
231 | unpacked[key] = that._unpack_models(sub_value); | |
226 | }); |
|
232 | }); | |
227 | return unpacked; |
|
233 | return unpacked; | |
228 | } else { |
|
234 | } else { | |
229 | var model = this.widget_manager.get_model(value); |
|
235 | var model = this.widget_manager.get_model(value); | |
230 | if (model !== null) { |
|
236 | if (model !== null) { | |
231 | return model; |
|
237 | return model; | |
232 | } else { |
|
238 | } else { | |
233 | return value; |
|
239 | return value; | |
234 | } |
|
240 | } | |
235 | } |
|
241 | } | |
236 | }, |
|
242 | }, | |
237 |
|
243 | |||
238 | }); |
|
244 | }); | |
239 | WidgetManager.register_widget_model('WidgetModel', WidgetModel); |
|
245 | WidgetManager.register_widget_model('WidgetModel', WidgetModel); | |
240 |
|
246 | |||
241 |
|
247 | |||
242 | var WidgetView = Backbone.View.extend({ |
|
248 | var WidgetView = Backbone.View.extend({ | |
243 | initialize: function(parameters) { |
|
249 | initialize: function(parameters) { | |
244 | // Public constructor. |
|
250 | // Public constructor. | |
245 | this.model.on('change',this.update,this); |
|
251 | this.model.on('change',this.update,this); | |
246 | this.options = parameters.options; |
|
252 | this.options = parameters.options; | |
247 | this.child_views = []; |
|
253 | this.child_views = []; | |
248 | this.model.views.push(this); |
|
254 | this.model.views.push(this); | |
249 | }, |
|
255 | }, | |
250 |
|
256 | |||
251 | update: function(){ |
|
257 | update: function(){ | |
252 | // Triggered on model change. |
|
258 | // Triggered on model change. | |
253 | // |
|
259 | // | |
254 | // Update view to be consistent with this.model |
|
260 | // Update view to be consistent with this.model | |
255 | }, |
|
261 | }, | |
256 |
|
262 | |||
257 | create_child_view: function(child_model, options) { |
|
263 | create_child_view: function(child_model, options) { | |
258 | // Create and return a child view. |
|
264 | // Create and return a child view. | |
259 | // |
|
265 | // | |
260 | // -given a model and (optionally) a view name if the view name is |
|
266 | // -given a model and (optionally) a view name if the view name is | |
261 | // not given, it defaults to the model's default view attribute. |
|
267 | // not given, it defaults to the model's default view attribute. | |
262 |
|
268 | |||
263 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior |
|
269 | // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior | |
264 | // it would be great to have the widget manager add the cell metadata |
|
270 | // it would be great to have the widget manager add the cell metadata | |
265 | // to the subview without having to add it here. |
|
271 | // to the subview without having to add it here. | |
266 | var child_view = this.model.widget_manager.create_view(child_model, options || {}, this); |
|
272 | var child_view = this.model.widget_manager.create_view(child_model, options || {}, this); | |
267 | this.child_views[child_model.id] = child_view; |
|
273 | this.child_views[child_model.id] = child_view; | |
268 | return child_view; |
|
274 | return child_view; | |
269 | }, |
|
275 | }, | |
270 |
|
276 | |||
271 | delete_child_view: function(child_model, options) { |
|
277 | delete_child_view: function(child_model, options) { | |
272 | // Delete a child view that was previously created using create_child_view. |
|
278 | // Delete a child view that was previously created using create_child_view. | |
273 | var view = this.child_views[child_model.id]; |
|
279 | var view = this.child_views[child_model.id]; | |
274 | if (view !== undefined) { |
|
280 | if (view !== undefined) { | |
275 | delete this.child_views[child_model.id]; |
|
281 | delete this.child_views[child_model.id]; | |
276 | view.remove(); |
|
282 | view.remove(); | |
277 | } |
|
283 | } | |
278 | }, |
|
284 | }, | |
279 |
|
285 | |||
280 | do_diff: function(old_list, new_list, removed_callback, added_callback) { |
|
286 | do_diff: function(old_list, new_list, removed_callback, added_callback) { | |
281 | // Difference a changed list and call remove and add callbacks for |
|
287 | // Difference a changed list and call remove and add callbacks for | |
282 | // each removed and added item in the new list. |
|
288 | // each removed and added item in the new list. | |
283 | // |
|
289 | // | |
284 | // Parameters |
|
290 | // Parameters | |
285 | // ---------- |
|
291 | // ---------- | |
286 | // old_list : array |
|
292 | // old_list : array | |
287 | // new_list : array |
|
293 | // new_list : array | |
288 | // removed_callback : Callback(item) |
|
294 | // removed_callback : Callback(item) | |
289 | // Callback that is called for each item removed. |
|
295 | // Callback that is called for each item removed. | |
290 | // added_callback : Callback(item) |
|
296 | // added_callback : Callback(item) | |
291 | // Callback that is called for each item added. |
|
297 | // Callback that is called for each item added. | |
292 |
|
298 | |||
293 |
|
299 | |||
294 | // removed items |
|
300 | // removed items | |
295 | _.each(_.difference(old_list, new_list), function(item, index, list) { |
|
301 | _.each(_.difference(old_list, new_list), function(item, index, list) { | |
296 | removed_callback(item); |
|
302 | removed_callback(item); | |
297 | }, this); |
|
303 | }, this); | |
298 |
|
304 | |||
299 | // added items |
|
305 | // added items | |
300 | _.each(_.difference(new_list, old_list), function(item, index, list) { |
|
306 | _.each(_.difference(new_list, old_list), function(item, index, list) { | |
301 | added_callback(item); |
|
307 | added_callback(item); | |
302 | }, this); |
|
308 | }, this); | |
303 | }, |
|
309 | }, | |
304 |
|
310 | |||
305 | callbacks: function(){ |
|
311 | callbacks: function(){ | |
306 | // Create msg callbacks for a comm msg. |
|
312 | // Create msg callbacks for a comm msg. | |
307 | return this.model.callbacks(this); |
|
313 | return this.model.callbacks(this); | |
308 | }, |
|
314 | }, | |
309 |
|
315 | |||
310 | render: function(){ |
|
316 | render: function(){ | |
311 | // Render the view. |
|
317 | // Render the view. | |
312 | // |
|
318 | // | |
313 | // By default, this is only called the first time the view is created |
|
319 | // By default, this is only called the first time the view is created | |
314 | }, |
|
320 | }, | |
315 |
|
321 | |||
316 | send: function (content) { |
|
322 | send: function (content) { | |
317 | // Send a custom msg associated with this view. |
|
323 | // Send a custom msg associated with this view. | |
318 | this.model.send(content, this.callbacks()); |
|
324 | this.model.send(content, this.callbacks()); | |
319 | }, |
|
325 | }, | |
320 |
|
326 | |||
321 | touch: function () { |
|
327 | touch: function () { | |
322 | this.model.save_changes(this.callbacks()); |
|
328 | this.model.save_changes(this.callbacks()); | |
323 | }, |
|
329 | }, | |
324 | }); |
|
330 | }); | |
325 |
|
331 | |||
326 |
|
332 | |||
327 | var DOMWidgetView = WidgetView.extend({ |
|
333 | var DOMWidgetView = WidgetView.extend({ | |
328 | initialize: function (options) { |
|
334 | initialize: function (options) { | |
329 | // Public constructor |
|
335 | // Public constructor | |
330 |
|
336 | |||
331 | // In the future we may want to make changes more granular |
|
337 | // In the future we may want to make changes more granular | |
332 | // (e.g., trigger on visible:change). |
|
338 | // (e.g., trigger on visible:change). | |
333 | this.model.on('change', this.update, this); |
|
339 | this.model.on('change', this.update, this); | |
334 | this.model.on('msg:custom', this.on_msg, this); |
|
340 | this.model.on('msg:custom', this.on_msg, this); | |
335 | DOMWidgetView.__super__.initialize.apply(this, arguments); |
|
341 | DOMWidgetView.__super__.initialize.apply(this, arguments); | |
336 | }, |
|
342 | }, | |
337 |
|
343 | |||
338 | on_msg: function(msg) { |
|
344 | on_msg: function(msg) { | |
339 | // Handle DOM specific msgs. |
|
345 | // Handle DOM specific msgs. | |
340 | switch(msg.msg_type) { |
|
346 | switch(msg.msg_type) { | |
341 | case 'add_class': |
|
347 | case 'add_class': | |
342 | this.add_class(msg.selector, msg.class_list); |
|
348 | this.add_class(msg.selector, msg.class_list); | |
343 | break; |
|
349 | break; | |
344 | case 'remove_class': |
|
350 | case 'remove_class': | |
345 | this.remove_class(msg.selector, msg.class_list); |
|
351 | this.remove_class(msg.selector, msg.class_list); | |
346 | break; |
|
352 | break; | |
347 | } |
|
353 | } | |
348 | }, |
|
354 | }, | |
349 |
|
355 | |||
350 | add_class: function (selector, class_list) { |
|
356 | add_class: function (selector, class_list) { | |
351 | // Add a DOM class to an element. |
|
357 | // Add a DOM class to an element. | |
352 | this._get_selector_element(selector).addClass(class_list); |
|
358 | this._get_selector_element(selector).addClass(class_list); | |
353 | }, |
|
359 | }, | |
354 |
|
360 | |||
355 | remove_class: function (selector, class_list) { |
|
361 | remove_class: function (selector, class_list) { | |
356 | // Remove a DOM class from an element. |
|
362 | // Remove a DOM class from an element. | |
357 | this._get_selector_element(selector).removeClass(class_list); |
|
363 | this._get_selector_element(selector).removeClass(class_list); | |
358 | }, |
|
364 | }, | |
359 |
|
365 | |||
360 | update: function () { |
|
366 | update: function () { | |
361 | // Update the contents of this view |
|
367 | // Update the contents of this view | |
362 | // |
|
368 | // | |
363 | // Called when the model is changed. The model may have been |
|
369 | // Called when the model is changed. The model may have been | |
364 | // changed by another view or by a state update from the back-end. |
|
370 | // changed by another view or by a state update from the back-end. | |
365 | // The very first update seems to happen before the element is |
|
371 | // The very first update seems to happen before the element is | |
366 | // finished rendering so we use setTimeout to give the element time |
|
372 | // finished rendering so we use setTimeout to give the element time | |
367 | // to render |
|
373 | // to render | |
368 | var e = this.$el; |
|
374 | var e = this.$el; | |
369 | var visible = this.model.get('visible'); |
|
375 | var visible = this.model.get('visible'); | |
370 | setTimeout(function() {e.toggle(visible)},0); |
|
376 | setTimeout(function() {e.toggle(visible)},0); | |
371 |
|
377 | |||
372 | var css = this.model.get('_css'); |
|
378 | var css = this.model.get('_css'); | |
373 | if (css === undefined) {return;} |
|
379 | if (css === undefined) {return;} | |
374 | var that = this; |
|
380 | var that = this; | |
375 | _.each(css, function(css_traits, selector){ |
|
381 | _.each(css, function(css_traits, selector){ | |
376 | // Apply the css traits to all elements that match the selector. |
|
382 | // Apply the css traits to all elements that match the selector. | |
377 | var elements = that._get_selector_element(selector); |
|
383 | var elements = that._get_selector_element(selector); | |
378 | if (elements.length > 0) { |
|
384 | if (elements.length > 0) { | |
379 | _.each(css_traits, function(css_value, css_key){ |
|
385 | _.each(css_traits, function(css_value, css_key){ | |
380 | elements.css(css_key, css_value); |
|
386 | elements.css(css_key, css_value); | |
381 | }); |
|
387 | }); | |
382 | } |
|
388 | } | |
383 | }); |
|
389 | }); | |
384 |
|
390 | |||
385 | }, |
|
391 | }, | |
386 |
|
392 | |||
387 | _get_selector_element: function (selector) { |
|
393 | _get_selector_element: function (selector) { | |
388 | // Get the elements via the css selector. |
|
394 | // Get the elements via the css selector. | |
389 |
|
395 | |||
390 | // If the selector is blank, apply the style to the $el_to_style |
|
396 | // If the selector is blank, apply the style to the $el_to_style | |
391 | // element. If the $el_to_style element is not defined, use apply |
|
397 | // element. If the $el_to_style element is not defined, use apply | |
392 | // the style to the view's element. |
|
398 | // the style to the view's element. | |
393 | var elements; |
|
399 | var elements; | |
394 | if (selector === undefined || selector === null || selector === '') { |
|
400 | if (selector === undefined || selector === null || selector === '') { | |
395 | if (this.$el_to_style === undefined) { |
|
401 | if (this.$el_to_style === undefined) { | |
396 | elements = this.$el; |
|
402 | elements = this.$el; | |
397 | } else { |
|
403 | } else { | |
398 | elements = this.$el_to_style; |
|
404 | elements = this.$el_to_style; | |
399 | } |
|
405 | } | |
400 | } else { |
|
406 | } else { | |
401 | elements = this.$el.find(selector); |
|
407 | elements = this.$el.find(selector); | |
402 | } |
|
408 | } | |
403 | return elements; |
|
409 | return elements; | |
404 | }, |
|
410 | }, | |
405 | }); |
|
411 | }); | |
406 |
|
412 | |||
407 | IPython.WidgetModel = WidgetModel; |
|
413 | IPython.WidgetModel = WidgetModel; | |
408 | IPython.WidgetView = WidgetView; |
|
414 | IPython.WidgetView = WidgetView; | |
409 | IPython.DOMWidgetView = DOMWidgetView; |
|
415 | IPython.DOMWidgetView = DOMWidgetView; | |
410 |
|
416 | |||
411 | // Pass through WidgetManager namespace. |
|
417 | // Pass through WidgetManager namespace. | |
412 | return WidgetManager; |
|
418 | return WidgetManager; | |
413 | }); |
|
419 | }); |
@@ -1,195 +1,195 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/widget"], function(WidgetManager){ |
|
17 | define(["notebook/js/widgets/widget"], function(WidgetManager){ | |
18 |
|
18 | |||
19 | var HTMLView = IPython.DOMWidgetView.extend({ |
|
19 | var HTMLView = IPython.DOMWidgetView.extend({ | |
20 | render : function(){ |
|
20 | render : function(){ | |
21 | // Called when view is rendered. |
|
21 | // Called when view is rendered. | |
22 | this.update(); // Set defaults. |
|
22 | this.update(); // Set defaults. | |
23 | }, |
|
23 | }, | |
24 |
|
24 | |||
25 | update : function(){ |
|
25 | update : function(){ | |
26 | // Update the contents of this view |
|
26 | // Update the contents of this view | |
27 | // |
|
27 | // | |
28 | // Called when the model is changed. The model may have been |
|
28 | // Called when the model is changed. The model may have been | |
29 | // changed by another view or by a state update from the back-end. |
|
29 | // changed by another view or by a state update from the back-end. | |
30 | this.$el.html(this.model.get('value')); // CAUTION! .html(...) CALL MANDITORY!!! |
|
30 | this.$el.html(this.model.get('value')); // CAUTION! .html(...) CALL MANDITORY!!! | |
31 | return HTMLView.__super__.update.apply(this); |
|
31 | return HTMLView.__super__.update.apply(this); | |
32 | }, |
|
32 | }, | |
33 | }); |
|
33 | }); | |
34 | WidgetManager.register_widget_view('HTMLView', HTMLView); |
|
34 | WidgetManager.register_widget_view('HTMLView', HTMLView); | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | var LatexView = IPython.DOMWidgetView.extend({ |
|
37 | var LatexView = IPython.DOMWidgetView.extend({ | |
38 | render : function(){ |
|
38 | render : function(){ | |
39 | // Called when view is rendered. |
|
39 | // Called when view is rendered. | |
40 | this.update(); // Set defaults. |
|
40 | this.update(); // Set defaults. | |
41 | }, |
|
41 | }, | |
42 |
|
42 | |||
43 | update : function(){ |
|
43 | update : function(){ | |
44 | // Update the contents of this view |
|
44 | // Update the contents of this view | |
45 | // |
|
45 | // | |
46 | // Called when the model is changed. The model may have been |
|
46 | // Called when the model is changed. The model may have been | |
47 | // changed by another view or by a state update from the back-end. |
|
47 | // changed by another view or by a state update from the back-end. | |
48 | this.$el.text(this.model.get('value')); |
|
48 | this.$el.text(this.model.get('value')); | |
49 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]); |
|
49 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]); | |
50 |
|
50 | |||
51 | return LatexView.__super__.update.apply(this); |
|
51 | return LatexView.__super__.update.apply(this); | |
52 | }, |
|
52 | }, | |
53 | }); |
|
53 | }); | |
54 | WidgetManager.register_widget_view('LatexView', LatexView); |
|
54 | WidgetManager.register_widget_view('LatexView', LatexView); | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | var TextAreaView = IPython.DOMWidgetView.extend({ |
|
57 | var TextAreaView = IPython.DOMWidgetView.extend({ | |
58 | render: function(){ |
|
58 | render: function(){ | |
59 | // Called when view is rendered. |
|
59 | // Called when view is rendered. | |
60 | this.$el |
|
60 | this.$el | |
61 | .addClass('widget-hbox'); |
|
61 | .addClass('widget-hbox'); | |
62 | this.$label = $('<div />') |
|
62 | this.$label = $('<div />') | |
63 | .appendTo(this.$el) |
|
63 | .appendTo(this.$el) | |
64 | .addClass('widget-hlabel') |
|
64 | .addClass('widget-hlabel') | |
65 | .hide(); |
|
65 | .hide(); | |
66 | this.$textbox = $('<textarea />') |
|
66 | this.$textbox = $('<textarea />') | |
67 | .attr('rows', 5) |
|
67 | .attr('rows', 5) | |
68 | .addClass('widget-text') |
|
68 | .addClass('widget-text') | |
69 | .appendTo(this.$el); |
|
69 | .appendTo(this.$el); | |
70 | this.$el_to_style = this.$textbox; // Set default element to style |
|
70 | this.$el_to_style = this.$textbox; // Set default element to style | |
71 | this.update(); // Set defaults. |
|
71 | this.update(); // Set defaults. | |
72 |
|
72 | |||
73 | this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this)); |
|
73 | this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this)); | |
74 | }, |
|
74 | }, | |
75 |
|
75 | |||
76 | _handle_textarea_msg: function (content){ |
|
76 | _handle_textarea_msg: function (content){ | |
77 | // Handle when a custom msg is recieved from the back-end. |
|
77 | // Handle when a custom msg is recieved from the back-end. | |
78 | if (content.method == "scroll_to_bottom") { |
|
78 | if (content.method == "scroll_to_bottom") { | |
79 | this.scroll_to_bottom(); |
|
79 | this.scroll_to_bottom(); | |
80 | } |
|
80 | } | |
81 | }, |
|
81 | }, | |
82 |
|
82 | |||
83 | scroll_to_bottom: function (){ |
|
83 | scroll_to_bottom: function (){ | |
84 | // Scroll the text-area view to the bottom. |
|
84 | // Scroll the text-area view to the bottom. | |
85 | this.$textbox.scrollTop(this.$textbox[0].scrollHeight); |
|
85 | this.$textbox.scrollTop(this.$textbox[0].scrollHeight); | |
86 | }, |
|
86 | }, | |
87 |
|
87 | |||
88 | update: function(options){ |
|
88 | update: function(options){ | |
89 | // Update the contents of this view |
|
89 | // Update the contents of this view | |
90 | // |
|
90 | // | |
91 | // Called when the model is changed. The model may have been |
|
91 | // Called when the model is changed. The model may have been | |
92 | // changed by another view or by a state update from the back-end. |
|
92 | // changed by another view or by a state update from the back-end. | |
93 | if (options === undefined || options.updated_view != this) { |
|
93 | if (options === undefined || options.updated_view != this) { | |
94 | this.$textbox.val(this.model.get('value')); |
|
94 | this.$textbox.val(this.model.get('value')); | |
95 |
|
95 | |||
96 | var disabled = this.model.get('disabled'); |
|
96 | var disabled = this.model.get('disabled'); | |
97 | this.$textbox.prop('disabled', disabled); |
|
97 | this.$textbox.prop('disabled', disabled); | |
98 |
|
98 | |||
99 | var description = this.model.get('description'); |
|
99 | var description = this.model.get('description'); | |
100 | if (description.length === 0) { |
|
100 | if (description.length === 0) { | |
101 | this.$label.hide(); |
|
101 | this.$label.hide(); | |
102 | } else { |
|
102 | } else { | |
103 | this.$label.text(description); |
|
103 | this.$label.text(description); | |
104 | this.$label.show(); |
|
104 | this.$label.show(); | |
105 | } |
|
105 | } | |
106 | } |
|
106 | } | |
107 | return TextAreaView.__super__.update.apply(this); |
|
107 | return TextAreaView.__super__.update.apply(this); | |
108 | }, |
|
108 | }, | |
109 |
|
109 | |||
110 | events: { |
|
110 | events: { | |
111 | // Dictionary of events and their handlers. |
|
111 | // Dictionary of events and their handlers. | |
112 | "keyup textarea" : "handleChanging", |
|
112 | "keyup textarea" : "handleChanging", | |
113 | "paste textarea" : "handleChanging", |
|
113 | "paste textarea" : "handleChanging", | |
114 | "cut textarea" : "handleChanging" |
|
114 | "cut textarea" : "handleChanging" | |
115 | }, |
|
115 | }, | |
116 |
|
116 | |||
117 | handleChanging: function(e) { |
|
117 | handleChanging: function(e) { | |
118 | // Handles and validates user input. |
|
118 | // Handles and validates user input. | |
119 |
|
119 | |||
120 | // Calling model.set will trigger all of the other views of the |
|
120 | // Calling model.set will trigger all of the other views of the | |
121 | // model to update. |
|
121 | // model to update. | |
122 | this.model.set('value', e.target.value, {updated_view: this}); |
|
122 | this.model.set('value', e.target.value, {updated_view: this}); | |
123 | this.touch(); |
|
123 | this.touch(); | |
124 | }, |
|
124 | }, | |
125 | }); |
|
125 | }); | |
126 | WidgetManager.register_widget_view('TextAreaView', TextAreaView); |
|
126 | WidgetManager.register_widget_view('TextAreaView', TextAreaView); | |
127 |
|
127 | |||
128 |
|
128 | |||
129 | var TextBoxView = IPython.DOMWidgetView.extend({ |
|
129 | var TextBoxView = IPython.DOMWidgetView.extend({ | |
130 | render: function(){ |
|
130 | render: function(){ | |
131 | // Called when view is rendered. |
|
131 | // Called when view is rendered. | |
132 | this.$el |
|
132 | this.$el | |
133 | .addClass('widget-hbox-single'); |
|
133 | .addClass('widget-hbox-single'); | |
134 | this.$label = $('<div />') |
|
134 | this.$label = $('<div />') | |
135 | .addClass('widget-hlabel') |
|
135 | .addClass('widget-hlabel') | |
136 | .appendTo(this.$el) |
|
136 | .appendTo(this.$el) | |
137 | .hide(); |
|
137 | .hide(); | |
138 | this.$textbox = $('<input type="text" />') |
|
138 | this.$textbox = $('<input type="text" />') | |
139 | .addClass('input') |
|
139 | .addClass('input') | |
140 | .addClass('widget-text') |
|
140 | .addClass('widget-text') | |
141 | .appendTo(this.$el); |
|
141 | .appendTo(this.$el); | |
142 | this.$el_to_style = this.$textbox; // Set default element to style |
|
142 | this.$el_to_style = this.$textbox; // Set default element to style | |
143 | this.update(); // Set defaults. |
|
143 | this.update(); // Set defaults. | |
144 | }, |
|
144 | }, | |
145 |
|
145 | |||
146 | update: function(options){ |
|
146 | update: function(options){ | |
147 | // Update the contents of this view |
|
147 | // Update the contents of this view | |
148 | // |
|
148 | // | |
149 | // Called when the model is changed. The model may have been |
|
149 | // Called when the model is changed. The model may have been | |
150 | // changed by another view or by a state update from the back-end. |
|
150 | // changed by another view or by a state update from the back-end. | |
151 | if (options === undefined || options.updated_view != this) { |
|
151 | if (options === undefined || options.updated_view != this) { | |
152 | if (this.$textbox.val() != this.model.get('value')) { |
|
152 | if (this.$textbox.val() != this.model.get('value')) { | |
153 | this.$textbox.val(this.model.get('value')); |
|
153 | this.$textbox.val(this.model.get('value')); | |
154 | } |
|
154 | } | |
155 |
|
155 | |||
156 | var disabled = this.model.get('disabled'); |
|
156 | var disabled = this.model.get('disabled'); | |
157 | this.$textbox.prop('disabled', disabled); |
|
157 | this.$textbox.prop('disabled', disabled); | |
158 |
|
158 | |||
159 | var description = this.model.get('description'); |
|
159 | var description = this.model.get('description'); | |
160 | if (description.length === 0) { |
|
160 | if (description.length === 0) { | |
161 | this.$label.hide(); |
|
161 | this.$label.hide(); | |
162 | } else { |
|
162 | } else { | |
163 | this.$label.text(description); |
|
163 | this.$label.text(description); | |
164 | this.$label.show(); |
|
164 | this.$label.show(); | |
165 | } |
|
165 | } | |
166 | } |
|
166 | } | |
167 | return TextBoxView.__super__.update.apply(this); |
|
167 | return TextBoxView.__super__.update.apply(this); | |
168 | }, |
|
168 | }, | |
169 |
|
169 | |||
170 | events: { |
|
170 | events: { | |
171 | // Dictionary of events and their handlers. |
|
171 | // Dictionary of events and their handlers. | |
172 | "keyup input" : "handleChanging", |
|
172 | "keyup input" : "handleChanging", | |
173 | "paste input" : "handleChanging", |
|
173 | "paste input" : "handleChanging", | |
174 | "cut input" : "handleChanging", |
|
174 | "cut input" : "handleChanging", | |
175 | "keypress input" : "handleKeypress" |
|
175 | "keypress input" : "handleKeypress" | |
176 | }, |
|
176 | }, | |
177 |
|
177 | |||
178 | handleChanging: function(e) { |
|
178 | handleChanging: function(e) { | |
179 |
// Handles |
|
179 | // Handles user input. | |
180 |
|
180 | |||
181 | // Calling model.set will trigger all of the other views of the |
|
181 | // Calling model.set will trigger all of the other views of the | |
182 | // model to update. |
|
182 | // model to update. | |
183 | this.model.set('value', e.target.value, {updated_view: this}); |
|
183 | this.model.set('value', e.target.value, {updated_view: this}); | |
184 | this.touch(); |
|
184 | this.touch(); | |
185 | }, |
|
185 | }, | |
186 |
|
186 | |||
187 | handleKeypress: function(e) { |
|
187 | handleKeypress: function(e) { | |
188 | // Handles text submition |
|
188 | // Handles text submition | |
189 | if (e.keyCode == 13) { // Return key |
|
189 | if (e.keyCode == 13) { // Return key | |
190 | this.send({event: 'submit'}); |
|
190 | this.send({event: 'submit'}); | |
191 | } |
|
191 | } | |
192 | }, |
|
192 | }, | |
193 | }); |
|
193 | }); | |
194 | WidgetManager.register_widget_view('TextBoxView', TextBoxView); |
|
194 | WidgetManager.register_widget_view('TextBoxView', TextBoxView); | |
195 | }); |
|
195 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now