Show More
@@ -3,194 +3,195 b'' | |||
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | "underscore", |
|
6 |
|
|
|
7 |
|
|
|
8 | ||
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 | ||
|
16 |
|
|
|
17 |
|
|
|
18 | this._models = {}; /* Dictionary of model ids and model instances */ | |
|
19 | ||
|
20 | // Register already-registered widget model types with the comm manager. | |
|
21 | var that = this; | |
|
22 | _.each(WidgetManager._model_types, function(model_type, model_name) { | |
|
23 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); | |
|
24 | }); | |
|
25 | }; | |
|
26 | ||
|
27 | //-------------------------------------------------------------------- | |
|
28 | // Class level | |
|
29 |
|
|
|
30 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ | |
|
31 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ | |
|
32 | WidgetManager._managers = []; /* List of widget managers */ | |
|
33 | ||
|
34 | WidgetManager.register_widget_model = function (model_name, model_type) { | |
|
35 | // Registers a widget model by name. | |
|
36 |
|
|
|
37 | ||
|
38 | // Register the widget with the comm manager. Make sure to pass this object's context | |
|
39 | // in so `this` works in the call back. | |
|
40 | _.each(WidgetManager._managers, function(instance, i) { | |
|
41 | if (instance.comm_manager !== null) { | |
|
42 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); | |
|
43 | } | |
|
44 | }); | |
|
45 | }; | |
|
46 | ||
|
47 | WidgetManager.register_widget_view = function (view_name, view_type) { | |
|
48 | // Registers a widget view by name. | |
|
49 | WidgetManager._view_types[view_name] = view_type; | |
|
50 | }; | |
|
51 | ||
|
52 | //-------------------------------------------------------------------- | |
|
53 | // Instance level | |
|
54 | //-------------------------------------------------------------------- | |
|
55 | WidgetManager.prototype.display_view = function(msg, model) { | |
|
56 | // Displays a view for a particular model. | |
|
57 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
|
58 | if (cell === null) { | |
|
59 | console.log("Could not determine where the display" + | |
|
60 | " message was from. Widget will not be displayed"); | |
|
61 | } else { | |
|
62 | var view = this.create_view(model, {cell: cell}); | |
|
63 | if (view === null) { | |
|
64 | console.error("View creation failed", model); | |
|
65 | } | |
|
66 | if (cell.widget_subarea) { | |
|
67 | cell.widget_area.show(); | |
|
68 | this._handle_display_view(view); | |
|
69 | cell.widget_subarea.append(view.$el); | |
|
70 | view.trigger('displayed'); | |
|
71 | } | |
|
6 | "backbone", | |
|
7 | ], function (_, Backbone) { | |
|
8 | ||
|
9 | //-------------------------------------------------------------------- | |
|
10 | // WidgetManager class | |
|
11 | //-------------------------------------------------------------------- | |
|
12 | var WidgetManager = function (comm_manager, keyboard_manager, notebook) { | |
|
13 | // Public constructor | |
|
14 | WidgetManager._managers.push(this); | |
|
15 | ||
|
16 | // Attach a comm manager to the | |
|
17 | this.keyboard_manager = keyboard_manager; | |
|
18 | this.notebook = notebook; | |
|
19 | this.comm_manager = comm_manager; | |
|
20 | this._models = {}; /* Dictionary of model ids and model instances */ | |
|
21 | ||
|
22 | // Register already-registered widget model types with the comm manager. | |
|
23 | var that = this; | |
|
24 | _.each(WidgetManager._model_types, function(model_type, model_name) { | |
|
25 | that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that)); | |
|
26 | }); | |
|
27 | }; | |
|
28 | ||
|
29 | //-------------------------------------------------------------------- | |
|
30 | // Class level | |
|
31 | //-------------------------------------------------------------------- | |
|
32 | WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */ | |
|
33 | WidgetManager._view_types = {}; /* Dictionary of view names and view types. */ | |
|
34 | WidgetManager._managers = []; /* List of widget managers */ | |
|
35 | ||
|
36 | WidgetManager.register_widget_model = function (model_name, model_type) { | |
|
37 | // Registers a widget model by name. | |
|
38 | WidgetManager._model_types[model_name] = model_type; | |
|
39 | ||
|
40 | // Register the widget with the comm manager. Make sure to pass this object's context | |
|
41 | // in so `this` works in the call back. | |
|
42 | _.each(WidgetManager._managers, function(instance, i) { | |
|
43 | if (instance.comm_manager !== null) { | |
|
44 | instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance)); | |
|
72 | 45 | } |
|
73 | }; | |
|
74 | ||
|
75 | WidgetManager.prototype._handle_display_view = function (view) { | |
|
76 | // Have the IPython keyboard manager disable its event | |
|
77 | // handling so the widget can capture keyboard input. | |
|
78 | // Note, this is only done on the outer most widgets. | |
|
79 | IPython.keyboard_manager.register_events(view.$el); | |
|
80 | ||
|
81 | if (view.additional_elements) { | |
|
82 | for (var i = 0; i < view.additional_elements.length; i++) { | |
|
83 | IPython.keyboard_manager.register_events(view.additional_elements[i]); | |
|
84 | } | |
|
85 | } | |
|
86 | }; | |
|
87 | ||
|
88 | WidgetManager.prototype.create_view = function(model, options, view) { | |
|
89 | // Creates a view for a particular model. | |
|
90 | var view_name = model.get('_view_name'); | |
|
91 | var ViewType = WidgetManager._view_types[view_name]; | |
|
92 |
if ( |
|
|
93 | ||
|
94 | // If a view is passed into the method, use that view's cell as | |
|
95 | // the cell for the view that is created. | |
|
96 | options = options || {}; | |
|
97 | if (view !== undefined) { | |
|
98 | options.cell = view.options.cell; | |
|
99 | } | |
|
100 | ||
|
101 | // Create and render the view... | |
|
102 | var parameters = {model: model, options: options}; | |
|
103 | view = new ViewType(parameters); | |
|
104 | view.render(); | |
|
105 | model.on('destroy', view.remove, view); | |
|
106 | return view; | |
|
46 | }); | |
|
47 | }; | |
|
48 | ||
|
49 | WidgetManager.register_widget_view = function (view_name, view_type) { | |
|
50 | // Registers a widget view by name. | |
|
51 | WidgetManager._view_types[view_name] = view_type; | |
|
52 | }; | |
|
53 | ||
|
54 | //-------------------------------------------------------------------- | |
|
55 | // Instance level | |
|
56 | //-------------------------------------------------------------------- | |
|
57 | WidgetManager.prototype.display_view = function(msg, model) { | |
|
58 | // Displays a view for a particular model. | |
|
59 | var cell = this.get_msg_cell(msg.parent_header.msg_id); | |
|
60 | if (cell === null) { | |
|
61 | console.log("Could not determine where the display" + | |
|
62 | " message was from. Widget will not be displayed"); | |
|
63 | } else { | |
|
64 | var view = this.create_view(model, {cell: cell}); | |
|
65 | if (view === null) { | |
|
66 | console.error("View creation failed", model); | |
|
107 | 67 | } |
|
108 | return null; | |
|
109 | }; | |
|
110 | ||
|
111 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
|
112 | var cell = null; | |
|
113 | // First, check to see if the msg was triggered by cell execution. | |
|
114 | if (IPython.notebook) { | |
|
115 | cell = IPython.notebook.get_msg_cell(msg_id); | |
|
68 | if (cell.widget_subarea) { | |
|
69 | cell.widget_area.show(); | |
|
70 | this._handle_display_view(view); | |
|
71 | cell.widget_subarea.append(view.$el); | |
|
72 | view.trigger('displayed'); | |
|
116 | 73 | } |
|
117 | if (cell !== null) { | |
|
118 | return cell; | |
|
74 | } | |
|
75 | }; | |
|
76 | ||
|
77 | WidgetManager.prototype._handle_display_view = function (view) { | |
|
78 | // Have the IPython keyboard manager disable its event | |
|
79 | // handling so the widget can capture keyboard input. | |
|
80 | // Note, this is only done on the outer most widgets. | |
|
81 | if (this.keyboard_manager) { | |
|
82 | this.keyboard_manager.register_events(view.$el); | |
|
83 | ||
|
84 | if (view.additional_elements) { | |
|
85 | for (var i = 0; i < view.additional_elements.length; i++) { | |
|
86 | this.keyboard_manager.register_events(view.additional_elements[i]); | |
|
119 | 87 | } |
|
120 | // Second, check to see if a get_cell callback was defined | |
|
121 | // for the message. get_cell callbacks are registered for | |
|
122 | // widget messages, so this block is actually checking to see if the | |
|
123 | // message was triggered by a widget. | |
|
124 | var kernel = this.comm_manager.kernel; | |
|
125 | if (kernel) { | |
|
126 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
|
127 | if (callbacks && callbacks.iopub && | |
|
128 | callbacks.iopub.get_cell !== undefined) { | |
|
129 | return callbacks.iopub.get_cell(); | |
|
130 | } | |
|
88 | } | |
|
89 | } | |
|
90 | }; | |
|
91 | ||
|
92 | WidgetManager.prototype.create_view = function(model, options, view) { | |
|
93 | // Creates a view for a particular model. | |
|
94 | var view_name = model.get('_view_name'); | |
|
95 | var ViewType = WidgetManager._view_types[view_name]; | |
|
96 | if (ViewType) { | |
|
97 | ||
|
98 | // If a view is passed into the method, use that view's cell as | |
|
99 | // the cell for the view that is created. | |
|
100 | options = options || {}; | |
|
101 | if (view !== undefined) { | |
|
102 | options.cell = view.options.cell; | |
|
131 | 103 | } |
|
132 | ||
|
133 | // Not triggered by a cell or widget (no get_cell callback | |
|
134 | // exists). | |
|
135 | return null; | |
|
136 | }; | |
|
137 | ||
|
138 | WidgetManager.prototype.callbacks = function (view) { | |
|
139 | // callback handlers specific a view | |
|
140 | var callbacks = {}; | |
|
141 | if (view && view.options.cell) { | |
|
142 | ||
|
143 | // Try to get output handlers | |
|
144 |
|
|
|
145 | var handle_output = null; | |
|
146 | var handle_clear_output = null; | |
|
147 | if (cell.output_area) { | |
|
148 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
|
149 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
|
150 |
|
|
|
151 | ||
|
152 | // Create callback dict using what is known | |
|
153 | var that = this; | |
|
154 | callbacks = { | |
|
155 | iopub : { | |
|
156 | output : handle_output, | |
|
157 | clear_output : handle_clear_output, | |
|
158 | ||
|
159 | // Special function only registered by widget messages. | |
|
160 | // Allows us to get the cell for a message so we know | |
|
161 | // where to add widgets if the code requires it. | |
|
162 | get_cell : function () { | |
|
163 | return cell; | |
|
164 | }, | |
|
165 | }, | |
|
166 | }; | |
|
104 | ||
|
105 | // Create and render the view... | |
|
106 | var parameters = {model: model, options: options}; | |
|
107 | view = new ViewType(parameters); | |
|
108 | view.render(); | |
|
109 | model.on('destroy', view.remove, view); | |
|
110 | return view; | |
|
111 | } | |
|
112 | return null; | |
|
113 | }; | |
|
114 | ||
|
115 | WidgetManager.prototype.get_msg_cell = function (msg_id) { | |
|
116 | var cell = null; | |
|
117 | // First, check to see if the msg was triggered by cell execution. | |
|
118 | if (this.notebook) { | |
|
119 | cell = this.notebook.get_msg_cell(msg_id); | |
|
120 | } | |
|
121 | if (cell !== null) { | |
|
122 | return cell; | |
|
123 | } | |
|
124 | // Second, check to see if a get_cell callback was defined | |
|
125 | // for the message. get_cell callbacks are registered for | |
|
126 | // widget messages, so this block is actually checking to see if the | |
|
127 | // message was triggered by a widget. | |
|
128 | var kernel = this.comm_manager.kernel; | |
|
129 | if (kernel) { | |
|
130 | var callbacks = kernel.get_callbacks_for_msg(msg_id); | |
|
131 | if (callbacks && callbacks.iopub && | |
|
132 | callbacks.iopub.get_cell !== undefined) { | |
|
133 | return callbacks.iopub.get_cell(); | |
|
167 | 134 | } |
|
168 | return callbacks; | |
|
169 |
|
|
|
170 | ||
|
171 | WidgetManager.prototype.get_model = function (model_id) { | |
|
172 | // Look-up a model instance by its id. | |
|
173 | var model = this._models[model_id]; | |
|
174 | if (model !== undefined && model.id == model_id) { | |
|
175 | return model; | |
|
135 | } | |
|
136 | ||
|
137 | // Not triggered by a cell or widget (no get_cell callback | |
|
138 | // exists). | |
|
139 | return null; | |
|
140 | }; | |
|
141 | ||
|
142 | WidgetManager.prototype.callbacks = function (view) { | |
|
143 | // callback handlers specific a view | |
|
144 | var callbacks = {}; | |
|
145 | if (view && view.options.cell) { | |
|
146 | ||
|
147 | // Try to get output handlers | |
|
148 | var cell = view.options.cell; | |
|
149 | var handle_output = null; | |
|
150 | var handle_clear_output = null; | |
|
151 | if (cell.output_area) { | |
|
152 | handle_output = $.proxy(cell.output_area.handle_output, cell.output_area); | |
|
153 | handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area); | |
|
176 | 154 | } |
|
177 | return null; | |
|
178 | }; | |
|
179 | 155 | |
|
180 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
|
181 | // Handle when a comm is opened. | |
|
156 | // Create callback dict using what is known | |
|
182 | 157 | var that = this; |
|
183 | var model_id = comm.comm_id; | |
|
184 | var widget_type_name = msg.content.target_name; | |
|
185 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); | |
|
186 | widget_model.on('comm:close', function () { | |
|
187 | delete that._models[model_id]; | |
|
188 | }); | |
|
189 | this._models[model_id] = widget_model; | |
|
190 | }; | |
|
191 | ||
|
192 | // For backwards compatability. | |
|
193 | IPython.WidgetManager = WidgetManager; | |
|
158 | callbacks = { | |
|
159 | iopub : { | |
|
160 | output : handle_output, | |
|
161 | clear_output : handle_clear_output, | |
|
162 | ||
|
163 | // Special function only registered by widget messages. | |
|
164 | // Allows us to get the cell for a message so we know | |
|
165 | // where to add widgets if the code requires it. | |
|
166 | get_cell : function () { | |
|
167 | return cell; | |
|
168 | }, | |
|
169 | }, | |
|
170 | }; | |
|
171 | } | |
|
172 | return callbacks; | |
|
173 | }; | |
|
174 | ||
|
175 | WidgetManager.prototype.get_model = function (model_id) { | |
|
176 | // Look-up a model instance by its id. | |
|
177 | var model = this._models[model_id]; | |
|
178 | if (model !== undefined && model.id == model_id) { | |
|
179 | return model; | |
|
180 | } | |
|
181 | return null; | |
|
182 | }; | |
|
183 | ||
|
184 | WidgetManager.prototype._handle_comm_open = function (comm, msg) { | |
|
185 | // Handle when a comm is opened. | |
|
186 | var that = this; | |
|
187 | var model_id = comm.comm_id; | |
|
188 | var widget_type_name = msg.content.target_name; | |
|
189 | var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm); | |
|
190 | widget_model.on('comm:close', function () { | |
|
191 | delete that._models[model_id]; | |
|
192 | }); | |
|
193 | this._models[model_id] = widget_model; | |
|
194 | }; | |
|
194 | 195 | |
|
195 | 196 | return WidgetManager; |
|
196 |
|
|
|
197 | }); |
@@ -208,20 +208,20 b' function(WidgetManager, _, Backbone){' | |||
|
208 | 208 | |
|
209 | 209 | _pack_models: function(value) { |
|
210 | 210 | // Replace models with model ids recursively. |
|
211 | var that = this; | |
|
212 | var packed; | |
|
211 | 213 | if (value instanceof Backbone.Model) { |
|
212 | 214 | return value.id; |
|
213 | 215 | |
|
214 | 216 | } else if ($.isArray(value)) { |
|
215 |
|
|
|
216 | var that = this; | |
|
217 | packed = []; | |
|
217 | 218 | _.each(value, function(sub_value, key) { |
|
218 | 219 | packed.push(that._pack_models(sub_value)); |
|
219 | 220 | }); |
|
220 | 221 | return packed; |
|
221 | 222 | |
|
222 | 223 | } else if (value instanceof Object) { |
|
223 |
|
|
|
224 | var that = this; | |
|
224 | packed = {}; | |
|
225 | 225 | _.each(value, function(sub_value, key) { |
|
226 | 226 | packed[key] = that._pack_models(sub_value); |
|
227 | 227 | }); |
@@ -3,7 +3,8 b'' | |||
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | "widgets/js/widget", |
|
6 | ], function(widget){ | |
|
6 | "base/js/utils", | |
|
7 | ], function(widget, utils){ | |
|
7 | 8 | |
|
8 | 9 | var DropdownView = widget.DOMWidgetView.extend({ |
|
9 | 10 | render : function(){ |
@@ -226,7 +227,7 b' define([' | |||
|
226 | 227 | if (item.trim().length == 0) { |
|
227 | 228 | item_html = " "; |
|
228 | 229 | } else { |
|
229 |
item_html = |
|
|
230 | item_html = utils.escape_html(item); | |
|
230 | 231 | } |
|
231 | 232 | var item_query = '[data-value="' + item + '"]'; |
|
232 | 233 | var $item_element = that.$buttongroup.find(item_query); |
@@ -3,12 +3,13 b'' | |||
|
3 | 3 | |
|
4 | 4 | define([ |
|
5 | 5 | "widgets/js/widget", |
|
6 | ], function(widget){ | |
|
6 | "base/js/utils", | |
|
7 | ], function(widget, utils){ | |
|
7 | 8 | |
|
8 | 9 | var AccordionView = widget.DOMWidgetView.extend({ |
|
9 | 10 | render: function(){ |
|
10 | 11 | // Called when view is rendered. |
|
11 |
var guid = 'panel-group' + |
|
|
12 | var guid = 'panel-group' + utils.uuid(); | |
|
12 | 13 | this.$el |
|
13 | 14 | .attr('id', guid) |
|
14 | 15 | .addClass('panel-group'); |
@@ -88,7 +89,7 b' define([' | |||
|
88 | 89 | // Called when a child is added to children list. |
|
89 | 90 | var view = this.create_child_view(model); |
|
90 | 91 | var index = this.containers.length; |
|
91 |
var uuid = |
|
|
92 | var uuid = utils.uuid(); | |
|
92 | 93 | var accordion_group = $('<div />') |
|
93 | 94 | .addClass('panel panel-default') |
|
94 | 95 | .appendTo(this.$el); |
@@ -141,7 +142,7 b' define([' | |||
|
141 | 142 | |
|
142 | 143 | render: function(){ |
|
143 | 144 | // Called when view is rendered. |
|
144 |
var uuid = 'tabs'+ |
|
|
145 | var uuid = 'tabs'+utils.uuid(); | |
|
145 | 146 | var that = this; |
|
146 | 147 | this.$tabs = $('<div />', {id: uuid}) |
|
147 | 148 | .addClass('nav') |
@@ -189,7 +190,7 b' define([' | |||
|
189 | 190 | // Called when a child is added to children list. |
|
190 | 191 | var view = this.create_child_view(model); |
|
191 | 192 | var index = this.containers.length; |
|
192 |
var uuid = |
|
|
193 | var uuid = utils.uuid(); | |
|
193 | 194 | |
|
194 | 195 | var that = this; |
|
195 | 196 | var tab = $('<li />') |
General Comments 0
You need to be logged in to leave comments.
Login now