2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-18 23:10:33 +03:00
Files
vue-meta/dist/vue-meta.min.js
T
2019-07-11 20:39:14 +00:00

2 lines
11 KiB
JavaScript

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).VueMeta=t()}(this,function(){"use strict";var e=null;function t(t,n){t.$root._vueMeta.initialized||!t.$root._vueMeta.initializing&&"watcher"!==n||(t.$root._vueMeta.initialized=null),t.$root._vueMeta.initialized&&!t.$root._vueMeta.paused&&function(t,n){void 0===n&&(n=10);clearTimeout(e),e=setTimeout(function(){t()},n)}(function(){return t.$meta().refresh()})}function n(e){return Array.isArray(e)}function r(e){return void 0===e}function i(e){return"object"==typeof e}function o(e){return"function"==typeof e}function a(e,t){return t&&i(e)?(n(e[t])||(e[t]=[]),e):n(e)?e:[]}function u(e,t,n){a(e,t),e[t].push(n)}function s(e){return void 0===e&&(e=this),e&&(!0===e._vueMeta||i(e._vueMeta))}function c(e){if(!e.$root._vueMeta.navGuards&&e.$root.$router){e.$root._vueMeta.navGuards=!0;var t=e.$root.$router,n=e.$root.$meta();t.beforeEach(function(e,t,r){n.pause(),r()}),t.afterEach(function(){var e=n.resume().metaInfo;e&&e.afterNavigation&&o(e.afterNavigation)&&e.afterNavigation(e)})}}var f=1;var l={title:void 0,titleChunk:"",titleTemplate:"%s",htmlAttrs:{},bodyAttrs:{},headAttrs:{},base:[],link:[],meta:[],style:[],script:[],noscript:[],__dangerouslyDisableSanitizers:[],__dangerouslyDisableSanitizersByTagID:{}},d={keyName:"metaInfo",attribute:"data-vue-meta",ssrAttribute:"data-vue-meta-server-rendered",tagIDKeyName:"vmid",contentKeyName:"content",metaTemplateKeyName:"template"},v=["titleChunk","titleTemplate","changed","__dangerouslyDisableSanitizers","__dangerouslyDisableSanitizersByTagID"],h=["__dangerouslyDisableSanitizers","__dangerouslyDisableSanitizersByTagID"],p=["htmlAttrs","headAttrs","bodyAttrs"],m=["allowfullscreen","amp","async","autofocus","autoplay","checked","compact","controls","declare","default","defaultchecked","defaultmuted","defaultselected","defer","disabled","enabled","formnovalidate","hidden","indeterminate","inert","ismap","itemscope","loop","multiple","muted","nohref","noresize","noshade","novalidate","nowrap","open","pauseonexit","readonly","required","reversed","scoped","seamless","selected","sortable","truespeed","typemustmatch","visible"],y=function(){return console.warn("This vue app/component has no vue-meta configuration")};function g(e){return void 0===e&&(e=!0),this.$root._vueMeta.paused=!0,function(){return b(e)}}function b(e){if(void 0===e&&(e=!0),this.$root._vueMeta.paused=!1,e)return this.$root.$meta().refresh()}function $(e,t,n,i){var a=e.component,u=e.metaTemplateKeyName,s=e.contentKeyName;return r(n)&&(n=t[u],delete t[u]),!!n&&(r(i)&&(i=t[s]),t[s]=o(n)?n.call(a,i):n.replace(/%s/g,i),!0)}function M(e,t){var n=arguments;if(!Array.prototype.findIndex){for(var r=0;r<e.length;r++)if(t.call(n[2],e[r],r,e))return r;return-1}return e.findIndex(t,arguments[2])}function _(e){return Array.from?Array.from(e):Array.prototype.slice.call(e)}function T(e,t){if(!Array.prototype.includes){for(var n in e)if(e[n]===t)return!0;return!1}return e.includes(t)}var A=[[/&/g,"&"],[/</g,"<"],[/>/g,">"],[/"/g,'"'],[/'/g,"'"]];var N=function(e){return function(e){return!!e&&"object"==typeof e}(e)&&!function(e){var t=Object.prototype.toString.call(e);return"[object RegExp]"===t||"[object Date]"===t||function(e){return e.$$typeof===w}(e)}(e)};var w="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function I(e,t){return!1!==t.clone&&t.isMergeableObject(e)?j((n=e,Array.isArray(n)?[]:{}),e,t):e;var n}function O(e,t,n){return e.concat(t).map(function(e){return I(e,n)})}function S(e){return Object.keys(e).concat(function(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(t){return e.propertyIsEnumerable(t)}):[]}(e))}function E(e,t,n){var r={};return n.isMergeableObject(e)&&S(e).forEach(function(t){r[t]=I(e[t],n)}),S(t).forEach(function(i){n.isMergeableObject(t[i])&&e[i]?r[i]=function(e,t){if(!t.customMerge)return j;var n=t.customMerge(e);return"function"==typeof n?n:j}(i,n)(e[i],t[i],n):r[i]=I(t[i],n)}),r}function j(e,t,n){(n=n||{}).arrayMerge=n.arrayMerge||O,n.isMergeableObject=n.isMergeableObject||N;var r=Array.isArray(t);return r===Array.isArray(e)?r?n.arrayMerge(e,t,n):E(e,t,n):I(t,n)}j.all=function(e,t){if(!Array.isArray(e))throw new Error("first argument should be an array");return e.reduce(function(e,n){return j(e,n,t)},{})};var z=j;function D(e,t,n){return void 0===n&&(n={}),t.hasOwnProperty("title")&&void 0===t.title&&delete t.title,p.forEach(function(e){if(t[e])for(var n in t[e])t[e].hasOwnProperty(n)&&void 0===t[e][n]&&(m.includes(n)&&console.warn("VueMeta: Please note that since v2 the value undefined is not used to indicate boolean attributes anymore, see migration guide for details"),delete t[e][n])}),z(e,t,{arrayMerge:function(e,t){return function(e,t,n){var r=e.component,i=e.tagIDKeyName,o=e.metaTemplateKeyName,a=e.contentKeyName,u=[];return t.forEach(function(e,t){if(e[i]){var s=M(n,function(t){return t[i]===e[i]}),c=n[s];if(-1!==s){if(c.hasOwnProperty(a)&&void 0===c[a]||c.hasOwnProperty("innerHTML")&&void 0===c.innerHTML)return u.push(e),void n.splice(s,1);if(null!==c[a]&&null!==c.innerHTML){var f=e[o];f&&(c[o]?c[a]||$({component:r,metaTemplateKeyName:o,contentKeyName:a},c,void 0,e[a]):$({component:r,metaTemplateKeyName:o,contentKeyName:a},c,f))}else n.splice(s,1)}else u.push(e)}else u.push(e)}),u.concat(n)}(n,e,t)}})}function k(e,t,n){void 0===e&&(e={}),void 0===n&&(n={});var a=e.keyName,u=e.metaTemplateKeyName,s=e.tagIDKeyName,c=t.$options,f=t.$children;if(t._inactive)return n;if(c[a]){var l=c[a];if(o(l)&&(l=l.call(t)),!i(l))return n;n=D(n,l,e)}return f.length&&f.forEach(function(t){(function(e){return void 0===e&&(e=this),e&&!r(e._vueMeta)})(t)&&(n=k(e,t,n))}),u&&n.meta&&(n.meta.forEach(function(t){return $(e,t)}),n.meta=n.meta.filter(function(e,t,n){return!e.hasOwnProperty(s)||t===M(n,function(t){return t[s]===e[s]})})),n}function K(e,t,r){void 0===e&&(e={}),void 0===r&&(r=[]);var o=k(e,t,l);o.title&&(o.titleChunk=o.title),o.titleTemplate&&"%s"!==o.titleTemplate&&$({component:t,contentKeyName:"title"},o,o.titleTemplate,o.titleChunk||""),o.base&&(o.base=Object.keys(o.base).length?[o.base]:[]);var u={doEscape:function(e){return r.reduce(function(e,t){var n=t[0],r=t[1];return e.replace(n,r)},e)}};return h.forEach(function(e,t){if(0===t)a(o,e);else if(1===t)for(var n in o[e])a(o[e],n);u[e]=o[e]}),o=function e(t,r,o){var a=r.tagIDKeyName,u=o.doEscape;void 0===u&&(u=function(e){return e});var s={};for(var c in t){var f=t[c];if(T(v,c))s[c]=f;else{var l=h[0];if(o[l]&&T(o[l],c))s[c]=f;else{var d=t[a];d&&(l=h[1],o[l]&&o[l][d]&&T(o[l][d],c))?s[c]=f:"string"==typeof f?s[c]=u(f):n(f)?s[c]=f.map(function(t){return i(t)?e(t,r,o):u(t)}):i(f)?s[c]=e(f,r,o):s[c]=f}}}return s}(o,e,u)}function x(e,t,r){void 0===e&&(e={});var i=e.attribute,o=r.getAttribute(i),a=o?o.split(","):[],u=_(a),s=[];for(var c in t)if(t.hasOwnProperty(c)){var f=T(m,c)?"":n(t[c])?t[c].join(" "):t[c];r.setAttribute(c,f||""),T(a,c)||a.push(c),s.push(u.indexOf(c))}var l=u.filter(function(e,t){return!T(s,t)}).reduce(function(e,t){return r.removeAttribute(t),e+1},0);a.length===l?r.removeAttribute(i):r.setAttribute(i,a.sort().join(","))}function P(e,t,n,r,i,o){void 0===t&&(t={});var a=t.attribute,u=t.tagIDKeyName,s=_(i.querySelectorAll(n+"["+a+'="'+e+'"], '+n+"[data-"+u+"]")),c=_(o.querySelectorAll(n+"["+a+'="'+e+'"][data-body="true"], '+n+"[data-"+u+'][data-body="true"]')),f=[u,"body"],l=[];if(r.length>1){var d=[];r=r.filter(function(e){var t=JSON.stringify(e),n=!T(d,t);return d.push(t),n})}r.length&&r.forEach(function(t){var r=document.createElement(n);r.setAttribute(a,e);var i,o=!0!==t.body?s:c;for(var u in t)if(t.hasOwnProperty(u))if("innerHTML"===u)r.innerHTML=t.innerHTML;else if("cssText"===u)r.styleSheet?r.styleSheet.cssText=t.cssText:r.appendChild(document.createTextNode(t.cssText));else{var d=T(f,u)?"data-"+u:u,v=T(m,u);if(v&&!t[u])continue;var h=v?"":t[u];r.setAttribute(d,h)}o.some(function(e,t){return i=t,r.isEqualNode(e)})&&(i||0===i)?o.splice(i,1):l.push(r)});var v=s.concat(c);return v.forEach(function(e){return e.parentNode.removeChild(e)}),l.forEach(function(e){"true"===e.getAttribute("data-body")?o.appendChild(e):i.appendChild(e)}),{oldTags:v,newTags:l}}function C(e,t){return e[t]||(e[t]=document.getElementsByTagName(t)[0]),e[t]}function H(e){return void 0===e&&(e={}),function(){var t=K(e,this.$root,A),r=function(e,t,r){void 0===t&&(t={});var i=t.ssrAttribute,o={},a=C(o,"html");if("ssr"===e&&a.hasAttribute(i))return a.removeAttribute(i),!1;var u,s={},c={};for(var f in r)if(!T(v,f))if("title"!==f){if(T(p,f)){var l=f.substr(0,4);x(t,r[f],C(o,l))}else if(n(r[f])){var d=P(e,t,f,r[f],C(o,"head"),C(o,"body")),h=d.oldTags,m=d.newTags;m.length&&(s[f]=m,c[f]=h)}}else void 0!==(u=r.title)&&(document.title=u);return{addedTags:s,removedTags:c}}(this.$root._vueMeta.appId,e,t);return r&&o(t.changed)&&t.changed(t,r.addedTags,r.removedTags),{vm:this,metaInfo:t,tags:r}}}function L(e,n){void 0===n&&(n={}),e.__vuemeta_installed||(e.__vuemeta_installed=!0,n=function(e){for(var t in e=i(e)?e:{},d)e[t]||(e[t]=d[t]);return e}(n),e.prototype.$meta=function(e){void 0===e&&(e={});var t=H(e),n=function(){};return function(){return this.$root._vueMeta?{getOptions:function(){return function(e){var t={};for(var n in e)t[n]=e[n];return t}(e)},refresh:t.bind(this),inject:n,pause:g.bind(this),resume:b.bind(this)}:{getOptions:y,refresh:y,inject:y,pause:y,resume:y}}}(n),e.mixin(function(e,n){var i=["activated","deactivated","beforeMount"];return{beforeCreate:function(){var a=this;if(Object.defineProperty(this,"_hasMetaInfo",{configurable:!0,get:function(){return e.config.devtools&&!this.$root._vueMeta.hasMetaInfoDeprecationWarningShown&&(console.warn("VueMeta DeprecationWarning: _hasMetaInfo has been deprecated and will be removed in a future version. Please use hasMetaInfo(vm) instead"),this.$root._vueMeta.hasMetaInfoDeprecationWarningShown=!0),s(this)}}),!r(this.$options[n.keyName])&&null!==this.$options[n.keyName]){if(this.$root._vueMeta||(this.$root._vueMeta={appId:f},f++),!this._vueMeta){this._vueMeta=!0;for(var l=this.$parent;l&&l!==this.$root;)r(l._vueMeta)&&(l._vueMeta=!1),l=l.$parent}o(this.$options[n.keyName])&&(this.$options.computed||(this.$options.computed={}),this.$options.computed.$metaInfo=this.$options[n.keyName],this.$isServer||u(this.$options,"created",function(){a.$watch("$metaInfo",function(){t(this,"watcher")})})),r(this.$root._vueMeta.initialized)&&(this.$root._vueMeta.initialized=this.$isServer,this.$root._vueMeta.initialized||(u(this.$options,"beforeMount",function(){a.$root.$el&&a.$root.$el.hasAttribute&&a.$root.$el.hasAttribute("data-server-rendered")&&(a.$root._vueMeta.appId="ssr")}),u(this.$options,"mounted",function(){a.$root._vueMeta.initialized||(a.$root._vueMeta.initializing=!0,a.$nextTick(function(){var e=this,r=this.$root.$meta().refresh(),i=r.tags,o=r.metaInfo;!1===i&&null===this.$root._vueMeta.initialized&&this.$nextTick(function(){return t(e,"initializing")}),this.$root._vueMeta.initialized=!0,delete this.$root._vueMeta.initializing,!n.refreshOnceOnNavigation&&o.afterNavigation&&c(this)}))}),n.refreshOnceOnNavigation&&c(this))),this.$isServer||(i.forEach(function(e){u(a.$options,e,function(){return t(a,e)})}),u(this.$options,"destroyed",function(){var e=setInterval(function(){a.$el&&null!==a.$el.offsetParent||(clearInterval(e),a.$parent&&t(a,"destroyed"))},50)}))}}}}(e,n)))}return r(window)||r(window.Vue)||L(window.Vue),{version:"2.0.5",install:L,hasMetaInfo:s}});