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