##// END OF EJS Templates
Merge pull request #6494 from takluyver/widget-comm-require...
Jonathan Frederic -
r18443:d6414f0f merge
parent child Browse files
Show More
@@ -7,7 +7,7 b' define(['
7 "jquery",
7 "jquery",
8 "base/js/namespace"
8 "base/js/namespace"
9 ], function (_, Backbone, $, IPython) {
9 ], function (_, Backbone, $, IPython) {
10
10 "use strict";
11 //--------------------------------------------------------------------
11 //--------------------------------------------------------------------
12 // WidgetManager class
12 // WidgetManager class
13 //--------------------------------------------------------------------
13 //--------------------------------------------------------------------
@@ -52,15 +52,14 b' define(['
52 console.log("Could not determine where the display" +
52 console.log("Could not determine where the display" +
53 " message was from. Widget will not be displayed");
53 " message was from. Widget will not be displayed");
54 } else {
54 } else {
55 var view = this.create_view(model, {cell: cell});
55 var that = this;
56 if (view === null) {
56 this.create_view(model, {cell: cell, callback: function(view) {
57 console.error("View creation failed", model);
57 that._handle_display_view(view);
58 }
58 if (cell.widget_subarea) {
59 this._handle_display_view(view);
59 cell.widget_subarea.append(view.$el);
60 if (cell.widget_subarea) {
60 }
61 cell.widget_subarea.append(view.$el);
61 view.trigger('displayed');
62 }
62 }});
63 view.trigger('displayed');
64 }
63 }
65 };
64 };
66
65
@@ -78,28 +77,43 b' define(['
78 }
77 }
79 }
78 }
80 };
79 };
80
81
81
82 WidgetManager.prototype.create_view = function(model, options, view) {
82 WidgetManager.prototype.create_view = function(model, options) {
83 // Creates a view for a particular model.
83 // Creates a view for a particular model.
84
84 var view_name = model.get('_view_name');
85 var view_name = model.get('_view_name');
85 var ViewType = WidgetManager._view_types[view_name];
86 var view_mod = model.get('_view_module');
86 if (ViewType) {
87 var errback = options.errback || function(err) {console.log(err);};
87
88
88 // If a view is passed into the method, use that view's cell as
89 var instantiate_view = function(ViewType) {
89 // the cell for the view that is created.
90 if (ViewType) {
90 options = options || {};
91 // If a view is passed into the method, use that view's cell as
91 if (view !== undefined) {
92 // the cell for the view that is created.
92 options.cell = view.options.cell;
93 options = options || {};
94 if (options.parent !== undefined) {
95 options.cell = options.parent.options.cell;
96 }
97
98 // Create and render the view...
99 var parameters = {model: model, options: options};
100 var view = new ViewType(parameters);
101 view.render();
102 model.on('destroy', view.remove, view);
103 options.callback(view);
104 } else {
105 errback({unknown_view: true, view_name: view_name,
106 view_module: view_mod});
93 }
107 }
108 };
94
109
95 // Create and render the view...
110 if (view_mod) {
96 var parameters = {model: model, options: options};
111 require([view_mod], function(module) {
97 view = new ViewType(parameters);
112 instantiate_view(module[view_name]);
98 view.render();
113 }, errback);
99 model.on('destroy', view.remove, view);
114 } else {
100 return view;
115 instantiate_view(WidgetManager._view_types[view_name]);
101 }
116 }
102 return null;
103 };
117 };
104
118
105 WidgetManager.prototype.get_msg_cell = function (msg_id) {
119 WidgetManager.prototype.get_msg_cell = function (msg_id) {
@@ -317,18 +317,21 b' define(["widgets/js/manager",'
317 // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior
317 // TODO: this is hacky, and makes the view depend on this cell attribute and widget manager behavior
318 // it would be great to have the widget manager add the cell metadata
318 // it would be great to have the widget manager add the cell metadata
319 // to the subview without having to add it here.
319 // to the subview without having to add it here.
320 options = $.extend({ parent: this }, options || {});
320 var that = this;
321 var child_view = this.model.widget_manager.create_view(child_model, options, this);
321 var old_callback = options.callback || function(view) {};
322
322 options = $.extend({ parent: this, callback: function(child_view) {
323 // Associate the view id with the model id.
323 // Associate the view id with the model id.
324 if (this.child_model_views[child_model.id] === undefined) {
324 if (that.child_model_views[child_model.id] === undefined) {
325 this.child_model_views[child_model.id] = [];
325 that.child_model_views[child_model.id] = [];
326 }
326 }
327 this.child_model_views[child_model.id].push(child_view.id);
327 that.child_model_views[child_model.id].push(child_view.id);
328
328
329 // Remember the view by id.
329 // Remember the view by id.
330 this.child_views[child_view.id] = child_view;
330 that.child_views[child_view.id] = child_view;
331 return child_view;
331 old_callback(child_view);
332 }}, options || {});
333
334 this.model.widget_manager.create_view(child_model, options);
332 },
335 },
333
336
334 pop_child_view: function(child_model) {
337 pop_child_view: function(child_model) {
@@ -74,13 +74,15 b' define(['
74
74
75 add_child_model: function(model) {
75 add_child_model: function(model) {
76 // Called when a model is added to the children list.
76 // Called when a model is added to the children list.
77 var view = this.create_child_view(model);
77 var that = this;
78 this.$box.append(view.$el);
78 this.create_child_view(model, {callback: function(view) {
79 that.$box.append(view.$el);
79
80
80 // Trigger the displayed event of the child view.
81 // Trigger the displayed event of the child view.
81 this.after_displayed(function() {
82 that.after_displayed(function() {
82 view.trigger('displayed');
83 view.trigger('displayed');
83 });
84 });
85 }});
84 },
86 },
85 });
87 });
86
88
@@ -81,7 +81,6 b' define(['
81
81
82 add_child_model: function(model) {
82 add_child_model: function(model) {
83 // Called when a child is added to children list.
83 // Called when a child is added to children list.
84 var view = this.create_child_view(model);
85 var index = this.containers.length;
84 var index = this.containers.length;
86 var uuid = utils.uuid();
85 var uuid = utils.uuid();
87 var accordion_group = $('<div />')
86 var accordion_group = $('<div />')
@@ -114,15 +113,18 b' define(['
114 var container_index = this.containers.push(accordion_group) - 1;
113 var container_index = this.containers.push(accordion_group) - 1;
115 accordion_group.container_index = container_index;
114 accordion_group.container_index = container_index;
116 this.model_containers[model.id] = accordion_group;
115 this.model_containers[model.id] = accordion_group;
117 accordion_inner.append(view.$el);
116
117 this.create_child_view(model, {callback: function(view) {
118 accordion_inner.append(view.$el);
118
119
119 this.update();
120 that.update();
120 this.update_titles();
121 that.update_titles();
121
122
122 // Trigger the displayed event of the child view.
123 // Trigger the displayed event of the child view.
123 this.after_displayed(function() {
124 that.after_displayed(function() {
124 view.trigger('displayed');
125 view.trigger('displayed');
125 });
126 });
127 }});
126 },
128 },
127 });
129 });
128
130
@@ -176,7 +178,6 b' define(['
176
178
177 add_child_model: function(model) {
179 add_child_model: function(model) {
178 // Called when a child is added to children list.
180 // Called when a child is added to children list.
179 var view = this.create_child_view(model);
180 var index = this.containers.length;
181 var index = this.containers.length;
181 var uuid = utils.uuid();
182 var uuid = utils.uuid();
182
183
@@ -184,34 +185,37 b' define(['
184 var tab = $('<li />')
185 var tab = $('<li />')
185 .css('list-style-type', 'none')
186 .css('list-style-type', 'none')
186 .appendTo(this.$tabs);
187 .appendTo(this.$tabs);
187 view.parent_tab = tab;
188
189 var tab_text = $('<a />')
190 .attr('href', '#' + uuid)
191 .attr('data-toggle', 'tab')
192 .text('Page ' + index)
193 .appendTo(tab)
194 .click(function (e) {
195
188
196 // Calling model.set will trigger all of the other views of the
189 this.create_child_view(model, {callback: function(view) {
197 // model to update.
190 view.parent_tab = tab;
198 that.model.set("selected_index", index, {updated_view: this});
191
199 that.touch();
192 var tab_text = $('<a />')
200 that.select_page(index);
193 .attr('href', '#' + uuid)
194 .attr('data-toggle', 'tab')
195 .text('Page ' + index)
196 .appendTo(tab)
197 .click(function (e) {
198
199 // Calling model.set will trigger all of the other views of the
200 // model to update.
201 that.model.set("selected_index", index, {updated_view: that});
202 that.touch();
203 that.select_page(index);
204 });
205 tab.tab_text_index = that.containers.push(tab_text) - 1;
206
207 var contents_div = $('<div />', {id: uuid})
208 .addClass('tab-pane')
209 .addClass('fade')
210 .append(view.$el)
211 .appendTo(that.$tab_contents);
212 view.parent_container = contents_div;
213
214 // Trigger the displayed event of the child view.
215 that.after_displayed(function() {
216 view.trigger('displayed');
201 });
217 });
202 tab.tab_text_index = this.containers.push(tab_text) - 1;
218 }});
203
204 var contents_div = $('<div />', {id: uuid})
205 .addClass('tab-pane')
206 .addClass('fade')
207 .append(view.$el)
208 .appendTo(this.$tab_contents);
209 view.parent_container = contents_div;
210
211 // Trigger the displayed event of the child view.
212 this.after_displayed(function() {
213 view.trigger('displayed');
214 });
215 },
219 },
216
220
217 update: function(options) {
221 update: function(options) {
@@ -100,6 +100,8 b' class Widget(LoggingConfigurable):'
100 #-------------------------------------------------------------------------
100 #-------------------------------------------------------------------------
101 _model_name = Unicode('WidgetModel', help="""Name of the backbone model
101 _model_name = Unicode('WidgetModel', help="""Name of the backbone model
102 registered in the front-end to create and sync this widget with.""")
102 registered in the front-end to create and sync this widget with.""")
103 _view_module = Unicode(help="""A requirejs module in which to find _view_name.
104 If empty, look in the global registry.""", sync=True)
103 _view_name = Unicode(None, allow_none=True, help="""Default view registered in the front-end
105 _view_name = Unicode(None, allow_none=True, help="""Default view registered in the front-end
104 to use to represent the widget.""", sync=True)
106 to use to represent the widget.""", sync=True)
105 comm = Instance('IPython.kernel.comm.Comm')
107 comm = Instance('IPython.kernel.comm.Comm')
General Comments 0
You need to be logged in to leave comments. Login now