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