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