Skip to content

Commit 37eed39

Browse files
committed
Improvements for modal positioning.
This is a work in progress snapshot. Modal height changes caused by the delayed load of images are handled slightly better now. This could be further improved by adding debouncing.
1 parent d446a99 commit 37eed39

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/pat/modal.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ define([
22
"jquery",
33
"../core/parser",
44
"../registry",
5+
"../utils",
56
"./inject"
6-
], function($, Parser, registry, inject) {
7+
], function($, Parser, registry, utils, inject) {
78
var parser = new Parser("modal");
89

910
parser.add_argument("class");
@@ -66,17 +67,14 @@ define([
6667
},
6768

6869
setPosition: function() {
69-
// bail if repositioning already in progress
70-
if ($('#pat-modal-clone').length > 0) {
70+
var $el = $('div.pat-modal,#pat-modal');
71+
if ($el.length === 0) {
7172
return;
7273
}
7374

74-
var $el = $('div.pat-modal,#pat-modal'),
75-
maxHeight = $(window).innerHeight() - $el.outerHeight(true) +
76-
$el.outerHeight();
77-
78-
if ($el.length === 0) {
79-
return;
75+
var $oldClone = $('#pat-modal-clone');
76+
if ($oldClone.length > 0) {
77+
$oldClone.remove();
8078
}
8179

8280
var $clone = $el.clone();
@@ -86,35 +84,30 @@ define([
8684
.css({
8785
'visibility': 'hidden',
8886
'position': 'absolute',
89-
'height': '',
90-
'max-height': maxHeight
87+
'height': ''
9188
}).appendTo('body');
9289

9390
// wait for browser to update DOM
9491
setTimeout(modal.measure, 0);
9592
},
9693

9794
measure: function() {
95+
var $clone = $('#pat-modal-clone');
96+
if ($clone.length === 0) {
97+
return;
98+
}
99+
98100
var $el = $('div.pat-modal,#pat-modal'),
99-
$clone = $('#pat-modal-clone'),
100101
maxHeight = $(window).innerHeight() - $clone.outerHeight(true) +
101102
$clone.outerHeight(),
102103
height = $clone.outerHeight();
103104

104105
$clone.remove();
105106

106107
if (maxHeight - height < 0) {
107-
$el.addClass('max-height')
108-
.css({
109-
'height': maxHeight,
110-
'max-height': ''
111-
});
108+
$el.addClass('max-height').css('height', maxHeight);
112109
} else {
113-
$el.removeClass('max-height')
114-
.css({
115-
'height': '',
116-
'max-height': maxHeight
117-
});
110+
$el.removeClass('max-height').css('height', '');
118111
}
119112

120113
var top = ($(window).innerHeight() - $el.outerHeight(true)) / 2;
@@ -131,7 +124,7 @@ define([
131124

132125
$(window).on('resize.pat-modal-position', modal.setPosition);
133126
$(window).on('pat-inject-content-loaded.pat-modal-position', '#pat-modal',
134-
modal.setPosition);
127+
modal.setPosition);
135128
$(document).on('patterns-injected.pat-modal-position', '#pat-modal,div.pat-modal',
136129
modal.setPosition);
137130

0 commit comments

Comments
 (0)