2017-11-13 21:00:00 -05:00
|
|
|
module.exports = function(options) {
|
2017-11-13 19:54:55 -05:00
|
|
|
var scaleFactor = 1,
|
|
|
|
element = options.element || document.getElementById(options.id),
|
2018-05-14 14:51:13 -04:00
|
|
|
elementWidth = Number(options.width) || element.offsetWidth,
|
|
|
|
elementHeight = Number(options.height) || element.offsetHeight,
|
2017-11-13 19:54:55 -05:00
|
|
|
valign = options.valign || "center",
|
|
|
|
halign = options.halign || "center",
|
|
|
|
fit = options.fit || "cover",
|
|
|
|
scale = options.scale;
|
|
|
|
|
|
|
|
// Apply required attributes to the element
|
|
|
|
element.style.position = "absolute";
|
|
|
|
element.parentElement.style.overflow = "hidden";
|
|
|
|
|
|
|
|
// Apply required transform-origin if the scale option is set to true
|
|
|
|
if (scale) {
|
|
|
|
element.style.transformOrigin = "left top";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply relative position to the parent if it doesn't already have relative, absolute or fixed positioning
|
|
|
|
if ([ "relative", "absolute", "fixed" ].indexOf(window.getComputedStyle(element.parentElement, null).getPropertyValue("position")) === -1) {
|
|
|
|
element.parentElement.style.position = "relative";
|
|
|
|
}
|
|
|
|
|
2018-05-14 15:10:46 -04:00
|
|
|
function parseVerticalAlignment() {
|
|
|
|
// Convert valign to decimal percent
|
|
|
|
switch (valign) {
|
|
|
|
case "top":
|
|
|
|
valign = 0;
|
2018-05-14 14:51:13 -04:00
|
|
|
break;
|
2018-05-11 15:00:36 -04:00
|
|
|
|
2018-05-14 14:51:13 -04:00
|
|
|
case "center":
|
2018-05-14 15:10:46 -04:00
|
|
|
valign = 0.5;
|
2018-05-14 14:51:13 -04:00
|
|
|
break;
|
2018-05-11 15:00:36 -04:00
|
|
|
|
2018-05-14 15:10:46 -04:00
|
|
|
case "bottom":
|
|
|
|
valign = 1;
|
2018-05-14 14:51:13 -04:00
|
|
|
break;
|
2018-05-11 15:00:36 -04:00
|
|
|
|
2018-05-14 14:51:13 -04:00
|
|
|
default:
|
2018-05-14 15:10:46 -04:00
|
|
|
valign = Number(valign) / 100;
|
2018-05-14 14:51:13 -04:00
|
|
|
}
|
2018-05-14 15:10:46 -04:00
|
|
|
}
|
2018-05-11 15:00:36 -04:00
|
|
|
|
2018-05-14 15:10:46 -04:00
|
|
|
function parseHorizontalAlignment() {
|
|
|
|
// Convert halign to decimal percent
|
|
|
|
switch (halign) {
|
|
|
|
case "left":
|
|
|
|
halign = 0;
|
2018-05-14 14:51:13 -04:00
|
|
|
break;
|
2018-05-11 15:00:36 -04:00
|
|
|
|
2018-05-14 14:51:13 -04:00
|
|
|
case "center":
|
2018-05-14 15:10:46 -04:00
|
|
|
halign = 0.5;
|
2018-05-14 14:51:13 -04:00
|
|
|
break;
|
2018-05-11 15:00:36 -04:00
|
|
|
|
2018-05-14 15:10:46 -04:00
|
|
|
case "right":
|
|
|
|
halign = 1;
|
2018-05-14 14:51:13 -04:00
|
|
|
break;
|
2018-05-11 15:00:36 -04:00
|
|
|
|
2018-05-14 14:51:13 -04:00
|
|
|
default:
|
2018-05-14 15:10:46 -04:00
|
|
|
halign = Number(halign) / 100;
|
2018-05-14 14:51:13 -04:00
|
|
|
}
|
2018-05-11 15:00:36 -04:00
|
|
|
}
|
|
|
|
|
2017-11-13 19:54:55 -05:00
|
|
|
function updateContain() {
|
|
|
|
var parentWidth = element.parentElement.offsetWidth,
|
2018-05-11 15:00:36 -04:00
|
|
|
parentHeight = element.parentElement.offsetHeight,
|
|
|
|
rightAlignment = 0 - (elementWidth * scaleFactor - parentWidth),
|
|
|
|
bottomAlignment = 0 - (elementHeight * scaleFactor - parentHeight);
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// Run the scale/position functionality if able to determine the parent element's width and height
|
|
|
|
if (parentWidth && parentHeight) {
|
|
|
|
// Calculate the scale factor
|
|
|
|
if (fit === "cover") {
|
|
|
|
if (parentWidth > parentHeight / elementHeight * elementWidth) {
|
|
|
|
scaleFactor = parentWidth / elementWidth;
|
|
|
|
} else {
|
|
|
|
scaleFactor = parentHeight / elementHeight;
|
|
|
|
}
|
|
|
|
} else if (fit === "contain") {
|
|
|
|
if (parentHeight > parentWidth / elementWidth * elementHeight) {
|
|
|
|
scaleFactor = parentWidth / elementWidth;
|
|
|
|
} else {
|
|
|
|
scaleFactor = parentHeight / elementHeight;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scaleFactor = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale the element using the scale factor
|
|
|
|
if (scale) {
|
|
|
|
element.style.transform = "scale(" + scaleFactor + ")";
|
|
|
|
} else {
|
|
|
|
element.style.width = elementWidth * scaleFactor + "px";
|
|
|
|
element.style.height = elementHeight * scaleFactor + "px";
|
|
|
|
}
|
|
|
|
|
2018-05-11 15:00:36 -04:00
|
|
|
// Align the element horizontally
|
2017-11-13 19:54:55 -05:00
|
|
|
if (parentWidth !== elementWidth * scaleFactor) {
|
2018-05-11 15:00:36 -04:00
|
|
|
// Align horizontally by percent
|
|
|
|
element.style.left = rightAlignment * halign + "px";
|
2017-11-13 19:54:55 -05:00
|
|
|
} else {
|
2018-05-11 15:00:36 -04:00
|
|
|
// Align the element against the left if the width of the parent and element are the same
|
2017-11-13 19:54:55 -05:00
|
|
|
element.style.left = "0px";
|
|
|
|
}
|
|
|
|
|
2018-05-11 15:00:36 -04:00
|
|
|
// Align the element vertically
|
2017-11-13 19:54:55 -05:00
|
|
|
if (parentHeight !== elementHeight * scaleFactor) {
|
2018-05-11 15:00:36 -04:00
|
|
|
// Align vertically by percent
|
|
|
|
element.style.top = bottomAlignment * valign + "px";
|
2017-11-13 19:54:55 -05:00
|
|
|
} else {
|
2018-05-11 15:00:36 -04:00
|
|
|
// Align the element against the top if the height of the parent and element are the same
|
2017-11-13 19:54:55 -05:00
|
|
|
element.style.top = "0px";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Try again in 30ms if the document didn't load enough to determine the parent element's width and height yet
|
2018-05-11 15:00:36 -04:00
|
|
|
setTimeout(updateContain, 30);
|
2017-11-13 19:54:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:51:13 -04:00
|
|
|
// Parse the valign and halign options
|
2018-05-14 15:10:46 -04:00
|
|
|
parseVerticalAlignment();
|
|
|
|
parseHorizontalAlignment();
|
2018-05-14 14:51:13 -04:00
|
|
|
|
2017-11-13 19:54:55 -05:00
|
|
|
// Run the function to scale and anchor the element
|
|
|
|
updateContain();
|
|
|
|
|
|
|
|
// External scale and anchor update function
|
|
|
|
this.update = updateContain;
|
|
|
|
|
|
|
|
// External function to set elementWidth
|
2018-05-14 14:51:13 -04:00
|
|
|
this.setWidth = function(newWidth) {
|
2018-05-14 15:10:46 -04:00
|
|
|
elementWidth = Number(newWidth);
|
2018-05-14 14:51:13 -04:00
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to set elementHeight
|
2018-05-14 14:51:13 -04:00
|
|
|
this.setHeight = function(newHeight) {
|
2018-05-14 15:10:46 -04:00
|
|
|
elementHeight = Number(newHeight);
|
2018-05-14 14:51:13 -04:00
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to set valign
|
2018-05-14 14:51:13 -04:00
|
|
|
this.setValign = function(newValign) {
|
|
|
|
valign = newValign;
|
2018-05-14 15:10:46 -04:00
|
|
|
parseVerticalAlignment();
|
2018-05-14 14:51:13 -04:00
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to set halign
|
2018-05-14 14:51:13 -04:00
|
|
|
this.setHalign = function(newHalign) {
|
|
|
|
halign = newHalign;
|
2018-05-14 15:10:46 -04:00
|
|
|
parseHorizontalAlignment();
|
2018-05-14 14:51:13 -04:00
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to set fit
|
2018-05-14 14:51:13 -04:00
|
|
|
this.setFit = function(newFit) {
|
|
|
|
fit = newFit;
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the elementWidth
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getWidth = function() {
|
|
|
|
return Number(elementWidth);
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the elementHeight
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getHeight = function() {
|
|
|
|
return Number(elementHeight);
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the current width
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getCurrentWidth = function() {
|
|
|
|
return scaleFactor * elementWidth;
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the current height
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getCurrentHeight = function() {
|
|
|
|
return scaleFactor * elementHeight;
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the current valign
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getValign = function() {
|
|
|
|
return valign;
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the current halign
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getHalign = function() {
|
|
|
|
return halign;
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the current fit
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getFit = function() {
|
|
|
|
return fit;
|
|
|
|
};
|
2017-11-13 19:54:55 -05:00
|
|
|
|
|
|
|
// External function to return the current scale factor
|
2018-05-14 14:51:13 -04:00
|
|
|
this.getScale = function() {
|
|
|
|
return scaleFactor;
|
|
|
|
};
|
2017-11-13 21:00:00 -05:00
|
|
|
};
|