Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial draft of plugin to resize/center images in a modal #108

Merged
merged 30 commits into from
May 20, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8154f68
Initial commit for plugins (non-working)
anselmh Feb 3, 2014
cc81022
Add html5video plugin
anselmh Feb 4, 2014
d9ecacd
Add resizing (WIP) to modal (imgs)
anselmh Feb 10, 2014
f2f8379
Add markup
anselmh Feb 10, 2014
12484f6
Match resize MQ to CSS Mobile MQ
anselmh Mar 24, 2014
25e8980
Fix resizing function to work with AMD, etc.
anselmh Apr 15, 2014
0611f50
More refactoring of the resize plugin code
anselmh Apr 15, 2014
5963805
Major refactor of the resize function (WIP)
anselmh Apr 16, 2014
f03d801
More work on resizing module.
anselmh Apr 17, 2014
5a81fe5
Fix up resizing when window smaller than img
anselmh Apr 17, 2014
10c3701
Merge branch 'plugins' of github.com:drublic/css-modal into plugins
drublic Apr 22, 2014
c9c89ef
More fixes on resizing, add more demo content.
anselmh Apr 23, 2014
d5ed671
Force centered position and left property
anselmh Apr 23, 2014
8f98c8b
This fixes several RWD bugs in resizing
anselmh Apr 23, 2014
1764e46
Fix up resizing edge cases with diff. img formats.
anselmh Apr 23, 2014
c008968
Add new scss partial for resizing plugin.
anselmh Apr 23, 2014
7f984c9
Add dynamic Stylesheet func for close btn position
anselmh Apr 23, 2014
99f9e8a
Clean up branch.
anselmh Apr 23, 2014
6d25cf4
Merge branch 'master' into plugin-resize
drublic Apr 24, 2014
594bb89
Update code for modal resize with fixes
drublic Apr 24, 2014
c268552
Use better functions for style injection and window.matchMedia
drublic Apr 24, 2014
86e5f0d
Update centering and resize of modal - WIP
drublic Apr 24, 2014
ad3a101
Include image scaling for modal
drublic Apr 28, 2014
abe28fe
Move image file into img folder
drublic May 1, 2014
c669006
Only add resize effect to modals with data attr
drublic May 1, 2014
71e309e
Merge branch 'master' into plugin-resize
drublic May 19, 2014
aa4921c
Update styling for close button and resize image
drublic May 19, 2014
2e0522e
Incorporate resizing for smaller screens
drublic May 20, 2014
a3656c5
Update node module grunt-contrib-jasmine
drublic May 20, 2014
15ff54c
Update min node version to v0.10.0 when working with Travis
drublic May 20, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update centering and resize of modal - WIP
  • Loading branch information
drublic committed Apr 24, 2014
commit 86e5f0d481dc578520d3b18b05ee6c75f923305a
10 changes: 5 additions & 5 deletions _modal-core.scss
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ html {
top: 25px;
right: 50%;
z-index: 20;
margin-right: -(($modal-max-width / 2) - 40);
margin-right: -($modal-max-width / 2);
}
}

Expand All @@ -181,10 +181,10 @@ html {
}

.modal-close {
&:after {
margin-right: 0;
right: 60px;
}
&:after {
margin-right: 0;
right: 20px;
}
}
}

Expand Down
269 changes: 91 additions & 178 deletions plugins/modal-resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@
*/
var CSSModal;

/**
* Include styles into the DOM
* @param {string} rule Styles to inject into the DOM
*/
var _injectStyles = function (rule) {
var element = document.createElement('style');

element.innerHTML = '<style>' + rule + '</style>';

document.head.appendChild(element);
};

/*!
* matchMedia() polyfill - Test a CSS media type/query in JS.
* Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas, David Knight.
Expand Down Expand Up @@ -72,18 +60,36 @@
};
}());

/**
* Include styles into the DOM
* @param {string} rule Styles to inject into the DOM
* @param {string} id Unique ID for styles
*/
var _injectStyles = function (rule, id) {
var element = document.createElement('div');

id = 'modal__rule' + (id || '');

// Remove all current rules
if (document.getElementById(id)) {
document.getElementById(id).parentNode.removeChild(document.getElementById(id));
}

element.id = 'modal__rule' + id;
element.innerHTML = '<style>' + rule + '</style>';

// Append a new rule
document.body.appendChild(element);
};

