viewport-extra
Version:
Enable to set min-width and max-width to viewport
7 lines (6 loc) • 6.02 kB
JavaScript
/*!
* Viewport Extra v2.4.1
* (c) dsktschy
* Released under the MIT License.
*/
var ViewportExtra=function(t){"use strict";var e=function(){return e=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},e.apply(this,arguments)};function n(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var r=function(t,e){var n;return isFinite(e)?((n=t*Math.pow(10,e))<0?Math.ceil:Math.floor)(n)/Math.pow(10,e):t},i=function(t){return t.replace(/\s+/g,"").replace(/[A-Z]+/g,(function(t){return"-".concat(t.charAt(0))})).toLowerCase()},o=function(t){return"number"==typeof t&&t>0&&t<1/0||"device-width"===t},u=function(t){return"number"==typeof t&&t>=0&&t<=10},a=function(t){return"number"==typeof t&&t>=0&&t<1/0},c=function(t){return"number"==typeof t&&t>0&&t<=1/0},d={width:"device-width",initialScale:1,minWidth:0,maxWidth:1/0},l=function(t){void 0===t&&(t={});for(var n=e({},d),r=0,i=Object.keys(t);r<i.length;r++){var l=i[r],f=t[l];null==f||"width"===l&&!o(f)||"initialScale"===l&&!u(f)||"minWidth"===l&&!a(f)||"maxWidth"===l&&!c(f)?console.warn("Viewport Extra received invalid ".concat(l,", so it is discarded.")):n[l]=f}return n},f=function(t){return{content:t}};function m(t,n,o){void 0===t&&(t=e({},d)),void 0===n&&(n=0),void 0===o&&(o=0);var u=t.width,a=t.initialScale,c=t.minWidth,l=t.maxWidth,f=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}(t,["minWidth","maxWidth"]);return c>l?console.warn("Viewport Extra received minWidth that is greater than maxWidth, so they are ignored."):"number"==typeof u?console.warn("Viewport Extra received fixed width, so minWidth and maxWidth are ignored."):n<c?(f.width=c,f.initialScale=n/c*a):n>l&&(f.width=l,f.initialScale=n/l*a),Object.keys(f).map((function(t){return"".concat(i(t),"=").concat("number"==typeof f[t]?r(f[t],o):f[t])})).sort().join(",")}var p=function(t){var e=t.querySelector('meta[name="viewport"]');if(e)return e;var n=t.createElement("meta");return n.setAttribute("name","viewport"),t.head.appendChild(n),n},v=function(t){return t.documentElement.clientWidth},s=1/0,h=function(t){return void 0===t&&(t={}),{unscaledComputing:(n=t.unscaledComputing,null!=n&&n),decimalPlaces:(e=t.decimalPlaces,null!=e?e:s)};var e,n},y=function(t,e){var n,r,i,o,u={},a=(n=t.unscaledComputing,null!=(r=e.unscaledComputing)?r:n),c=(i=t.decimalPlaces,null!=(o=e.decimalPlaces)?o:i);return void 0!==a&&(u.unscaledComputing=a),void 0!==c&&(u.decimalPlaces=c),u},g=function(t){return t.decimalPlaces},w=function(t){return void 0===t&&(t={}),{content:l(t.content),media:(e=t.media,null!=e?e:"")};var e},b=function(t,n){var r,i,o,u,a={},c=(r=t.content,i=n.content,r?e(e({},r),null!=i?i:{}):i),d=(o=t.media,null!=(u=n.media)?u:o);return c&&(a.content=c),void 0!==d&&(a.media=d),a};function x(t,e,n){return t&&void 0!==e&&void 0!==n?m(t.content,e,n):m()}var A,C,O=function(t){return function(e,n){return t(n.media)?b(e,n):e}},P=function(t){return function(t,n){return void 0===t&&(t={}),void 0!==n?e(e({},t),{decimalPlaces:n}):t}((r=void 0,o=function(t){return e=t.getAttribute("data-unscaled-computing"),null!=(n=t.getAttribute("data-extra-unscaled-computing"))?n:e;var e,n}(t),void 0===r&&(r={}),void 0!==(i=null!==o||void 0)?e(e({},r),{unscaledComputing:i}):r),(n=function(t){return e=t.getAttribute("data-decimal-places"),null!=(n=t.getAttribute("data-extra-decimal-places"))?n:e;var e,n}(t),null!==n?+n:void 0));var n,r,i,o},W=function(t){return function(t,n){return void 0===t&&(t={}),void 0!==n?e(e({},t),{media:n}):t}((r=void 0,o=function(t){return e=t.getAttribute("content"),n=t.getAttribute("data-extra-content"),e?n?[e,n].join(","):e:n;var e,n}(t),i=o?o.split(",").reduce((function(t,e){var n=e.split("="),r=n[0],i=n[1],o=r.trim();if(!o)return t;var u=i.trim();if(!u)return t;var a,c=+u;return t[(a=o,a.replace(/\s+/g,"").toLowerCase().replace(/-./g,(function(t){return t.charAt(1).toUpperCase()})))]=isNaN(c)?u:c,t}),{}):void 0,void 0===r&&(r={}),i?e(e({},r),{content:i}):r),(n=function(t){return e=t.getAttribute("data-media"),null!=(n=t.getAttribute("data-extra-media"))?n:e;var e,n}(t),null!=n?n:void 0));var n,r,i,o},j=function(t,e){return t.setAttribute("content",e)},S=function(t,e,n,r){(function(t){return t.unscaledComputing})(r)&&j(t,x()),j(t,x(n(),e(),g(r)))},E=function(t){return function(e){return void 0===e||t(e).matches}},M=null,V=[],k=null,q=[];"undefined"!=typeof window&&(M=p(document),A=document,C=A.querySelectorAll('meta[name="viewport-extra"]'),V=Array.prototype.slice.call(C),k=h(n([P(M)],V.map(P),!0).reduce(y)),q=n([W(M)],V.map(W),!0),function(t,e,n,r){j(t,x()),j(t,x(n(),e(),g(r)))}(M,(function(){return v(document)}),(function(){return w(q.reduce(O(E(matchMedia)),w()))}),k));var L=function(t,e){void 0===e&&(e={}),"undefined"!=typeof window&&M&&k&&(k=h([k,e].reduce(y)),q=n(n([],q,!0),t,!0),S(M,(function(){return v(document)}),(function(){return w(q.reduce(O(E(matchMedia)),w()))}),k))},N=function(t){"undefined"!=typeof window&&M&&k&&(q=n(n([],q,!0),[f(t)],!1),S(M,(function(){return v(document)}),(function(){return w(q.reduce(O(E(matchMedia)),w()))}),k))},R=function(){return w(q.reduce(b,w())).content},_=function(){"undefined"!=typeof window&&(M=p(document))},F=function(){function t(t){N("number"==typeof t?{minWidth:t}:t)}return t.setParameters=L,t.setContent=N,t.getContent=R,t.updateReference=_,t}();return t.default=F,t.getContent=R,t.setContent=N,t.setParameters=L,t.updateReference=_,Object.defineProperty(t,"__esModule",{value:!0}),(t=t.default).default=t,t}({});