##// END OF EJS Templates
Merge pull request #5010 from jdfreder/widget-ff...
Thomas Kluyver -
r15020:12bac7bd merge
parent child Browse files
Show More
@@ -1,272 +1,274 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // ContainerWidget
9 // ContainerWidget
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 **/
15 **/
16
16
17 define(["notebook/js/widgets/widget"], function(WidgetManager) {
17 define(["notebook/js/widgets/widget"], function(WidgetManager) {
18
18
19 var ContainerView = IPython.DOMWidgetView.extend({
19 var ContainerView = IPython.DOMWidgetView.extend({
20 render: function(){
20 render: function(){
21 // Called when view is rendered.
21 // Called when view is rendered.
22 this.$el.addClass('widget-container');
22 this.$el.addClass('widget-container')
23 .addClass('vbox');
23 this.children={};
24 this.children={};
24 this.update_children([], this.model.get('_children'));
25 this.update_children([], this.model.get('_children'));
25 this.model.on('change:_children', function(model, value, options) {
26 this.model.on('change:_children', function(model, value, options) {
26 this.update_children(model.previous('_children'), value);
27 this.update_children(model.previous('_children'), value);
27 }, this);
28 }, this);
28 this.update();
29 this.update();
29 },
30 },
30
31
31 update_children: function(old_list, new_list) {
32 update_children: function(old_list, new_list) {
32 // Called when the children list changes.
33 // Called when the children list changes.
33 this.do_diff(old_list,
34 this.do_diff(old_list,
34 new_list,
35 new_list,
35 $.proxy(this.remove_child_model, this),
36 $.proxy(this.remove_child_model, this),
36 $.proxy(this.add_child_model, this));
37 $.proxy(this.add_child_model, this));
37 },
38 },
38
39
39 remove_child_model: function(model) {
40 remove_child_model: function(model) {
40 // Called when a model is removed from the children list.
41 // Called when a model is removed from the children list.
41 this.child_views[model.id].remove();
42 this.child_views[model.id].remove();
42 this.delete_child_view(model);
43 this.delete_child_view(model);
43 },
44 },
44
45
45 add_child_model: function(model) {
46 add_child_model: function(model) {
46 // Called when a model is added to the children list.
47 // Called when a model is added to the children list.
47 var view = this.create_child_view(model);
48 var view = this.create_child_view(model);
48 this.$el.append(view.$el);
49 this.$el.append(view.$el);
49 },
50 },
50
51
51 update: function(){
52 update: function(){
52 // Update the contents of this view
53 // Update the contents of this view
53 //
54 //
54 // Called when the model is changed. The model may have been
55 // Called when the model is changed. The model may have been
55 // changed by another view or by a state update from the back-end.
56 // changed by another view or by a state update from the back-end.
56 return ContainerView.__super__.update.apply(this);
57 return ContainerView.__super__.update.apply(this);
57 },
58 },
58 });
59 });
59
60
60 WidgetManager.register_widget_view('ContainerView', ContainerView);
61 WidgetManager.register_widget_view('ContainerView', ContainerView);
61
62
62 var PopupView = IPython.DOMWidgetView.extend({
63 var PopupView = IPython.DOMWidgetView.extend({
63 render: function(){
64 render: function(){
64 // Called when view is rendered.
65 // Called when view is rendered.
65 var that = this;
66 var that = this;
66 this.children={};
67 this.children={};
67
68
68 this.$el.on("remove", function(){
69 this.$el.on("remove", function(){
69 that.$window.remove();
70 that.$window.remove();
70 });
71 });
71 this.$window = $('<div />')
72 this.$window = $('<div />')
72 .addClass('modal widget-modal')
73 .addClass('modal widget-modal')
73 .appendTo($('#notebook-container'))
74 .appendTo($('#notebook-container'))
74 .mousedown(function(){
75 .mousedown(function(){
75 that.bring_to_front();
76 that.bring_to_front();
76 });
77 });
77 this.$title_bar = $('<div />')
78 this.$title_bar = $('<div />')
78 .addClass('popover-title')
79 .addClass('popover-title')
79 .appendTo(this.$window)
80 .appendTo(this.$window)
80 .mousedown(function(){
81 .mousedown(function(){
81 that.bring_to_front();
82 that.bring_to_front();
82 });
83 });
83 this.$close = $('<button />')
84 this.$close = $('<button />')
84 .addClass('close icon-remove')
85 .addClass('close icon-remove')
85 .css('margin-left', '5px')
86 .css('margin-left', '5px')
86 .appendTo(this.$title_bar)
87 .appendTo(this.$title_bar)
87 .click(function(){
88 .click(function(){
88 that.hide();
89 that.hide();
89 event.stopPropagation();
90 event.stopPropagation();
90 });
91 });
91 this.$minimize = $('<button />')
92 this.$minimize = $('<button />')
92 .addClass('close icon-arrow-down')
93 .addClass('close icon-arrow-down')
93 .appendTo(this.$title_bar)
94 .appendTo(this.$title_bar)
94 .click(function(){
95 .click(function(){
95 that.popped_out = !that.popped_out;
96 that.popped_out = !that.popped_out;
96 if (!that.popped_out) {
97 if (!that.popped_out) {
97 that.$minimize
98 that.$minimize
98 .removeClass('icon-arrow-down')
99 .removeClass('icon-arrow-down')
99 .addClass('icon-arrow-up');
100 .addClass('icon-arrow-up');
100
101
101 that.$window
102 that.$window
102 .draggable('destroy')
103 .draggable('destroy')
103 .resizable('destroy')
104 .resizable('destroy')
104 .removeClass('widget-modal modal')
105 .removeClass('widget-modal modal')
105 .addClass('docked-widget-modal')
106 .addClass('docked-widget-modal')
106 .detach()
107 .detach()
107 .insertBefore(that.$show_button);
108 .insertBefore(that.$show_button);
108 that.$show_button.hide();
109 that.$show_button.hide();
109 that.$close.hide();
110 that.$close.hide();
110 } else {
111 } else {
111 that.$minimize
112 that.$minimize
112 .addClass('icon-arrow-down')
113 .addClass('icon-arrow-down')
113 .removeClass('icon-arrow-up');
114 .removeClass('icon-arrow-up');
114
115
115 that.$window
116 that.$window
116 .removeClass('docked-widget-modal')
117 .removeClass('docked-widget-modal')
117 .addClass('widget-modal modal')
118 .addClass('widget-modal modal')
118 .detach()
119 .detach()
119 .appendTo($('#notebook-container'))
120 .appendTo($('#notebook-container'))
120 .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
121 .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
121 .resizable()
122 .resizable()
122 .children('.ui-resizable-handle').show();
123 .children('.ui-resizable-handle').show();
123 that.show();
124 that.show();
124 that.$show_button.show();
125 that.$show_button.show();
125 that.$close.show();
126 that.$close.show();
126 }
127 }
127 event.stopPropagation();
128 event.stopPropagation();
128 });
129 });
129 this.$title = $('<div />')
130 this.$title = $('<div />')
130 .addClass('widget-modal-title')
131 .addClass('widget-modal-title')
131 .text(' ')
132 .text(' ')
132 .appendTo(this.$title_bar);
133 .appendTo(this.$title_bar);
133 this.$body = $('<div />')
134 this.$body = $('<div />')
134 .addClass('modal-body')
135 .addClass('modal-body')
135 .addClass('widget-modal-body')
136 .addClass('widget-modal-body')
136 .addClass('widget-container')
137 .addClass('widget-container')
138 .addClass('vbox')
137 .appendTo(this.$window);
139 .appendTo(this.$window);
138
140
139 this.$show_button = $('<button />')
141 this.$show_button = $('<button />')
140 .text(' ')
142 .text(' ')
141 .addClass('btn btn-info widget-modal-show')
143 .addClass('btn btn-info widget-modal-show')
142 .appendTo(this.$el)
144 .appendTo(this.$el)
143 .click(function(){
145 .click(function(){
144 that.show();
146 that.show();
145 });
147 });
146
148
147 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
149 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
148 this.$window.resizable();
150 this.$window.resizable();
149 this.$window.on('resize', function(){
151 this.$window.on('resize', function(){
150 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
152 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
151 });
153 });
152
154
153 this.$el_to_style = this.$body;
155 this.$el_to_style = this.$body;
154 this._shown_once = false;
156 this._shown_once = false;
155 this.popped_out = true;
157 this.popped_out = true;
156
158
157 this.update_children([], this.model.get('_children'));
159 this.update_children([], this.model.get('_children'));
158 this.model.on('change:_children', function(model, value, options) {
160 this.model.on('change:_children', function(model, value, options) {
159 this.update_children(model.previous('_children'), value);
161 this.update_children(model.previous('_children'), value);
160 }, this);
162 }, this);
161 this.update();
163 this.update();
162 },
164 },
163
165
164 hide: function() {
166 hide: function() {
165 // Called when the modal hide button is clicked.
167 // Called when the modal hide button is clicked.
166 this.$window.hide();
168 this.$window.hide();
167 this.$show_button.removeClass('btn-info');
169 this.$show_button.removeClass('btn-info');
168 },
170 },
169
171
170 show: function() {
172 show: function() {
171 // Called when the modal show button is clicked.
173 // Called when the modal show button is clicked.
172 this.$show_button.addClass('btn-info');
174 this.$show_button.addClass('btn-info');
173 this.$window.show();
175 this.$window.show();
174 if (this.popped_out) {
176 if (this.popped_out) {
175 this.$window.css("positon", "absolute");
177 this.$window.css("positon", "absolute");
176 this.$window.css("top", "0px");
178 this.$window.css("top", "0px");
177 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
179 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
178 $(window).scrollLeft()) + "px");
180 $(window).scrollLeft()) + "px");
179 this.bring_to_front();
181 this.bring_to_front();
180 }
182 }
181 },
183 },
182
184
183 bring_to_front: function() {
185 bring_to_front: function() {
184 // Make the modal top-most, z-ordered about the other modals.
186 // Make the modal top-most, z-ordered about the other modals.
185 var $widget_modals = $(".widget-modal");
187 var $widget_modals = $(".widget-modal");
186 var max_zindex = 0;
188 var max_zindex = 0;
187 $widget_modals.each(function (index, el){
189 $widget_modals.each(function (index, el){
188 max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
190 max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
189 });
191 });
190
192
191 // Start z-index of widget modals at 2000
193 // Start z-index of widget modals at 2000
192 max_zindex = Math.max(max_zindex, 2000);
194 max_zindex = Math.max(max_zindex, 2000);
193
195
194 $widget_modals.each(function (index, el){
196 $widget_modals.each(function (index, el){
195 $el = $(el);
197 $el = $(el);
196 if (max_zindex == parseInt($el.css('z-index'))) {
198 if (max_zindex == parseInt($el.css('z-index'))) {
197 $el.css('z-index', max_zindex - 1);
199 $el.css('z-index', max_zindex - 1);
198 }
200 }
199 });
201 });
200 this.$window.css('z-index', max_zindex);
202 this.$window.css('z-index', max_zindex);
201 },
203 },
202
204
203 update_children: function(old_list, new_list) {
205 update_children: function(old_list, new_list) {
204 // Called when the children list is modified.
206 // Called when the children list is modified.
205 this.do_diff(old_list,
207 this.do_diff(old_list,
206 new_list,
208 new_list,
207 $.proxy(this.remove_child_model, this),
209 $.proxy(this.remove_child_model, this),
208 $.proxy(this.add_child_model, this));
210 $.proxy(this.add_child_model, this));
209 },
211 },
210
212
211 remove_child_model: function(model) {
213 remove_child_model: function(model) {
212 // Called when a child is removed from children list.
214 // Called when a child is removed from children list.
213 this.child_views[model.id].remove();
215 this.child_views[model.id].remove();
214 this.delete_child_view(model);
216 this.delete_child_view(model);
215 },
217 },
216
218
217 add_child_model: function(model) {
219 add_child_model: function(model) {
218 // Called when a child is added to children list.
220 // Called when a child is added to children list.
219 var view = this.create_child_view(model);
221 var view = this.create_child_view(model);
220 this.$body.append(view.$el);
222 this.$body.append(view.$el);
221 },
223 },
222
224
223 update: function(){
225 update: function(){
224 // Update the contents of this view
226 // Update the contents of this view
225 //
227 //
226 // Called when the model is changed. The model may have been
228 // Called when the model is changed. The model may have been
227 // changed by another view or by a state update from the back-end.
229 // changed by another view or by a state update from the back-end.
228 var description = this.model.get('description');
230 var description = this.model.get('description');
229 if (description.length === 0) {
231 if (description.length === 0) {
230 this.$title.text(' '); // Preserve title height
232 this.$title.text(' '); // Preserve title height
231 } else {
233 } else {
232 this.$title.text(description);
234 this.$title.text(description);
233 }
235 }
234
236
235 var button_text = this.model.get('button_text');
237 var button_text = this.model.get('button_text');
236 if (button_text.length === 0) {
238 if (button_text.length === 0) {
237 this.$show_button.text(' '); // Preserve button height
239 this.$show_button.text(' '); // Preserve button height
238 } else {
240 } else {
239 this.$show_button.text(button_text);
241 this.$show_button.text(button_text);
240 }
242 }
241
243
242 if (!this._shown_once) {
244 if (!this._shown_once) {
243 this._shown_once = true;
245 this._shown_once = true;
244 this.show();
246 this.show();
245 }
247 }
246
248
247 return PopupView.__super__.update.apply(this);
249 return PopupView.__super__.update.apply(this);
248 },
250 },
249
251
250 _get_selector_element: function(selector) {
252 _get_selector_element: function(selector) {
251 // Get an element view a 'special' jquery selector. (see widget.js)
253 // Get an element view a 'special' jquery selector. (see widget.js)
252 //
254 //
253 // Since the modal actually isn't within the $el in the DOM, we need to extend
255 // Since the modal actually isn't within the $el in the DOM, we need to extend
254 // the selector logic to allow the user to set css on the modal if need be.
256 // the selector logic to allow the user to set css on the modal if need be.
255 // The convention used is:
257 // The convention used is:
256 // "modal" - select the modal div
258 // "modal" - select the modal div
257 // "modal [selector]" - select element(s) within the modal div.
259 // "modal [selector]" - select element(s) within the modal div.
258 // "[selector]" - select elements within $el
260 // "[selector]" - select elements within $el
259 // "" - select the $el_to_style
261 // "" - select the $el_to_style
260 if (selector.substring(0, 5) == 'modal') {
262 if (selector.substring(0, 5) == 'modal') {
261 if (selector == 'modal') {
263 if (selector == 'modal') {
262 return this.$window;
264 return this.$window;
263 } else {
265 } else {
264 return this.$window.find(selector.substring(6));
266 return this.$window.find(selector.substring(6));
265 }
267 }
266 } else {
268 } else {
267 return PopupView.__super__._get_selector_element.apply(this, [selector]);
269 return PopupView.__super__._get_selector_element.apply(this, [selector]);
268 }
270 }
269 },
271 },
270 });
272 });
271 WidgetManager.register_widget_view('PopupView', PopupView);
273 WidgetManager.register_widget_view('PopupView', PopupView);
272 });
274 });
General Comments 0
You need to be logged in to leave comments. Login now