##// END OF EJS Templates
Fixed modal centering code
Jonathan Frederic -
Show More
@@ -1,192 +1,194
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/widget"], function(widget_manager) {
17 define(["notebook/js/widget"], function(widget_manager) {
18
18
19 var set_flex_property = function(element, property_name, enabled) {
19 var set_flex_property = function(element, property_name, enabled) {
20 if (enabled) {
20 if (enabled) {
21 element.addClass(property_name);
21 element.addClass(property_name);
22 } else {
22 } else {
23 element.removeClass(property_name);
23 element.removeClass(property_name);
24 }
24 }
25 };
25 };
26
26
27 var set_flex_properties = function(context, element) {
27 var set_flex_properties = function(context, element) {
28
28
29 // Apply flexible box model properties by adding and removing
29 // Apply flexible box model properties by adding and removing
30 // corrosponding CSS classes.
30 // corrosponding CSS classes.
31 // Defined in IPython/html/static/base/less/flexbox.less
31 // Defined in IPython/html/static/base/less/flexbox.less
32 set_flex_property(element, 'vbox', context.model.get('_vbox'));
32 set_flex_property(element, 'vbox', context.model.get('_vbox'));
33 set_flex_property(element, 'hbox', context.model.get('_hbox'));
33 set_flex_property(element, 'hbox', context.model.get('_hbox'));
34 set_flex_property(element, 'start', context.model.get('_pack_start'));
34 set_flex_property(element, 'start', context.model.get('_pack_start'));
35 set_flex_property(element, 'center', context.model.get('_pack_center'));
35 set_flex_property(element, 'center', context.model.get('_pack_center'));
36 set_flex_property(element, 'end', context.model.get('_pack_end'));
36 set_flex_property(element, 'end', context.model.get('_pack_end'));
37 set_flex_property(element, 'align-start', context.model.get('_align_start'));
37 set_flex_property(element, 'align-start', context.model.get('_align_start'));
38 set_flex_property(element, 'align-center', context.model.get('_align_center'));
38 set_flex_property(element, 'align-center', context.model.get('_align_center'));
39 set_flex_property(element, 'align-end', context.model.get('_align_end'));
39 set_flex_property(element, 'align-end', context.model.get('_align_end'));
40 set_flex_property(element, 'box-flex0', context.model.get('_flex0'));
40 set_flex_property(element, 'box-flex0', context.model.get('_flex0'));
41 set_flex_property(element, 'box-flex1', context.model.get('_flex1'));
41 set_flex_property(element, 'box-flex1', context.model.get('_flex1'));
42 set_flex_property(element, 'box-flex2', context.model.get('_flex2'));
42 set_flex_property(element, 'box-flex2', context.model.get('_flex2'));
43 };
43 };
44
44
45
45
46
46
47 var ContainerModel = IPython.WidgetModel.extend({});
47 var ContainerModel = IPython.WidgetModel.extend({});
48 widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel);
48 widget_manager.register_widget_model('ContainerWidgetModel', ContainerModel);
49
49
50 var ContainerView = IPython.WidgetView.extend({
50 var ContainerView = IPython.WidgetView.extend({
51
51
52 render: function(){
52 render: function(){
53 this.$el
53 this.$el
54 .addClass('widget-container');
54 .addClass('widget-container');
55 },
55 },
56
56
57 update: function(){
57 update: function(){
58 set_flex_properties(this, this.$el);
58 set_flex_properties(this, this.$el);
59 return IPython.WidgetView.prototype.update.call(this);
59 return IPython.WidgetView.prototype.update.call(this);
60 },
60 },
61
61
62 display_child: function(view) {
62 display_child: function(view) {
63 this.$el.append(view.$el);
63 this.$el.append(view.$el);
64 },
64 },
65 });
65 });
66
66
67 widget_manager.register_widget_view('ContainerView', ContainerView);
67 widget_manager.register_widget_view('ContainerView', ContainerView);
68
68
69
69
70 var ModalView = IPython.WidgetView.extend({
70 var ModalView = IPython.WidgetView.extend({
71
71
72 render: function(){
72 render: function(){
73 var that = this;
73 var that = this;
74 this.$el
74 this.$el
75 .html('')
75 .html('')
76 .on("remove", function(){
76 .on("remove", function(){
77 that.$window.remove();
77 that.$window.remove();
78 });
78 });
79 this.$window = $('<div />')
79 this.$window = $('<div />')
80 .addClass('modal widget-modal')
80 .addClass('modal widget-modal')
81 .appendTo($('#notebook-container'));
81 .appendTo($('#notebook-container'));
82 this.$title_bar = $('<div />')
82 this.$title_bar = $('<div />')
83 .addClass('popover-title')
83 .addClass('popover-title')
84 .appendTo(this.$window);
84 .appendTo(this.$window);
85 var that = this;
85 var that = this;
86 $('<button />')
86 $('<button />')
87 .addClass('close')
87 .addClass('close')
88 .html('&times;')
88 .html('&times;')
89 .appendTo(this.$title_bar)
89 .appendTo(this.$title_bar)
90 .click(function(){
90 .click(function(){
91 that.hide();
91 that.hide();
92 event.stopPropagation();
92 event.stopPropagation();
93 });
93 });
94 this.$title = $('<div />')
94 this.$title = $('<div />')
95 .addClass('widget-modal-title')
95 .addClass('widget-modal-title')
96 .html('&nbsp;')
96 .html('&nbsp;')
97 .appendTo(this.$title_bar);
97 .appendTo(this.$title_bar);
98 this.$body = $('<div />')
98 this.$body = $('<div />')
99 .addClass('modal-body')
99 .addClass('modal-body')
100 .addClass('widget-modal-body')
100 .addClass('widget-modal-body')
101 .addClass('widget-container')
101 .addClass('widget-container')
102 .appendTo(this.$window);
102 .appendTo(this.$window);
103
103
104 this.$show_button = $('<button />')
104 this.$show_button = $('<button />')
105 .html('&nbsp;')
105 .html('&nbsp;')
106 .addClass('btn btn-info widget-modal-show')
106 .addClass('btn btn-info widget-modal-show')
107 .appendTo(this.$el)
107 .appendTo(this.$el)
108 .click(function(){
108 .click(function(){
109 that.show();
109 that.show();
110 });
110 });
111
111
112 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
112 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
113 this.$window.resizable();
113 this.$window.resizable();
114 this.$window.on('resize', function(){
114 this.$window.on('resize', function(){
115 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
115 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
116 })
116 })
117
117
118 this.$el_to_style = this.$body;
118 this.$el_to_style = this.$body;
119 this._shown_once = false;
119 this._shown_once = false;
120 },
120 },
121
121
122 hide: function() {
122 hide: function() {
123 this.$window.hide();
123 this.$window.hide();
124 this.$show_button.removeClass('btn-info');
124 this.$show_button.removeClass('btn-info');
125 },
125 },
126
126
127 show: function() {
127 show: function() {
128 this.$window.show();
129 this.$show_button.addClass('btn-info');
128 this.$show_button.addClass('btn-info');
130 this.$window.css("top", Math.max(0, (($(window).height() - this.$window.outerHeight()) / 2) +
129
131 $(window).scrollTop()) + "px");
130 this.$window.show();
132 this.$window.css("left", Math.max(0, (($(window).width() - this.$window.outerWidth()) / 2) +
131 this.$window.css("positon", "absolute")
133 $(window).scrollLeft()) + "px");
132 this.$window.css("top", Math.max(0, (($('body').outerHeight() - this.$window.outerHeight()) / 2) +
133 $(window).scrollTop()) + "px");
134 this.$window.css("left", Math.max(0, (($('body').outerWidth() - this.$window.outerWidth()) / 2) +
135 $(window).scrollLeft()) + "px");
134 },
136 },
135
137
136 update: function(){
138 update: function(){
137 set_flex_properties(this, this.$body);
139 set_flex_properties(this, this.$body);
138
140
139 var description = this.model.get('description');
141 var description = this.model.get('description');
140 description = description.replace(/ /g, '&nbsp;', 'm');
142 description = description.replace(/ /g, '&nbsp;', 'm');
141 description = description.replace(/\n/g, '<br>\n', 'm');
143 description = description.replace(/\n/g, '<br>\n', 'm');
142 if (description.length == 0) {
144 if (description.length == 0) {
143 this.$title.html('&nbsp;'); // Preserve title height
145 this.$title.html('&nbsp;'); // Preserve title height
144 } else {
146 } else {
145 this.$title.html(description);
147 this.$title.html(description);
146 }
148 }
147
149
148 var button_text = this.model.get('button_text');
150 var button_text = this.model.get('button_text');
149 button_text = button_text.replace(/ /g, '&nbsp;', 'm');
151 button_text = button_text.replace(/ /g, '&nbsp;', 'm');
150 button_text = button_text.replace(/\n/g, '<br>\n', 'm');
152 button_text = button_text.replace(/\n/g, '<br>\n', 'm');
151 if (button_text.length == 0) {
153 if (button_text.length == 0) {
152 this.$show_button.html('&nbsp;'); // Preserve button height
154 this.$show_button.html('&nbsp;'); // Preserve button height
153 } else {
155 } else {
154 this.$show_button.html(button_text);
156 this.$show_button.html(button_text);
155 }
157 }
156
158
157 if (!this._shown_once) {
159 if (!this._shown_once) {
158 this._shown_once = true;
160 this._shown_once = true;
159 this.show();
161 this.show();
160 }
162 }
161
163
162 return IPython.WidgetView.prototype.update.call(this);
164 return IPython.WidgetView.prototype.update.call(this);
163 },
165 },
164
166
165 display_child: function(view) {
167 display_child: function(view) {
166 this.$body.append(view.$el);
168 this.$body.append(view.$el);
167 },
169 },
168
170
169 _get_selector_element: function(selector) {
171 _get_selector_element: function(selector) {
170
172
171 // Since the modal actually isn't within the $el in the DOM, we need to extend
173 // Since the modal actually isn't within the $el in the DOM, we need to extend
172 // the selector logic to allow the user to set css on the modal if need be.
174 // the selector logic to allow the user to set css on the modal if need be.
173 // The convention used is:
175 // The convention used is:
174 // "modal" - select the modal div
176 // "modal" - select the modal div
175 // "modal [selector]" - select element(s) within the modal div.
177 // "modal [selector]" - select element(s) within the modal div.
176 // "[selector]" - select elements within $el
178 // "[selector]" - select elements within $el
177 // "" - select the $el_to_style
179 // "" - select the $el_to_style
178 if (selector.substring(0, 5) == 'modal') {
180 if (selector.substring(0, 5) == 'modal') {
179 if (selector == 'modal') {
181 if (selector == 'modal') {
180 return this.$window;
182 return this.$window;
181 } else {
183 } else {
182 return this.$window.find(selector.substring(6));
184 return this.$window.find(selector.substring(6));
183 }
185 }
184 } else {
186 } else {
185 return IPython.WidgetView.prototype._get_selector_element.call(this, selector);
187 return IPython.WidgetView.prototype._get_selector_element.call(this, selector);
186 }
188 }
187 },
189 },
188
190
189 });
191 });
190
192
191 widget_manager.register_widget_view('ModalView', ModalView);
193 widget_manager.register_widget_view('ModalView', ModalView);
192 });
194 });
General Comments 0
You need to be logged in to leave comments. Login now