|
@@
-3,194
+3,195
b''
|
|
3
|
|
|
3
|
|
|
4
|
define([
|
|
4
|
define([
|
|
5
|
"underscore",
|
|
5
|
"underscore",
|
|
6
|
"backbone",
|
|
6
|
"backbone",
|
|
7
|
], function (_, Backbone) {
|
|
7
|
], function (_, Backbone) {
|
|
8
|
|
|
8
|
|
|
9
|
//--------------------------------------------------------------------
|
|
9
|
//--------------------------------------------------------------------
|
|
10
|
// WidgetManager class
|
|
10
|
// WidgetManager class
|
|
11
|
//--------------------------------------------------------------------
|
|
11
|
//--------------------------------------------------------------------
|
|
12
|
var WidgetManager = function (comm_manager) {
|
|
12
|
var WidgetManager = function (comm_manager, keyboard_manager, notebook) {
|
|
13
|
// Public constructor
|
|
13
|
// Public constructor
|
|
14
|
WidgetManager._managers.push(this);
|
|
14
|
WidgetManager._managers.push(this);
|
|
15
|
|
|
15
|
|
|
16
|
// Attach a comm manager to the
|
|
16
|
// Attach a comm manager to the
|
|
17
|
this.comm_manager = comm_manager;
|
|
17
|
this.keyboard_manager = keyboard_manager;
|
|
18
|
this._models = {}; /* Dictionary of model ids and model instances */
|
|
18
|
this.notebook = notebook;
|
|
19
|
|
|
19
|
this.comm_manager = comm_manager;
|
|
20
|
// Register already-registered widget model types with the comm manager.
|
|
20
|
this._models = {}; /* Dictionary of model ids and model instances */
|
|
21
|
var that = this;
|
|
21
|
|
|
22
|
_.each(WidgetManager._model_types, function(model_type, model_name) {
|
|
22
|
// Register already-registered widget model types with the comm manager.
|
|
23
|
that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
|
|
23
|
var that = this;
|
|
24
|
});
|
|
24
|
_.each(WidgetManager._model_types, function(model_type, model_name) {
|
|
25
|
};
|
|
25
|
that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
|
|
26
|
|
|
26
|
});
|
|
27
|
//--------------------------------------------------------------------
|
|
27
|
};
|
|
28
|
// Class level
|
|
28
|
|
|
29
|
//--------------------------------------------------------------------
|
|
29
|
//--------------------------------------------------------------------
|
|
30
|
WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
|
|
30
|
// Class level
|
|
31
|
WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
|
|
31
|
//--------------------------------------------------------------------
|
|
32
|
WidgetManager._managers = []; /* List of widget managers */
|
|
32
|
WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
|
|
33
|
|
|
33
|
WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
|
|
34
|
WidgetManager.register_widget_model = function (model_name, model_type) {
|
|
34
|
WidgetManager._managers = []; /* List of widget managers */
|
|
35
|
// Registers a widget model by name.
|
|
35
|
|
|
36
|
WidgetManager._model_types[model_name] = model_type;
|
|
36
|
WidgetManager.register_widget_model = function (model_name, model_type) {
|
|
37
|
|
|
37
|
// Registers a widget model by name.
|
|
38
|
// Register the widget with the comm manager. Make sure to pass this object's context
|
|
38
|
WidgetManager._model_types[model_name] = model_type;
|
|
39
|
// in so `this` works in the call back.
|
|
39
|
|
|
40
|
_.each(WidgetManager._managers, function(instance, i) {
|
|
40
|
// Register the widget with the comm manager. Make sure to pass this object's context
|
|
41
|
if (instance.comm_manager !== null) {
|
|
41
|
// in so `this` works in the call back.
|
|
42
|
instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
|
|
42
|
_.each(WidgetManager._managers, function(instance, i) {
|
|
43
|
}
|
|
43
|
if (instance.comm_manager !== null) {
|
|
44
|
});
|
|
44
|
instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
|
|
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
|
}
|
|
|
|
|
72
|
}
|
|
45
|
}
|
|
73
|
};
|
|
46
|
});
|
|
74
|
|
|
47
|
};
|
|
75
|
WidgetManager.prototype._handle_display_view = function (view) {
|
|
48
|
|
|
76
|
// Have the IPython keyboard manager disable its event
|
|
49
|
WidgetManager.register_widget_view = function (view_name, view_type) {
|
|
77
|
// handling so the widget can capture keyboard input.
|
|
50
|
// Registers a widget view by name.
|
|
78
|
// Note, this is only done on the outer most widgets.
|
|
51
|
WidgetManager._view_types[view_name] = view_type;
|
|
79
|
IPython.keyboard_manager.register_events(view.$el);
|
|
52
|
};
|
|
80
|
|
|
53
|
|
|
81
|
if (view.additional_elements) {
|
|
54
|
//--------------------------------------------------------------------
|
|
82
|
for (var i = 0; i < view.additional_elements.length; i++) {
|
|
55
|
// Instance level
|
|
83
|
IPython.keyboard_manager.register_events(view.additional_elements[i]);
|
|
56
|
//--------------------------------------------------------------------
|
|
84
|
}
|
|
57
|
WidgetManager.prototype.display_view = function(msg, model) {
|
|
85
|
}
|
|
58
|
// Displays a view for a particular model.
|
|
86
|
};
|
|
59
|
var cell = this.get_msg_cell(msg.parent_header.msg_id);
|
|
87
|
|
|
60
|
if (cell === null) {
|
|
88
|
WidgetManager.prototype.create_view = function(model, options, view) {
|
|
61
|
console.log("Could not determine where the display" +
|
|
89
|
// Creates a view for a particular model.
|
|
62
|
" message was from. Widget will not be displayed");
|
|
90
|
var view_name = model.get('_view_name');
|
|
63
|
} else {
|
|
91
|
var ViewType = WidgetManager._view_types[view_name];
|
|
64
|
var view = this.create_view(model, {cell: cell});
|
|
92
|
if (ViewType) {
|
|
65
|
if (view === null) {
|
|
93
|
|
|
66
|
console.error("View creation failed", model);
|
|
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;
|
|
|
|
|
107
|
}
|
|
67
|
}
|
|
108
|
return null;
|
|
68
|
if (cell.widget_subarea) {
|
|
109
|
};
|
|
69
|
cell.widget_area.show();
|
|
110
|
|
|
70
|
this._handle_display_view(view);
|
|
111
|
WidgetManager.prototype.get_msg_cell = function (msg_id) {
|
|
71
|
cell.widget_subarea.append(view.$el);
|
|
112
|
var cell = null;
|
|
72
|
view.trigger('displayed');
|
|
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);
|
|
|
|
|
116
|
}
|
|
73
|
}
|
|
117
|
if (cell !== null) {
|
|
74
|
}
|
|
118
|
return cell;
|
|
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
|
|
88
|
}
|
|
121
|
// for the message. get_cell callbacks are registered for
|
|
89
|
}
|
|
122
|
// widget messages, so this block is actually checking to see if the
|
|
90
|
};
|
|
123
|
// message was triggered by a widget.
|
|
91
|
|
|
124
|
var kernel = this.comm_manager.kernel;
|
|
92
|
WidgetManager.prototype.create_view = function(model, options, view) {
|
|
125
|
if (kernel) {
|
|
93
|
// Creates a view for a particular model.
|
|
126
|
var callbacks = kernel.get_callbacks_for_msg(msg_id);
|
|
94
|
var view_name = model.get('_view_name');
|
|
127
|
if (callbacks && callbacks.iopub &&
|
|
95
|
var ViewType = WidgetManager._view_types[view_name];
|
|
128
|
callbacks.iopub.get_cell !== undefined) {
|
|
96
|
if (ViewType) {
|
|
129
|
return callbacks.iopub.get_cell();
|
|
97
|
|
|
130
|
}
|
|
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
|
|
|
104
|
|
|
133
|
// Not triggered by a cell or widget (no get_cell callback
|
|
105
|
// Create and render the view...
|
|
134
|
// exists).
|
|
106
|
var parameters = {model: model, options: options};
|
|
135
|
return null;
|
|
107
|
view = new ViewType(parameters);
|
|
136
|
};
|
|
108
|
view.render();
|
|
137
|
|
|
109
|
model.on('destroy', view.remove, view);
|
|
138
|
WidgetManager.prototype.callbacks = function (view) {
|
|
110
|
return view;
|
|
139
|
// callback handlers specific a view
|
|
111
|
}
|
|
140
|
var callbacks = {};
|
|
112
|
return null;
|
|
141
|
if (view && view.options.cell) {
|
|
113
|
};
|
|
142
|
|
|
114
|
|
|
143
|
// Try to get output handlers
|
|
115
|
WidgetManager.prototype.get_msg_cell = function (msg_id) {
|
|
144
|
var cell = view.options.cell;
|
|
116
|
var cell = null;
|
|
145
|
var handle_output = null;
|
|
117
|
// First, check to see if the msg was triggered by cell execution.
|
|
146
|
var handle_clear_output = null;
|
|
118
|
if (this.notebook) {
|
|
147
|
if (cell.output_area) {
|
|
119
|
cell = this.notebook.get_msg_cell(msg_id);
|
|
148
|
handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
|
|
120
|
}
|
|
149
|
handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
|
|
121
|
if (cell !== null) {
|
|
150
|
}
|
|
122
|
return cell;
|
|
151
|
|
|
123
|
}
|
|
152
|
// Create callback dict using what is known
|
|
124
|
// Second, check to see if a get_cell callback was defined
|
|
153
|
var that = this;
|
|
125
|
// for the message. get_cell callbacks are registered for
|
|
154
|
callbacks = {
|
|
126
|
// widget messages, so this block is actually checking to see if the
|
|
155
|
iopub : {
|
|
127
|
// message was triggered by a widget.
|
|
156
|
output : handle_output,
|
|
128
|
var kernel = this.comm_manager.kernel;
|
|
157
|
clear_output : handle_clear_output,
|
|
129
|
if (kernel) {
|
|
158
|
|
|
130
|
var callbacks = kernel.get_callbacks_for_msg(msg_id);
|
|
159
|
// Special function only registered by widget messages.
|
|
131
|
if (callbacks && callbacks.iopub &&
|
|
160
|
// Allows us to get the cell for a message so we know
|
|
132
|
callbacks.iopub.get_cell !== undefined) {
|
|
161
|
// where to add widgets if the code requires it.
|
|
133
|
return callbacks.iopub.get_cell();
|
|
162
|
get_cell : function () {
|
|
|
|
|
163
|
return cell;
|
|
|
|
|
164
|
},
|
|
|
|
|
165
|
},
|
|
|
|
|
166
|
};
|
|
|
|
|
167
|
}
|
|
134
|
}
|
|
168
|
return callbacks;
|
|
135
|
}
|
|
169
|
};
|
|
136
|
|
|
170
|
|
|
137
|
// Not triggered by a cell or widget (no get_cell callback
|
|
171
|
WidgetManager.prototype.get_model = function (model_id) {
|
|
138
|
// exists).
|
|
172
|
// Look-up a model instance by its id.
|
|
139
|
return null;
|
|
173
|
var model = this._models[model_id];
|
|
140
|
};
|
|
174
|
if (model !== undefined && model.id == model_id) {
|
|
141
|
|
|
175
|
return model;
|
|
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) {
|
|
156
|
// Create callback dict using what is known
|
|
181
|
// Handle when a comm is opened.
|
|
|
|
|
182
|
var that = this;
|
|
157
|
var that = this;
|
|
183
|
var model_id = comm.comm_id;
|
|
158
|
callbacks = {
|
|
184
|
var widget_type_name = msg.content.target_name;
|
|
159
|
iopub : {
|
|
185
|
var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
|
|
160
|
output : handle_output,
|
|
186
|
widget_model.on('comm:close', function () {
|
|
161
|
clear_output : handle_clear_output,
|
|
187
|
delete that._models[model_id];
|
|
162
|
|
|
188
|
});
|
|
163
|
// Special function only registered by widget messages.
|
|
189
|
this._models[model_id] = widget_model;
|
|
164
|
// Allows us to get the cell for a message so we know
|
|
190
|
};
|
|
165
|
// where to add widgets if the code requires it.
|
|
191
|
|
|
166
|
get_cell : function () {
|
|
192
|
// For backwards compatability.
|
|
167
|
return cell;
|
|
193
|
IPython.WidgetManager = WidgetManager;
|
|
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
|
return WidgetManager;
|
|
196
|
return WidgetManager;
|
|
196
|
});
|
|
197
|
});
|