/*
* Get natural dimensions of an image
* (can only handle 1 image)
* @param selector {QuerySelector} (required)
*/
var getNaturalImageDimensions = function (selector) {
if (!selector) {
throw new Error('getNaturalImageDimensions: Missing argument `selector`.');
var getNaturalImageDimensions = function (element) {
if (!element) {
return;
}

var element = CSSModal.activeElement.querySelector(selector);

// Polyfill IE 6/7/8
if (typeof element.naturalWidth === 'undefined') {
var image = new Image();
Expand All @@ -94,12 +100,23 @@
width: image.width,
height: image.height
};
} else {
return {
width: element.naturalWidth,
height: element.naturalHeight
};
}

return {
width: element.naturalWidth,
height: element.naturalHeight
};
};

var getDimentions = function (element) {
if (!element || element.length === 0) {
return;
}

return {
width: element.offsetWidth,
height: element.offsetHeight
};
};

/*
Expand All @@ -110,159 +127,24 @@
throw new Error('Error: No active modal.');
}

var windowHeight = window.innerHeight;
var windowWidth = window.innerWidth;
var margin = 20;
var element = CSSModal.activeElement.querySelector('.modal-inner');
var elementContent = CSSModal.activeElement.querySelector('.modal-content');

var $modalContent = CSSModal.activeElement.querySelector('.modal-content');
var $modalInner = CSSModal.activeElement.querySelector('.modal-inner');

var computedStyle = window.getComputedStyle($modalContent);
var imageDimentions = getNaturalImageDimensions(CSSModal.activeElement.querySelector('img'));
var containerDimentions = getDimentions(element);

var contentPadding = {
top: parseInt(computedStyle.getPropertyValue('padding-top'), 10),
left: parseInt(computedStyle.getPropertyValue('padding-left'), 10),
right: parseInt(computedStyle.getPropertyValue('padding-right'), 10),
bottom: parseInt(computedStyle.getPropertyValue('padding-bottom'), 10)
};
element.style.width = 'auto';
elementContent.style.maxHeight = 'none';

// Calculate commulated vertical and horizontal paddings
var contentPaddingHorizontal = contentPadding.left + contentPadding.right;
var contentPaddingVertical = contentPadding.top + contentPadding.bottom;
console.log(containerDimentions);

// Set img to max-height: 100%;
if ($modalContent.querySelector('img') === null) {
return;
if (containerDimentions.width > global.innerWidth) {
console.log('zu breit');
}

$modalContent.querySelector('img').style.maxHeight = '100%';

var naturalImageWidth = getNaturalImageDimensions('img').width;
var naturalImageHeight = getNaturalImageDimensions('img').height;
var ratioWH = (naturalImageWidth / naturalImageHeight);

var newMarginLeft;
var newWidth;
var newHeight;

if (windowWidth < windowHeight) { // If width smaller than height
newMarginLeft = 0;
newHeight = 0;

if (naturalImageWidth < windowWidth) {
newWidth = naturalImageWidth;
newHeight = ratioWH * newWidth;
} else {

// Portrait image
if (naturalImageHeight > naturalImageWidth) {
newHeight = windowHeight - (2 * margin) - contentPaddingVertical;

if (naturalImageHeight < newHeight) {
newHeight = naturalImageHeight;
}

newWidth = ratioWH * newHeight + margin;
}

// Landscape image
if (naturalImageHeight < naturalImageWidth) {
newWidth = windowWidth - (2 * margin) - contentPaddingHorizontal;
newHeight = 'auto';

if (naturalImageWidth < newWidth) {
newWidth = naturalImageWidth;
}
}
}

newMarginLeft = (newWidth / 2)*-1;

// Resize modal and images/video/iframe accordingly respecting aspect-ratio
if (newWidth) {
$modalInner.style.left = '50%'; // Force 50% to center
}

$modalInner.style.width = newWidth + 'px';
$modalContent.style.maxHeight = '100%';

if (newHeight === 'auto') {
$modalInner.style.height = newHeight;
} else {
$modalInner.style.height = newHeight + 'px';
}

$modalInner.style.marginLeft = newMarginLeft + 'px';

// If width wider than height
} else {
newHeight = (windowHeight - (2 * margin) + contentPaddingVertical);
newWidth = ratioWH * (newHeight - (2 * margin) - contentPaddingVertical);

// Reset height attribute (otherwise causes issues onresize)
$modalInner.style.height = '';

if ((naturalImageHeight < naturalImageWidth)) {
// Landscape image
if (naturalImageWidth < newWidth) {
newWidth = naturalImageWidth - (2 * margin) - contentPaddingHorizontal;
} else if (naturalImageHeight < newHeight) {
newHeight = naturalImageHeight - (2 * margin) - contentPaddingVertical;
}
}

// Portrait image
if (naturalImageHeight > naturalImageWidth) {
if ((newHeight + (2 * margin) + contentPaddingVertical) > windowHeight) {
newHeight = windowHeight - (2 * margin) + contentPaddingVertical;
newWidth = ratioWH * (newHeight - (2 * margin) - contentPaddingVertical);
}
}

// Don’t build bigger box than the image itself is
if (naturalImageWidth < newWidth) {
newWidth = naturalImageWidth;
} else if (naturalImageHeight < newHeight) {
newHeight = naturalImageHeight;
}

newMarginLeft = (newWidth / 2)*-1;

// Resize modal and images/video/iframe accordingly respecting aspect-ratio
$modalContent.style.maxHeight = '100%';
$modalInner.style.maxHeight = newHeight + 'px';

if (window.matchMedia('(min-width: 30em)').matches) {
$modalInner.style.left = '50%'; // Force 50% to center
}

if (windowWidth > newWidth) {
$modalInner.style.width = newWidth + 'px';
$modalInner.style.marginLeft = newMarginLeft + 'px';
} else {
newWidth = windowWidth - (2 * margin) - contentPaddingHorizontal;
newMarginLeft = (newWidth / 2)*-1;

$modalInner.style.width = newWidth + 'px';
$modalInner.style.marginLeft = '';

if (window.matchMedia('(min-width: 30em)').matches) {
$modalInner.style.marginLeft = newMarginLeft + 'px';
}
}
if (containerDimentions.height > global.innerHeight) {
console.log('zu hoch');
}

// Move close button to proper position
var closeButton = '.modal--fade .modal-close:after, .modal--plainscreen .modal-close:after, .modal--zoomin .modal-close:after, .modal--zoomout .modal-close:after, .modal--slidefromtop .modal-close:after, .modal--bouncefromtop .modal-close:after, .modal--bouncefromtopshaky .modal-close:after, .modal--show .modal-close:after, ._modal .modal-close:after';
var closeButtonMarginLeft = 0;
var closeButtonWidth = parseInt(window.getComputedStyle(CSSModal.activeElement.querySelector('.modal-close'), '::after').getPropertyValue('width'), 10);

closeButtonMarginLeft = Math.abs((newWidth / 2) - closeButtonWidth - contentPaddingHorizontal);

// Append unit
closeButtonMarginLeft += 'px';

_injectStyles(closeButton + '{ margin-left: ' + closeButtonMarginLeft + '}');
};

/*
Expand All @@ -271,17 +153,10 @@
*/
var resetModal = function (CSSModal) {
var $modalInner = CSSModal.activeElement.querySelector('.modal-inner');
var $modalContent = CSSModal.activeElement.querySelector('.modal-content');
var $modalContentImg = $modalContent.querySelector('img');

if ($modalInner.style) {
$modalInner.style.maxHeight = '';
$modalInner.style.height = '';
$modalInner.style.width = '';
$modalInner.style.top = '';
$modalInner.style.left = '';
$modalInner.style.marginLeft = '';
$modalContent.style.maxHeight = '';
$modalContentImg.style.maxHeight = '';
}
};

Expand All @@ -297,6 +172,42 @@
}
};

