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