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