var getHorizontalOffset = function () {
var element = CSSModal.activeElement.querySelector('.modal-inner');
var elementWidth = parseInt(global.getComputedStyle(element).getPropertyValue('width'), 10);
var offset = (global.innerWidth - elementWidth) / 2;

return offset;
};

var getVerticalOffset = function () {
var element = CSSModal.activeElement.querySelector('.modal-inner');
var elementHeight = parseInt(global.getComputedStyle(element).getPropertyValue('height'), 10);
var offset = (global.innerHeight - elementHeight) / 2;

return offset;
};

var positionModal = function () {
var offset = {
top: getVerticalOffset(),
left: getHorizontalOffset()
};

var element = CSSModal.activeElement.querySelector('.modal-inner');
var elementWidth = parseInt(global.getComputedStyle(element).getPropertyValue('width'), 10);

element.style.top = offset.top + 'px';
element.style.left = offset.left + 'px';
element.style.marginLeft = 0;

// Close button
_injectStyles('.modal-close:after {' +
'top: ' + (offset.top - 28) + 'px !important;' +
'margin-right: -' + elementWidth / 2 + 'px !important;' +
'}', element.id);
};


/**
* Initial call
Expand All @@ -309,10 +220,12 @@
*/
CSSModal.on('resize', window, function () {
resizeModal();
positionModal();
});

CSSModal.on('cssmodal:show', document, function () {
resizeModal();
positionModal();
});

};
Expand Down