##// END OF EJS Templates
handle_msg a display_model method.
Jonathan Frederic -
Show More
@@ -1,277 +1,274
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 method must be in widgetmanager.js file instead of
29 29 // widget.js so it can be overwritten for different contexts.
30 30 Backbone.sync = function (method, model, options, error) {
31 31 var result = model._handle_sync(method, options);
32 32 if (options.success) {
33 33 options.success(result);
34 34 }
35 35 };
36 36
37 37 //--------------------------------------------------------------------
38 38 // WidgetManager class
39 39 //--------------------------------------------------------------------
40 40 var WidgetManager = function () {
41 41 this.comm_manager = null;
42 42 this._model_types = {}; /* Dictionary of model type names
43 43 (target_name) and model types. */
44 44 this._view_types = {}; /* Dictionary of view names and view types. */
45 45 this._models = {}; /* Dictionary of model ids and model instances */
46 46 };
47 47
48 48
49 49 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
50 50 this.comm_manager = comm_manager;
51 51
52 52 // Register already-registered widget model types with the comm manager.
53 53 for (var widget_model_name in this._model_types) {
54 54 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
55 55 }
56 56 };
57 57
58 58
59 59 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
60 60 // Register the widget with the comm manager. Make sure to pass this object's context
61 61 // in so `this` works in the call back.
62 62 if (this.comm_manager !== null) {
63 63 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
64 64 }
65 65 this._model_types[widget_model_name] = widget_model_type;
66 66 };
67 67
68 68
69 69 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
70 70 this._view_types[widget_view_name] = widget_view_type;
71 71 };
72 72
73 73
74 WidgetManager.prototype.handle_msg = function(msg, model) {
75 var method = msg.content.data.method;
76 switch (method) {
77 case 'display':
78 var cell = this.get_msg_cell(msg.parent_header.msg_id);
79 if (cell === null) {
80 console.log("Could not determine where the display" +
81 " message was from. Widget will not be displayed");
82 } else {
83 var view = this.create_view(model);
84 if (view !== undefined
85 && cell.widget_subarea !== undefined
86 && cell.widget_subarea !== null) {
87
88 view.cell = cell;
89 cell.widget_area.show();
90 cell.widget_subarea.append(view.$el);
91 }
92 }
93 break;
74 WidgetManager.prototype.display_view = function(msg_id, model) {
75 var cell = this.get_msg_cell(msg_id);
76 if (cell === null) {
77 console.log("Could not determine where the display" +
78 " message was from. Widget will not be displayed");
79 } else {
80 var view = this.create_view(model);
81 if (view !== undefined
82 && cell.widget_subarea !== undefined
83 && cell.widget_subarea !== null) {
84
85 view.cell = cell;
86 cell.widget_area.show();
87 cell.widget_subarea.append(view.$el);
88 }
94 89 }
95 }
90 },
91
96 92
97 93 <<<<<<< HEAD
98 94 <<<<<<< HEAD
99 95 <<<<<<< HEAD
100 96 WidgetManager.prototype.create_view = function(model, view_name, cell) {
101 97 =======
102 98 WidgetManager.prototype.create_view = function(model, view_name, options) {
103 99 >>>>>>> Completely remove cell from model and view.
104 100 view_name = view_name || model.get('default_view_name');
105 101 <<<<<<< HEAD
106 102 =======
107 103 WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
108 104 view_name = view_name || model.get('default_view_name');
109 105 >>>>>>> Add widget view options in creating child views
110 106 var ViewType = this.widget_view_types[view_name];
111 107 =======
112 108 =======
113 109 WidgetManager.prototype.create_view = function(model, options) {
114 110 var view_name = model.get('view_name');
115 111 >>>>>>> remove msg.content.data.view_name and corrosponding create_view param
116 112 var ViewType = this._view_types[view_name];
117 113 >>>>>>> _model_types, _view_types, _models - and document what keys and values are
118 114 if (ViewType !== undefined && ViewType !== null) {
119 115 var view = new ViewType({model: model, widget_manager: this, options: options});
120 116 view.render();
121 117 model.views.push(view);
122 118 model.on('destroy', view.remove, view);
123 119 <<<<<<< HEAD
124 120 <<<<<<< HEAD
125 121 /*
126 122 // TODO: handle view deletion. Don't forget to delete child views
127 123 var that = this;
128 124 view.$el.on("remove", function () {
129 125 var index = that.views.indexOf(view);
130 126 if (index > -1) {
131 127 that.views.splice(index, 1);
132 128 =======
133 129 /*
134 130 // TODO: handle view deletion. Don't forget to delete child views
135 131 var that = this;
136 132 view.$el.on("remove", function () {
137 133 var index = that.views.indexOf(view);
138 134 if (index > -1) {
139 135 that.views.splice(index, 1);
140 136 }
141 137 view.remove(); // Clean-up view
142 138
143 139 // Close the comm if there are no views left.
144 140 if (that.views.length() === 0) {
145 141 //trigger comm close event?
146 142 }
147 143
148 144
149 145 if (that.comm !== undefined) {
150 146 that.comm.close();
151 147 delete that.comm.model; // Delete ref so GC will collect widget model.
152 148 delete that.comm;
153 149 >>>>>>> Add widget view options in creating child views
154 150 }
155 151 view.remove(); // Clean-up view
156 152
157 153 // Close the comm if there are no views left.
158 154 if (that.views.length() === 0) {
159 155 //trigger comm close event?
160 156 }
161 157
162 158
163 159 if (that.comm !== undefined) {
164 160 that.comm.close();
165 161 delete that.comm.model; // Delete ref so GC will collect widget model.
166 162 delete that.comm;
167 163 }
168 164 delete that.model_id; // Delete id from model so widget manager cleans up.
169 165 });
170 166 */
171 167 =======
172 168 >>>>>>> remove msg.content.data.view_name and corrosponding create_view param
173 169 return view;
174 170 }
175 171 },
176 172
173
177 174 WidgetManager.prototype.get_msg_cell = function (msg_id) {
178 175 var cell = null;
179 176 // First, check to see if the msg was triggered by cell execution.
180 177 if (IPython.notebook !== undefined && IPython.notebook !== null) {
181 178 cell = IPython.notebook.get_msg_cell(msg_id);
182 179 }
183 180 if (cell !== null) {
184 181 return cell
185 182 }
186 183 // Second, check to see if a get_cell callback was defined
187 184 // for the message. get_cell callbacks are registered for
188 185 // widget messages, so this block is actually checking to see if the
189 186 // message was triggered by a widget.
190 187 var kernel = this.get_kernel();
191 188 if (kernel !== undefined && kernel !== null) {
192 189 var callbacks = kernel.get_callbacks_for_msg(msg_id);
193 190 if (callbacks !== undefined &&
194 191 callbacks.iopub !== undefined &&
195 192 callbacks.iopub.get_cell !== undefined) {
196 193
197 194 return callbacks.iopub.get_cell();
198 195 }
199 196 }
200 197
201 198 // Not triggered by a cell or widget (no get_cell callback
202 199 // exists).
203 200 return null;
204 201 };
205 202
206 203 WidgetManager.prototype.callbacks = function (view) {
207 204 // callback handlers specific a view
208 205 var callbacks = {};
209 206 var cell = view.cell;
210 207 if (cell !== null) {
211 208 // Try to get output handlers
212 209 var handle_output = null;
213 210 var handle_clear_output = null;
214 211 if (cell.output_area !== undefined && cell.output_area !== null) {
215 212 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
216 213 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
217 214 }
218 215
219 216 // Create callback dict using what is known
220 217 var that = this;
221 218 callbacks = {
222 219 iopub : {
223 220 output : handle_output,
224 221 clear_output : handle_clear_output,
225 222
226 223 status : function (msg) {
227 224 view.model._handle_status(msg, that.callbacks(view));
228 225 },
229 226
230 227 // Special function only registered by widget messages.
231 228 // Allows us to get the cell for a message so we know
232 229 // where to add widgets if the code requires it.
233 230 get_cell : function () {
234 231 return cell;
235 232 },
236 233 },
237 234 };
238 235 }
239 236 return callbacks;
240 237 };
241 238
242 239
243 240 WidgetManager.prototype.get_model = function (model_id) {
244 241 var model = this._models[model_id];
245 242 if (model !== undefined && model.id == model_id) {
246 243 return model;
247 244 }
248 245 return null;
249 246 };
250 247
251 248
252 249 WidgetManager.prototype.get_kernel = function () {
253 250 if (this.comm_manager === null) {
254 251 return null;
255 252 } else {
256 253 return this.comm_manager.kernel;
257 254 }
258 255 };
259 256
260 257
261 258 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
262 259 var widget_type_name = msg.content.target_name;
263 260 var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm);
264 261 this._models[comm.comm_id] = widget_model; // comm_id == model_id
265 262 };
266 263
267 264 //--------------------------------------------------------------------
268 265 // Init code
269 266 //--------------------------------------------------------------------
270 267 IPython.WidgetManager = WidgetManager;
271 268 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
272 269 IPython.widget_manager = new WidgetManager();
273 270 }
274 271
275 272 return IPython.widget_manager;
276 273 });
277 274 }());
@@ -1,312 +1,312
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 // Base Widget Model and View classes
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 **/
16 16
17 17 define(["notebook/js/widgetmanager",
18 18 "underscore",
19 19 "backbone"],
20 20 function(widget_manager, underscore, backbone){
21 21
22 22 //--------------------------------------------------------------------
23 23 // WidgetModel class
24 24 //--------------------------------------------------------------------
25 25 var WidgetModel = Backbone.Model.extend({
26 26 constructor: function (widget_manager, model_id, comm) {
27 27 this.widget_manager = widget_manager;
28 28 this.pending_msgs = 0;
29 29 this.msg_throttle = 3;
30 30 this.msg_buffer = null;
31 31 this.id = model_id;
32 32 this.views = [];
33 33
34 34 if (comm !== undefined) {
35 35 // Remember comm associated with the model.
36 36 this.comm = comm;
37 37 comm.model = this;
38 38
39 39 // Hook comm messages up to model.
40 40 comm.on_close($.proxy(this._handle_comm_closed, this));
41 41 comm.on_msg($.proxy(this._handle_comm_msg, this));
42 42 }
43 43 return Backbone.Model.apply(this);
44 44 },
45 45
46 46 send: function (content, callbacks) {
47 47 if (this.comm !== undefined) {
48 48 var data = {method: 'custom', custom_content: content};
49 49 this.comm.send(data, callbacks);
50 50 }
51 51 },
52 52
53 53 // Handle when a widget is closed.
54 54 _handle_comm_closed: function (msg) {
55 55 this.trigger('comm:close');
56 56 delete this.comm.model; // Delete ref so GC will collect widget model.
57 57 delete this.comm;
58 58 delete this.model_id; // Delete id from model so widget manager cleans up.
59 59 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
60 60 },
61 61
62 62
63 63 // Handle incoming comm msg.
64 64 _handle_comm_msg: function (msg) {
65 65 var method = msg.content.data.method;
66 66 switch (method) {
67 67 case 'update':
68 68 this.apply_update(msg.content.data.state);
69 69 break;
70 70 case 'custom':
71 71 this.trigger('msg:custom', msg.content.data.custom_content);
72 72 break;
73 default:
74 // pass on to widget manager
75 this.widget_manager.handle_msg(msg, this);
73 case 'display':
74 this.widget_manager.display_view(msg.parent_header.msg_id, this);
75 break;
76 76 }
77 77 },
78 78
79 79
80 80 // Handle when a widget is updated via the python side.
81 81 apply_update: function (state) {
82 82 this.updating = true;
83 83 try {
84 84 for (var key in state) {
85 85 if (state.hasOwnProperty(key)) {
86 86 this.set(key, state[key]);
87 87 }
88 88 }
89 89 //TODO: are there callbacks that make sense in this case? If so, attach them here as an option
90 90 this.save();
91 91 } finally {
92 92 this.updating = false;
93 93 }
94 94 },
95 95
96 96
97 97 _handle_status: function (msg, callbacks) {
98 98 //execution_state : ('busy', 'idle', 'starting')
99 99 if (this.comm !== undefined && msg.content.execution_state ==='idle') {
100 100 // Send buffer if this message caused another message to be
101 101 // throttled.
102 102 if (this.msg_buffer !== null &&
103 103 this.msg_throttle === this.pending_msgs) {
104 104 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
105 105 this.comm.send(data, callbacks);
106 106 this.msg_buffer = null;
107 107 } else {
108 108 // Only decrease the pending message count if the buffer
109 109 // doesn't get flushed (sent).
110 110 --this.pending_msgs;
111 111 }
112 112 }
113 113 },
114 114
115 115
116 116 // Custom syncronization logic.
117 117 _handle_sync: function (method, options) {
118 118 var model_json = this.toJSON();
119 119 var attr;
120 120
121 121 // Only send updated state if the state hasn't been changed
122 122 // during an update.
123 123 if (this.comm !== undefined) {
124 124 if (!this.updating) {
125 125 if (this.pending_msgs >= this.msg_throttle) {
126 126 // The throttle has been exceeded, buffer the current msg so
127 127 // it can be sent once the kernel has finished processing
128 128 // some of the existing messages.
129 129 if (method=='patch') {
130 130 if (this.msg_buffer === null) {
131 131 this.msg_buffer = $.extend({}, model_json); // Copy
132 132 }
133 133 for (attr in options.attrs) {
134 134 this.msg_buffer[attr] = options.attrs[attr];
135 135 }
136 136 } else {
137 137 this.msg_buffer = $.extend({}, model_json); // Copy
138 138 }
139 139
140 140 } else {
141 141 // We haven't exceeded the throttle, send the message like
142 142 // normal. If this is a patch operation, just send the
143 143 // changes.
144 144 var send_json = model_json;
145 145 if (method =='patch') {
146 146 send_json = {};
147 147 for (attr in options.attrs) {
148 148 send_json[attr] = options.attrs[attr];
149 149 }
150 150 }
151 151
152 152 var data = {method: 'backbone', sync_data: send_json};
153 153 this.comm.send(data, options.callbacks);
154 154 this.pending_msgs++;
155 155 }
156 156 }
157 157 }
158 158
159 159 // Since the comm is a one-way communication, assume the message
160 160 // arrived.
161 161 return model_json;
162 162 },
163 163
164 164 });
165 165
166 166
167 167 //--------------------------------------------------------------------
168 168 // WidgetView class
169 169 //--------------------------------------------------------------------
170 170 var BaseWidgetView = Backbone.View.extend({
171 171 initialize: function(options) {
172 172 this.model.on('change',this.update,this);
173 173 this.widget_manager = options.widget_manager;
174 174 this.comm_manager = options.widget_manager.comm_manager;
175 175 this.options = options.options;
176 176 this.child_views = [];
177 177 this.model.views.push(this);
178 178 },
179 179
180 180 update: function(){
181 181 // update view to be consistent with this.model
182 182 // triggered on model change
183 183 },
184 184
185 185 child_view: function(model_id, view_name, options) {
186 186 // create and return a child view, given a model id for a model and (optionally) a view name
187 187 // if the view name is not given, it defaults to the model's default view attribute
188 188 var child_model = this.widget_manager.get_model(model_id);
189 189 var child_view = this.widget_manager.create_view(child_model, view_name, options);
190 190 this.child_views[model_id] = child_view;
191 191 return child_view;
192 192 },
193 193
194 194 update_child_views: function(old_list, new_list) {
195 195 // this function takes an old list and new list of model ids
196 196 // views in child_views that correspond to deleted ids are deleted
197 197 // views corresponding to added ids are added child_views
198 198
199 199 // delete old views
200 200 _.each(_.difference(old_list, new_list), function(element, index, list) {
201 201 var view = this.child_views[element];
202 202 delete this.child_views[element];
203 203 view.remove();
204 204 }, this);
205 205
206 206 // add new views
207 207 _.each(_.difference(new_list, old_list), function(element, index, list) {
208 208 // this function adds the view to the child_views dictionary
209 209 this.child_view(element);
210 210 }, this);
211 211 },
212 212
213 213 callbacks: function(){
214 214 return this.widget_manager.callbacks(this);
215 215 }
216 216
217 217 render: function(){
218 218 // render the view. By default, this is only called the first time the view is created
219 219 },
220 220 send: function (content) {
221 221 this.model.send(content, this.callbacks());
222 222 },
223 223
224 224 touch: function () {
225 225 this.model.save(this.model.changedAttributes(), {patch: true, callbacks: this.callbacks()});
226 226 },
227 227
228 228 });
229 229
230 230 var WidgetView = BaseWidgetView.extend({
231 231 initialize: function (options) {
232 232 // TODO: make changes more granular (e.g., trigger on visible:change)
233 233 this.model.on('change', this.update, this);
234 234 this.model.on('msg:custom', this.on_msg, this);
235 235 BaseWidgetView.prototype.initialize.apply(this, arguments);
236 236 },
237 237
238 238 on_msg: function(msg) {
239 239 switch(msg.msg_type) {
240 240 case 'add_class':
241 241 this.add_class(msg.selector, msg.class_list);
242 242 break;
243 243 case 'remove_class':
244 244 this.remove_class(msg.selector, msg.class_list);
245 245 break;
246 246 }
247 247 },
248 248
249 249 add_class: function (selector, class_list) {
250 250 var elements = this._get_selector_element(selector);
251 251 if (elements.length > 0) {
252 252 elements.addClass(class_list);
253 253 }
254 254 },
255 255
256 256 remove_class: function (selector, class_list) {
257 257 var elements = this._get_selector_element(selector);
258 258 if (elements.length > 0) {
259 259 elements.removeClass(class_list);
260 260 }
261 261 },
262 262
263 263 update: function () {
264 264 // the very first update seems to happen before the element is finished rendering
265 265 // so we use setTimeout to give the element time to render
266 266 var e = this.$el;
267 267 var visible = this.model.get('visible');
268 268 setTimeout(function() {e.toggle(visible)},0);
269 269
270 270 var css = this.model.get('_css');
271 271 if (css === undefined) {return;}
272 272 for (var selector in css) {
273 273 if (css.hasOwnProperty(selector)) {
274 274 // Apply the css traits to all elements that match the selector.
275 275 var elements = this._get_selector_element(selector);
276 276 if (elements.length > 0) {
277 277 var css_traits = css[selector];
278 278 for (var css_key in css_traits) {
279 279 if (css_traits.hasOwnProperty(css_key)) {
280 280 elements.css(css_key, css_traits[css_key]);
281 281 }
282 282 }
283 283 }
284 284 }
285 285 }
286 286 },
287 287
288 288 _get_selector_element: function (selector) {
289 289 // Get the elements via the css selector. If the selector is
290 290 // blank, apply the style to the $el_to_style element. If
291 291 // the $el_to_style element is not defined, use apply the
292 292 // style to the view's element.
293 293 var elements;
294 294 if (selector === undefined || selector === null || selector === '') {
295 295 if (this.$el_to_style === undefined) {
296 296 elements = this.$el;
297 297 } else {
298 298 elements = this.$el_to_style;
299 299 }
300 300 } else {
301 301 elements = this.$el.find(selector);
302 302 }
303 303 return elements;
304 304 },
305 305 });
306 306
307 307 IPython.WidgetModel = WidgetModel;
308 308 IPython.WidgetView = WidgetView;
309 309 IPython.BaseWidgetView = BaseWidgetView;
310 310
311 311 return widget_manager;
312 312 });
General Comments 0
You need to be logged in to leave comments. Login now