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