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