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