aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/style.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2019-06-01 19:55:14 +1000
committerSam Potts <sam@potts.es>2019-06-01 19:55:14 +1000
commit12c6282d14e3e06a7784760f83affc9195afed1f (patch)
treeaf1c6be692c4cf2679e7efc604896b686974368d /src/js/utils/style.js
parent996075decc6e8c0f0c5059dccea21b16020eb78b (diff)
parent0249772f019762ebd494ac409e597103820413c3 (diff)
downloadplyr-12c6282d14e3e06a7784760f83affc9195afed1f.tar.lz
plyr-12c6282d14e3e06a7784760f83affc9195afed1f.tar.xz
plyr-12c6282d14e3e06a7784760f83affc9195afed1f.zip
Merge branch 'develop' into css-variables
# Conflicts: # .eslintrc # demo/dist/demo.css # demo/dist/demo.js # demo/dist/demo.min.js # demo/dist/demo.min.js.map # dist/plyr.css # dist/plyr.js # dist/plyr.min.js # dist/plyr.min.js.map # dist/plyr.min.mjs # dist/plyr.min.mjs.map # dist/plyr.mjs # dist/plyr.polyfilled.js # dist/plyr.polyfilled.min.js # dist/plyr.polyfilled.min.js.map # dist/plyr.polyfilled.min.mjs # dist/plyr.polyfilled.min.mjs.map # dist/plyr.polyfilled.mjs # gulpfile.js # package.json
Diffstat (limited to 'src/js/utils/style.js')
-rw-r--r--src/js/utils/style.js70
1 files changed, 57 insertions, 13 deletions
diff --git a/src/js/utils/style.js b/src/js/utils/style.js
index a8eb393b..6f3069c9 100644
--- a/src/js/utils/style.js
+++ b/src/js/utils/style.js
@@ -4,26 +4,68 @@
import is from './is';
-/* function reduceAspectRatio(width, height) {
- const getRatio = (w, h) => (h === 0 ? w : getRatio(h, w % h));
- const ratio = getRatio(width, height);
- return `${width / ratio}:${height / ratio}`;
-} */
+export function validateRatio(input) {
+ if (!is.array(input) && (!is.string(input) || !input.includes(':'))) {
+ return false;
+ }
-// Set aspect ratio for responsive container
-export function setAspectRatio(input) {
- let ratio = input;
+ const ratio = is.array(input) ? input : input.split(':');
+
+ return ratio.map(Number).every(is.number);
+}
+
+export function reduceAspectRatio(ratio) {
+ if (!is.array(ratio) || !ratio.every(is.number)) {
+ return null;
+ }
+
+ const [width, height] = ratio;
+ const getDivider = (w, h) => (h === 0 ? w : getDivider(h, w % h));
+ const divider = getDivider(width, height);
+
+ return [width / divider, height / divider];
+}
+
+export function getAspectRatio(input) {
+ const parse = ratio => {
+ if (!validateRatio(ratio)) {
+ return null;
+ }
- if (!is.string(ratio) && !is.nullOrUndefined(this.embed)) {
+ return ratio.split(':').map(Number);
+ };
+
+ // Provided ratio
+ let ratio = parse(input);
+
+ // Get from config
+ if (ratio === null) {
+ ratio = parse(this.config.ratio);
+ }
+
+ // Get from embed
+ if (ratio === null && !is.empty(this.embed) && is.array(this.embed.ratio)) {
({ ratio } = this.embed);
}
- if (!is.string(ratio)) {
- ({ ratio } = this.config);
+ // Get from HTML5 video
+ if (ratio === null && this.isHTML5) {
+ const { videoWidth, videoHeight } = this.media;
+ ratio = reduceAspectRatio([videoWidth, videoHeight]);
+ }
+
+ return ratio;
+}
+
+// Set aspect ratio for responsive container
+export function setAspectRatio(input) {
+ if (!this.isVideo) {
+ return {};
}
- const [x, y] = ratio.split(':').map(Number);
- const padding = (100 / x) * y;
+ const ratio = getAspectRatio.call(this, input);
+ const [w, h] = is.array(ratio) ? ratio : [0, 0];
+ const padding = (100 / w) * h;
this.elements.wrapper.style.paddingBottom = `${padding}%`;
@@ -32,6 +74,8 @@ export function setAspectRatio(input) {
const height = 240;
const offset = (height - padding) / (height / 50);
this.media.style.transform = `translateY(-${offset}%)`;
+ } else if (this.isHTML5) {
+ this.elements.wrapper.classList.toggle(this.config.classNames.videoFixedRatio, ratio !== null);
}
return { padding, ratio };