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