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