##// END OF EJS Templates
Unconditionally register $el with keyboard manager...
Jonathan Frederic -
Show More
@@ -1,210 +1,210
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 (_, 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 cell.widget_area.show();
87 87 this._handle_display_view(view);
88 88 cell.widget_subarea.append(view.$el);
89 89 }
90 90 }
91 91 };
92 92
93 93 WidgetManager.prototype._handle_display_view = function (view) {
94 94 // Have the IPython keyboard manager disable its event
95 95 // handling so the widget can capture keyboard input.
96 96 // Note, this is only done on the outer most widgets.
97 if (view.elements) {
98 for (var i = 0; i < view.elements.length; i++) {
99 IPython.keyboard_manager.register_events(view.elements[i]);
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]);
100 102 }
101 } else {
102 IPython.keyboard_manager.register_events(view.$el);
103 }
103 }
104 104 };
105 105
106 106 WidgetManager.prototype.create_view = function(model, options, view) {
107 107 // Creates a view for a particular model.
108 108 var view_name = model.get('_view_name');
109 109 var ViewType = WidgetManager._view_types[view_name];
110 110 if (ViewType) {
111 111
112 112 // If a view is passed into the method, use that view's cell as
113 113 // the cell for the view that is created.
114 114 options = options || {};
115 115 if (view !== undefined) {
116 116 options.cell = view.options.cell;
117 117 }
118 118
119 119 // Create and render the view...
120 120 var parameters = {model: model, options: options};
121 121 view = new ViewType(parameters);
122 122 view.render();
123 123 model.views.push(view);
124 124 model.on('destroy', view.remove, view);
125 125 return view;
126 126 }
127 127 return null;
128 128 };
129 129
130 130 WidgetManager.prototype.get_msg_cell = function (msg_id) {
131 131 var cell = null;
132 132 // First, check to see if the msg was triggered by cell execution.
133 133 if (IPython.notebook) {
134 134 cell = IPython.notebook.get_msg_cell(msg_id);
135 135 }
136 136 if (cell !== null) {
137 137 return cell;
138 138 }
139 139 // Second, check to see if a get_cell callback was defined
140 140 // for the message. get_cell callbacks are registered for
141 141 // widget messages, so this block is actually checking to see if the
142 142 // message was triggered by a widget.
143 143 var kernel = this.comm_manager.kernel;
144 144 if (kernel) {
145 145 var callbacks = kernel.get_callbacks_for_msg(msg_id);
146 146 if (callbacks && callbacks.iopub &&
147 147 callbacks.iopub.get_cell !== undefined) {
148 148 return callbacks.iopub.get_cell();
149 149 }
150 150 }
151 151
152 152 // Not triggered by a cell or widget (no get_cell callback
153 153 // exists).
154 154 return null;
155 155 };
156 156
157 157 WidgetManager.prototype.callbacks = function (view) {
158 158 // callback handlers specific a view
159 159 var callbacks = {};
160 160 if (view && view.options.cell) {
161 161
162 162 // Try to get output handlers
163 163 var cell = view.options.cell;
164 164 var handle_output = null;
165 165 var handle_clear_output = null;
166 166 if (cell.output_area) {
167 167 handle_output = $.proxy(cell.output_area.handle_output, cell.output_area);
168 168 handle_clear_output = $.proxy(cell.output_area.handle_clear_output, cell.output_area);
169 169 }
170 170
171 171 // Create callback dict using what is known
172 172 var that = this;
173 173 callbacks = {
174 174 iopub : {
175 175 output : handle_output,
176 176 clear_output : handle_clear_output,
177 177
178 178 // Special function only registered by widget messages.
179 179 // Allows us to get the cell for a message so we know
180 180 // where to add widgets if the code requires it.
181 181 get_cell : function () {
182 182 return cell;
183 183 },
184 184 },
185 185 };
186 186 }
187 187 return callbacks;
188 188 };
189 189
190 190 WidgetManager.prototype.get_model = function (model_id) {
191 191 // Look-up a model instance by its id.
192 192 var model = this._models[model_id];
193 193 if (model !== undefined && model.id == model_id) {
194 194 return model;
195 195 }
196 196 return null;
197 197 };
198 198
199 199 WidgetManager.prototype._handle_comm_open = function (comm, msg) {
200 200 // Handle when a comm is opened.
201 201 var model_id = comm.comm_id;
202 202 var widget_type_name = msg.content.target_name;
203 203 var widget_model = new WidgetManager._model_types[widget_type_name](this, model_id, comm);
204 204 this._models[model_id] = widget_model;
205 205 };
206 206
207 207 IPython.WidgetManager = WidgetManager;
208 208 return IPython.WidgetManager;
209 209 });
210 210 }());
@@ -1,282 +1,282
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 // ContainerWidget
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 **/
16 16
17 17 define(["notebook/js/widgets/widget"], function(WidgetManager) {
18 18
19 19 var ContainerView = IPython.DOMWidgetView.extend({
20 20 render: function(){
21 21 // Called when view is rendered.
22 22 this.$el.addClass('widget-container')
23 23 .addClass('vbox');
24 24 this.children={};
25 25 this.update_children([], this.model.get('_children'));
26 26 this.model.on('change:_children', function(model, value, options) {
27 27 this.update_children(model.previous('_children'), value);
28 28 }, this);
29 29 this.update();
30 30 },
31 31
32 32 update_children: function(old_list, new_list) {
33 33 // Called when the children list changes.
34 34 this.do_diff(old_list,
35 35 new_list,
36 36 $.proxy(this.remove_child_model, this),
37 37 $.proxy(this.add_child_model, this));
38 38 },
39 39
40 40 remove_child_model: function(model) {
41 41 // Called when a model is removed from the children list.
42 42 this.child_views[model.id].remove();
43 43 this.delete_child_view(model);
44 44 },
45 45
46 46 add_child_model: function(model) {
47 47 // Called when a model is added to the children list.
48 48 var view = this.create_child_view(model);
49 49 this.$el.append(view.$el);
50 50 },
51 51
52 52 update: function(){
53 53 // Update the contents of this view
54 54 //
55 55 // Called when the model is changed. The model may have been
56 56 // changed by another view or by a state update from the back-end.
57 57 return ContainerView.__super__.update.apply(this);
58 58 },
59 59 });
60 60
61 61 WidgetManager.register_widget_view('ContainerView', ContainerView);
62 62
63 63 var PopupView = IPython.DOMWidgetView.extend({
64 64 render: function(){
65 65 // Called when view is rendered.
66 66 var that = this;
67 67 this.children={};
68 68
69 69 this.$el.on("remove", function(){
70 70 that.$window.remove();
71 71 });
72 72 this.$window = $('<div />')
73 73 .addClass('modal widget-modal')
74 74 .appendTo($('#notebook-container'))
75 75 .mousedown(function(){
76 76 that.bring_to_front();
77 77 });
78 78
79 79 // Set the elements array since the this.$window element is not child
80 80 // of this.$el and the parent widget manager or other widgets may
81 81 // need to know about all of the top-level widgets. The IPython
82 82 // widget manager uses this to register the elements with the
83 83 // keyboard manager.
84 this.elements = [this.$el, this.$window]
84 this.additional_elements = [this.$window]
85 85
86 86 this.$title_bar = $('<div />')
87 87 .addClass('popover-title')
88 88 .appendTo(this.$window)
89 89 .mousedown(function(){
90 90 that.bring_to_front();
91 91 });
92 92 this.$close = $('<button />')
93 93 .addClass('close icon-remove')
94 94 .css('margin-left', '5px')
95 95 .appendTo(this.$title_bar)
96 96 .click(function(){
97 97 that.hide();
98 98 event.stopPropagation();
99 99 });
100 100 this.$minimize = $('<button />')
101 101 .addClass('close icon-arrow-down')
102 102 .appendTo(this.$title_bar)
103 103 .click(function(){
104 104 that.popped_out = !that.popped_out;
105 105 if (!that.popped_out) {
106 106 that.$minimize
107 107 .removeClass('icon-arrow-down')
108 108 .addClass('icon-arrow-up');
109 109
110 110 that.$window
111 111 .draggable('destroy')
112 112 .resizable('destroy')
113 113 .removeClass('widget-modal modal')
114 114 .addClass('docked-widget-modal')
115 115 .detach()
116 116 .insertBefore(that.$show_button);
117 117 that.$show_button.hide();
118 118 that.$close.hide();
119 119 } else {
120 120 that.$minimize
121 121 .addClass('icon-arrow-down')
122 122 .removeClass('icon-arrow-up');
123 123
124 124 that.$window
125 125 .removeClass('docked-widget-modal')
126 126 .addClass('widget-modal modal')
127 127 .detach()
128 128 .appendTo($('#notebook-container'))
129 129 .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
130 130 .resizable()
131 131 .children('.ui-resizable-handle').show();
132 132 that.show();
133 133 that.$show_button.show();
134 134 that.$close.show();
135 135 }
136 136 event.stopPropagation();
137 137 });
138 138 this.$title = $('<div />')
139 139 .addClass('widget-modal-title')
140 140 .text(' ')
141 141 .appendTo(this.$title_bar);
142 142 this.$body = $('<div />')
143 143 .addClass('modal-body')
144 144 .addClass('widget-modal-body')
145 145 .addClass('widget-container')
146 146 .addClass('vbox')
147 147 .appendTo(this.$window);
148 148
149 149 this.$show_button = $('<button />')
150 150 .text(' ')
151 151 .addClass('btn btn-info widget-modal-show')
152 152 .appendTo(this.$el)
153 153 .click(function(){
154 154 that.show();
155 155 });
156 156
157 157 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
158 158 this.$window.resizable();
159 159 this.$window.on('resize', function(){
160 160 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
161 161 });
162 162
163 163 this.$el_to_style = this.$body;
164 164 this._shown_once = false;
165 165 this.popped_out = true;
166 166
167 167 this.update_children([], this.model.get('_children'));
168 168 this.model.on('change:_children', function(model, value, options) {
169 169 this.update_children(model.previous('_children'), value);
170 170 }, this);
171 171 this.update();
172 172 },
173 173
174 174 hide: function() {
175 175 // Called when the modal hide button is clicked.
176 176 this.$window.hide();
177 177 this.$show_button.removeClass('btn-info');
178 178 },
179 179
180 180 show: function() {
181 181 // Called when the modal show button is clicked.
182 182 this.$show_button.addClass('btn-info');
183 183 this.$window.show();
184 184 if (this.popped_out) {
185 185 this.$window.css("positon", "absolute");
186 186 this.$window.css("top", "0px");
187 187 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
188 188 $(window).scrollLeft()) + "px");
189 189 this.bring_to_front();
190 190 }
191 191 },
192 192
193 193 bring_to_front: function() {
194 194 // Make the modal top-most, z-ordered about the other modals.
195 195 var $widget_modals = $(".widget-modal");
196 196 var max_zindex = 0;
197 197 $widget_modals.each(function (index, el){
198 198 max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
199 199 });
200 200
201 201 // Start z-index of widget modals at 2000
202 202 max_zindex = Math.max(max_zindex, 2000);
203 203
204 204 $widget_modals.each(function (index, el){
205 205 $el = $(el);
206 206 if (max_zindex == parseInt($el.css('z-index'))) {
207 207 $el.css('z-index', max_zindex - 1);
208 208 }
209 209 });
210 210 this.$window.css('z-index', max_zindex);
211 211 },
212 212
213 213 update_children: function(old_list, new_list) {
214 214 // Called when the children list is modified.
215 215 this.do_diff(old_list,
216 216 new_list,
217 217 $.proxy(this.remove_child_model, this),
218 218 $.proxy(this.add_child_model, this));
219 219 },
220 220
221 221 remove_child_model: function(model) {
222 222 // Called when a child is removed from children list.
223 223 this.child_views[model.id].remove();
224 224 this.delete_child_view(model);
225 225 },
226 226
227 227 add_child_model: function(model) {
228 228 // Called when a child is added to children list.
229 229 var view = this.create_child_view(model);
230 230 this.$body.append(view.$el);
231 231 },
232 232
233 233 update: function(){
234 234 // Update the contents of this view
235 235 //
236 236 // Called when the model is changed. The model may have been
237 237 // changed by another view or by a state update from the back-end.
238 238 var description = this.model.get('description');
239 239 if (description.length === 0) {
240 240 this.$title.text(' '); // Preserve title height
241 241 } else {
242 242 this.$title.text(description);
243 243 }
244 244
245 245 var button_text = this.model.get('button_text');
246 246 if (button_text.length === 0) {
247 247 this.$show_button.text(' '); // Preserve button height
248 248 } else {
249 249 this.$show_button.text(button_text);
250 250 }
251 251
252 252 if (!this._shown_once) {
253 253 this._shown_once = true;
254 254 this.show();
255 255 }
256 256
257 257 return PopupView.__super__.update.apply(this);
258 258 },
259 259
260 260 _get_selector_element: function(selector) {
261 261 // Get an element view a 'special' jquery selector. (see widget.js)
262 262 //
263 263 // Since the modal actually isn't within the $el in the DOM, we need to extend
264 264 // the selector logic to allow the user to set css on the modal if need be.
265 265 // The convention used is:
266 266 // "modal" - select the modal div
267 267 // "modal [selector]" - select element(s) within the modal div.
268 268 // "[selector]" - select elements within $el
269 269 // "" - select the $el_to_style
270 270 if (selector.substring(0, 5) == 'modal') {
271 271 if (selector == 'modal') {
272 272 return this.$window;
273 273 } else {
274 274 return this.$window.find(selector.substring(6));
275 275 }
276 276 } else {
277 277 return PopupView.__super__._get_selector_element.apply(this, [selector]);
278 278 }
279 279 },
280 280 });
281 281 WidgetManager.register_widget_view('PopupView', PopupView);
282 282 });
General Comments 0
You need to be logged in to leave comments. Login now