##// END OF EJS Templates
Only call register_events on top level widget.
Brian E. Granger -
Show More
@@ -1,210 +1,207 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 var that = this;
41 41 _.each(WidgetManager._model_types, function(model_type, model_name) {
42 42 that.comm_manager.register_target(model_name, $.proxy(that._handle_comm_open, that));
43 43 });
44 44 };
45 45
46 46 //--------------------------------------------------------------------
47 47 // Class level
48 48 //--------------------------------------------------------------------
49 49 WidgetManager._model_types = {}; /* Dictionary of model type names (target_name) and model types. */
50 50 WidgetManager._view_types = {}; /* Dictionary of view names and view types. */
51 51 WidgetManager._managers = []; /* List of widget managers */
52 52
53 53 WidgetManager.register_widget_model = function (model_name, model_type) {
54 54 // Registers a widget model by name.
55 55 WidgetManager._model_types[model_name] = model_type;
56 56
57 57 // Register the widget with the comm manager. Make sure to pass this object's context
58 58 // in so `this` works in the call back.
59 59 _.each(WidgetManager._managers, function(instance, i) {
60 60 if (instance.comm_manager !== null) {
61 61 instance.comm_manager.register_target(model_name, $.proxy(instance._handle_comm_open, instance));
62 62 }
63 63 });
64 64 };
65 65
66 66 WidgetManager.register_widget_view = function (view_name, view_type) {
67 67 // Registers a widget view by name.
68 68 WidgetManager._view_types[view_name] = view_type;
69 69 };
70 70
71 71 //--------------------------------------------------------------------
72 72 // Instance level
73 73 //--------------------------------------------------------------------
74 74 WidgetManager.prototype.display_view = function(msg, model) {
75 75 // Displays a view for a particular model.
76 76 var cell = this.get_msg_cell(msg.parent_header.msg_id);
77 77 if (cell === null) {
78 78 console.log("Could not determine where the display" +
79 79 " message was from. Widget will not be displayed");
80 80 } else {
81 81 var view = this.create_view(model, {cell: cell});
82 82 if (view === null) {
83 83 console.error("View creation failed", model);
84 84 }
85 85 if (cell.widget_subarea) {
86 86
87 87 cell.widget_area.show();
88 // Have the IPython keyboard manager disable its event
89 // handling so the widget can capture keyboard input.
90 // Note, this is only done on the outer most widget.
91 IPython.keyboard_manager.register_events(view.$el);
88 92 cell.widget_subarea.append(view.$el);
89 93 }
90 94 }
91 95 };
92 96
93 97 WidgetManager.prototype.create_view = function(model, options, view) {
94 98 // Creates a view for a particular model.
95 99 var view_name = model.get('_view_name');
96 100 var ViewType = WidgetManager._view_types[view_name];
97 101 if (ViewType) {
98 102
99 103 // If a view is passed into the method, use that view's cell as
100 104 // the cell for the view that is created.
101 105 options = options || {};
102 106 if (view !== undefined) {
103 107 options.cell = view.options.cell;
104 108 }
105 109
106 110 // Create and render the view...
107 111 var parameters = {model: model, options: options};
108 112 view = new ViewType(parameters);
109 113 view.render();
110 114 model.views.push(view);
111 115 model.on('destroy', view.remove, view);
112 116
113 117 this._handle_new_view(view);
114 118 return view;
115 119 }
116 120 return null;
117 121 };
118 122
119 123 WidgetManager.prototype._handle_new_view = function (view) {
120 124 // Called when a view has been created and rendered.
121
122 // If the view has a well defined element, inform the keyboard
123 // manager about the view's element, so as the element can
124 // escape the dreaded command mode.
125 if (view.$el) {
126 IPython.keyboard_manager.register_events(view.$el);
127 }
128 125 };
129 126
130 127 WidgetManager.prototype.get_msg_cell = function (msg_id) {
131 128 var cell = null;
132 129 // First, check to see if the msg was triggered by cell execution.
133 130 if (IPython.notebook) {
134 131 cell = IPython.notebook.get_msg_cell(msg_id);
135 132 }
136 133 if (cell !== null) {
137 134 return cell;
138 135 }
139 136 // Second, check to see if a get_cell callback was defined
140 137 // for the message. get_cell callbacks are registered for
141 138 // widget messages, so this block is actually checking to see if the
142 139 // message was triggered by a widget.
143 140 var kernel = this.comm_manager.kernel;
144 141 if (kernel) {
145 142 var callbacks = kernel.get_callbacks_for_msg(msg_id);
146 143 if (callbacks && callbacks.iopub &&
147 144 callbacks.iopub.get_cell !== undefined) {
148 145 return callbacks.iopub.get_cell();
149 146 }
150 147 }
151 148
152 149 // Not triggered by a cell or widget (no get_cell callback
153 150 // exists).
154 151 return null;
155 152 };
156 153
157 154 WidgetManager.prototype.callbacks = function (view) {
158 155 // callback handlers specific a view
159 156 var callbacks = {};
160 157 if (view && view.options.cell) {
161 158
162 159 // Try to get output handlers
163 160 var cell = view.options.cell;
164 161 var handle_output = null;
165 162 var handle_clear_output = null;
166 163 if (cell.output_area) {
167 164 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
168 165 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
169 166 }
170 167
171 168 // Create callback dict using what is known
172 169 var that = this;
173 170 callbacks = {
174 171 iopub : {
175 172 output : handle_output,
176 173 clear_output : handle_clear_output,
177 174
178 175 // Special function only registered by widget messages.
179 176 // Allows us to get the cell for a message so we know
180 177 // where to add widgets if the code requires it.
181 178 get_cell : function () {
182 179 return cell;
183 180 },
184 181 },
185 182 };
186 183 }
187 184 return callbacks;
188 185 };
189 186
190 187 WidgetManager.prototype.get_model = function (model_id) {
191 188 // Look-up a model instance by its id.
192 189 var model = this._models[model_id];
193 190 if (model !== undefined && model.id == model_id) {
194 191 return model;
195 192 }
196 193 return null;
197 194 };
198 195
199 196 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
200 197 // Handle when a comm is opened.
201 198 var model_id = comm.comm_id;
202 199 var widget_type_name = msg.content.target_name;
203 200 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
204 201 this._models[model_id] = widget_model;
205 202 };
206 203
207 204 IPython.WidgetManager = WidgetManager;
208 205 return IPython.WidgetManager;
209 206 });
210 207 }());
General Comments 0
You need to be logged in to leave comments. Login now