##// END OF EJS Templates
Use require.js where possible.
Jonathan Frederic -
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 "use strict";
19 "use strict";
23
20
24 // Only run once on a notebook.
21 // Only run once on a notebook.
25 if (IPython.notebook.widget_manager == undefined) {
22 require(["components/underscore/underscore-min",
26
23 "components/backbone/backbone-min"], function () {
27 //-----------------------------------------------------------------------
24 if (IPython.notebook.widget_manager == undefined) {
28 // WidgetModel class
25
29 //-----------------------------------------------------------------------
26 //-----------------------------------------------------------------------
30 var WidgetModel = Backbone.Model.extend({
27 // WidgetModel class
31 apply: function(sender) {
28 //-----------------------------------------------------------------------
32 this.save();
29 var WidgetModel = Backbone.Model.extend({
33
30 apply: function(sender) {
34 for (var index in this.views) {
31 this.save();
35 var view = this.views[index];
32
36 if (view !== sender) {
33 for (var index in this.views) {
37 view.refresh();
34 var view = this.views[index];
35 if (view !== sender) {
36 view.refresh();
37 }
38 }
38 }
39 }
39 }
40 }
40 });
41 });
41
42
42
43
43 //-----------------------------------------------------------------------
44 //-----------------------------------------------------------------------
44 // WidgetView class
45 // WidgetView class
45 //-----------------------------------------------------------------------
46 //-----------------------------------------------------------------------
46 var WidgetView = Backbone.View.extend({
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();
55
47
56 if (this.model.css != undefined) {
48 initialize: function() {
57 for (var selector in this.model.css) {
49 this.model.on('change',this.refresh,this);
58 if (this.model.css.hasOwnProperty(selector)) {
50 },
59
51
60 // Get the elements via the css selector. If the selector is
52 refresh: function() {
61 // blank, assume the current element is the target.
53 this.update();
62 var elements = this.$el.find(selector);
54
63 if (selector=='') {
55 if (this.model.css != undefined) {
64 elements = this.$el;
56 for (var selector in this.model.css) {
65 }
57 if (this.model.css.hasOwnProperty(selector)) {
66
58
67 // Apply the css traits to all elements that match the selector.
59 // Get the elements via the css selector. If the selector is
68 if (elements.length>0){
60 // blank, assume the current element is the target.
69 var css_traits = this.model.css[selector];
61 var elements = this.$el.find(selector);
70 for (var css_key in css_traits) {
62 if (selector=='') {
71 if (css_traits.hasOwnProperty(css_key)) {
63 elements = this.$el;
72 elements.css(css_key, css_traits[css_key]);
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 }
78 },
79 },
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 }
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.
82 //-----------------------------------------------------------------------
115 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
83 // WidgetManager class
116 this.widget_view_types[widget_view_name] = widget_view_type;
84 //-----------------------------------------------------------------------
117 }
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.
101 // Register a widget model type.
120 WidgetManager.prototype.handle_com_open = function (comm, msg) {
102 WidgetManager.prototype.register_widget_model = function (widget_model_name, widget_model_type) {
121 var widget_type_name = msg.content.target_name;
103
122
104 // Register the widget with the comm manager. Make sure to pass this object's context
123 // Create the corresponding widget model.
105 // in so `this` works in the call back.
124 var widget_model = new this.widget_model_types[widget_type_name];
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.
113 // Register a widget view type.
127 widget_model.comm = comm;
114 WidgetManager.prototype.register_widget_view = function (widget_view_name, widget_view_type) {
128 comm.model = widget_model;
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.
118 // Handle when a comm is opened.
131 widget_model.views = [];
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.
125 // Remember comm associated with the model.
134 var that = this;
126 widget_model.comm = comm;
135 var handle_close = function(msg) {
127 comm.model = widget_model;
136 that.handle_comm_closed(comm, msg);
137 }
138 comm.on_close(handle_close);
139
128
140 // Handle incomming messages.
129 // Create an array to remember the views associated with the model.
141 var handle_msg = function(msg) {
130 widget_model.views = [];
142 that.handle_comm_msg(comm, msg);
143 }
144 comm.on_msg(handle_msg);
145 }
146
131
147 // Create view that represents the model.
132 // Add a handle to delete the control when the comm is closed.
148 WidgetManager.prototype.show_view = function (widget_area, widget_model, widget_view_name) {
133 var that = this;
149 var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model});
134 var handle_close = function(msg) {
150 widget_view.render();
135 that.handle_comm_closed(comm, msg);
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);
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.
139 // Handle incomming messages.
162 if (widget_model.views.length()==0) {
140 var handle_msg = function(msg) {
163 widget_model.comm.close();
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.
146 // Create view that represents the model.
168 widget_area
147 WidgetManager.prototype.show_view = function (widget_area, widget_model, widget_view_name) {
169 .append($("<div />").append(widget_view.$el))
148 var widget_view = new this.widget_view_types[widget_view_name]({model: widget_model});
170 .parent().show(); // Show the widget_area (parent of widget_subarea)
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.
160 // Close the comm if there are no views left.
173 widget_view.refresh();
161 if (widget_model.views.length()==0) {
174 }
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.
175 // Handle incomming comm msg.
177 WidgetManager.prototype.handle_comm_msg = function (comm, msg) {
176 WidgetManager.prototype.handle_comm_msg = function (comm, msg) {
178 // Different logic for different methods.
177 // Different logic for different methods.
179 var method = msg.content.data.method;
178 var method = msg.content.data.method;
180 switch (method){
179 switch (method){
181 case 'show':
180 case 'show':
182
181
183 // TODO: Get cell from registered output handler.
182 // TODO: Get cell from registered output handler.
184 var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
183 var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
185 var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea');
184 var widget_subarea = cell.element.find('.widget_area').find('.widget_subarea');
186
185
187 if (msg.content.data.parent != undefined) {
186 if (msg.content.data.parent != undefined) {
188 var find_results = widget_subarea.find("." + msg.content.data.parent);
187 var find_results = widget_subarea.find("." + msg.content.data.parent);
189 if (find_results.length > 0) {
188 if (find_results.length > 0) {
190 widget_subarea = find_results;
189 widget_subarea = find_results;
190 }
191 }
191 }
192 }
193
192
194 this.show_view(widget_subarea, comm.model, msg.content.data.view_name);
193 this.show_view(widget_subarea, comm.model, msg.content.data.view_name);
195 break;
194 break;
196 case 'update':
195 case 'update':
197 this.handle_update(comm, msg.content.data.state);
196 this.handle_update(comm, msg.content.data.state);
198 break;
197 break;
198 }
199 }
199 }
200 }
201
200
202 // Handle when a widget is updated via the python side.
201 // Handle when a widget is updated via the python side.
203 WidgetManager.prototype.handle_update = function (comm, state) {
202 WidgetManager.prototype.handle_update = function (comm, state) {
204 for (var key in state) {
203 for (var key in state) {
205 if (state.hasOwnProperty(key)) {
204 if (state.hasOwnProperty(key)) {
206 if (key=="_css"){
205 if (key=="_css"){
207 comm.model.css = state[key];
206 comm.model.css = state[key];
208 } else {
207 } else {
209 comm.model.set(key, state[key]);
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.
215 // Handle when a widget is closed.
217 WidgetManager.prototype.handle_comm_closed = function (comm, msg) {
216 WidgetManager.prototype.handle_comm_closed = function (comm, msg) {
218 for (var view_index in comm.model.views) {
217 for (var view_index in comm.model.views) {
219 var view = comm.model.views[view_index];
218 var view = comm.model.views[view_index];
220 view.remove();
219 view.remove();
220 }
221 }
221 }
222 }
223
222
224 // Get the cell output area corresponding to the comm.
223 // Get the cell output area corresponding to the comm.
225 WidgetManager.prototype._get_comm_outputarea = function (comm) {
224 WidgetManager.prototype._get_comm_outputarea = function (comm) {
226 // TODO: get element from comm instead of guessing
225 // TODO: get element from comm instead of guessing
227 var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index())
226 var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index())
228 return cell.output_area;
227 return cell.output_area;
229 }
228 }
230
229
231 // Send widget state to python backend.
230 // Send widget state to python backend.
232 WidgetManager.prototype.send_sync = function (method, model) {
231 WidgetManager.prototype.send_sync = function (method, model) {
233
232
234 // Create a callback for the output if the widget has an output area associate with it.
233 // Create a callback for the output if the widget has an output area associate with it.
235 var callbacks = {};
234 var callbacks = {};
236 var comm = model.comm;
235 var comm = model.comm;
237 var outputarea = this._get_comm_outputarea(comm);
236 var outputarea = this._get_comm_outputarea(comm);
238 if (outputarea != null) {
237 if (outputarea != null) {
239 callbacks = {
238 callbacks = {
240 iopub : {
239 iopub : {
241 output : $.proxy(outputarea.handle_output, outputarea),
240 output : $.proxy(outputarea.handle_output, outputarea),
242 clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
241 clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
242 };
243 };
243 };
244 };
244 var model_json = model.toJSON();
245 var model_json = model.toJSON();
245 var data = {sync_method: method, sync_data: model_json};
246 var data = {sync_method: method, sync_data: model_json};
246 comm.send(data, callbacks);
247 comm.send(data, callbacks);
247 return model_json;
248 return model_json;
248 }
249 }
250
249
251 IPython.WidgetManager = WidgetManager;
250 IPython.WidgetManager = WidgetManager;
252 IPython.WidgetModel = WidgetModel;
251 IPython.WidgetModel = WidgetModel;
253 IPython.WidgetView = WidgetView;
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 <script src="{{static_url("components/jquery/jquery.min.js") }}" type="text/javascript" charset="utf-8"></script>
71 <script src="{{static_url("components/jquery/jquery.min.js") }}" type="text/javascript" charset="utf-8"></script>
72 <script src="{{static_url("components/jquery-ui/ui/minified/jquery-ui.min.js") }}" type="text/javascript" charset="utf-8"></script>
72 <script src="{{static_url("components/jquery-ui/ui/minified/jquery-ui.min.js") }}" type="text/javascript" charset="utf-8"></script>
73 <script src="{{static_url("components/bootstrap/bootstrap/js/bootstrap.min.js") }}" type="text/javascript" charset="utf-8"></script>
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 <script src="{{static_url("base/js/namespace.js") }}" type="text/javascript" charset="utf-8"></script>
74 <script src="{{static_url("base/js/namespace.js") }}" type="text/javascript" charset="utf-8"></script>
77 <script src="{{static_url("base/js/page.js") }}" type="text/javascript" charset="utf-8"></script>
75 <script src="{{static_url("base/js/page.js") }}" type="text/javascript" charset="utf-8"></script>
78 <script src="{{static_url("auth/js/loginwidget.js") }}" type="text/javascript" charset="utf-8"></script>
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