##// END OF EJS Templates
- ModalView can now be docked and undocked...
Jonathan Frederic -
Show More
@@ -1,193 +1,259
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/widget"], function(widget_manager) {
18 18
19 19 var set_flex_property = function(element, property_name, enabled) {
20 20 if (enabled) {
21 21 element.addClass(property_name);
22 22 } else {
23 23 element.removeClass(property_name);
24 24 }
25 25 };
26 26
27 27 var set_flex_properties = function(context, element) {
28 28
29 29 // Apply flexible box model properties by adding and removing
30 30 // corrosponding CSS classes.
31 31 // Defined in IPython/html/static/base/less/flexbox.less
32 32 set_flex_property(element, 'vbox', context.model.get('_vbox'));
33 33 set_flex_property(element, 'hbox', context.model.get('_hbox'));
34 34 set_flex_property(element, 'start', context.model.get('_pack_start'));
35 35 set_flex_property(element, 'center', context.model.get('_pack_center'));
36 36 set_flex_property(element, 'end', context.model.get('_pack_end'));
37 37 set_flex_property(element, 'align-start', context.model.get('_align_start'));
38 38 set_flex_property(element, 'align-center', context.model.get('_align_center'));
39 39 set_flex_property(element, 'align-end', context.model.get('_align_end'));
40 40 set_flex_property(element, 'box-flex0', context.model.get('_flex0'));
41 41 set_flex_property(element, 'box-flex1', context.model.get('_flex1'));
42 42 set_flex_property(element, 'box-flex2', context.model.get('_flex2'));
43 43 };
44 44
45 45
46 46
47 47 var ContainerModel = IPython.WidgetModel.extend({});
48 48 widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel);
49 49
50 50 var ContainerView = IPython.WidgetView.extend({
51 51
52 52 render: function(){
53 53 this.$el
54 54 .addClass('widget-container');
55 55 },
56 56
57 57 update: function(){
58 58 set_flex_properties(this, this.$el);
59 59 return IPython.WidgetView.prototype.update.call(this);
60 60 },
61 61
62 62 display_child: function(view) {
63 63 this.$el.append(view.$el);
64 64 },
65 65 });
66 66
67 67 widget_manager.register_widget_view('ContainerView', ContainerView);
68 68
69 69
70 70 var ModalView = IPython.WidgetView.extend({
71 71
72 72 render: function(){
73 73 var that = this;
74 74 this.$el
75 75 .html('')
76 76 .on("remove", function(){
77 77 that.$window.remove();
78 78 });
79 79 this.$window = $('<div />')
80 80 .addClass('modal widget-modal')
81 .appendTo($('#notebook-container'));
81 .appendTo($('#notebook-container'))
82 .mousedown(function(){
83 that.bring_to_front();
84 });
82 85 this.$title_bar = $('<div />')
83 86 .addClass('popover-title')
84 .appendTo(this.$window);
85 var that = this;
86 $('<button />')
87 .addClass('close')
88 .html('&times;')
87 .appendTo(this.$window)
88 .mousedown(function(){
89 that.bring_to_front();
90 });;
91 this.$close = $('<button />')
92 .addClass('close icon-remove')
93 .css('margin-left', '5px')
89 94 .appendTo(this.$title_bar)
90 95 .click(function(){
91 96 that.hide();
92 97 event.stopPropagation();
93 98 });
99 this.$minimize = $('<button />')
100 .addClass('close icon-arrow-down')
101 .appendTo(this.$title_bar)
102 .click(function(){
103 that.popped_out = !that.popped_out;
104 if (!that.popped_out) {
105 that.$minimize
106 .removeClass('icon-arrow-down')
107 .addClass('icon-arrow-up');
108
109 that.$window
110 .draggable('destroy')
111 .resizable('destroy')
112 .removeClass('widget-modal modal')
113 .addClass('docked-widget-modal')
114 .detach()
115 .insertBefore(that.$show_button);
116 that.$show_button.hide();
117 that.$close.hide();
118 } else {
119 that.$minimize
120 .addClass('icon-arrow-down')
121 .removeClass('icon-arrow-up');
122
123 that.$window
124 .removeClass('docked-widget-modal')
125 .addClass('widget-modal modal')
126 .detach()
127 .appendTo($('#notebook-container'))
128 .draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'})
129 .resizable()
130 .children('.ui-resizable-handle').show();
131 that.show();
132 that.$show_button.show();
133 that.$close.show();
134 }
135 event.stopPropagation();
136 });
94 137 this.$title = $('<div />')
95 138 .addClass('widget-modal-title')
96 139 .html('&nbsp;')
97 140 .appendTo(this.$title_bar);
98 141 this.$body = $('<div />')
99 142 .addClass('modal-body')
100 143 .addClass('widget-modal-body')
101 144 .addClass('widget-container')
102 145 .appendTo(this.$window);
103 146
104 147 this.$show_button = $('<button />')
105 148 .html('&nbsp;')
106 149 .addClass('btn btn-info widget-modal-show')
107 150 .appendTo(this.$el)
108 151 .click(function(){
109 152 that.show();
110 153 });
111 154
112 155 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
113 156 this.$window.resizable();
114 157 this.$window.on('resize', function(){
115 158 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
116 159 })
117 160
118 161 this.$el_to_style = this.$body;
119 162 this._shown_once = false;
163 this.popped_out = true;
120 164 },
121 165
122 166 hide: function() {
123 167 this.$window.hide();
124 168 this.$show_button.removeClass('btn-info');
125 169 },
126 170
127 171 show: function() {
128 172 this.$show_button.addClass('btn-info');
129 173
130 174 this.$window.show();
131 this.$window.css("positon", "absolute")
132 this.$window.css("top", "0px");
133 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
134 $(window).scrollLeft()) + "px");
175 if (this.popped_out) {
176 this.$window.css("positon", "absolute")
177 this.$window.css("top", "0px");
178 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
179 $(window).scrollLeft()) + "px");
180 this.bring_to_front();
181 }
182 },
183
184 bring_to_front: function() {
185 var $widget_modals = $(".widget-modal");
186 var max_zindex = 0;
187 $widget_modals.each(function (index, el){
188 max_zindex = Math.max(max_zindex, parseInt($(el).css('z-index')));
189 });
190
191 // Start z-index of widget modals at 2000
192 max_zindex = Math.max(max_zindex, 2000);
193
194 $widget_modals.each(function (index, el){
195 $el = $(el)
196 if (max_zindex == parseInt($el.css('z-index'))) {
197 $el.css('z-index', max_zindex - 1);
198 }
199 });
200 this.$window.css('z-index', max_zindex);
135 201 },
136 202
137 203 update: function(){
138 204 set_flex_properties(this, this.$body);
139 205
140 206 var description = this.model.get('description');
141 207 description = description.replace(/ /g, '&nbsp;', 'm');
142 208 description = description.replace(/\n/g, '<br>\n', 'm');
143 209 if (description.length == 0) {
144 210 this.$title.html('&nbsp;'); // Preserve title height
145 211 } else {
146 212 this.$title.html(description);
147 213 }
148 214
149 215 var button_text = this.model.get('button_text');
150 216 button_text = button_text.replace(/ /g, '&nbsp;', 'm');
151 217 button_text = button_text.replace(/\n/g, '<br>\n', 'm');
152 218 if (button_text.length == 0) {
153 219 this.$show_button.html('&nbsp;'); // Preserve button height
154 220 } else {
155 221 this.$show_button.html(button_text);
156 222 }
157 223
158 224 if (!this._shown_once) {
159 225 this._shown_once = true;
160 226 this.show();
161 227 }
162 228
163 229 return IPython.WidgetView.prototype.update.call(this);
164 230 },
165 231
166 232 display_child: function(view) {
167 233 this.$body.append(view.$el);
168 234 },
169 235
170 236 _get_selector_element: function(selector) {
171 237
172 238 // Since the modal actually isn't within the $el in the DOM, we need to extend
173 239 // the selector logic to allow the user to set css on the modal if need be.
174 240 // The convention used is:
175 241 // "modal" - select the modal div
176 242 // "modal [selector]" - select element(s) within the modal div.
177 243 // "[selector]" - select elements within $el
178 244 // "" - select the $el_to_style
179 245 if (selector.substring(0, 5) == 'modal') {
180 246 if (selector == 'modal') {
181 247 return this.$window;
182 248 } else {
183 249 return this.$window.find(selector.substring(6));
184 250 }
185 251 } else {
186 252 return IPython.WidgetView.prototype._get_selector_element.call(this, selector);
187 253 }
188 254 },
189 255
190 256 });
191 257
192 258 widget_manager.register_widget_view('ModalView', ModalView);
193 259 });
General Comments 0
You need to be logged in to leave comments. Login now