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