Show More
@@ -20,8 +20,8 b'' | |||||
20 | // Use require.js 'define' method so that require.js is intelligent enough to |
|
20 | // Use require.js 'define' method so that require.js is intelligent enough to | |
21 | // syncronously load everything within this file when it is being 'required' |
|
21 | // syncronously load everything within this file when it is being 'required' | |
22 | // elsewhere. |
|
22 | // elsewhere. | |
23 |
define([" |
|
23 | define(["../../components/underscore/underscore-min.js", | |
24 |
" |
|
24 | "../../components/backbone/backbone-min.js", | |
25 | ], function(){ |
|
25 | ], function(){ | |
26 |
|
26 | |||
27 | // Only run once on a notebook. |
|
27 | // Only run once on a notebook. | |
@@ -31,15 +31,236 b' define(["static/components/underscore/underscore-min.js",' | |||||
31 | // WidgetModel class |
|
31 | // WidgetModel class | |
32 | //-------------------------------------------------------------------- |
|
32 | //-------------------------------------------------------------------- | |
33 | var WidgetModel = Backbone.Model.extend({ |
|
33 | var WidgetModel = Backbone.Model.extend({ | |
34 | apply: function(sender) { |
|
34 | constructor: function(comm_manager, comm, widget_view_types) { | |
|
35 | this.comm_manager = comm_manager; | |||
|
36 | this.widget_view_types = widget_view_types; | |||
|
37 | this.pending_msgs = 0; | |||
|
38 | this.msg_throttle = 3; | |||
|
39 | this.msg_buffer = {}; | |||
|
40 | this.views = {}; | |||
|
41 | ||||
|
42 | // Remember comm associated with the model. | |||
|
43 | this.comm = comm; | |||
|
44 | comm.model = this; | |||
|
45 | ||||
|
46 | // Hook comm messages up to model. | |||
|
47 | comm.on_close($.proxy(this.handle_comm_closed, this)); | |||
|
48 | comm.on_msg($.proxy(this.handle_comm_msg, this)); | |||
|
49 | ||||
|
50 | return Backbone.Model.apply(this); | |||
|
51 | }, | |||
|
52 | ||||
|
53 | ||||
|
54 | update_other_views: function(caller) { | |||
|
55 | this.last_modified_view = caller; | |||
35 | this.save(this.changedAttributes(), {patch: true}); |
|
56 | this.save(this.changedAttributes(), {patch: true}); | |
36 |
|
57 | |||
37 | for (var index in this.views) { |
|
58 | for (var cell_index in this.views) { | |
38 | var view = this.views[index]; |
|
59 | var view = this.views[cell_index]; | |
39 |
if (view !== |
|
60 | if (view !== caller) { | |
40 | view.refresh(); |
|
61 | view.refresh(); | |
41 | } |
|
62 | } | |
42 | } |
|
63 | } | |
|
64 | }, | |||
|
65 | ||||
|
66 | ||||
|
67 | handle_status: function (output_area, msg) { | |||
|
68 | //execution_state : ('busy', 'idle', 'starting') | |||
|
69 | if (msg.content.execution_state=='idle') { | |||
|
70 | ||||
|
71 | // Send buffer if this message caused another message to be | |||
|
72 | // throttled. | |||
|
73 | if (this.msg_throttle == this.pending_msgs && | |||
|
74 | this.msg_buffer.length > 0) { | |||
|
75 | ||||
|
76 | var output_area = this._get_msg_output_area(msg); | |||
|
77 | var callbacks = this._make_callbacks(output_area); | |||
|
78 | var data = {sync_method: 'patch', sync_data: this.msg_buffer}; | |||
|
79 | comm.send(data, callbacks); | |||
|
80 | this.msg_buffer = {}; | |||
|
81 | } else { | |||
|
82 | ||||
|
83 | // Only decrease the pending message count if the buffer | |||
|
84 | // doesn't get flushed (sent). | |||
|
85 | --this.pending_msgs; | |||
|
86 | } | |||
|
87 | } | |||
|
88 | }, | |||
|
89 | ||||
|
90 | ||||
|
91 | // Custom syncronization logic. | |||
|
92 | handle_sync: function (method, options) { | |||
|
93 | var model_json = this.toJSON(); | |||
|
94 | ||||
|
95 | // Only send updated state if the state hasn't been changed | |||
|
96 | // during an update. | |||
|
97 | if (!this.updating) { | |||
|
98 | if (this.pending_msgs >= this.msg_throttle) { | |||
|
99 | // The throttle has been exceeded, buffer the current msg so | |||
|
100 | // it can be sent once the kernel has finished processing | |||
|
101 | // some of the existing messages. | |||
|
102 | if (method=='patch') { | |||
|
103 | for (var attr in options.attrs) { | |||
|
104 | this.msg_buffer[attr] = options.attrs[attr]; | |||
|
105 | } | |||
|
106 | } else { | |||
|
107 | this.msg_buffer = $.extend({}, model_json); // Copy | |||
|
108 | } | |||
|
109 | ||||
|
110 | } else { | |||
|
111 | // We haven't exceeded the throttle, send the message like | |||
|
112 | // normal. If this is a patch operation, just send the | |||
|
113 | // changes. | |||
|
114 | var send_json = model_json; | |||
|
115 | if (method=='patch') { | |||
|
116 | send_json = {}; | |||
|
117 | for (var attr in options.attrs) { | |||
|
118 | send_json[attr] = options.attrs[attr]; | |||
|
119 | } | |||
|
120 | } | |||
|
121 | ||||
|
122 | var data = {sync_method: method, sync_data: send_json}; | |||
|
123 | var output_area = this._get_view_output_area(this.last_modified_view); | |||
|
124 | var callbacks = this._make_callbacks(); | |||
|
125 | this.comm.send(data, callbacks); | |||
|
126 | this.pending_msgs++; | |||
|
127 | } | |||
|
128 | } | |||
|
129 | ||||
|
130 | // Since the comm is a one-way communication, assume the message | |||
|
131 | // arrived. | |||
|
132 | return model_json; | |||
|
133 | }, | |||
|
134 | ||||
|
135 | ||||
|
136 | // Handle incomming comm msg. | |||
|
137 | handle_comm_msg: function (comm, msg) { | |||
|
138 | var method = msg.content.data.method; | |||
|
139 | switch (method){ | |||
|
140 | case 'display': | |||
|
141 | ||||
|
142 | ////////////////////////// TODO: Get cell index via currently executing cell. | |||
|
143 | var cell_index = IPython.notebook.get_selected_index()-1; | |||
|
144 | ||||
|
145 | this.display_view(msg.content.data.view_name, | |||
|
146 | msg.content.data.parent, | |||
|
147 | cell_index); | |||
|
148 | break; | |||
|
149 | case 'update': | |||
|
150 | this.handle_update(msg.content.data.state); | |||
|
151 | break; | |||
|
152 | } | |||
|
153 | } | |||
|
154 | ||||
|
155 | ||||
|
156 | // Handle when a widget is updated via the python side. | |||
|
157 | handle_update: function (state) { | |||
|
158 | this.updating = true; | |||
|
159 | try { | |||
|
160 | for (var key in state) { | |||
|
161 | if (state.hasOwnProperty(key)) { | |||
|
162 | if (key == "_css"){ | |||
|
163 | this.css = state[key]; | |||
|
164 | } else { | |||
|
165 | this.set(key, state[key]); | |||
|
166 | } | |||
|
167 | } | |||
|
168 | } | |||
|
169 | this.id = this.comm.comm_id; | |||
|
170 | this.save(); | |||
|
171 | } finally { | |||
|
172 | this.updating = false; | |||
|
173 | } | |||
|
174 | } | |||
|
175 | ||||
|
176 | ||||
|
177 | // Handle when a widget is closed. | |||
|
178 | handle_comm_closed: function (msg) { | |||
|
179 | for (var cell_index in this.views) { | |||
|
180 | var view = this.views[cell_index]; | |||
|
181 | view.remove(); | |||
|
182 | } | |||
|
183 | } | |||
|
184 | ||||
|
185 | ||||
|
186 | // Create view that represents the model. | |||
|
187 | display_view = function (view_name, parent_comm_id, cell_index) { | |||
|
188 | var view = new this.widget_view_types[view_name]({model: this}); | |||
|
189 | view.render(); | |||
|
190 | this.views[cell_index] = view; | |||
|
191 | view.cell_index = cell_index; | |||
|
192 | ||||
|
193 | // Handle when the view element is remove from the page. | |||
|
194 | var that = this; | |||
|
195 | view.$el.on("remove", function(){ | |||
|
196 | var index = that.views.indexOf(view); | |||
|
197 | if (index > -1) { | |||
|
198 | that.views.splice(index, 1); | |||
|
199 | } | |||
|
200 | view.remove(); // Clean-up view | |||
|
201 | ||||
|
202 | // Close the comm if there are no views left. | |||
|
203 | if (that.views.length()==0) { | |||
|
204 | that.comm.close(); | |||
|
205 | } | |||
|
206 | }); | |||
|
207 | ||||
|
208 | var display_child = null; | |||
|
209 | if (parent_comm_id != undefined) { | |||
|
210 | var parent_comm = this.comm_manager.comms[parent_comm_id]; | |||
|
211 | var parent_model = parent_comm.model; | |||
|
212 | var parent_view = parent_model.views[cell_id]; | |||
|
213 | if (parent_view.display_child != undefined) { | |||
|
214 | display_child = parent_view.display_child; | |||
|
215 | } | |||
|
216 | } | |||
|
217 | ||||
|
218 | if (display_child != null) { | |||
|
219 | display_child(view.$el); | |||
|
220 | } else { | |||
|
221 | // No parent view is defined or exists. Add the view's | |||
|
222 | // element to cell's widget div. | |||
|
223 | var cell = IPython.notebook.get_cell(cell_index); | |||
|
224 | cell.element.find('.widget_area').find('.widget_subarea') | |||
|
225 | .append(view.$el) | |||
|
226 | .parent().show(); // Show the widget_area (parent of widget_subarea) | |||
|
227 | ||||
|
228 | } | |||
|
229 | ||||
|
230 | // Update the view based on the model contents. | |||
|
231 | view.refresh(); | |||
|
232 | } | |||
|
233 | ||||
|
234 | ||||
|
235 | // Build a callback dict. | |||
|
236 | _make_callbacks: function (output_area) { | |||
|
237 | var callbacks = {}; | |||
|
238 | if (output_area != null) { | |||
|
239 | var that = this; | |||
|
240 | callbacks = { | |||
|
241 | iopub : { | |||
|
242 | output : $.proxy(output_area.handle_output, output_area), | |||
|
243 | clear_output : $.proxy(output_area.handle_clear_output, output_area), | |||
|
244 | status : function(msg){ | |||
|
245 | that.handle_status(output_area, msg); | |||
|
246 | }, | |||
|
247 | }, | |||
|
248 | }; | |||
|
249 | } | |||
|
250 | return callbacks; | |||
|
251 | }, | |||
|
252 | ||||
|
253 | ||||
|
254 | // Get the cell output area corresponding to the view. | |||
|
255 | _get_view_output_area : function (view) { | |||
|
256 | return this._get_cell_output_area(view.cell_index); | |||
|
257 | } | |||
|
258 | ||||
|
259 | ||||
|
260 | // Get the cell output area corresponding to the cell id. | |||
|
261 | _get_cell_output_area : function (cell_id) { | |||
|
262 | var cell = IPython.notebook.get_cell(cell_id) | |||
|
263 | return cell.output_area; | |||
43 | } |
|
264 | } | |
44 | }); |
|
265 | }); | |
45 |
|
266 | |||
@@ -53,8 +274,8 b' define(["static/components/underscore/underscore-min.js",' | |||||
53 | this.model.on('change',this.refresh,this); |
|
274 | this.model.on('change',this.refresh,this); | |
54 | }, |
|
275 | }, | |
55 |
|
276 | |||
56 |
|
|
277 | update: function() { | |
57 | this.update(); |
|
278 | var results = Backbone.Model.prototype.update.call(this); | |
58 |
|
279 | |||
59 | if (this.model.css != undefined) { |
|
280 | if (this.model.css != undefined) { | |
60 | for (var selector in this.model.css) { |
|
281 | for (var selector in this.model.css) { | |
@@ -79,6 +300,7 b' define(["static/components/underscore/underscore-min.js",' | |||||
79 | } |
|
300 | } | |
80 | } |
|
301 | } | |
81 | } |
|
302 | } | |
|
303 | return results; | |||
82 | }, |
|
304 | }, | |
83 | }); |
|
305 | }); | |
84 |
|
306 | |||
@@ -86,240 +308,43 b' define(["static/components/underscore/underscore-min.js",' | |||||
86 | //-------------------------------------------------------------------- |
|
308 | //-------------------------------------------------------------------- | |
87 | // WidgetManager class |
|
309 | // WidgetManager class | |
88 | //-------------------------------------------------------------------- |
|
310 | //-------------------------------------------------------------------- | |
89 | // Public constructor |
|
|||
90 | var WidgetManager = function(comm_manager){ |
|
311 | var WidgetManager = function(comm_manager){ | |
91 | this.comm_manager = comm_manager; |
|
312 | this.comm_manager = comm_manager; | |
92 | this.widget_model_types = {}; |
|
313 | this.widget_model_types = {}; | |
93 | this.widget_view_types = {}; |
|
314 | this.widget_view_types = {}; | |
94 | this.model_widget_views = {}; |
|
|||
95 | this.pending_msgs = 0; |
|
|||
96 | this.msg_throttle = 3; |
|
|||
97 | this.msg_buffer = {}; |
|
|||
98 |
|
315 | |||
99 | var that = this; |
|
316 | var that = this; | |
100 | Backbone.sync = function(method, model, options, error) { |
|
317 | Backbone.sync = function(method, model, options, error) { | |
101 |
var result = |
|
318 | var result = model.handle_sync(method, options); | |
102 | if (options.success) { |
|
319 | if (options.success) { | |
103 | options.success(result); |
|
320 | options.success(result); | |
104 | } |
|
321 | } | |
105 | }; |
|
322 | }; | |
106 | } |
|
323 | } | |
107 |
|
324 | |||
108 | // Register a widget model type. |
|
325 | ||
109 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { |
|
326 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
110 |
|
||||
111 | // Register the widget with the comm manager. Make sure to pass this object's context |
|
327 | // Register the widget with the comm manager. Make sure to pass this object's context | |
112 | // in so `this` works in the call back. |
|
328 | // in so `this` works in the call back. | |
113 | this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); |
|
329 | this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); | |
114 |
|
||||
115 | // Register the types of the model and view correspong to this widget type. Later |
|
|||
116 | // the widget manager will initialize these when the comm is opened. |
|
|||
117 | this.widget_model_types[widget_model_name] = widget_model_type; |
|
330 | this.widget_model_types[widget_model_name] = widget_model_type; | |
118 | } |
|
331 | } | |
119 |
|
332 | |||
120 | // Register a widget view type. |
|
333 | ||
121 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { |
|
334 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
122 | this.widget_view_types[widget_view_name] = widget_view_type; |
|
335 | this.widget_view_types[widget_view_name] = widget_view_type; | |
123 | } |
|
336 | } | |
124 |
|
337 | |||
125 | // Handle when a comm is opened. |
|
338 | ||
126 | WidgetManager.prototype.handle_com_open = function (comm, msg) { |
|
339 | WidgetManager.prototype.handle_com_open = function (comm, msg) { | |
127 | var widget_type_name = msg.content.target_name; |
|
340 | var widget_type_name = msg.content.target_name; | |
128 |
|
341 | var widget_model = new this.widget_model_types[widget_type_name](this.comm_manager, comm, view_types); | ||
129 | // Create the corresponding widget model. |
|
|||
130 | var widget_model = new this.widget_model_types[widget_type_name]; |
|
|||
131 |
|
||||
132 | // Remember comm associated with the model. |
|
|||
133 | widget_model.comm = comm; |
|
|||
134 | comm.model = widget_model; |
|
|||
135 |
|
||||
136 | // Create an array to remember the views associated with the model. |
|
|||
137 | widget_model.views = []; |
|
|||
138 |
|
||||
139 | // Add a handle to delete the control when the comm is closed. |
|
|||
140 | var that = this; |
|
|||
141 | var handle_close = function(msg) { |
|
|||
142 | that.handle_comm_closed(comm, msg); |
|
|||
143 | } |
|
|||
144 | comm.on_close(handle_close); |
|
|||
145 |
|
||||
146 | // Handle incomming messages. |
|
|||
147 | var handle_msg = function(msg) { |
|
|||
148 | that.handle_comm_msg(comm, msg); |
|
|||
149 | } |
|
|||
150 | comm.on_msg(handle_msg); |
|
|||
151 | } |
|
342 | } | |
152 |
|
343 | |||
153 | // Create view that represents the model. |
|
|||
154 | WidgetManager.prototype.show_view = function (widget_area, widget_model, widget_view_name) { |
|
|||
155 | var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model}); |
|
|||
156 | widget_view.render(); |
|
|||
157 | widget_model.views.push(widget_view); |
|
|||
158 |
|
||||
159 | // Handle when the view element is remove from the page. |
|
|||
160 | widget_view.$el.on("remove", function(){ |
|
|||
161 | var index = widget_model.views.indexOf(widget_view); |
|
|||
162 | if (index > -1) { |
|
|||
163 | widget_model.views.splice(index, 1); |
|
|||
164 | } |
|
|||
165 | widget_view.remove(); // Clean-up view |
|
|||
166 |
|
||||
167 | // Close the comm if there are no views left. |
|
|||
168 | if (widget_model.views.length()==0) { |
|
|||
169 | widget_model.comm.close(); |
|
|||
170 | } |
|
|||
171 | }); |
|
|||
172 |
|
||||
173 | // Add the view's element to cell's widget div. |
|
|||
174 | widget_area |
|
|||
175 | .append(widget_view.$el) |
|
|||
176 | .parent().show(); // Show the widget_area (parent of widget_subarea) |
|
|||
177 |
|
||||
178 | // Update the view based on the model contents. |
|
|||
179 | widget_view.refresh(); |
|
|||
180 | } |
|
|||
181 |
|
||||
182 | // Handle incomming comm msg. |
|
|||
183 | WidgetManager.prototype.handle_comm_msg = function (comm, msg) { |
|
|||
184 | // Different logic for different methods. |
|
|||
185 | var method = msg.content.data.method; |
|
|||
186 | switch (method){ |
|
|||
187 | case 'show': |
|
|||
188 |
|
||||
189 | // TODO: Get cell from registered output handler. |
|
|||
190 | var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1); |
|
|||
191 | var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea'); |
|
|||
192 |
|
||||
193 | if (msg.content.data.parent != undefined) { |
|
|||
194 | var find_results = widget_subarea.find("." + msg.content.data.parent); |
|
|||
195 | if (find_results.length > 0) { |
|
|||
196 | widget_subarea = find_results; |
|
|||
197 | } |
|
|||
198 | } |
|
|||
199 |
|
||||
200 | this.show_view(widget_subarea, comm.model, msg.content.data.view_name); |
|
|||
201 | break; |
|
|||
202 | case 'update': |
|
|||
203 | this.handle_update(comm, msg.content.data.state); |
|
|||
204 | break; |
|
|||
205 | } |
|
|||
206 | } |
|
|||
207 |
|
||||
208 | // Handle when a widget is updated via the python side. |
|
|||
209 | WidgetManager.prototype.handle_update = function (comm, state) { |
|
|||
210 | this.updating = true; |
|
|||
211 | for (var key in state) { |
|
|||
212 | if (state.hasOwnProperty(key)) { |
|
|||
213 | if (key == "_css"){ |
|
|||
214 | comm.model.css = state[key]; |
|
|||
215 | } else { |
|
|||
216 | comm.model.set(key, state[key]); |
|
|||
217 | } |
|
|||
218 | } |
|
|||
219 | } |
|
|||
220 | comm.model.id = comm.comm_id; |
|
|||
221 | comm.model.save(); |
|
|||
222 | this.updating = false; |
|
|||
223 | } |
|
|||
224 |
|
||||
225 | // Handle when a widget is closed. |
|
|||
226 | WidgetManager.prototype.handle_comm_closed = function (comm, msg) { |
|
|||
227 | for (var view_index in comm.model.views) { |
|
|||
228 | var view = comm.model.views[view_index]; |
|
|||
229 | view.remove(); |
|
|||
230 | } |
|
|||
231 | } |
|
|||
232 |
|
||||
233 | // Handle when a msg status changes in the kernel. |
|
|||
234 | WidgetManager.prototype.handle_status = function (msg) { |
|
|||
235 | //execution_state : ('busy', 'idle', 'starting') |
|
|||
236 | if (msg.content.execution_state=='idle') { |
|
|||
237 | // Send buffer if this message caused another message to be |
|
|||
238 | // throttled. |
|
|||
239 | if (this.msg_throttle == --this.pending_msgs && |
|
|||
240 | this.msg_buffer.length > 0) { |
|
|||
241 | var outputarea = this._get_msg_outputarea(msg); |
|
|||
242 | var callbacks = this._make_callbacks(outputarea); |
|
|||
243 | var data = {sync_method: 'patch', sync_data: this.msg_buffer}; |
|
|||
244 | comm.send(data, callbacks); |
|
|||
245 | this.pending_msgs++; |
|
|||
246 | this.msg_buffer = {}; |
|
|||
247 | } |
|
|||
248 | } |
|
|||
249 | } |
|
|||
250 |
|
||||
251 | // Get the cell output area corresponding to the comm. |
|
|||
252 | WidgetManager.prototype._get_comm_outputarea = function (comm) { |
|
|||
253 | // TODO: get element from comm instead of guessing |
|
|||
254 | var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()) |
|
|||
255 | return cell.output_area; |
|
|||
256 | } |
|
|||
257 |
|
||||
258 | // Get the cell output area corresponding to the msg_id. |
|
|||
259 | WidgetManager.prototype._get_msg_outputarea = function (msg) { |
|
|||
260 | // TODO: get element from msg_id instead of guessing |
|
|||
261 | // msg.parent_header.msg_id |
|
|||
262 | var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()) |
|
|||
263 | return cell.output_area; |
|
|||
264 | } |
|
|||
265 |
|
||||
266 | // Build a callback dict. |
|
|||
267 | WidgetManager.prototype._make_callbacks = function (outputarea) { |
|
|||
268 | var callbacks = {}; |
|
|||
269 | if (outputarea != null) { |
|
|||
270 | callbacks = { |
|
|||
271 | iopub : { |
|
|||
272 | status : $.proxy(this.handle_status, this), |
|
|||
273 | output : $.proxy(outputarea.handle_output, outputarea), |
|
|||
274 | clear_output : $.proxy(outputarea.handle_clear_output, outputarea)} |
|
|||
275 | }; |
|
|||
276 | } |
|
|||
277 | return callbacks; |
|
|||
278 | } |
|
|||
279 |
|
||||
280 | // Send widget state to python backend. |
|
|||
281 | WidgetManager.prototype.handle_sync = function (method, model, options) { |
|
|||
282 | var model_json = model.toJSON(); |
|
|||
283 |
|
||||
284 | // Only send updated state if the state hasn't been changed during an update. |
|
|||
285 | if (!this.updating) { |
|
|||
286 | // Create a callback for the output if the widget has an output area associate with it. |
|
|||
287 | var callbacks = this._make_callbacks(this._get_comm_outputarea(comm)); |
|
|||
288 | var comm = model.comm; |
|
|||
289 |
|
||||
290 | if (this.pending_msgs >= this.msg_throttle) { |
|
|||
291 | // The throttle has been exceeded, buffer the current msg so |
|
|||
292 | // it can be sent once the kernel has finished processing |
|
|||
293 | // some of the existing messages. |
|
|||
294 | if (method=='patch') { |
|
|||
295 | for (var attr in options.attrs) { |
|
|||
296 | this.msg_buffer[attr] = options.attrs[attr]; |
|
|||
297 | } |
|
|||
298 | } else { |
|
|||
299 | this.msg_buffer = $.extend({}, model_json); // Copy |
|
|||
300 | } |
|
|||
301 | } else { |
|
|||
302 | // We haven't exceeded the throttle, send the message like |
|
|||
303 | // normal. If this is a patch operation, just send the |
|
|||
304 | // changes. |
|
|||
305 | var send_json = model_json; |
|
|||
306 | if (method=='patch') { |
|
|||
307 | send_json = {}; |
|
|||
308 | for (var attr in options.attrs) { |
|
|||
309 | send_json[attr] = options.attrs[attr]; |
|
|||
310 | } |
|
|||
311 | } |
|
|||
312 | var data = {sync_method: method, sync_data: send_json}; |
|
|||
313 | comm.send(data, callbacks); |
|
|||
314 | this.pending_msgs++; |
|
|||
315 | } |
|
|||
316 | } |
|
|||
317 |
|
||||
318 | // Since the comm is a one-way communication, assume the message |
|
|||
319 | // arrived. |
|
|||
320 | return model_json; |
|
|||
321 | } |
|
|||
322 |
|
344 | |||
|
345 | //-------------------------------------------------------------------- | |||
|
346 | // Init code | |||
|
347 | //-------------------------------------------------------------------- | |||
323 | IPython.WidgetManager = WidgetManager; |
|
348 | IPython.WidgetManager = WidgetManager; | |
324 | IPython.WidgetModel = WidgetModel; |
|
349 | IPython.WidgetModel = WidgetModel; | |
325 | IPython.WidgetView = WidgetView; |
|
350 | IPython.WidgetView = WidgetView; |
@@ -1,5 +1,5 b'' | |||||
1 |
|
1 | |||
2 | require(["notebook/js/widget"], function(){ |
|
2 | require(["../static/notebook/js/widget"], function(){ | |
3 |
|
3 | |||
4 | var BoolWidgetModel = IPython.WidgetModel.extend({}); |
|
4 | var BoolWidgetModel = IPython.WidgetModel.extend({}); | |
5 | IPython.notebook.widget_manager.register_widget_model('BoolWidgetModel', BoolWidgetModel); |
|
5 | IPython.notebook.widget_manager.register_widget_model('BoolWidgetModel', BoolWidgetModel); | |
@@ -9,8 +9,7 b' require(["notebook/js/widget"], function(){' | |||||
9 | // Called when view is rendered. |
|
9 | // Called when view is rendered. | |
10 | render : function(){ |
|
10 | render : function(){ | |
11 | this.$el |
|
11 | this.$el | |
12 | .html('') |
|
12 | .html(''); | |
13 | .addClass(this.model.comm.comm_id); |
|
|||
14 |
|
13 | |||
15 | var $label = $('<label />') |
|
14 | var $label = $('<label />') | |
16 | .addClass('checkbox') |
|
15 | .addClass('checkbox') | |
@@ -39,7 +38,7 b' require(["notebook/js/widget"], function(){' | |||||
39 | handleChanged: function(e) { |
|
38 | handleChanged: function(e) { | |
40 | this.user_invoked_update = true; |
|
39 | this.user_invoked_update = true; | |
41 | this.model.set('value', $(e.target).prop('checked')); |
|
40 | this.model.set('value', $(e.target).prop('checked')); | |
42 |
this.model. |
|
41 | this.model.update_other_views(this); | |
43 | this.user_invoked_update = false; |
|
42 | this.user_invoked_update = false; | |
44 | }, |
|
43 | }, | |
45 | }); |
|
44 | }); | |
@@ -51,8 +50,7 b' require(["notebook/js/widget"], function(){' | |||||
51 | // Called when view is rendered. |
|
50 | // Called when view is rendered. | |
52 | render : function(){ |
|
51 | render : function(){ | |
53 | this.$el |
|
52 | this.$el | |
54 | .html('') |
|
53 | .html(''); | |
55 | .addClass(this.model.comm.comm_id); |
|
|||
56 |
|
54 | |||
57 | this.$button = $('<button />') |
|
55 | this.$button = $('<button />') | |
58 | .addClass('btn') |
|
56 | .addClass('btn') | |
@@ -82,7 +80,7 b' require(["notebook/js/widget"], function(){' | |||||
82 | handleClick: function(e) { |
|
80 | handleClick: function(e) { | |
83 | this.user_invoked_update = true; |
|
81 | this.user_invoked_update = true; | |
84 | this.model.set('value', ! $(e.target).hasClass('active')); |
|
82 | this.model.set('value', ! $(e.target).hasClass('active')); | |
85 |
this.model. |
|
83 | this.model.update_other_views(this); | |
86 | this.user_invoked_update = false; |
|
84 | this.user_invoked_update = false; | |
87 | }, |
|
85 | }, | |
88 | }); |
|
86 | }); |
@@ -1,5 +1,5 b'' | |||||
1 |
|
1 | |||
2 | require(["notebook/js/widget"], function(){ |
|
2 | require(["../static/notebook/js/widget"], function(){ | |
3 |
|
3 | |||
4 | var ButtonWidgetModel = IPython.WidgetModel.extend({}); |
|
4 | var ButtonWidgetModel = IPython.WidgetModel.extend({}); | |
5 | IPython.notebook.widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel); |
|
5 | IPython.notebook.widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel); | |
@@ -13,7 +13,7 b' require(["notebook/js/widget"], function(){' | |||||
13 | .addClass('btn') |
|
13 | .addClass('btn') | |
14 | .click(function() { |
|
14 | .click(function() { | |
15 | that.model.set('clicks', that.model.get('clicks') + 1); |
|
15 | that.model.set('clicks', that.model.get('clicks') + 1); | |
16 |
that.model. |
|
16 | that.model.update_other_views(that); | |
17 | }); |
|
17 | }); | |
18 |
|
18 | |||
19 | this.update(); // Set defaults. |
|
19 | this.update(); // Set defaults. |
@@ -1,4 +1,4 b'' | |||||
1 | require(["notebook/js/widget"], function(){ |
|
1 | require(["../static/notebook/js/widget"], function(){ | |
2 | var ContainerModel = IPython.WidgetModel.extend({}); |
|
2 | var ContainerModel = IPython.WidgetModel.extend({}); | |
3 | IPython.notebook.widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel); |
|
3 | IPython.notebook.widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel); | |
4 |
|
4 | |||
@@ -6,8 +6,7 b' require(["notebook/js/widget"], function(){' | |||||
6 |
|
6 | |||
7 | render : function(){ |
|
7 | render : function(){ | |
8 | this.$el = $('<div />') |
|
8 | this.$el = $('<div />') | |
9 | .addClass('widget_container') |
|
9 | .addClass('widget_container'); | |
10 | .addClass(this.model.comm.comm_id); |
|
|||
11 | }, |
|
10 | }, | |
12 |
|
11 | |||
13 | update : function(){ |
|
12 | update : function(){ | |
@@ -24,6 +23,10 b' require(["notebook/js/widget"], function(){' | |||||
24 | } |
|
23 | } | |
25 | } |
|
24 | } | |
26 | }, |
|
25 | }, | |
|
26 | ||||
|
27 | display_child : function($element) { | |||
|
28 | this.$el.append($element); | |||
|
29 | }, | |||
27 | }); |
|
30 | }); | |
28 |
|
31 | |||
29 | IPython.notebook.widget_manager.register_widget_view('ContainerView', ContainerView); |
|
32 | IPython.notebook.widget_manager.register_widget_view('ContainerView', ContainerView); |
@@ -1,4 +1,4 b'' | |||||
1 | require(["notebook/js/widget"], function(){ |
|
1 | require(["../static/notebook/js/widget"], function(){ | |
2 | var FloatWidgetModel = IPython.WidgetModel.extend({}); |
|
2 | var FloatWidgetModel = IPython.WidgetModel.extend({}); | |
3 | IPython.notebook.widget_manager.register_widget_model('FloatWidgetModel', FloatWidgetModel); |
|
3 | IPython.notebook.widget_manager.register_widget_model('FloatWidgetModel', FloatWidgetModel); | |
4 | }); No newline at end of file |
|
4 | }); |
@@ -1,4 +1,4 b'' | |||||
1 | require(["notebook/js/widget"], function(){ |
|
1 | require(["../static/notebook/js/widget"], function(){ | |
2 | var FloatRangeWidgetModel = IPython.WidgetModel.extend({}); |
|
2 | var FloatRangeWidgetModel = IPython.WidgetModel.extend({}); | |
3 | IPython.notebook.widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel); |
|
3 | IPython.notebook.widget_manager.register_widget_model('FloatRangeWidgetModel', FloatRangeWidgetModel); | |
4 |
|
4 | |||
@@ -7,8 +7,7 b' require(["notebook/js/widget"], function(){' | |||||
7 | // Called when view is rendered. |
|
7 | // Called when view is rendered. | |
8 | render : function(){ |
|
8 | render : function(){ | |
9 | this.$el |
|
9 | this.$el | |
10 | .html('') |
|
10 | .html(''); | |
11 | .addClass(this.model.comm.comm_id); |
|
|||
12 | this.$slider = $('<div />') |
|
11 | this.$slider = $('<div />') | |
13 | .slider({}) |
|
12 | .slider({}) | |
14 | .addClass('slider'); |
|
13 | .addClass('slider'); | |
@@ -41,7 +40,7 b' require(["notebook/js/widget"], function(){' | |||||
41 | events: { "slide" : "handleSliderChange" }, |
|
40 | events: { "slide" : "handleSliderChange" }, | |
42 | handleSliderChange: function(e, ui) { |
|
41 | handleSliderChange: function(e, ui) { | |
43 | this.model.set('value', ui.value); |
|
42 | this.model.set('value', ui.value); | |
44 |
this.model. |
|
43 | this.model.update_other_views(this); | |
45 | }, |
|
44 | }, | |
46 | }); |
|
45 | }); | |
47 |
|
46 | |||
@@ -53,8 +52,7 b' require(["notebook/js/widget"], function(){' | |||||
53 | // Called when view is rendered. |
|
52 | // Called when view is rendered. | |
54 | render : function(){ |
|
53 | render : function(){ | |
55 | this.$el |
|
54 | this.$el | |
56 | .html('') |
|
55 | .html(''); | |
57 | .addClass(this.model.comm.comm_id); |
|
|||
58 | this.$textbox = $('<input type="text" />') |
|
56 | this.$textbox = $('<input type="text" />') | |
59 | .addClass('input') |
|
57 | .addClass('input') | |
60 | .appendTo(this.$el); |
|
58 | .appendTo(this.$el); | |
@@ -106,7 +104,7 b' require(["notebook/js/widget"], function(){' | |||||
106 | if (numericalValue != this.model.get('value')) { |
|
104 | if (numericalValue != this.model.get('value')) { | |
107 | this.changing = true; |
|
105 | this.changing = true; | |
108 | this.model.set('value', numericalValue); |
|
106 | this.model.set('value', numericalValue); | |
109 |
this.model. |
|
107 | this.model.update_other_views(this); | |
110 | this.changing = false; |
|
108 | this.changing = false; | |
111 | } |
|
109 | } | |
112 | } |
|
110 | } |
@@ -1,4 +1,4 b'' | |||||
1 | require(["notebook/js/widget"], function(){ |
|
1 | require(["../static/notebook/js/widget"], function(){ | |
2 | var IntWidgetModel = IPython.WidgetModel.extend({}); |
|
2 | var IntWidgetModel = IPython.WidgetModel.extend({}); | |
3 | IPython.notebook.widget_manager.register_widget_model('IntWidgetModel', IntWidgetModel); |
|
3 | IPython.notebook.widget_manager.register_widget_model('IntWidgetModel', IntWidgetModel); | |
4 | }); No newline at end of file |
|
4 | }); |
@@ -1,4 +1,4 b'' | |||||
1 | require(["notebook/js/widget"], function(){ |
|
1 | require(["../static/notebook/js/widget"], function(){ | |
2 | var IntRangeWidgetModel = IPython.WidgetModel.extend({}); |
|
2 | var IntRangeWidgetModel = IPython.WidgetModel.extend({}); | |
3 | IPython.notebook.widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel); |
|
3 | IPython.notebook.widget_manager.register_widget_model('IntRangeWidgetModel', IntRangeWidgetModel); | |
4 |
|
4 | |||
@@ -41,7 +41,7 b' require(["notebook/js/widget"], function(){' | |||||
41 | events: { "slide" : "handleSliderChange" }, |
|
41 | events: { "slide" : "handleSliderChange" }, | |
42 | handleSliderChange: function(e, ui) { |
|
42 | handleSliderChange: function(e, ui) { | |
43 | this.model.set('value', ~~ui.value); // Double bit-wise not to truncate decimel |
|
43 | this.model.set('value', ~~ui.value); // Double bit-wise not to truncate decimel | |
44 |
this.model. |
|
44 | this.model.update_other_views(this); | |
45 | }, |
|
45 | }, | |
46 | }); |
|
46 | }); | |
47 |
|
47 | |||
@@ -105,7 +105,7 b' require(["notebook/js/widget"], function(){' | |||||
105 | if (numericalValue != this.model.get('value')) { |
|
105 | if (numericalValue != this.model.get('value')) { | |
106 | this.changing = true; |
|
106 | this.changing = true; | |
107 | this.model.set('value', numericalValue); |
|
107 | this.model.set('value', numericalValue); | |
108 |
this.model. |
|
108 | this.model.update_other_views(this); | |
109 | this.changing = false; |
|
109 | this.changing = false; | |
110 | } |
|
110 | } | |
111 | } |
|
111 | } |
@@ -1,4 +1,4 b'' | |||||
1 | require(["notebook/js/widget"], function(){ |
|
1 | require(["../static/notebook/js/widget"], function(){ | |
2 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); |
|
2 | var SelectionWidgetModel = IPython.WidgetModel.extend({}); | |
3 | IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); |
|
3 | IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel); | |
4 |
|
4 | |||
@@ -8,8 +8,7 b' require(["notebook/js/widget"], function(){' | |||||
8 | render : function(){ |
|
8 | render : function(){ | |
9 |
|
9 | |||
10 | this.$el |
|
10 | this.$el | |
11 | .html('') |
|
11 | .html(''); | |
12 | .addClass(this.model.comm.comm_id); |
|
|||
13 | this.$buttongroup = $('<div />') |
|
12 | this.$buttongroup = $('<div />') | |
14 | .addClass('widget_item') |
|
13 | .addClass('widget_item') | |
15 | .addClass('btn-group') |
|
14 | .addClass('btn-group') | |
@@ -44,7 +43,7 b' require(["notebook/js/widget"], function(){' | |||||
44 | .html(items[index]) |
|
43 | .html(items[index]) | |
45 | .on('click', function(e){ |
|
44 | .on('click', function(e){ | |
46 | that.model.set('value', $(e.target).html(), this ); |
|
45 | that.model.set('value', $(e.target).html(), this ); | |
47 |
that.model. |
|
46 | that.model.update_other_views(that); | |
48 | }) |
|
47 | }) | |
49 |
|
48 | |||
50 | this.$droplist.append($('<li />').append(item_button)) |
|
49 | this.$droplist.append($('<li />').append(item_button)) | |
@@ -72,8 +71,7 b' require(["notebook/js/widget"], function(){' | |||||
72 | // Called when view is rendered. |
|
71 | // Called when view is rendered. | |
73 | render : function(){ |
|
72 | render : function(){ | |
74 | this.$el |
|
73 | this.$el | |
75 | .html('') |
|
74 | .html(''); | |
76 | .addClass(this.model.comm.comm_id); |
|
|||
77 | this.update(); |
|
75 | this.update(); | |
78 | }, |
|
76 | }, | |
79 |
|
77 | |||
@@ -99,7 +97,7 b' require(["notebook/js/widget"], function(){' | |||||
99 | .prependTo($label) |
|
97 | .prependTo($label) | |
100 | .on('click', function(e){ |
|
98 | .on('click', function(e){ | |
101 | that.model.set('value', $(e.target).val(), this); |
|
99 | that.model.set('value', $(e.target).val(), this); | |
102 |
that.model. |
|
100 | that.model.update_other_views(); | |
103 | }); |
|
101 | }); | |
104 | } |
|
102 | } | |
105 |
|
103 | |||
@@ -137,8 +135,7 b' require(["notebook/js/widget"], function(){' | |||||
137 | // Called when view is rendered. |
|
135 | // Called when view is rendered. | |
138 | render : function(){ |
|
136 | render : function(){ | |
139 | this.$el |
|
137 | this.$el | |
140 | .html('') |
|
138 | .html(''); | |
141 | .addClass(this.model.comm.comm_id); |
|
|||
142 | this.$buttongroup = $('<div />') |
|
139 | this.$buttongroup = $('<div />') | |
143 | .addClass('btn-group') |
|
140 | .addClass('btn-group') | |
144 | .attr('data-toggle', 'buttons-radio') |
|
141 | .attr('data-toggle', 'buttons-radio') | |
@@ -164,7 +161,7 b' require(["notebook/js/widget"], function(){' | |||||
164 | .appendTo(this.$buttongroup) |
|
161 | .appendTo(this.$buttongroup) | |
165 | .on('click', function(e){ |
|
162 | .on('click', function(e){ | |
166 | that.model.set('value', $(e.target).html(), this); |
|
163 | that.model.set('value', $(e.target).html(), this); | |
167 |
that.model. |
|
164 | that.model.update_other_views(); | |
168 | }); |
|
165 | }); | |
169 | } |
|
166 | } | |
170 |
|
167 |
@@ -1,4 +1,4 b'' | |||||
1 | require(["notebook/js/widget"], function(){ |
|
1 | require(["../static/notebook/js/widget"], function(){ | |
2 | var StringWidgetModel = IPython.WidgetModel.extend({}); |
|
2 | var StringWidgetModel = IPython.WidgetModel.extend({}); | |
3 | IPython.notebook.widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel); |
|
3 | IPython.notebook.widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel); | |
4 |
|
4 | |||
@@ -6,8 +6,7 b' require(["notebook/js/widget"], function(){' | |||||
6 |
|
6 | |||
7 | // Called when view is rendered. |
|
7 | // Called when view is rendered. | |
8 | render : function(){ |
|
8 | render : function(){ | |
9 | this.$el = $('<div />') |
|
9 | this.$el = $('<div />'); | |
10 | .addClass(this.model.comm.comm_id); |
|
|||
11 | this.update(); // Set defaults. |
|
10 | this.update(); // Set defaults. | |
12 | }, |
|
11 | }, | |
13 |
|
12 | |||
@@ -26,8 +25,7 b' require(["notebook/js/widget"], function(){' | |||||
26 | // Called when view is rendered. |
|
25 | // Called when view is rendered. | |
27 | render : function(){ |
|
26 | render : function(){ | |
28 | this.$el |
|
27 | this.$el | |
29 | .html('') |
|
28 | .html(''); | |
30 | .addClass(this.model.comm.comm_id); |
|
|||
31 | this.$textbox = $('<textarea />') |
|
29 | this.$textbox = $('<textarea />') | |
32 | .attr('rows', 5) |
|
30 | .attr('rows', 5) | |
33 | .appendTo(this.$el); |
|
31 | .appendTo(this.$el); | |
@@ -50,7 +48,7 b' require(["notebook/js/widget"], function(){' | |||||
50 | handleChanging: function(e) { |
|
48 | handleChanging: function(e) { | |
51 | this.user_invoked_update = true; |
|
49 | this.user_invoked_update = true; | |
52 | this.model.set('value', e.target.value); |
|
50 | this.model.set('value', e.target.value); | |
53 |
this.model. |
|
51 | this.model.update_other_views(this); | |
54 | this.user_invoked_update = false; |
|
52 | this.user_invoked_update = false; | |
55 | }, |
|
53 | }, | |
56 | }); |
|
54 | }); | |
@@ -62,8 +60,7 b' require(["notebook/js/widget"], function(){' | |||||
62 | // Called when view is rendered. |
|
60 | // Called when view is rendered. | |
63 | render : function(){ |
|
61 | render : function(){ | |
64 | this.$el |
|
62 | this.$el | |
65 | .html('') |
|
63 | .html(''); | |
66 | .addClass(this.model.comm.comm_id); |
|
|||
67 | this.$textbox = $('<input type="text" />') |
|
64 | this.$textbox = $('<input type="text" />') | |
68 | .addClass('input') |
|
65 | .addClass('input') | |
69 | .appendTo(this.$el); |
|
66 | .appendTo(this.$el); | |
@@ -86,7 +83,7 b' require(["notebook/js/widget"], function(){' | |||||
86 | handleChanging: function(e) { |
|
83 | handleChanging: function(e) { | |
87 | this.user_invoked_update = true; |
|
84 | this.user_invoked_update = true; | |
88 | this.model.set('value', e.target.value); |
|
85 | this.model.set('value', e.target.value); | |
89 |
this.model. |
|
86 | this.model.update_other_views(this); | |
90 | this.user_invoked_update = false; |
|
87 | this.user_invoked_update = false; | |
91 | }, |
|
88 | }, | |
92 | }); |
|
89 | }); |
@@ -18,7 +18,7 b' def init_widget_js():' | |||||
18 | name = filename.rsplit('.', 1)[0] |
|
18 | name = filename.rsplit('.', 1)[0] | |
19 | if not (name == 'widget' or name == '__init__') and name.startswith('widget_'): |
|
19 | if not (name == 'widget' or name == '__init__') and name.startswith('widget_'): | |
20 | # Remove 'widget_' from the start of the name before compiling the path. |
|
20 | # Remove 'widget_' from the start of the name before compiling the path. | |
21 | js_path = 'static/notebook/js/widgets/%s.js' % name[7:] |
|
21 | js_path = '../static/notebook/js/widgets/%s.js' % name[7:] | |
22 | display(Javascript(data='$.getScript("%s");' % js_path)) |
|
22 | display(Javascript(data='$.getScript("%s");' % js_path)) | |
23 |
|
23 | |||
24 |
|
24 | |||
@@ -141,13 +141,13 b' class Widget(LoggingConfigurable):' | |||||
141 |
|
141 | |||
142 | # Show view. |
|
142 | # Show view. | |
143 | if self.parent is None: |
|
143 | if self.parent is None: | |
144 |
self.comm.send({"method": " |
|
144 | self.comm.send({"method": "display", "view_name": view_name}) | |
145 | else: |
|
145 | else: | |
146 |
self.comm.send({"method": " |
|
146 | self.comm.send({"method": "display", | |
147 | "view_name": view_name, |
|
147 | "view_name": view_name, | |
148 | "parent": self.parent.comm.comm_id}) |
|
148 | "parent": self.parent.comm.comm_id}) | |
149 |
|
149 | |||
150 |
# Now |
|
150 | # Now display children if any. | |
151 | for child in self.children: |
|
151 | for child in self.children: | |
152 | child._repr_widget_() |
|
152 | child._repr_widget_() | |
153 | return None |
|
153 | return None |
General Comments 0
You need to be logged in to leave comments.
Login now