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