##// END OF EJS Templates
move backbone sync outside the widget manager class
Jonathan Frederic -
Show More
@@ -1,284 +1,284
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
28 Backbone.sync = function (method, model, options, error) {
29 var result = model._handle_sync(method, options);
30 if (options.success) {
31 options.success(result);
32 }
33 };
27
34
28 //--------------------------------------------------------------------
35 //--------------------------------------------------------------------
29 // WidgetManager class
36 // WidgetManager class
30 //--------------------------------------------------------------------
37 //--------------------------------------------------------------------
31 var WidgetManager = function () {
38 var WidgetManager = function () {
32 this.comm_manager = null;
39 this.comm_manager = null;
33 this._model_types = {}; /* Dictionary of model type names
40 this._model_types = {}; /* Dictionary of model type names
34 (target_name) and model types. */
41 (target_name) and model types. */
35 this._view_types = {}; /* Dictionary of view names and view types. */
42 this._view_types = {}; /* Dictionary of view names and view types. */
36 this._models = {}; /* Dictionary of model ids and model instances */
43 this._models = {}; /* Dictionary of model ids and model instances */
37
38 Backbone.sync = function (method, model, options, error) {
39 var result = model._handle_sync(method, options);
40 if (options.success) {
41 options.success(result);
42 }
43 };
44 };
44 };
45
45
46
46
47 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
47 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
48 this.comm_manager = comm_manager;
48 this.comm_manager = comm_manager;
49
49
50 // Register already-registered widget model types with the comm manager.
50 // Register already-registered widget model types with the comm manager.
51 for (var widget_model_name in this._model_types) {
51 for (var widget_model_name in this._model_types) {
52 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
52 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
53 }
53 }
54 };
54 };
55
55
56
56
57 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
57 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
58 // Register the widget with the comm manager. Make sure to pass this object's context
58 // Register the widget with the comm manager. Make sure to pass this object's context
59 // in so `this` works in the call back.
59 // in so `this` works in the call back.
60 if (this.comm_manager !== null) {
60 if (this.comm_manager !== null) {
61 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
61 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
62 }
62 }
63 this._model_types[widget_model_name] = widget_model_type;
63 this._model_types[widget_model_name] = widget_model_type;
64 };
64 };
65
65
66
66
67 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
67 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
68 this._view_types[widget_view_name] = widget_view_type;
68 this._view_types[widget_view_name] = widget_view_type;
69 };
69 };
70
70
71
71
72 WidgetManager.prototype.handle_msg = function(msg, model) {
72 WidgetManager.prototype.handle_msg = function(msg, model) {
73 var method = msg.content.data.method;
73 var method = msg.content.data.method;
74 switch (method) {
74 switch (method) {
75 case 'display':
75 case 'display':
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, msg.content.data.view_name);
81 var view = this.create_view(model, msg.content.data.view_name);
82 if (view !== undefined
82 if (view !== undefined
83 && cell.widget_subarea !== undefined
83 && cell.widget_subarea !== undefined
84 && cell.widget_subarea !== null) {
84 && cell.widget_subarea !== null) {
85
85
86 view.cell = cell;
86 view.cell = cell;
87 cell.widget_area.show();
87 cell.widget_area.show();
88 cell.widget_subarea.append(view.$el);
88 cell.widget_subarea.append(view.$el);
89 }
89 }
90 }
90 }
91 break;
91 break;
92 }
92 }
93 }
93 }
94
94
95 <<<<<<< HEAD
95 <<<<<<< HEAD
96 <<<<<<< HEAD
96 <<<<<<< HEAD
97 WidgetManager.prototype.create_view = function(model, view_name, cell) {
97 WidgetManager.prototype.create_view = function(model, view_name, cell) {
98 =======
98 =======
99 WidgetManager.prototype.create_view = function(model, view_name, options) {
99 WidgetManager.prototype.create_view = function(model, view_name, options) {
100 >>>>>>> Completely remove cell from model and view.
100 >>>>>>> Completely remove cell from model and view.
101 view_name = view_name || model.get('default_view_name');
101 view_name = view_name || model.get('default_view_name');
102 <<<<<<< HEAD
102 <<<<<<< HEAD
103 =======
103 =======
104 WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
104 WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
105 view_name = view_name || model.get('default_view_name');
105 view_name = view_name || model.get('default_view_name');
106 >>>>>>> Add widget view options in creating child views
106 >>>>>>> Add widget view options in creating child views
107 var ViewType = this.widget_view_types[view_name];
107 var ViewType = this.widget_view_types[view_name];
108 =======
108 =======
109 var ViewType = this._view_types[view_name];
109 var ViewType = this._view_types[view_name];
110 >>>>>>> _model_types, _view_types, _models - and document what keys and values are
110 >>>>>>> _model_types, _view_types, _models - and document what keys and values are
111 if (ViewType !== undefined && ViewType !== null) {
111 if (ViewType !== undefined && ViewType !== null) {
112 var view = new ViewType({model: model, widget_manager: this, options: options});
112 var view = new ViewType({model: model, widget_manager: this, options: options});
113 view.render();
113 view.render();
114 model.views.push(view);
114 model.views.push(view);
115 model.on('destroy', view.remove, view);
115 model.on('destroy', view.remove, view);
116 <<<<<<< HEAD
116 <<<<<<< HEAD
117 /*
117 /*
118 // TODO: handle view deletion. Don't forget to delete child views
118 // TODO: handle view deletion. Don't forget to delete child views
119 var that = this;
119 var that = this;
120 view.$el.on("remove", function () {
120 view.$el.on("remove", function () {
121 var index = that.views.indexOf(view);
121 var index = that.views.indexOf(view);
122 if (index > -1) {
122 if (index > -1) {
123 that.views.splice(index, 1);
123 that.views.splice(index, 1);
124 =======
124 =======
125 /*
125 /*
126 // TODO: handle view deletion. Don't forget to delete child views
126 // TODO: handle view deletion. Don't forget to delete child views
127 var that = this;
127 var that = this;
128 view.$el.on("remove", function () {
128 view.$el.on("remove", function () {
129 var index = that.views.indexOf(view);
129 var index = that.views.indexOf(view);
130 if (index > -1) {
130 if (index > -1) {
131 that.views.splice(index, 1);
131 that.views.splice(index, 1);
132 }
132 }
133 view.remove(); // Clean-up view
133 view.remove(); // Clean-up view
134
134
135 // Close the comm if there are no views left.
135 // Close the comm if there are no views left.
136 if (that.views.length() === 0) {
136 if (that.views.length() === 0) {
137 //trigger comm close event?
137 //trigger comm close event?
138 }
138 }
139
139
140
140
141 if (that.comm !== undefined) {
141 if (that.comm !== undefined) {
142 that.comm.close();
142 that.comm.close();
143 delete that.comm.model; // Delete ref so GC will collect widget model.
143 delete that.comm.model; // Delete ref so GC will collect widget model.
144 delete that.comm;
144 delete that.comm;
145 >>>>>>> Add widget view options in creating child views
145 >>>>>>> Add widget view options in creating child views
146 }
146 }
147 view.remove(); // Clean-up view
147 view.remove(); // Clean-up view
148
148
149 // Close the comm if there are no views left.
149 // Close the comm if there are no views left.
150 if (that.views.length() === 0) {
150 if (that.views.length() === 0) {
151 //trigger comm close event?
151 //trigger comm close event?
152 }
152 }
153
153
154
154
155 if (that.comm !== undefined) {
155 if (that.comm !== undefined) {
156 that.comm.close();
156 that.comm.close();
157 delete that.comm.model; // Delete ref so GC will collect widget model.
157 delete that.comm.model; // Delete ref so GC will collect widget model.
158 delete that.comm;
158 delete that.comm;
159 }
159 }
160 delete that.model_id; // Delete id from model so widget manager cleans up.
160 delete that.model_id; // Delete id from model so widget manager cleans up.
161 });
161 });
162 */
162 */
163 return view;
163 return view;
164 }
164 }
165 },
165 },
166
166
167 WidgetManager.prototype.get_msg_cell = function (msg_id) {
167 WidgetManager.prototype.get_msg_cell = function (msg_id) {
168 var cell = null;
168 var cell = null;
169 // First, check to see if the msg was triggered by cell execution.
169 // First, check to see if the msg was triggered by cell execution.
170 if (IPython.notebook !== undefined && IPython.notebook !== null) {
170 if (IPython.notebook !== undefined && IPython.notebook !== null) {
171 cell = IPython.notebook.get_msg_cell(msg_id);
171 cell = IPython.notebook.get_msg_cell(msg_id);
172 }
172 }
173 if (cell !== null) {
173 if (cell !== null) {
174 return cell
174 return cell
175 }
175 }
176 // Second, check to see if a get_cell callback was defined
176 // Second, check to see if a get_cell callback was defined
177 // for the message. get_cell callbacks are registered for
177 // for the message. get_cell callbacks are registered for
178 // widget messages, so this block is actually checking to see if the
178 // widget messages, so this block is actually checking to see if the
179 // message was triggered by a widget.
179 // message was triggered by a widget.
180 var kernel = this.get_kernel();
180 var kernel = this.get_kernel();
181 if (kernel !== undefined && kernel !== null) {
181 if (kernel !== undefined && kernel !== null) {
182 var callbacks = kernel.get_callbacks_for_msg(msg_id);
182 var callbacks = kernel.get_callbacks_for_msg(msg_id);
183 if (callbacks !== undefined &&
183 if (callbacks !== undefined &&
184 callbacks.iopub !== undefined &&
184 callbacks.iopub !== undefined &&
185 callbacks.iopub.get_cell !== undefined) {
185 callbacks.iopub.get_cell !== undefined) {
186
186
187 return callbacks.iopub.get_cell();
187 return callbacks.iopub.get_cell();
188 }
188 }
189 }
189 }
190
190
191 // Not triggered by a cell or widget (no get_cell callback
191 // Not triggered by a cell or widget (no get_cell callback
192 // exists).
192 // exists).
193 return null;
193 return null;
194 };
194 };
195
195
196 WidgetManager.prototype.callbacks = function (view) {
196 WidgetManager.prototype.callbacks = function (view) {
197 // callback handlers specific a view
197 // callback handlers specific a view
198 var callbacks = {};
198 var callbacks = {};
199 var cell = view.cell;
199 var cell = view.cell;
200 if (cell !== null) {
200 if (cell !== null) {
201 // Try to get output handlers
201 // Try to get output handlers
202 var handle_output = null;
202 var handle_output = null;
203 var handle_clear_output = null;
203 var handle_clear_output = null;
204 if (cell.output_area !== undefined && cell.output_area !== null) {
204 if (cell.output_area !== undefined && cell.output_area !== null) {
205 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
205 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
206 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
206 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
207 }
207 }
208
208
209 // Create callback dict using what is known
209 // Create callback dict using what is known
210 var that = this;
210 var that = this;
211 callbacks = {
211 callbacks = {
212 iopub : {
212 iopub : {
213 output : handle_output,
213 output : handle_output,
214 clear_output : handle_clear_output,
214 clear_output : handle_clear_output,
215
215
216 status : function (msg) {
216 status : function (msg) {
217 view.model._handle_status(msg, that.callbacks(view));
217 view.model._handle_status(msg, that.callbacks(view));
218 },
218 },
219
219
220 // Special function only registered by widget messages.
220 // Special function only registered by widget messages.
221 // Allows us to get the cell for a message so we know
221 // Allows us to get the cell for a message so we know
222 // where to add widgets if the code requires it.
222 // where to add widgets if the code requires it.
223 get_cell : function () {
223 get_cell : function () {
224 return cell;
224 return cell;
225 },
225 },
226 },
226 },
227 };
227 };
228 }
228 }
229 return callbacks;
229 return callbacks;
230 };
230 };
231
231
232
232
233 WidgetManager.prototype.get_model = function (model_id) {
233 WidgetManager.prototype.get_model = function (model_id) {
234 var model = this._models[model_id];
234 var model = this._models[model_id];
235 if (model !== undefined && model.id == model_id) {
235 if (model !== undefined && model.id == model_id) {
236 return model;
236 return model;
237 }
237 }
238 return null;
238 return null;
239 };
239 };
240
240
241
241
242 WidgetManager.prototype.get_kernel = function () {
242 WidgetManager.prototype.get_kernel = function () {
243 if (this.comm_manager === null) {
243 if (this.comm_manager === null) {
244 return null;
244 return null;
245 } else {
245 } else {
246 return this.comm_manager.kernel;
246 return this.comm_manager.kernel;
247 }
247 }
248 };
248 };
249
249
250
250
251 WidgetManager.prototype.on_create_widget = function (callback) {
251 WidgetManager.prototype.on_create_widget = function (callback) {
252 this._create_widget_callback = callback;
252 this._create_widget_callback = callback;
253 };
253 };
254
254
255
255
256 WidgetManager.prototype._handle_create_widget = function (widget_model) {
256 WidgetManager.prototype._handle_create_widget = function (widget_model) {
257 if (this._create_widget_callback) {
257 if (this._create_widget_callback) {
258 try {
258 try {
259 this._create_widget_callback(widget_model);
259 this._create_widget_callback(widget_model);
260 } catch (e) {
260 } catch (e) {
261 console.log("Exception in WidgetManager callback", e, widget_model);
261 console.log("Exception in WidgetManager callback", e, widget_model);
262 }
262 }
263 }
263 }
264 };
264 };
265
265
266
266
267 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
267 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
268 var widget_type_name = msg.content.target_name;
268 var widget_type_name = msg.content.target_name;
269 var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm);
269 var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm);
270 this._models[comm.comm_id] = widget_model; // comm_id == model_id
270 this._models[comm.comm_id] = widget_model; // comm_id == model_id
271 this._handle_create_widget(widget_model);
271 this._handle_create_widget(widget_model);
272 };
272 };
273
273
274 //--------------------------------------------------------------------
274 //--------------------------------------------------------------------
275 // Init code
275 // Init code
276 //--------------------------------------------------------------------
276 //--------------------------------------------------------------------
277 IPython.WidgetManager = WidgetManager;
277 IPython.WidgetManager = WidgetManager;
278 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
278 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
279 IPython.widget_manager = new WidgetManager();
279 IPython.widget_manager = new WidgetManager();
280 }
280 }
281
281
282 return IPython.widget_manager;
282 return IPython.widget_manager;
283 });
283 });
284 }());
284 }());
General Comments 0
You need to be logged in to leave comments. Login now