##// END OF EJS Templates
Add a helper method that acts on the changes made to a list.
Jonathan Frederic -
Show More
@@ -1,325 +1,350 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(widget_manager, underscore, backbone){
20 function(widget_manager, underscore, backbone){
21
21
22 //--------------------------------------------------------------------
22 //--------------------------------------------------------------------
23 // WidgetModel class
23 // WidgetModel class
24 //--------------------------------------------------------------------
24 //--------------------------------------------------------------------
25 var WidgetModel = Backbone.Model.extend({
25 var WidgetModel = Backbone.Model.extend({
26 constructor: function (widget_manager, model_id, comm) {
26 constructor: function (widget_manager, model_id, comm) {
27 // Construcctor
27 // Construcctor
28 //
28 //
29 // Creates a WidgetModel instance.
29 // Creates a WidgetModel instance.
30 //
30 //
31 // Parameters
31 // Parameters
32 // ----------
32 // ----------
33 // widget_manager : WidgetManager instance
33 // widget_manager : WidgetManager instance
34 // model_id : string
34 // model_id : string
35 // An ID unique to this model.
35 // An ID unique to this model.
36 // comm : Comm instance (optional)
36 // comm : Comm instance (optional)
37 this.widget_manager = widget_manager;
37 this.widget_manager = widget_manager;
38 this.pending_msgs = 0;
38 this.pending_msgs = 0;
39 this.msg_throttle = 3;
39 this.msg_throttle = 3;
40 this.msg_buffer = null;
40 this.msg_buffer = null;
41 this.key_value_lock = null;
41 this.key_value_lock = null;
42 this.id = model_id;
42 this.id = model_id;
43 this.views = [];
43 this.views = [];
44
44
45 if (comm !== undefined) {
45 if (comm !== undefined) {
46 // Remember comm associated with the model.
46 // Remember comm associated with the model.
47 this.comm = comm;
47 this.comm = comm;
48 comm.model = this;
48 comm.model = this;
49
49
50 // Hook comm messages up to model.
50 // Hook comm messages up to model.
51 comm.on_close($.proxy(this._handle_comm_closed, this));
51 comm.on_close($.proxy(this._handle_comm_closed, this));
52 comm.on_msg($.proxy(this._handle_comm_msg, this));
52 comm.on_msg($.proxy(this._handle_comm_msg, this));
53 }
53 }
54 return Backbone.Model.apply(this);
54 return Backbone.Model.apply(this);
55 },
55 },
56
56
57 send: function (content, callbacks) {
57 send: function (content, callbacks) {
58 if (this.comm !== undefined) {
58 if (this.comm !== undefined) {
59 var data = {method: 'custom', custom_content: content};
59 var data = {method: 'custom', custom_content: content};
60 this.comm.send(data, callbacks);
60 this.comm.send(data, callbacks);
61 }
61 }
62 },
62 },
63
63
64 // Handle when a widget is closed.
64 // Handle when a widget is closed.
65 _handle_comm_closed: function (msg) {
65 _handle_comm_closed: function (msg) {
66 this.trigger('comm:close');
66 this.trigger('comm:close');
67 delete this.comm.model; // Delete ref so GC will collect widget model.
67 delete this.comm.model; // Delete ref so GC will collect widget model.
68 delete this.comm;
68 delete this.comm;
69 delete this.model_id; // Delete id from model so widget manager cleans up.
69 delete this.model_id; // Delete id from model so widget manager cleans up.
70 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
70 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
71 },
71 },
72
72
73
73
74 // Handle incoming comm msg.
74 // Handle incoming comm msg.
75 _handle_comm_msg: function (msg) {
75 _handle_comm_msg: function (msg) {
76 var method = msg.content.data.method;
76 var method = msg.content.data.method;
77 switch (method) {
77 switch (method) {
78 case 'update':
78 case 'update':
79 this.apply_update(msg.content.data.state);
79 this.apply_update(msg.content.data.state);
80 break;
80 break;
81 case 'custom':
81 case 'custom':
82 this.trigger('msg:custom', msg.content.data.custom_content);
82 this.trigger('msg:custom', msg.content.data.custom_content);
83 break;
83 break;
84 case 'display':
84 case 'display':
85 this.widget_manager.display_view(msg.parent_header.msg_id, this);
85 this.widget_manager.display_view(msg.parent_header.msg_id, this);
86 break;
86 break;
87 }
87 }
88 },
88 },
89
89
90
90
91 // Handle when a widget is updated via the python side.
91 // Handle when a widget is updated via the python side.
92 apply_update: function (state) {
92 apply_update: function (state) {
93 for (var key in state) {
93 for (var key in state) {
94 if (state.hasOwnProperty(key)) {
94 if (state.hasOwnProperty(key)) {
95 var value = state[key];
95 var value = state[key];
96 this.key_value_lock = [key, value];
96 this.key_value_lock = [key, value];
97 try {
97 try {
98 this.set(key, state[key]);
98 this.set(key, state[key]);
99 } finally {
99 } finally {
100 this.key_value_lock = null;
100 this.key_value_lock = null;
101 }
101 }
102 }
102 }
103 }
103 }
104 //TODO: are there callbacks that make sense in this case? If so, attach them here as an option
104 //TODO: are there callbacks that make sense in this case? If so, attach them here as an option
105 this.save();
105 this.save();
106 },
106 },
107
107
108
108
109 _handle_status: function (msg, callbacks) {
109 _handle_status: function (msg, callbacks) {
110 //execution_state : ('busy', 'idle', 'starting')
110 //execution_state : ('busy', 'idle', 'starting')
111 if (this.comm !== undefined && msg.content.execution_state ==='idle') {
111 if (this.comm !== undefined && msg.content.execution_state ==='idle') {
112 // Send buffer if this message caused another message to be
112 // Send buffer if this message caused another message to be
113 // throttled.
113 // throttled.
114 if (this.msg_buffer !== null &&
114 if (this.msg_buffer !== null &&
115 this.msg_throttle === this.pending_msgs) {
115 this.msg_throttle === this.pending_msgs) {
116 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
116 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
117 this.comm.send(data, callbacks);
117 this.comm.send(data, callbacks);
118 this.msg_buffer = null;
118 this.msg_buffer = null;
119 } else {
119 } else {
120 // Only decrease the pending message count if the buffer
120 // Only decrease the pending message count if the buffer
121 // doesn't get flushed (sent).
121 // doesn't get flushed (sent).
122 --this.pending_msgs;
122 --this.pending_msgs;
123 }
123 }
124 }
124 }
125 },
125 },
126
126
127
127
128 // Custom syncronization logic.
128 // Custom syncronization logic.
129 _handle_sync: function (method, options) {
129 _handle_sync: function (method, options) {
130 var model_json = this.toJSON();
130 var model_json = this.toJSON();
131 var attr;
131 var attr;
132
132
133 // Only send updated state if the state hasn't been changed
133 // Only send updated state if the state hasn't been changed
134 // during an update.
134 // during an update.
135 if (this.comm !== undefined) {
135 if (this.comm !== undefined) {
136 if (this.pending_msgs >= this.msg_throttle) {
136 if (this.pending_msgs >= this.msg_throttle) {
137 // The throttle has been exceeded, buffer the current msg so
137 // The throttle has been exceeded, buffer the current msg so
138 // it can be sent once the kernel has finished processing
138 // it can be sent once the kernel has finished processing
139 // some of the existing messages.
139 // some of the existing messages.
140 if (method=='patch') {
140 if (method=='patch') {
141 if (this.msg_buffer === null) {
141 if (this.msg_buffer === null) {
142 this.msg_buffer = $.extend({}, model_json); // Copy
142 this.msg_buffer = $.extend({}, model_json); // Copy
143 }
143 }
144 for (attr in options.attrs) {
144 for (attr in options.attrs) {
145 var value = options.attrs[attr];
145 var value = options.attrs[attr];
146 if (this.key_value_lock === null || attr != this.key_value_lock[0] || value != this.key_value_lock[1]) {
146 if (this.key_value_lock === null || attr != this.key_value_lock[0] || value != this.key_value_lock[1]) {
147 this.msg_buffer[attr] = value;
147 this.msg_buffer[attr] = value;
148 }
148 }
149 }
149 }
150 } else {
150 } else {
151 this.msg_buffer = $.extend({}, model_json); // Copy
151 this.msg_buffer = $.extend({}, model_json); // Copy
152 }
152 }
153
153
154 } else {
154 } else {
155 // We haven't exceeded the throttle, send the message like
155 // We haven't exceeded the throttle, send the message like
156 // normal. If this is a patch operation, just send the
156 // normal. If this is a patch operation, just send the
157 // changes.
157 // changes.
158 var send_json = model_json;
158 var send_json = model_json;
159 if (method =='patch') {
159 if (method =='patch') {
160 send_json = {};
160 send_json = {};
161 for (attr in options.attrs) {
161 for (attr in options.attrs) {
162 var value = options.attrs[attr];
162 var value = options.attrs[attr];
163 if (this.key_value_lock === null || attr != this.key_value_lock[0] || value != this.key_value_lock[1]) {
163 if (this.key_value_lock === null || attr != this.key_value_lock[0] || value != this.key_value_lock[1]) {
164 send_json[attr] = value;
164 send_json[attr] = value;
165 }
165 }
166 }
166 }
167 }
167 }
168
168
169 var data = {method: 'backbone', sync_data: send_json};
169 var data = {method: 'backbone', sync_data: send_json};
170 this.comm.send(data, options.callbacks);
170 this.comm.send(data, options.callbacks);
171 this.pending_msgs++;
171 this.pending_msgs++;
172 }
172 }
173 }
173 }
174
174
175 // Since the comm is a one-way communication, assume the message
175 // Since the comm is a one-way communication, assume the message
176 // arrived.
176 // arrived.
177 return model_json;
177 return model_json;
178 },
178 },
179
179
180 });
180 });
181
181
182
182
183 //--------------------------------------------------------------------
183 //--------------------------------------------------------------------
184 // WidgetView class
184 // WidgetView class
185 //--------------------------------------------------------------------
185 //--------------------------------------------------------------------
186 var WidgetView = Backbone.View.extend({
186 var WidgetView = Backbone.View.extend({
187 initialize: function(parameters) {
187 initialize: function(parameters) {
188 this.model.on('change',this.update,this);
188 this.model.on('change',this.update,this);
189 this.options = parameters.options;
189 this.options = parameters.options;
190 this.child_views = [];
190 this.child_views = [];
191 this.model.views.push(this);
191 this.model.views.push(this);
192 },
192 },
193
193
194 update: function(){
194 update: function(){
195 // update view to be consistent with this.model
195 // update view to be consistent with this.model
196 // triggered on model change
196 // triggered on model change
197 },
197 },
198
198
199 child_view: function(model_id, options) {
199 child_view: function(model_id, options) {
200 // create and return a child view, given a model id for a model and (optionally) a view name
200 // create and return a child view, given a model id for a model and (optionally) a view name
201 // if the view name is not given, it defaults to the model's default view attribute
201 // if the view name is not given, it defaults to the model's default view attribute
202 var child_model = this.widget_manager.get_model(model_id);
202 var child_model = this.widget_manager.get_model(model_id);
203 var child_view = this.widget_manager.create_view(child_model, options);
203 var child_view = this.widget_manager.create_view(child_model, options);
204 this.child_views[model_id] = child_view;
204 this.child_views[model_id] = child_view;
205 return child_view;
205 return child_view;
206 },
206 },
207
207
208 update_child_views: function(old_list, new_list) {
208 update_child_views: function(old_list, new_list) {
209 // this function takes an old list and new list of model ids
209 // this function takes an old list and new list of model ids
210 // views in child_views that correspond to deleted ids are deleted
210 // views in child_views that correspond to deleted ids are deleted
211 // views corresponding to added ids are added child_views
211 // views corresponding to added ids are added child_views
212
212
213 // delete old views
213 // delete old views
214 _.each(_.difference(old_list, new_list), function(element, index, list) {
214 _.each(_.difference(old_list, new_list), function(element, index, list) {
215 var view = this.child_views[element];
215 var view = this.child_views[element];
216 delete this.child_views[element];
216 delete this.child_views[element];
217 view.remove();
217 view.remove();
218 }, this);
218 }, this);
219
219
220 // add new views
220 // add new views
221 _.each(_.difference(new_list, old_list), function(element, index, list) {
221 _.each(_.difference(new_list, old_list), function(element, index, list) {
222 // this function adds the view to the child_views dictionary
222 // this function adds the view to the child_views dictionary
223 this.child_view(element);
223 this.child_view(element);
224 }, this);
224 }, this);
225 },
225 },
226
226
227 do_diff: function(old_list, new_list, removed_callback, added_callback) {
228 // Difference a changed list and call remove and add callbacks for
229 // each removed and added item in the new list.
230 //
231 // Parameters
232 // ----------
233 // old_list : array
234 // new_list : array
235 // removed_callback : Callback(item)
236 // Callback that is called for each item removed.
237 // added_callback : Callback(item)
238 // Callback that is called for each item added.
239
240
241 // removed items
242 _.each(_.difference(old_list, new_list), function(item, index, list) {
243 removed_callback(item);
244 }, this);
245
246 // added items
247 _.each(_.difference(new_list, old_list), function(item, index, list) {
248 added_callback(item);
249 }, this);
250 }
251
227 callbacks: function(){
252 callbacks: function(){
228 return this.widget_manager.callbacks(this);
253 return this.widget_manager.callbacks(this);
229 },
254 },
230
255
231 render: function(){
256 render: function(){
232 // render the view. By default, this is only called the first time the view is created
257 // render the view. By default, this is only called the first time the view is created
233 },
258 },
234 send: function (content) {
259 send: function (content) {
235 this.model.send(content, this.callbacks());
260 this.model.send(content, this.callbacks());
236 },
261 },
237
262
238 touch: function () {
263 touch: function () {
239 this.model.save(this.model.changedAttributes(), {patch: true, callbacks: this.callbacks()});
264 this.model.save(this.model.changedAttributes(), {patch: true, callbacks: this.callbacks()});
240 },
265 },
241
266
242 });
267 });
243
268
244 var DOMWidgetView = WidgetView.extend({
269 var DOMWidgetView = WidgetView.extend({
245 initialize: function (options) {
270 initialize: function (options) {
246 // TODO: make changes more granular (e.g., trigger on visible:change)
271 // TODO: make changes more granular (e.g., trigger on visible:change)
247 this.model.on('change', this.update, this);
272 this.model.on('change', this.update, this);
248 this.model.on('msg:custom', this.on_msg, this);
273 this.model.on('msg:custom', this.on_msg, this);
249 WidgetView.prototype.initialize.apply(this, arguments);
274 WidgetView.prototype.initialize.apply(this, arguments);
250 },
275 },
251
276
252 on_msg: function(msg) {
277 on_msg: function(msg) {
253 switch(msg.msg_type) {
278 switch(msg.msg_type) {
254 case 'add_class':
279 case 'add_class':
255 this.add_class(msg.selector, msg.class_list);
280 this.add_class(msg.selector, msg.class_list);
256 break;
281 break;
257 case 'remove_class':
282 case 'remove_class':
258 this.remove_class(msg.selector, msg.class_list);
283 this.remove_class(msg.selector, msg.class_list);
259 break;
284 break;
260 }
285 }
261 },
286 },
262
287
263 add_class: function (selector, class_list) {
288 add_class: function (selector, class_list) {
264 this._get_selector_element(selector).addClass(class_list);
289 this._get_selector_element(selector).addClass(class_list);
265 },
290 },
266
291
267 remove_class: function (selector, class_list) {
292 remove_class: function (selector, class_list) {
268 this._get_selector_element(selector).removeClass(class_list);
293 this._get_selector_element(selector).removeClass(class_list);
269 },
294 },
270
295
271 update: function () {
296 update: function () {
272 // Update the contents of this view
297 // Update the contents of this view
273 //
298 //
274 // Called when the model is changed. The model may have been
299 // Called when the model is changed. The model may have been
275 // changed by another view or by a state update from the back-end.
300 // changed by another view or by a state update from the back-end.
276 // The very first update seems to happen before the element is
301 // The very first update seems to happen before the element is
277 // finished rendering so we use setTimeout to give the element time
302 // finished rendering so we use setTimeout to give the element time
278 // to render
303 // to render
279 var e = this.$el;
304 var e = this.$el;
280 var visible = this.model.get('visible');
305 var visible = this.model.get('visible');
281 setTimeout(function() {e.toggle(visible)},0);
306 setTimeout(function() {e.toggle(visible)},0);
282
307
283 var css = this.model.get('_css');
308 var css = this.model.get('_css');
284 if (css === undefined) {return;}
309 if (css === undefined) {return;}
285 for (var selector in css) {
310 for (var selector in css) {
286 if (css.hasOwnProperty(selector)) {
311 if (css.hasOwnProperty(selector)) {
287 // Apply the css traits to all elements that match the selector.
312 // Apply the css traits to all elements that match the selector.
288 var elements = this._get_selector_element(selector);
313 var elements = this._get_selector_element(selector);
289 if (elements.length > 0) {
314 if (elements.length > 0) {
290 var css_traits = css[selector];
315 var css_traits = css[selector];
291 for (var css_key in css_traits) {
316 for (var css_key in css_traits) {
292 if (css_traits.hasOwnProperty(css_key)) {
317 if (css_traits.hasOwnProperty(css_key)) {
293 elements.css(css_key, css_traits[css_key]);
318 elements.css(css_key, css_traits[css_key]);
294 }
319 }
295 }
320 }
296 }
321 }
297 }
322 }
298 }
323 }
299 },
324 },
300
325
301 _get_selector_element: function (selector) {
326 _get_selector_element: function (selector) {
302 // Get the elements via the css selector. If the selector is
327 // Get the elements via the css selector. If the selector is
303 // blank, apply the style to the $el_to_style element. If
328 // blank, apply the style to the $el_to_style element. If
304 // the $el_to_style element is not defined, use apply the
329 // the $el_to_style element is not defined, use apply the
305 // style to the view's element.
330 // style to the view's element.
306 var elements;
331 var elements;
307 if (selector === undefined || selector === null || selector === '') {
332 if (selector === undefined || selector === null || selector === '') {
308 if (this.$el_to_style === undefined) {
333 if (this.$el_to_style === undefined) {
309 elements = this.$el;
334 elements = this.$el;
310 } else {
335 } else {
311 elements = this.$el_to_style;
336 elements = this.$el_to_style;
312 }
337 }
313 } else {
338 } else {
314 elements = this.$el.find(selector);
339 elements = this.$el.find(selector);
315 }
340 }
316 return elements;
341 return elements;
317 },
342 },
318 });
343 });
319
344
320 IPython.WidgetModel = WidgetModel;
345 IPython.WidgetModel = WidgetModel;
321 IPython.WidgetView = WidgetView;
346 IPython.WidgetView = WidgetView;
322 IPython.DOMWidgetView = DOMWidgetView;
347 IPython.DOMWidgetView = DOMWidgetView;
323
348
324 return widget_manager;
349 return widget_manager;
325 });
350 });
General Comments 0
You need to be logged in to leave comments. Login now