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