##// END OF EJS Templates
handle_msg a display_model method.
Jonathan Frederic -
Show More
@@ -1,277 +1,274
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 // WidgetModel, WidgetView, and WidgetManager
9 // WidgetModel, WidgetView, and WidgetManager
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * Base Widget classes
12 * Base Widget classes
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule widget
15 * @submodule widget
16 */
16 */
17
17
18 (function () {
18 (function () {
19 "use strict";
19 "use strict";
20
20
21 // Use require.js 'define' method so that require.js is intelligent enough to
21 // Use require.js 'define' method so that require.js is intelligent enough to
22 // syncronously load everything within this file when it is being 'required'
22 // syncronously load everything within this file when it is being 'required'
23 // elsewhere.
23 // elsewhere.
24 define(["underscore",
24 define(["underscore",
25 "backbone",
25 "backbone",
26 ], function (underscore, backbone) {
26 ], function (underscore, backbone) {
27
27
28 // Backbone.sync method must be in widgetmanager.js file instead of
28 // Backbone.sync method must be in widgetmanager.js file instead of
29 // widget.js so it can be overwritten for different contexts.
29 // widget.js so it can be overwritten for different contexts.
30 Backbone.sync = function (method, model, options, error) {
30 Backbone.sync = function (method, model, options, error) {
31 var result = model._handle_sync(method, options);
31 var result = model._handle_sync(method, options);
32 if (options.success) {
32 if (options.success) {
33 options.success(result);
33 options.success(result);
34 }
34 }
35 };
35 };
36
36
37 //--------------------------------------------------------------------
37 //--------------------------------------------------------------------
38 // WidgetManager class
38 // WidgetManager class
39 //--------------------------------------------------------------------
39 //--------------------------------------------------------------------
40 var WidgetManager = function () {
40 var WidgetManager = function () {
41 this.comm_manager = null;
41 this.comm_manager = null;
42 this._model_types = {}; /* Dictionary of model type names
42 this._model_types = {}; /* Dictionary of model type names
43 (target_name) and model types. */
43 (target_name) and model types. */
44 this._view_types = {}; /* Dictionary of view names and view types. */
44 this._view_types = {}; /* Dictionary of view names and view types. */
45 this._models = {}; /* Dictionary of model ids and model instances */
45 this._models = {}; /* Dictionary of model ids and model instances */
46 };
46 };
47
47
48
48
49 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
49 WidgetManager.prototype.attach_comm_manager = function (comm_manager) {
50 this.comm_manager = comm_manager;
50 this.comm_manager = comm_manager;
51
51
52 // Register already-registered widget model types with the comm manager.
52 // Register already-registered widget model types with the comm manager.
53 for (var widget_model_name in this._model_types) {
53 for (var widget_model_name in this._model_types) {
54 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
54 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
55 }
55 }
56 };
56 };
57
57
58
58
59 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
59 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
60 // Register the widget with the comm manager. Make sure to pass this object's context
60 // Register the widget with the comm manager. Make sure to pass this object's context
61 // in so `this` works in the call back.
61 // in so `this` works in the call back.
62 if (this.comm_manager !== null) {
62 if (this.comm_manager !== null) {
63 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
63 this.comm_manager.register_target(widget_model_name, $.proxy(this._handle_comm_open, this));
64 }
64 }
65 this._model_types[widget_model_name] = widget_model_type;
65 this._model_types[widget_model_name] = widget_model_type;
66 };
66 };
67
67
68
68
69 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
69 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
70 this._view_types[widget_view_name] = widget_view_type;
70 this._view_types[widget_view_name] = widget_view_type;
71 };
71 };
72
72
73
73
74 WidgetManager.prototype.handle_msg = function(msg, model) {
74 WidgetManager.prototype.display_view = function(msg_id, model) {
75 var method = msg.content.data.method;
75 var cell = this.get_msg_cell(msg_id);
76 switch (method) {
76 if (cell === null) {
77 case 'display':
77 console.log("Could not determine where the display" +
78 var cell = this.get_msg_cell(msg.parent_header.msg_id);
78 " message was from. Widget will not be displayed");
79 if (cell === null) {
79 } else {
80 console.log("Could not determine where the display" +
80 var view = this.create_view(model);
81 " message was from. Widget will not be displayed");
81 if (view !== undefined
82 } else {
82 && cell.widget_subarea !== undefined
83 var view = this.create_view(model);
83 && cell.widget_subarea !== null) {
84 if (view !== undefined
84
85 && cell.widget_subarea !== undefined
85 view.cell = cell;
86 && cell.widget_subarea !== null) {
86 cell.widget_area.show();
87
87 cell.widget_subarea.append(view.$el);
88 view.cell = cell;
88 }
89 cell.widget_area.show();
90 cell.widget_subarea.append(view.$el);
91 }
92 }
93 break;
94 }
89 }
95 }
90 },
91
96
92
97 <<<<<<< HEAD
93 <<<<<<< HEAD
98 <<<<<<< HEAD
94 <<<<<<< HEAD
99 <<<<<<< HEAD
95 <<<<<<< HEAD
100 WidgetManager.prototype.create_view = function(model, view_name, cell) {
96 WidgetManager.prototype.create_view = function(model, view_name, cell) {
101 =======
97 =======
102 WidgetManager.prototype.create_view = function(model, view_name, options) {
98 WidgetManager.prototype.create_view = function(model, view_name, options) {
103 >>>>>>> Completely remove cell from model and view.
99 >>>>>>> Completely remove cell from model and view.
104 view_name = view_name || model.get('default_view_name');
100 view_name = view_name || model.get('default_view_name');
105 <<<<<<< HEAD
101 <<<<<<< HEAD
106 =======
102 =======
107 WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
103 WidgetManager.prototype.create_view = function(model, view_name, cell, options) {
108 view_name = view_name || model.get('default_view_name');
104 view_name = view_name || model.get('default_view_name');
109 >>>>>>> Add widget view options in creating child views
105 >>>>>>> Add widget view options in creating child views
110 var ViewType = this.widget_view_types[view_name];
106 var ViewType = this.widget_view_types[view_name];
111 =======
107 =======
112 =======
108 =======
113 WidgetManager.prototype.create_view = function(model, options) {
109 WidgetManager.prototype.create_view = function(model, options) {
114 var view_name = model.get('view_name');
110 var view_name = model.get('view_name');
115 >>>>>>> remove msg.content.data.view_name and corrosponding create_view param
111 >>>>>>> remove msg.content.data.view_name and corrosponding create_view param
116 var ViewType = this._view_types[view_name];
112 var ViewType = this._view_types[view_name];
117 >>>>>>> _model_types, _view_types, _models - and document what keys and values are
113 >>>>>>> _model_types, _view_types, _models - and document what keys and values are
118 if (ViewType !== undefined && ViewType !== null) {
114 if (ViewType !== undefined && ViewType !== null) {
119 var view = new ViewType({model: model, widget_manager: this, options: options});
115 var view = new ViewType({model: model, widget_manager: this, options: options});
120 view.render();
116 view.render();
121 model.views.push(view);
117 model.views.push(view);
122 model.on('destroy', view.remove, view);
118 model.on('destroy', view.remove, view);
123 <<<<<<< HEAD
119 <<<<<<< HEAD
124 <<<<<<< HEAD
120 <<<<<<< HEAD
125 /*
121 /*
126 // TODO: handle view deletion. Don't forget to delete child views
122 // TODO: handle view deletion. Don't forget to delete child views
127 var that = this;
123 var that = this;
128 view.$el.on("remove", function () {
124 view.$el.on("remove", function () {
129 var index = that.views.indexOf(view);
125 var index = that.views.indexOf(view);
130 if (index > -1) {
126 if (index > -1) {
131 that.views.splice(index, 1);
127 that.views.splice(index, 1);
132 =======
128 =======
133 /*
129 /*
134 // TODO: handle view deletion. Don't forget to delete child views
130 // TODO: handle view deletion. Don't forget to delete child views
135 var that = this;
131 var that = this;
136 view.$el.on("remove", function () {
132 view.$el.on("remove", function () {
137 var index = that.views.indexOf(view);
133 var index = that.views.indexOf(view);
138 if (index > -1) {
134 if (index > -1) {
139 that.views.splice(index, 1);
135 that.views.splice(index, 1);
140 }
136 }
141 view.remove(); // Clean-up view
137 view.remove(); // Clean-up view
142
138
143 // Close the comm if there are no views left.
139 // Close the comm if there are no views left.
144 if (that.views.length() === 0) {
140 if (that.views.length() === 0) {
145 //trigger comm close event?
141 //trigger comm close event?
146 }
142 }
147
143
148
144
149 if (that.comm !== undefined) {
145 if (that.comm !== undefined) {
150 that.comm.close();
146 that.comm.close();
151 delete that.comm.model; // Delete ref so GC will collect widget model.
147 delete that.comm.model; // Delete ref so GC will collect widget model.
152 delete that.comm;
148 delete that.comm;
153 >>>>>>> Add widget view options in creating child views
149 >>>>>>> Add widget view options in creating child views
154 }
150 }
155 view.remove(); // Clean-up view
151 view.remove(); // Clean-up view
156
152
157 // Close the comm if there are no views left.
153 // Close the comm if there are no views left.
158 if (that.views.length() === 0) {
154 if (that.views.length() === 0) {
159 //trigger comm close event?
155 //trigger comm close event?
160 }
156 }
161
157
162
158
163 if (that.comm !== undefined) {
159 if (that.comm !== undefined) {
164 that.comm.close();
160 that.comm.close();
165 delete that.comm.model; // Delete ref so GC will collect widget model.
161 delete that.comm.model; // Delete ref so GC will collect widget model.
166 delete that.comm;
162 delete that.comm;
167 }
163 }
168 delete that.model_id; // Delete id from model so widget manager cleans up.
164 delete that.model_id; // Delete id from model so widget manager cleans up.
169 });
165 });
170 */
166 */
171 =======
167 =======
172 >>>>>>> remove msg.content.data.view_name and corrosponding create_view param
168 >>>>>>> remove msg.content.data.view_name and corrosponding create_view param
173 return view;
169 return view;
174 }
170 }
175 },
171 },
176
172
173
177 WidgetManager.prototype.get_msg_cell = function (msg_id) {
174 WidgetManager.prototype.get_msg_cell = function (msg_id) {
178 var cell = null;
175 var cell = null;
179 // First, check to see if the msg was triggered by cell execution.
176 // First, check to see if the msg was triggered by cell execution.
180 if (IPython.notebook !== undefined && IPython.notebook !== null) {
177 if (IPython.notebook !== undefined && IPython.notebook !== null) {
181 cell = IPython.notebook.get_msg_cell(msg_id);
178 cell = IPython.notebook.get_msg_cell(msg_id);
182 }
179 }
183 if (cell !== null) {
180 if (cell !== null) {
184 return cell
181 return cell
185 }
182 }
186 // Second, check to see if a get_cell callback was defined
183 // Second, check to see if a get_cell callback was defined
187 // for the message. get_cell callbacks are registered for
184 // for the message. get_cell callbacks are registered for
188 // widget messages, so this block is actually checking to see if the
185 // widget messages, so this block is actually checking to see if the
189 // message was triggered by a widget.
186 // message was triggered by a widget.
190 var kernel = this.get_kernel();
187 var kernel = this.get_kernel();
191 if (kernel !== undefined && kernel !== null) {
188 if (kernel !== undefined && kernel !== null) {
192 var callbacks = kernel.get_callbacks_for_msg(msg_id);
189 var callbacks = kernel.get_callbacks_for_msg(msg_id);
193 if (callbacks !== undefined &&
190 if (callbacks !== undefined &&
194 callbacks.iopub !== undefined &&
191 callbacks.iopub !== undefined &&
195 callbacks.iopub.get_cell !== undefined) {
192 callbacks.iopub.get_cell !== undefined) {
196
193
197 return callbacks.iopub.get_cell();
194 return callbacks.iopub.get_cell();
198 }
195 }
199 }
196 }
200
197
201 // Not triggered by a cell or widget (no get_cell callback
198 // Not triggered by a cell or widget (no get_cell callback
202 // exists).
199 // exists).
203 return null;
200 return null;
204 };
201 };
205
202
206 WidgetManager.prototype.callbacks = function (view) {
203 WidgetManager.prototype.callbacks = function (view) {
207 // callback handlers specific a view
204 // callback handlers specific a view
208 var callbacks = {};
205 var callbacks = {};
209 var cell = view.cell;
206 var cell = view.cell;
210 if (cell !== null) {
207 if (cell !== null) {
211 // Try to get output handlers
208 // Try to get output handlers
212 var handle_output = null;
209 var handle_output = null;
213 var handle_clear_output = null;
210 var handle_clear_output = null;
214 if (cell.output_area !== undefined && cell.output_area !== null) {
211 if (cell.output_area !== undefined && cell.output_area !== null) {
215 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
212 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
216 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
213 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
217 }
214 }
218
215
219 // Create callback dict using what is known
216 // Create callback dict using what is known
220 var that = this;
217 var that = this;
221 callbacks = {
218 callbacks = {
222 iopub : {
219 iopub : {
223 output : handle_output,
220 output : handle_output,
224 clear_output : handle_clear_output,
221 clear_output : handle_clear_output,
225
222
226 status : function (msg) {
223 status : function (msg) {
227 view.model._handle_status(msg, that.callbacks(view));
224 view.model._handle_status(msg, that.callbacks(view));
228 },
225 },
229
226
230 // Special function only registered by widget messages.
227 // Special function only registered by widget messages.
231 // Allows us to get the cell for a message so we know
228 // Allows us to get the cell for a message so we know
232 // where to add widgets if the code requires it.
229 // where to add widgets if the code requires it.
233 get_cell : function () {
230 get_cell : function () {
234 return cell;
231 return cell;
235 },
232 },
236 },
233 },
237 };
234 };
238 }
235 }
239 return callbacks;
236 return callbacks;
240 };
237 };
241
238
242
239
243 WidgetManager.prototype.get_model = function (model_id) {
240 WidgetManager.prototype.get_model = function (model_id) {
244 var model = this._models[model_id];
241 var model = this._models[model_id];
245 if (model !== undefined && model.id == model_id) {
242 if (model !== undefined && model.id == model_id) {
246 return model;
243 return model;
247 }
244 }
248 return null;
245 return null;
249 };
246 };
250
247
251
248
252 WidgetManager.prototype.get_kernel = function () {
249 WidgetManager.prototype.get_kernel = function () {
253 if (this.comm_manager === null) {
250 if (this.comm_manager === null) {
254 return null;
251 return null;
255 } else {
252 } else {
256 return this.comm_manager.kernel;
253 return this.comm_manager.kernel;
257 }
254 }
258 };
255 };
259
256
260
257
261 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
258 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
262 var widget_type_name = msg.content.target_name;
259 var widget_type_name = msg.content.target_name;
263 var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm);
260 var widget_model = new this._model_types[widget_type_name](this, comm.comm_id, comm);
264 this._models[comm.comm_id] = widget_model; // comm_id == model_id
261 this._models[comm.comm_id] = widget_model; // comm_id == model_id
265 };
262 };
266
263
267 //--------------------------------------------------------------------
264 //--------------------------------------------------------------------
268 // Init code
265 // Init code
269 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
270 IPython.WidgetManager = WidgetManager;
267 IPython.WidgetManager = WidgetManager;
271 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
268 if (IPython.widget_manager === undefined || IPython.widget_manager === null) {
272 IPython.widget_manager = new WidgetManager();
269 IPython.widget_manager = new WidgetManager();
273 }
270 }
274
271
275 return IPython.widget_manager;
272 return IPython.widget_manager;
276 });
273 });
277 }());
274 }());
@@ -1,312 +1,312
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 this.widget_manager = widget_manager;
27 this.widget_manager = widget_manager;
28 this.pending_msgs = 0;
28 this.pending_msgs = 0;
29 this.msg_throttle = 3;
29 this.msg_throttle = 3;
30 this.msg_buffer = null;
30 this.msg_buffer = null;
31 this.id = model_id;
31 this.id = model_id;
32 this.views = [];
32 this.views = [];
33
33
34 if (comm !== undefined) {
34 if (comm !== undefined) {
35 // Remember comm associated with the model.
35 // Remember comm associated with the model.
36 this.comm = comm;
36 this.comm = comm;
37 comm.model = this;
37 comm.model = this;
38
38
39 // Hook comm messages up to model.
39 // Hook comm messages up to model.
40 comm.on_close($.proxy(this._handle_comm_closed, this));
40 comm.on_close($.proxy(this._handle_comm_closed, this));
41 comm.on_msg($.proxy(this._handle_comm_msg, this));
41 comm.on_msg($.proxy(this._handle_comm_msg, this));
42 }
42 }
43 return Backbone.Model.apply(this);
43 return Backbone.Model.apply(this);
44 },
44 },
45
45
46 send: function (content, callbacks) {
46 send: function (content, callbacks) {
47 if (this.comm !== undefined) {
47 if (this.comm !== undefined) {
48 var data = {method: 'custom', custom_content: content};
48 var data = {method: 'custom', custom_content: content};
49 this.comm.send(data, callbacks);
49 this.comm.send(data, callbacks);
50 }
50 }
51 },
51 },
52
52
53 // Handle when a widget is closed.
53 // Handle when a widget is closed.
54 _handle_comm_closed: function (msg) {
54 _handle_comm_closed: function (msg) {
55 this.trigger('comm:close');
55 this.trigger('comm:close');
56 delete this.comm.model; // Delete ref so GC will collect widget model.
56 delete this.comm.model; // Delete ref so GC will collect widget model.
57 delete this.comm;
57 delete this.comm;
58 delete this.model_id; // Delete id from model so widget manager cleans up.
58 delete this.model_id; // Delete id from model so widget manager cleans up.
59 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
59 // TODO: Handle deletion, like this.destroy(), and delete views, etc.
60 },
60 },
61
61
62
62
63 // Handle incoming comm msg.
63 // Handle incoming comm msg.
64 _handle_comm_msg: function (msg) {
64 _handle_comm_msg: function (msg) {
65 var method = msg.content.data.method;
65 var method = msg.content.data.method;
66 switch (method) {
66 switch (method) {
67 case 'update':
67 case 'update':
68 this.apply_update(msg.content.data.state);
68 this.apply_update(msg.content.data.state);
69 break;
69 break;
70 case 'custom':
70 case 'custom':
71 this.trigger('msg:custom', msg.content.data.custom_content);
71 this.trigger('msg:custom', msg.content.data.custom_content);
72 break;
72 break;
73 default:
73 case 'display':
74 // pass on to widget manager
74 this.widget_manager.display_view(msg.parent_header.msg_id, this);
75 this.widget_manager.handle_msg(msg, this);
75 break;
76 }
76 }
77 },
77 },
78
78
79
79
80 // Handle when a widget is updated via the python side.
80 // Handle when a widget is updated via the python side.
81 apply_update: function (state) {
81 apply_update: function (state) {
82 this.updating = true;
82 this.updating = true;
83 try {
83 try {
84 for (var key in state) {
84 for (var key in state) {
85 if (state.hasOwnProperty(key)) {
85 if (state.hasOwnProperty(key)) {
86 this.set(key, state[key]);
86 this.set(key, state[key]);
87 }
87 }
88 }
88 }
89 //TODO: are there callbacks that make sense in this case? If so, attach them here as an option
89 //TODO: are there callbacks that make sense in this case? If so, attach them here as an option
90 this.save();
90 this.save();
91 } finally {
91 } finally {
92 this.updating = false;
92 this.updating = false;
93 }
93 }
94 },
94 },
95
95
96
96
97 _handle_status: function (msg, callbacks) {
97 _handle_status: function (msg, callbacks) {
98 //execution_state : ('busy', 'idle', 'starting')
98 //execution_state : ('busy', 'idle', 'starting')
99 if (this.comm !== undefined && msg.content.execution_state ==='idle') {
99 if (this.comm !== undefined && msg.content.execution_state ==='idle') {
100 // Send buffer if this message caused another message to be
100 // Send buffer if this message caused another message to be
101 // throttled.
101 // throttled.
102 if (this.msg_buffer !== null &&
102 if (this.msg_buffer !== null &&
103 this.msg_throttle === this.pending_msgs) {
103 this.msg_throttle === this.pending_msgs) {
104 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
104 var data = {method: 'backbone', sync_method: 'update', sync_data: this.msg_buffer};
105 this.comm.send(data, callbacks);
105 this.comm.send(data, callbacks);
106 this.msg_buffer = null;
106 this.msg_buffer = null;
107 } else {
107 } else {
108 // Only decrease the pending message count if the buffer
108 // Only decrease the pending message count if the buffer
109 // doesn't get flushed (sent).
109 // doesn't get flushed (sent).
110 --this.pending_msgs;
110 --this.pending_msgs;
111 }
111 }
112 }
112 }
113 },
113 },
114
114
115
115
116 // Custom syncronization logic.
116 // Custom syncronization logic.
117 _handle_sync: function (method, options) {
117 _handle_sync: function (method, options) {
118 var model_json = this.toJSON();
118 var model_json = this.toJSON();
119 var attr;
119 var attr;
120
120
121 // Only send updated state if the state hasn't been changed
121 // Only send updated state if the state hasn't been changed
122 // during an update.
122 // during an update.
123 if (this.comm !== undefined) {
123 if (this.comm !== undefined) {
124 if (!this.updating) {
124 if (!this.updating) {
125 if (this.pending_msgs >= this.msg_throttle) {
125 if (this.pending_msgs >= this.msg_throttle) {
126 // The throttle has been exceeded, buffer the current msg so
126 // The throttle has been exceeded, buffer the current msg so
127 // it can be sent once the kernel has finished processing
127 // it can be sent once the kernel has finished processing
128 // some of the existing messages.
128 // some of the existing messages.
129 if (method=='patch') {
129 if (method=='patch') {
130 if (this.msg_buffer === null) {
130 if (this.msg_buffer === null) {
131 this.msg_buffer = $.extend({}, model_json); // Copy
131 this.msg_buffer = $.extend({}, model_json); // Copy
132 }
132 }
133 for (attr in options.attrs) {
133 for (attr in options.attrs) {
134 this.msg_buffer[attr] = options.attrs[attr];
134 this.msg_buffer[attr] = options.attrs[attr];
135 }
135 }
136 } else {
136 } else {
137 this.msg_buffer = $.extend({}, model_json); // Copy
137 this.msg_buffer = $.extend({}, model_json); // Copy
138 }
138 }
139
139
140 } else {
140 } else {
141 // We haven't exceeded the throttle, send the message like
141 // We haven't exceeded the throttle, send the message like
142 // normal. If this is a patch operation, just send the
142 // normal. If this is a patch operation, just send the
143 // changes.
143 // changes.
144 var send_json = model_json;
144 var send_json = model_json;
145 if (method =='patch') {
145 if (method =='patch') {
146 send_json = {};
146 send_json = {};
147 for (attr in options.attrs) {
147 for (attr in options.attrs) {
148 send_json[attr] = options.attrs[attr];
148 send_json[attr] = options.attrs[attr];
149 }
149 }
150 }
150 }
151
151
152 var data = {method: 'backbone', sync_data: send_json};
152 var data = {method: 'backbone', sync_data: send_json};
153 this.comm.send(data, options.callbacks);
153 this.comm.send(data, options.callbacks);
154 this.pending_msgs++;
154 this.pending_msgs++;
155 }
155 }
156 }
156 }
157 }
157 }
158
158
159 // Since the comm is a one-way communication, assume the message
159 // Since the comm is a one-way communication, assume the message
160 // arrived.
160 // arrived.
161 return model_json;
161 return model_json;
162 },
162 },
163
163
164 });
164 });
165
165
166
166
167 //--------------------------------------------------------------------
167 //--------------------------------------------------------------------
168 // WidgetView class
168 // WidgetView class
169 //--------------------------------------------------------------------
169 //--------------------------------------------------------------------
170 var BaseWidgetView = Backbone.View.extend({
170 var BaseWidgetView = Backbone.View.extend({
171 initialize: function(options) {
171 initialize: function(options) {
172 this.model.on('change',this.update,this);
172 this.model.on('change',this.update,this);
173 this.widget_manager = options.widget_manager;
173 this.widget_manager = options.widget_manager;
174 this.comm_manager = options.widget_manager.comm_manager;
174 this.comm_manager = options.widget_manager.comm_manager;
175 this.options = options.options;
175 this.options = options.options;
176 this.child_views = [];
176 this.child_views = [];
177 this.model.views.push(this);
177 this.model.views.push(this);
178 },
178 },
179
179
180 update: function(){
180 update: function(){
181 // update view to be consistent with this.model
181 // update view to be consistent with this.model
182 // triggered on model change
182 // triggered on model change
183 },
183 },
184
184
185 child_view: function(model_id, view_name, options) {
185 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
186 // 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
187 // 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);
188 var child_model = this.widget_manager.get_model(model_id);
189 var child_view = this.widget_manager.create_view(child_model, view_name, options);
189 var child_view = this.widget_manager.create_view(child_model, view_name, options);
190 this.child_views[model_id] = child_view;
190 this.child_views[model_id] = child_view;
191 return child_view;
191 return child_view;
192 },
192 },
193
193
194 update_child_views: function(old_list, new_list) {
194 update_child_views: function(old_list, new_list) {
195 // this function takes an old list and new list of model ids
195 // this function takes an old list and new list of model ids
196 // views in child_views that correspond to deleted ids are deleted
196 // views in child_views that correspond to deleted ids are deleted
197 // views corresponding to added ids are added child_views
197 // views corresponding to added ids are added child_views
198
198
199 // delete old views
199 // delete old views
200 _.each(_.difference(old_list, new_list), function(element, index, list) {
200 _.each(_.difference(old_list, new_list), function(element, index, list) {
201 var view = this.child_views[element];
201 var view = this.child_views[element];
202 delete this.child_views[element];
202 delete this.child_views[element];
203 view.remove();
203 view.remove();
204 }, this);
204 }, this);
205
205
206 // add new views
206 // add new views
207 _.each(_.difference(new_list, old_list), function(element, index, list) {
207 _.each(_.difference(new_list, old_list), function(element, index, list) {
208 // this function adds the view to the child_views dictionary
208 // this function adds the view to the child_views dictionary
209 this.child_view(element);
209 this.child_view(element);
210 }, this);
210 }, this);
211 },
211 },
212
212
213 callbacks: function(){
213 callbacks: function(){
214 return this.widget_manager.callbacks(this);
214 return this.widget_manager.callbacks(this);
215 }
215 }
216
216
217 render: function(){
217 render: function(){
218 // render the view. By default, this is only called the first time the view is created
218 // render the view. By default, this is only called the first time the view is created
219 },
219 },
220 send: function (content) {
220 send: function (content) {
221 this.model.send(content, this.callbacks());
221 this.model.send(content, this.callbacks());
222 },
222 },
223
223
224 touch: function () {
224 touch: function () {
225 this.model.save(this.model.changedAttributes(), {patch: true, callbacks: this.callbacks()});
225 this.model.save(this.model.changedAttributes(), {patch: true, callbacks: this.callbacks()});
226 },
226 },
227
227
228 });
228 });
229
229
230 var WidgetView = BaseWidgetView.extend({
230 var WidgetView = BaseWidgetView.extend({
231 initialize: function (options) {
231 initialize: function (options) {
232 // TODO: make changes more granular (e.g., trigger on visible:change)
232 // TODO: make changes more granular (e.g., trigger on visible:change)
233 this.model.on('change', this.update, this);
233 this.model.on('change', this.update, this);
234 this.model.on('msg:custom', this.on_msg, this);
234 this.model.on('msg:custom', this.on_msg, this);
235 BaseWidgetView.prototype.initialize.apply(this, arguments);
235 BaseWidgetView.prototype.initialize.apply(this, arguments);
236 },
236 },
237
237
238 on_msg: function(msg) {
238 on_msg: function(msg) {
239 switch(msg.msg_type) {
239 switch(msg.msg_type) {
240 case 'add_class':
240 case 'add_class':
241 this.add_class(msg.selector, msg.class_list);
241 this.add_class(msg.selector, msg.class_list);
242 break;
242 break;
243 case 'remove_class':
243 case 'remove_class':
244 this.remove_class(msg.selector, msg.class_list);
244 this.remove_class(msg.selector, msg.class_list);
245 break;
245 break;
246 }
246 }
247 },
247 },
248
248
249 add_class: function (selector, class_list) {
249 add_class: function (selector, class_list) {
250 var elements = this._get_selector_element(selector);
250 var elements = this._get_selector_element(selector);
251 if (elements.length > 0) {
251 if (elements.length > 0) {
252 elements.addClass(class_list);
252 elements.addClass(class_list);
253 }
253 }
254 },
254 },
255
255
256 remove_class: function (selector, class_list) {
256 remove_class: function (selector, class_list) {
257 var elements = this._get_selector_element(selector);
257 var elements = this._get_selector_element(selector);
258 if (elements.length > 0) {
258 if (elements.length > 0) {
259 elements.removeClass(class_list);
259 elements.removeClass(class_list);
260 }
260 }
261 },
261 },
262
262
263 update: function () {
263 update: function () {
264 // the very first update seems to happen before the element is finished rendering
264 // 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
265 // so we use setTimeout to give the element time to render
266 var e = this.$el;
266 var e = this.$el;
267 var visible = this.model.get('visible');
267 var visible = this.model.get('visible');
268 setTimeout(function() {e.toggle(visible)},0);
268 setTimeout(function() {e.toggle(visible)},0);
269
269
270 var css = this.model.get('_css');
270 var css = this.model.get('_css');
271 if (css === undefined) {return;}
271 if (css === undefined) {return;}
272 for (var selector in css) {
272 for (var selector in css) {
273 if (css.hasOwnProperty(selector)) {
273 if (css.hasOwnProperty(selector)) {
274 // Apply the css traits to all elements that match the selector.
274 // Apply the css traits to all elements that match the selector.
275 var elements = this._get_selector_element(selector);
275 var elements = this._get_selector_element(selector);
276 if (elements.length > 0) {
276 if (elements.length > 0) {
277 var css_traits = css[selector];
277 var css_traits = css[selector];
278 for (var css_key in css_traits) {
278 for (var css_key in css_traits) {
279 if (css_traits.hasOwnProperty(css_key)) {
279 if (css_traits.hasOwnProperty(css_key)) {
280 elements.css(css_key, css_traits[css_key]);
280 elements.css(css_key, css_traits[css_key]);
281 }
281 }
282 }
282 }
283 }
283 }
284 }
284 }
285 }
285 }
286 },
286 },
287
287
288 _get_selector_element: function (selector) {
288 _get_selector_element: function (selector) {
289 // Get the elements via the css selector. If the selector is
289 // Get the elements via the css selector. If the selector is
290 // blank, apply the style to the $el_to_style element. If
290 // blank, apply the style to the $el_to_style element. If
291 // the $el_to_style element is not defined, use apply the
291 // the $el_to_style element is not defined, use apply the
292 // style to the view's element.
292 // style to the view's element.
293 var elements;
293 var elements;
294 if (selector === undefined || selector === null || selector === '') {
294 if (selector === undefined || selector === null || selector === '') {
295 if (this.$el_to_style === undefined) {
295 if (this.$el_to_style === undefined) {
296 elements = this.$el;
296 elements = this.$el;
297 } else {
297 } else {
298 elements = this.$el_to_style;
298 elements = this.$el_to_style;
299 }
299 }
300 } else {
300 } else {
301 elements = this.$el.find(selector);
301 elements = this.$el.find(selector);
302 }
302 }
303 return elements;
303 return elements;
304 },
304 },
305 });
305 });
306
306
307 IPython.WidgetModel = WidgetModel;
307 IPython.WidgetModel = WidgetModel;
308 IPython.WidgetView = WidgetView;
308 IPython.WidgetView = WidgetView;
309 IPython.BaseWidgetView = BaseWidgetView;
309 IPython.BaseWidgetView = BaseWidgetView;
310
310
311 return widget_manager;
311 return widget_manager;
312 });
312 });
General Comments 0
You need to be logged in to leave comments. Login now