##// END OF EJS Templates
Fixed modal centering code
Jonathan Frederic -
Show More
@@ -1,192 +1,194
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 81 .appendTo($('#notebook-container'));
82 82 this.$title_bar = $('<div />')
83 83 .addClass('popover-title')
84 84 .appendTo(this.$window);
85 85 var that = this;
86 86 $('<button />')
87 87 .addClass('close')
88 88 .html('&times;')
89 89 .appendTo(this.$title_bar)
90 90 .click(function(){
91 91 that.hide();
92 92 event.stopPropagation();
93 93 });
94 94 this.$title = $('<div />')
95 95 .addClass('widget-modal-title')
96 96 .html('&nbsp;')
97 97 .appendTo(this.$title_bar);
98 98 this.$body = $('<div />')
99 99 .addClass('modal-body')
100 100 .addClass('widget-modal-body')
101 101 .addClass('widget-container')
102 102 .appendTo(this.$window);
103 103
104 104 this.$show_button = $('<button />')
105 105 .html('&nbsp;')
106 106 .addClass('btn btn-info widget-modal-show')
107 107 .appendTo(this.$el)
108 108 .click(function(){
109 109 that.show();
110 110 });
111 111
112 112 this.$window.draggable({handle: '.popover-title', snap: '#notebook, .modal', snapMode: 'both'});
113 113 this.$window.resizable();
114 114 this.$window.on('resize', function(){
115 115 that.$body.outerHeight(that.$window.innerHeight() - that.$title_bar.outerHeight());
116 116 })
117 117
118 118 this.$el_to_style = this.$body;
119 119 this._shown_once = false;
120 120 },
121 121
122 122 hide: function() {
123 123 this.$window.hide();
124 124 this.$show_button.removeClass('btn-info');
125 125 },
126 126
127 127 show: function() {
128 this.$window.show();
129 128 this.$show_button.addClass('btn-info');
130 this.$window.css("top", Math.max(0, (($(window).height() - this.$window.outerHeight()) / 2) +
131 $(window).scrollTop()) + "px");
132 this.$window.css("left", Math.max(0, (($(window).width() - this.$window.outerWidth()) / 2) +
133 $(window).scrollLeft()) + "px");
129
130 this.$window.show();
131 this.$window.css("positon", "absolute")
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 138 update: function(){
137 139 set_flex_properties(this, this.$body);
138 140
139 141 var description = this.model.get('description');
140 142 description = description.replace(/ /g, '&nbsp;', 'm');
141 143 description = description.replace(/\n/g, '<br>\n', 'm');
142 144 if (description.length == 0) {
143 145 this.$title.html('&nbsp;'); // Preserve title height
144 146 } else {
145 147 this.$title.html(description);
146 148 }
147 149
148 150 var button_text = this.model.get('button_text');
149 151 button_text = button_text.replace(/ /g, '&nbsp;', 'm');
150 152 button_text = button_text.replace(/\n/g, '<br>\n', 'm');
151 153 if (button_text.length == 0) {
152 154 this.$show_button.html('&nbsp;'); // Preserve button height
153 155 } else {
154 156 this.$show_button.html(button_text);
155 157 }
156 158
157 159 if (!this._shown_once) {
158 160 this._shown_once = true;
159 161 this.show();
160 162 }
161 163
162 164 return IPython.WidgetView.prototype.update.call(this);
163 165 },
164 166
165 167 display_child: function(view) {
166 168 this.$body.append(view.$el);
167 169 },
168 170
169 171 _get_selector_element: function(selector) {
170 172
171 173 // Since the modal actually isn't within the $el in the DOM, we need to extend
172 174 // the selector logic to allow the user to set css on the modal if need be.
173 175 // The convention used is:
174 176 // "modal" - select the modal div
175 177 // "modal [selector]" - select element(s) within the modal div.
176 178 // "[selector]" - select elements within $el
177 179 // "" - select the $el_to_style
178 180 if (selector.substring(0, 5) == 'modal') {
179 181 if (selector == 'modal') {
180 182 return this.$window;
181 183 } else {
182 184 return this.$window.find(selector.substring(6));
183 185 }
184 186 } else {
185 187 return IPython.WidgetView.prototype._get_selector_element.call(this, selector);
186 188 }
187 189 },
188 190
189 191 });
190 192
191 193 widget_manager.register_widget_view('ModalView', ModalView);
192 194 });
General Comments 0
You need to be logged in to leave comments. Login now