viewport-extra
Version:
Enable setting minimum and maximum viewport width
309 lines (281 loc) • 17.6 kB
JavaScript
/*! Viewport Extra v3.0.0 | (c) dsktschy | MIT License */
var ViewportExtra = (function (exports) {
'use strict';
var arrayFrom = function (arrayLike) {
return Array.prototype.slice.call(arrayLike);
};
var ensureViewportMetaElement = function (doc) {
var viewportMetaElement = doc.querySelector('meta[name="viewport"]');
if (viewportMetaElement)
return viewportMetaElement;
var htmlMetaElement = doc.createElement("meta");
htmlMetaElement.setAttribute("name", "viewport");
doc.head.appendChild(htmlMetaElement);
return htmlMetaElement;
};
var getMetaElementList = function (doc) {
return arrayFrom(doc.querySelectorAll('meta[name="viewport"],meta[name="viewport-extra"]'));
};
var getDocumentClientWidth = function (doc) {
return doc.documentElement.clientWidth;
};
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var defaultDecimalPlaces = Infinity;
var createDecimalPlaces = function (optionalDecimalPlaces) { return optionalDecimalPlaces !== null && optionalDecimalPlaces !== void 0 ? optionalDecimalPlaces : defaultDecimalPlaces; };
var mergeOptionalDecimalPlaces = function (precedingOptionalDecimalPlaces, followingOptionalDecimalPlaces) {
return followingOptionalDecimalPlaces !== null && followingOptionalDecimalPlaces !== void 0 ? followingOptionalDecimalPlaces : precedingOptionalDecimalPlaces;
};
var createGlobalParameters = function (partialGlobalParameters) {
if (partialGlobalParameters === void 0) { partialGlobalParameters = {}; }
return ({
decimalPlaces: createDecimalPlaces(partialGlobalParameters.decimalPlaces),
});
};
var mergePartialGlobalParameters = function (precedingPartialGlobalParameters, followingPartialGlobalParameters) {
var partialGlobalParameters = {};
var optionalDecimalPlaces = mergeOptionalDecimalPlaces(precedingPartialGlobalParameters.decimalPlaces, followingPartialGlobalParameters.decimalPlaces);
if (typeof optionalDecimalPlaces !== "undefined")
partialGlobalParameters.decimalPlaces = optionalDecimalPlaces;
return partialGlobalParameters;
};
var assignOptionalDecimalPlaces = function (optionalPartialGlobalParameters, optionalDecimalPlaces) {
return typeof optionalDecimalPlaces !== "undefined"
? __assign(__assign({}, (optionalPartialGlobalParameters !== null && optionalPartialGlobalParameters !== void 0 ? optionalPartialGlobalParameters : {})), { decimalPlaces: optionalDecimalPlaces }) : (optionalPartialGlobalParameters !== null && optionalPartialGlobalParameters !== void 0 ? optionalPartialGlobalParameters : {});
};
var getDecimalPlaces = function (globalParameters) {
return globalParameters.decimalPlaces;
};
var camelizeKebabCaseString = function (str) {
return str
.replace(/\s+/g, "")
.toLowerCase()
.replace(/-./g, function (s) { return s[1].toUpperCase(); });
};
var kebabizeCamelCaseString = function (str) {
return str
.replace(/\s+/g, "")
.replace(/[A-Z]+/g, function (s) { return "-".concat(s[0]); })
.toLowerCase();
};
var mergeNullableContentAttributes = function (precedingNullableContentAttribute, followingNullableContentAttribute) {
return precedingNullableContentAttribute
? followingNullableContentAttribute
? [
precedingNullableContentAttribute,
followingNullableContentAttribute,
].join(",")
: precedingNullableContentAttribute
: followingNullableContentAttribute;
};
var createOptionalPartialContent = function (nullableContentAttribute) {
return nullableContentAttribute
? nullableContentAttribute
.split(",")
.reduce(function (partialContent, equalSeparatedContent) {
var _a = equalSeparatedContent
.split("=")
.map(function (keyOrValue) { return keyOrValue.trim(); }), key = _a[0], value = _a[1];
if (key && value) {
var numberValue = +value;
// biome-ignore lint/suspicious/noGlobalIsNan: isNaN is safe to use here
partialContent[camelizeKebabCaseString(key)] = isNaN(numberValue)
? value
: numberValue;
}
return partialContent;
}, {})
: undefined;
};
var mergeNullableDecimalPlacesAttribute = function (precedingNullableDecimalPlacesAttribute, followingNullableDecimalPlacesAttribute) {
return followingNullableDecimalPlacesAttribute !== null && followingNullableDecimalPlacesAttribute !== void 0 ? followingNullableDecimalPlacesAttribute : precedingNullableDecimalPlacesAttribute;
};
var createOptionalDecimalPlaces = function (nullableDecimalPlacesAttribute) {
return nullableDecimalPlacesAttribute !== null
? +nullableDecimalPlacesAttribute
: undefined;
};
var mergeNullableMediaAttribute = function (precedingNullableMediaAttribute, followingNullableMediaAttribute) {
return followingNullableMediaAttribute !== null && followingNullableMediaAttribute !== void 0 ? followingNullableMediaAttribute : precedingNullableMediaAttribute;
};
var createOptionalMedia = function (nullableMediaAttribute) { return nullableMediaAttribute !== null && nullableMediaAttribute !== void 0 ? nullableMediaAttribute : undefined; };
var mathTrunc = function (num) { return (num < 0 ? Math.ceil : Math.floor)(num); };
var truncateDecimalNumber = function (num, decimalPlaces) {
// biome-ignore lint/suspicious/noGlobalIsFinite: isFinite is safe to use here
return isFinite(decimalPlaces)
? mathTrunc(num * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces)
: num;
};
var defaultContent = {
width: "device-width",
initialScale: 1,
minimumWidth: 0,
maximumWidth: Infinity,
};
var createContent = function (partialContent) {
if (partialContent === void 0) { partialContent = {}; }
return (__assign(__assign({}, defaultContent), partialContent));
};
var mergeOptionalPartialContent = function (precedingOptionalPartialContent, followingOptionalPartialContent) {
return precedingOptionalPartialContent
? __assign(__assign({}, precedingOptionalPartialContent), (followingOptionalPartialContent !== null && followingOptionalPartialContent !== void 0 ? followingOptionalPartialContent : {})) : followingOptionalPartialContent;
};
var createContentAttribute$1 = function (content,
// biome-ignore lint/style/noInferrableTypes: number type cannot be inferred from initialization
documentClientWidth, decimalPlaces) {
if (content === void 0) { content = __assign({}, defaultContent); }
if (documentClientWidth === void 0) { documentClientWidth = 0; }
if (decimalPlaces === void 0) { decimalPlaces = 0; }
var width = content.width, initialScale = content.initialScale;
var _minimumWidth = content.minimumWidth, _maximumWidth = content.maximumWidth, minWidth = content.minWidth, maxWidth = content.maxWidth, contentWithoutExtraProperties = __rest(content, ["minimumWidth", "maximumWidth", "minWidth", "maxWidth"]);
var minimumWidth = minWidth !== null && minWidth !== void 0 ? minWidth : _minimumWidth;
var maximumWidth = maxWidth !== null && maxWidth !== void 0 ? maxWidth : _maximumWidth;
if (minimumWidth <= maximumWidth && width === "device-width") {
if (documentClientWidth < minimumWidth) {
contentWithoutExtraProperties.width = minimumWidth;
contentWithoutExtraProperties.initialScale =
(documentClientWidth / minimumWidth) * initialScale;
}
else if (documentClientWidth > maximumWidth) {
contentWithoutExtraProperties.width = maximumWidth;
contentWithoutExtraProperties.initialScale =
(documentClientWidth / maximumWidth) * initialScale;
}
}
return Object.keys(contentWithoutExtraProperties)
.map(function (key) {
return "".concat(kebabizeCamelCaseString(key), "=").concat(typeof contentWithoutExtraProperties[key] === "number"
? truncateDecimalNumber(contentWithoutExtraProperties[key], decimalPlaces)
: contentWithoutExtraProperties[key]);
})
.sort()
.join(",");
};
var defaultMedia = "";
var createMedia = function (optionalMedia) {
return optionalMedia !== null && optionalMedia !== void 0 ? optionalMedia : defaultMedia;
};
var mergeOptionalMedia = function (precedingOptionalMedia, followingOptionalMedia) { return followingOptionalMedia !== null && followingOptionalMedia !== void 0 ? followingOptionalMedia : precedingOptionalMedia; };
var createMediaSpecificParameters = function (partialMediaSpecificParameters) {
if (partialMediaSpecificParameters === void 0) { partialMediaSpecificParameters = {}; }
return ({
content: createContent(partialMediaSpecificParameters.content),
media: createMedia(partialMediaSpecificParameters.media),
});
};
var mergePartialMediaSpecificParameters = function (precedingPartialMediaSpecificParameters, followingPartialMediaSpecificParameters) {
var partialMediaSpecificParameters = {};
var optionalPartialContent = mergeOptionalPartialContent(precedingPartialMediaSpecificParameters.content, followingPartialMediaSpecificParameters.content);
var optionalMedia = mergeOptionalMedia(precedingPartialMediaSpecificParameters.media, followingPartialMediaSpecificParameters.media);
if (optionalPartialContent)
partialMediaSpecificParameters.content = optionalPartialContent;
if (typeof optionalMedia !== "undefined")
partialMediaSpecificParameters.media = optionalMedia;
return partialMediaSpecificParameters;
};
var createContentAttribute = function (optionalMediaSpecificParameters, optionalDocumentClientWidth, optionalDecimalPlaces) {
return optionalMediaSpecificParameters &&
typeof optionalDocumentClientWidth !== "undefined" &&
typeof optionalDecimalPlaces !== "undefined"
? createContentAttribute$1(optionalMediaSpecificParameters.content, optionalDocumentClientWidth, optionalDecimalPlaces)
: createContentAttribute$1();
};
var assignOptionalPartialContent = function (optionalPartialMediaSpecificParameters, optionalPartialContent) {
return optionalPartialContent
? __assign(__assign({}, (optionalPartialMediaSpecificParameters !== null && optionalPartialMediaSpecificParameters !== void 0 ? optionalPartialMediaSpecificParameters : {})), { content: optionalPartialContent }) : (optionalPartialMediaSpecificParameters !== null && optionalPartialMediaSpecificParameters !== void 0 ? optionalPartialMediaSpecificParameters : {});
};
var assignOptionalMedia = function (optionalPartialMediaSpecificParameters, optionalMedia) {
return typeof optionalMedia !== "undefined"
? __assign(__assign({}, (optionalPartialMediaSpecificParameters !== null && optionalPartialMediaSpecificParameters !== void 0 ? optionalPartialMediaSpecificParameters : {})), { media: optionalMedia }) : (optionalPartialMediaSpecificParameters !== null && optionalPartialMediaSpecificParameters !== void 0 ? optionalPartialMediaSpecificParameters : {});
};
var createPartialMediaSpecificParametersMerger = function (isMatchingCurrentViewport) {
return function (precedingPartialMediaSpecificParameters, followingPartialMediaSpecificParameters) {
return isMatchingCurrentViewport(followingPartialMediaSpecificParameters.media)
? mergePartialMediaSpecificParameters(precedingPartialMediaSpecificParameters, followingPartialMediaSpecificParameters)
: precedingPartialMediaSpecificParameters;
};
};
var getNullableDecimalPlacesAttribute = function (htmlMetaElement) {
return mergeNullableDecimalPlacesAttribute(htmlMetaElement.getAttribute("data-decimal-places"), htmlMetaElement.getAttribute("data-extra-decimal-places"));
};
var createPartialGlobalParameters = function (htmlMetaElement) {
return assignOptionalDecimalPlaces(undefined, createOptionalDecimalPlaces(getNullableDecimalPlacesAttribute(htmlMetaElement)));
};
var getNullableContentAttribute = function (htmlMetaElement) {
return mergeNullableContentAttributes(htmlMetaElement.getAttribute("content"), htmlMetaElement.getAttribute("data-extra-content"));
};
var getNullableMediaAttribute = function (htmlMetaElement) {
return mergeNullableMediaAttribute(htmlMetaElement.getAttribute("data-media"), htmlMetaElement.getAttribute("data-extra-media"));
};
var createPartialMediaSpecificParameters = function (htmlMetaElement) {
return assignOptionalMedia(assignOptionalPartialContent(undefined, createOptionalPartialContent(getNullableContentAttribute(htmlMetaElement))), createOptionalMedia(getNullableMediaAttribute(htmlMetaElement)));
};
var setContentAttribute = function (htmlMetaElement, contentAttribute) { return htmlMetaElement.setAttribute("content", contentAttribute); };
var applyMediaSpecificParametersTruncated = function (htmlMetaElement, getDocumentClientWidth, getMediaSpecificParameters, globalParameters) {
setContentAttribute(htmlMetaElement, createContentAttribute());
setContentAttribute(htmlMetaElement, createContentAttribute(getMediaSpecificParameters(), getDocumentClientWidth(), getDecimalPlaces(globalParameters)));
};
var createMatchMediaPredicate = function (mm) {
return function (media) {
return typeof media !== "undefined" ? mm(media).matches : true;
};
};
var setParameters = function (partialMediaSpecificParametersList, partialGlobalParameters) {
if (partialGlobalParameters === void 0) { partialGlobalParameters = {}; }
if (typeof window === "undefined")
return;
applyMediaSpecificParametersTruncated(ensureViewportMetaElement(document), function () { return getDocumentClientWidth(document); }, function () {
return createMediaSpecificParameters(partialMediaSpecificParametersList.reduce(createPartialMediaSpecificParametersMerger(createMatchMediaPredicate(matchMedia)),
// Value that does not need to check matching current viewport
createMediaSpecificParameters()));
}, createGlobalParameters(partialGlobalParameters));
};
var activateAttributes = function () {
if (typeof window === "undefined")
return;
var metaElementList = getMetaElementList(document);
setParameters(metaElementList.map(createPartialMediaSpecificParameters), metaElementList
.map(createPartialGlobalParameters)
.reduce(mergePartialGlobalParameters));
};
activateAttributes();
exports.apply = setParameters;
return exports;
})({});
//# sourceMappingURL=viewport-extra.js.map