Show More
@@ -16,242 +16,242 b'' | |||
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | |
|
19 | // require(['components/underscore/underscore-min.js', | |
|
20 | // 'components/backbone/backbone-min.js'], | |
|
21 | ||
|
22 | 19 | "use strict"; |
|
23 | 20 | |
|
24 | 21 | // Only run once on a notebook. |
|
25 | if (IPython.notebook.widget_manager == undefined) { | |
|
26 | ||
|
27 | //----------------------------------------------------------------------- | |
|
28 | // WidgetModel class | |
|
29 | //----------------------------------------------------------------------- | |
|
30 | var WidgetModel = Backbone.Model.extend({ | |
|
31 | apply: function(sender) { | |
|
32 | this.save(); | |
|
33 | ||
|
34 |
|
|
|
35 | var view = this.views[index]; | |
|
36 |
|
|
|
37 |
view. |
|
|
22 | require(["components/underscore/underscore-min", | |
|
23 | "components/backbone/backbone-min"], function () { | |
|
24 | if (IPython.notebook.widget_manager == undefined) { | |
|
25 | ||
|
26 | //----------------------------------------------------------------------- | |
|
27 | // WidgetModel class | |
|
28 | //----------------------------------------------------------------------- | |
|
29 | var WidgetModel = Backbone.Model.extend({ | |
|
30 | apply: function(sender) { | |
|
31 | this.save(); | |
|
32 | ||
|
33 | for (var index in this.views) { | |
|
34 | var view = this.views[index]; | |
|
35 | if (view !== sender) { | |
|
36 | view.refresh(); | |
|
37 | } | |
|
38 | 38 | } |
|
39 | 39 | } |
|
40 | } | |
|
41 | }); | |
|
42 | ||
|
43 | ||
|
44 | //----------------------------------------------------------------------- | |
|
45 | // WidgetView class | |
|
46 | //----------------------------------------------------------------------- | |
|
47 | var WidgetView = Backbone.View.extend({ | |
|
48 | ||
|
49 | initialize: function() { | |
|
50 | this.model.on('change',this.refresh,this); | |
|
51 | }, | |
|
52 | ||
|
53 | refresh: function() { | |
|
54 | this.update(); | |
|
40 | }); | |
|
41 | ||
|
42 | ||
|
43 | //----------------------------------------------------------------------- | |
|
44 | // WidgetView class | |
|
45 | //----------------------------------------------------------------------- | |
|
46 | var WidgetView = Backbone.View.extend({ | |
|
55 | 47 | |
|
56 | if (this.model.css != undefined) { | |
|
57 | for (var selector in this.model.css) { | |
|
58 | if (this.model.css.hasOwnProperty(selector)) { | |
|
59 |
|
|
|
60 | // Get the elements via the css selector. If the selector is | |
|
61 | // blank, assume the current element is the target. | |
|
62 | var elements = this.$el.find(selector); | |
|
63 | if (selector=='') { | |
|
64 |
|
|
|
65 | } | |
|
66 | ||
|
67 |
|
|
|
68 | if (elements.length>0){ | |
|
69 |
var |
|
|
70 |
f |
|
|
71 | if (css_traits.hasOwnProperty(css_key)) { | |
|
72 | elements.css(css_key, css_traits[css_key]); | |
|
48 | initialize: function() { | |
|
49 | this.model.on('change',this.refresh,this); | |
|
50 | }, | |
|
51 | ||
|
52 | refresh: function() { | |
|
53 | this.update(); | |
|
54 | ||
|
55 | if (this.model.css != undefined) { | |
|
56 | for (var selector in this.model.css) { | |
|
57 | if (this.model.css.hasOwnProperty(selector)) { | |
|
58 | ||
|
59 | // Get the elements via the css selector. If the selector is | |
|
60 | // blank, assume the current element is the target. | |
|
61 | var elements = this.$el.find(selector); | |
|
62 | if (selector=='') { | |
|
63 | elements = this.$el; | |
|
64 | } | |
|
65 | ||
|
66 | // Apply the css traits to all elements that match the selector. | |
|
67 | if (elements.length>0){ | |
|
68 | var css_traits = this.model.css[selector]; | |
|
69 | for (var css_key in css_traits) { | |
|
70 | if (css_traits.hasOwnProperty(css_key)) { | |
|
71 | elements.css(css_key, css_traits[css_key]); | |
|
72 | } | |
|
73 | 73 | } |
|
74 | 74 | } |
|
75 | 75 | } |
|
76 | 76 | } |
|
77 | 77 | } |
|
78 | } | |
|
79 |
} |
|
|
80 | }); | |
|
81 | ||
|
82 | ||
|
83 | //----------------------------------------------------------------------- | |
|
84 | // WidgetManager class | |
|
85 | //----------------------------------------------------------------------- | |
|
86 | // Public constructor | |
|
87 | var WidgetManager = function(comm_manager){ | |
|
88 | this.comm_manager = comm_manager; | |
|
89 | this.widget_model_types = {}; | |
|
90 | this.widget_view_types = {}; | |
|
91 | this.model_widget_views = {}; | |
|
92 | ||
|
93 | var that = this; | |
|
94 | Backbone.sync = function(method, model, options, error) { | |
|
95 | var result = that.send_sync(method, model); | |
|
96 | if (options.success) { | |
|
97 | options.success(result); | |
|
98 | } | |
|
99 | }; | |
|
100 | } | |
|
78 | }, | |
|
79 | }); | |
|
101 | 80 | |
|
102 | // Register a widget model type. | |
|
103 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
|
104 | ||
|
105 | // Register the widget with the comm manager. Make sure to pass this object's context | |
|
106 | // in so `this` works in the call back. | |
|
107 | this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); | |
|
108 | ||
|
109 | // Register the types of the model and view correspong to this widget type. Later | |
|
110 | // the widget manager will initialize these when the comm is opened. | |
|
111 | this.widget_model_types[widget_model_name] = widget_model_type; | |
|
112 | } | |
|
113 | 81 | |
|
114 | // Register a widget view type. | |
|
115 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
|
116 | this.widget_view_types[widget_view_name] = widget_view_type; | |
|
117 | } | |
|
82 | //----------------------------------------------------------------------- | |
|
83 | // WidgetManager class | |
|
84 | //----------------------------------------------------------------------- | |
|
85 | // Public constructor | |
|
86 | var WidgetManager = function(comm_manager){ | |
|
87 | this.comm_manager = comm_manager; | |
|
88 | this.widget_model_types = {}; | |
|
89 | this.widget_view_types = {}; | |
|
90 | this.model_widget_views = {}; | |
|
91 | ||
|
92 | var that = this; | |
|
93 | Backbone.sync = function(method, model, options, error) { | |
|
94 | var result = that.send_sync(method, model); | |
|
95 | if (options.success) { | |
|
96 | options.success(result); | |
|
97 | } | |
|
98 | }; | |
|
99 | } | |
|
118 | 100 | |
|
119 | // Handle when a comm is opened. | |
|
120 | WidgetManager.prototype.handle_com_open = function (comm, msg) { | |
|
121 | var widget_type_name = msg.content.target_name; | |
|
122 | ||
|
123 | // Create the corresponding widget model. | |
|
124 | var widget_model = new this.widget_model_types[widget_type_name]; | |
|
101 | // Register a widget model type. | |
|
102 | WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) { | |
|
103 | ||
|
104 | // Register the widget with the comm manager. Make sure to pass this object's context | |
|
105 | // in so `this` works in the call back. | |
|
106 | this.comm_manager.register_target(widget_model_name, $.proxy(this.handle_com_open, this)); | |
|
107 | ||
|
108 | // Register the types of the model and view correspong to this widget type. Later | |
|
109 | // the widget manager will initialize these when the comm is opened. | |
|
110 | this.widget_model_types[widget_model_name] = widget_model_type; | |
|
111 | } | |
|
125 | 112 | |
|
126 | // Remember comm associated with the model. | |
|
127 | widget_model.comm = comm; | |
|
128 | comm.model = widget_model; | |
|
113 | // Register a widget view type. | |
|
114 | WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) { | |
|
115 | this.widget_view_types[widget_view_name] = widget_view_type; | |
|
116 | } | |
|
129 | 117 | |
|
130 | // Create an array to remember the views associated with the model. | |
|
131 | widget_model.views = []; | |
|
118 | // Handle when a comm is opened. | |
|
119 | WidgetManager.prototype.handle_com_open = function (comm, msg) { | |
|
120 | var widget_type_name = msg.content.target_name; | |
|
121 | ||
|
122 | // Create the corresponding widget model. | |
|
123 | var widget_model = new this.widget_model_types[widget_type_name]; | |
|
132 | 124 | |
|
133 | // Add a handle to delete the control when the comm is closed. | |
|
134 | var that = this; | |
|
135 | var handle_close = function(msg) { | |
|
136 | that.handle_comm_closed(comm, msg); | |
|
137 | } | |
|
138 | comm.on_close(handle_close); | |
|
125 | // Remember comm associated with the model. | |
|
126 | widget_model.comm = comm; | |
|
127 | comm.model = widget_model; | |
|
139 | 128 | |
|
140 | // Handle incomming messages. | |
|
141 | var handle_msg = function(msg) { | |
|
142 | that.handle_comm_msg(comm, msg); | |
|
143 | } | |
|
144 | comm.on_msg(handle_msg); | |
|
145 | } | |
|
129 | // Create an array to remember the views associated with the model. | |
|
130 | widget_model.views = []; | |
|
146 | 131 | |
|
147 | // Create view that represents the model. | |
|
148 | WidgetManager.prototype.show_view = function (widget_area, widget_model, widget_view_name) { | |
|
149 | var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model}); | |
|
150 | widget_view.render(); | |
|
151 | widget_model.views.push(widget_view); | |
|
152 | ||
|
153 | // Handle when the view element is remove from the page. | |
|
154 | widget_view.$el.on("remove", function(){ | |
|
155 | var index = widget_model.views.indexOf(widget_view); | |
|
156 | if (index > -1) { | |
|
157 | widget_model.views.splice(index, 1); | |
|
132 | // Add a handle to delete the control when the comm is closed. | |
|
133 | var that = this; | |
|
134 | var handle_close = function(msg) { | |
|
135 | that.handle_comm_closed(comm, msg); | |
|
158 | 136 | } |
|
159 | widget_view.remove(); // Clean-up view | |
|
137 | comm.on_close(handle_close); | |
|
160 | 138 | |
|
161 | // Close the comm if there are no views left. | |
|
162 | if (widget_model.views.length()==0) { | |
|
163 | widget_model.comm.close(); | |
|
139 | // Handle incomming messages. | |
|
140 | var handle_msg = function(msg) { | |
|
141 | that.handle_comm_msg(comm, msg); | |
|
164 | 142 | } |
|
165 | }); | |
|
143 | comm.on_msg(handle_msg); | |
|
144 | } | |
|
166 | 145 | |
|
167 | // Add the view's element to cell's widget div. | |
|
168 | widget_area | |
|
169 | .append($("<div />").append(widget_view.$el)) | |
|
170 | .parent().show(); // Show the widget_area (parent of widget_subarea) | |
|
146 | // Create view that represents the model. | |
|
147 | WidgetManager.prototype.show_view = function (widget_area, widget_model, widget_view_name) { | |
|
148 | var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model}); | |
|
149 | widget_view.render(); | |
|
150 | widget_model.views.push(widget_view); | |
|
151 | ||
|
152 | // Handle when the view element is remove from the page. | |
|
153 | widget_view.$el.on("remove", function(){ | |
|
154 | var index = widget_model.views.indexOf(widget_view); | |
|
155 | if (index > -1) { | |
|
156 | widget_model.views.splice(index, 1); | |
|
157 | } | |
|
158 | widget_view.remove(); // Clean-up view | |
|
171 | 159 | |
|
172 | // Update the view based on the model contents. | |
|
173 | widget_view.refresh(); | |
|
174 | } | |
|
160 | // Close the comm if there are no views left. | |
|
161 | if (widget_model.views.length()==0) { | |
|
162 | widget_model.comm.close(); | |
|
163 | } | |
|
164 | }); | |
|
165 | ||
|
166 | // Add the view's element to cell's widget div. | |
|
167 | widget_area | |
|
168 | .append($("<div />").append(widget_view.$el)) | |
|
169 | .parent().show(); // Show the widget_area (parent of widget_subarea) | |
|
170 | ||
|
171 | // Update the view based on the model contents. | |
|
172 | widget_view.refresh(); | |
|
173 | } | |
|
175 | 174 | |
|
176 | // Handle incomming comm msg. | |
|
177 | WidgetManager.prototype.handle_comm_msg = function (comm, msg) { | |
|
178 | // Different logic for different methods. | |
|
179 | var method = msg.content.data.method; | |
|
180 | switch (method){ | |
|
181 | case 'show': | |
|
182 | ||
|
183 | // TODO: Get cell from registered output handler. | |
|
184 | var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1); | |
|
185 | var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea'); | |
|
186 | ||
|
187 | if (msg.content.data.parent != undefined) { | |
|
188 | var find_results = widget_subarea.find("." + msg.content.data.parent); | |
|
189 | if (find_results.length > 0) { | |
|
190 | widget_subarea = find_results; | |
|
175 | // Handle incomming comm msg. | |
|
176 | WidgetManager.prototype.handle_comm_msg = function (comm, msg) { | |
|
177 | // Different logic for different methods. | |
|
178 | var method = msg.content.data.method; | |
|
179 | switch (method){ | |
|
180 | case 'show': | |
|
181 | ||
|
182 | // TODO: Get cell from registered output handler. | |
|
183 | var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1); | |
|
184 | var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea'); | |
|
185 | ||
|
186 | if (msg.content.data.parent != undefined) { | |
|
187 | var find_results = widget_subarea.find("." + msg.content.data.parent); | |
|
188 | if (find_results.length > 0) { | |
|
189 | widget_subarea = find_results; | |
|
190 | } | |
|
191 | 191 | } |
|
192 | } | |
|
193 | 192 | |
|
194 | this.show_view(widget_subarea, comm.model, msg.content.data.view_name); | |
|
195 | break; | |
|
196 | case 'update': | |
|
197 | this.handle_update(comm, msg.content.data.state); | |
|
198 | break; | |
|
193 | this.show_view(widget_subarea, comm.model, msg.content.data.view_name); | |
|
194 | break; | |
|
195 | case 'update': | |
|
196 | this.handle_update(comm, msg.content.data.state); | |
|
197 | break; | |
|
198 | } | |
|
199 | 199 | } |
|
200 | } | |
|
201 | 200 | |
|
202 | // Handle when a widget is updated via the python side. | |
|
203 | WidgetManager.prototype.handle_update = function (comm, state) { | |
|
204 | for (var key in state) { | |
|
205 | if (state.hasOwnProperty(key)) { | |
|
206 | if (key=="_css"){ | |
|
207 | comm.model.css = state[key]; | |
|
208 | } else { | |
|
209 | comm.model.set(key, state[key]); | |
|
201 | // Handle when a widget is updated via the python side. | |
|
202 | WidgetManager.prototype.handle_update = function (comm, state) { | |
|
203 | for (var key in state) { | |
|
204 | if (state.hasOwnProperty(key)) { | |
|
205 | if (key=="_css"){ | |
|
206 | comm.model.css = state[key]; | |
|
207 | } else { | |
|
208 | comm.model.set(key, state[key]); | |
|
209 | } | |
|
210 | 210 | } |
|
211 | 211 | } |
|
212 | comm.model.save(); | |
|
212 | 213 | } |
|
213 | comm.model.save(); | |
|
214 | } | |
|
215 | 214 | |
|
216 | // Handle when a widget is closed. | |
|
217 | WidgetManager.prototype.handle_comm_closed = function (comm, msg) { | |
|
218 | for (var view_index in comm.model.views) { | |
|
219 | var view = comm.model.views[view_index]; | |
|
220 | view.remove(); | |
|
215 | // Handle when a widget is closed. | |
|
216 | WidgetManager.prototype.handle_comm_closed = function (comm, msg) { | |
|
217 | for (var view_index in comm.model.views) { | |
|
218 | var view = comm.model.views[view_index]; | |
|
219 | view.remove(); | |
|
220 | } | |
|
221 | 221 | } |
|
222 | } | |
|
223 | 222 | |
|
224 | // Get the cell output area corresponding to the comm. | |
|
225 | WidgetManager.prototype._get_comm_outputarea = function (comm) { | |
|
226 | // TODO: get element from comm instead of guessing | |
|
227 | var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()) | |
|
228 | return cell.output_area; | |
|
229 | } | |
|
223 | // Get the cell output area corresponding to the comm. | |
|
224 | WidgetManager.prototype._get_comm_outputarea = function (comm) { | |
|
225 | // TODO: get element from comm instead of guessing | |
|
226 | var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()) | |
|
227 | return cell.output_area; | |
|
228 | } | |
|
230 | 229 | |
|
231 | // Send widget state to python backend. | |
|
232 | WidgetManager.prototype.send_sync = function (method, model) { | |
|
233 | ||
|
234 | // Create a callback for the output if the widget has an output area associate with it. | |
|
235 | var callbacks = {}; | |
|
236 | var comm = model.comm; | |
|
237 | var outputarea = this._get_comm_outputarea(comm); | |
|
238 | if (outputarea != null) { | |
|
239 | callbacks = { | |
|
240 | iopub : { | |
|
241 | output : $.proxy(outputarea.handle_output, outputarea), | |
|
242 | clear_output : $.proxy(outputarea.handle_clear_output, outputarea)} | |
|
230 | // Send widget state to python backend. | |
|
231 | WidgetManager.prototype.send_sync = function (method, model) { | |
|
232 | ||
|
233 | // Create a callback for the output if the widget has an output area associate with it. | |
|
234 | var callbacks = {}; | |
|
235 | var comm = model.comm; | |
|
236 | var outputarea = this._get_comm_outputarea(comm); | |
|
237 | if (outputarea != null) { | |
|
238 | callbacks = { | |
|
239 | iopub : { | |
|
240 | output : $.proxy(outputarea.handle_output, outputarea), | |
|
241 | clear_output : $.proxy(outputarea.handle_clear_output, outputarea)} | |
|
242 | }; | |
|
243 | 243 | }; |
|
244 | }; | |
|
245 | var model_json = model.toJSON(); | |
|
246 | var data = {sync_method: method, sync_data: model_json}; | |
|
247 | comm.send(data, callbacks); | |
|
248 | return model_json; | |
|
249 | } | |
|
244 | var model_json = model.toJSON(); | |
|
245 | var data = {sync_method: method, sync_data: model_json}; | |
|
246 | comm.send(data, callbacks); | |
|
247 | return model_json; | |
|
248 | } | |
|
250 | 249 | |
|
251 | IPython.WidgetManager = WidgetManager; | |
|
252 | IPython.WidgetModel = WidgetModel; | |
|
253 | IPython.WidgetView = WidgetView; | |
|
250 | IPython.WidgetManager = WidgetManager; | |
|
251 | IPython.WidgetModel = WidgetModel; | |
|
252 | IPython.WidgetView = WidgetView; | |
|
254 | 253 | |
|
255 | IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager); | |
|
254 | IPython.notebook.widget_manager = new WidgetManager(IPython.notebook.kernel.comm_manager); | |
|
256 | 255 | |
|
257 | } | |
|
256 | } | |
|
257 | }); |
@@ -71,8 +71,6 b'' | |||
|
71 | 71 | <script src="{{static_url("components/jquery/jquery.min.js") }}" type="text/javascript" charset="utf-8"></script> |
|
72 | 72 | <script src="{{static_url("components/jquery-ui/ui/minified/jquery-ui.min.js") }}" type="text/javascript" charset="utf-8"></script> |
|
73 | 73 | <script src="{{static_url("components/bootstrap/bootstrap/js/bootstrap.min.js") }}" type="text/javascript" charset="utf-8"></script> |
|
74 | <script src="{{static_url("components/underscore/underscore-min.js") }}" type="text/javascript" charset="utf-8"></script> | |
|
75 | <script src="{{static_url("components/backbone/backbone-min.js") }}" type="text/javascript" charset="utf-8"></script> | |
|
76 | 74 | <script src="{{static_url("base/js/namespace.js") }}" type="text/javascript" charset="utf-8"></script> |
|
77 | 75 | <script src="{{static_url("base/js/page.js") }}" type="text/javascript" charset="utf-8"></script> |
|
78 | 76 | <script src="{{static_url("auth/js/loginwidget.js") }}" type="text/javascript" charset="utf-8"></script> |
General Comments 0
You need to be logged in to leave comments.
Login now