mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-09 21:12:26 +03:00
2 lines
12 KiB
JavaScript
2 lines
12 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";let 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=10){clearTimeout(e),e=setTimeout(()=>{t()},n)}(()=>t.$meta().refresh())}function n(e){return Array.isArray(e)}function o(e){return void 0===e}function r(e){return"object"==typeof e}function i(e){return"object"==typeof e&&null!==e}function a(e){return"function"==typeof e}function s(e,t){return t&&r(e)?(n(e[t])||(e[t]=[]),e):n(e)?e:[]}function u(e,t,n){s(e,t),e[t].push(n)}function c(e=this){return e&&(!0===e._vueMeta||r(e._vueMeta))}function l(e){if(e.$root._vueMeta.navGuards||!e.$root.$router)return;e.$root._vueMeta.navGuards=!0;const t=e.$root.$router,n=e.$root.$meta();t.beforeEach((e,t,o)=>{n.pause(),o()}),t.afterEach(()=>{const{metaInfo:e}=n.resume();e&&e.afterNavigation&&a(e.afterNavigation)&&e.afterNavigation(e)})}const f=function(){try{return!o(window)}catch(e){return!1}}()?window:global,d=f.console=f.console||{};function h(...e){d&&d.warn&&d.warn(...e)}const p=()=>h("This vue app/component has no vue-meta configuration");let m=1;const y={title:void 0,titleChunk:"",titleTemplate:"%s",htmlAttrs:{},bodyAttrs:{},headAttrs:{},base:[],link:[],meta:[],style:[],script:[],noscript:[],__dangerouslyDisableSanitizers:[],__dangerouslyDisableSanitizersByTagID:{}},v={keyName:"metaInfo",attribute:"data-vue-meta",ssrAttribute:"data-vue-meta-server-rendered",tagIDKeyName:"vmid",contentKeyName:"content",metaTemplateKeyName:"template",ssrAppId:"ssr"},b=["titleChunk","titleTemplate","changed","__dangerouslyDisableSanitizers","__dangerouslyDisableSanitizersByTagID"],g=["__dangerouslyDisableSanitizers","__dangerouslyDisableSanitizersByTagID"],$=["htmlAttrs","headAttrs","bodyAttrs"],_=["link","style","script"],M=["body","pbody"],A=["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"];function T(e=!0){return this.$root._vueMeta.paused=!0,()=>N(e)}function N(e=!0){if(this.$root._vueMeta.paused=!1,e)return this.$root.$meta().refresh()}function I({component:e,metaTemplateKeyName:t,contentKeyName:n},r,i,s){return o(i)&&(i=r[t],delete r[t]),!!i&&(o(s)&&(s=r[n]),r[n]=a(i)?i.call(e,s):i.replace(/%s/g,s),!0)}function w(e,t){if(!Array.prototype.findIndex){for(let n=0;n<e.length;n++)if(t.call(arguments[2],e[n],n,e))return n;return-1}return e.findIndex(t,arguments[2])}function O(e){return Array.from?Array.from(e):Array.prototype.slice.call(e)}function j(e,t){if(!Array.prototype.includes){for(const n in e)if(e[n]===t)return!0;return!1}return e.includes(t)}const S=[[/&/g,"&"],[/</g,"<"],[/>/g,">"],[/"/g,'"'],[/'/g,"'"]];var K=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===k}(e)}(e)};var k="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function D(e,t){return!1!==t.clone&&t.isMergeableObject(e)?P((n=e,Array.isArray(n)?[]:{}),e,t):e;var n}function z(e,t,n){return e.concat(t).map(function(e){return D(e,n)})}function E(e){return Object.keys(e).concat(function(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(t){return e.propertyIsEnumerable(t)}):[]}(e))}function x(e,t,n){var o={};return n.isMergeableObject(e)&&E(e).forEach(function(t){o[t]=D(e[t],n)}),E(t).forEach(function(r){n.isMergeableObject(t[r])&&e[r]?o[r]=function(e,t){if(!t.customMerge)return P;var n=t.customMerge(e);return"function"==typeof n?n:P}(r,n)(e[r],t[r],n):o[r]=D(t[r],n)}),o}function P(e,t,n){(n=n||{}).arrayMerge=n.arrayMerge||z,n.isMergeableObject=n.isMergeableObject||K;var o=Array.isArray(t);return o===Array.isArray(e)?o?n.arrayMerge(e,t,n):x(e,t,n):D(t,n)}P.all=function(e,t){if(!Array.isArray(e))throw new Error("first argument should be an array");return e.reduce(function(e,n){return P(e,n,t)},{})};var C=P;function L(e,t,n={}){return t.hasOwnProperty("title")&&void 0===t.title&&delete t.title,$.forEach(e=>{if(t[e])for(const n in t[e])t[e].hasOwnProperty(n)&&void 0===t[e][n]&&(j(A,n)&&h("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])}),C(e,t,{arrayMerge:(e,t)=>(function({component:e,tagIDKeyName:t,metaTemplateKeyName:n,contentKeyName:o},r,i){const a=[];return r.forEach((r,s)=>{if(!r[t])return void a.push(r);const u=w(i,e=>e[t]===r[t]),c=i[u];if(-1===u)return void a.push(r);if(c.hasOwnProperty(o)&&void 0===c[o]||c.hasOwnProperty("innerHTML")&&void 0===c.innerHTML)return a.push(r),void i.splice(u,1);if(null===c[o]||null===c.innerHTML)return void i.splice(u,1);const l=r[n];l&&(c[n]?c[o]||I({component:e,metaTemplateKeyName:n,contentKeyName:o},c,void 0,r[o]):I({component:e,metaTemplateKeyName:n,contentKeyName:o},c,l))}),a.concat(i)})(n,e,t)})}function H(e={},t,n={}){const{keyName:i,metaTemplateKeyName:s,tagIDKeyName:u}=e,{$options:c,$children:l}=t;if(t._inactive)return n;if(c[i]){let o=c[i];if(a(o)&&(o=o.call(t)),!r(o))return n;n=L(n,o,e)}return l.length&&l.forEach(t=>{(function(e=this){return e&&!o(e._vueMeta)})(t)&&(n=H(e,t,n))}),s&&n.meta&&(n.meta.forEach(t=>I(e,t)),n.meta=n.meta.filter((e,t,n)=>!e.hasOwnProperty(u)||t===w(n,t=>t[u]===e[u]))),n}function B(e={},t,o=[]){let r=H(e,t,y);r.title&&(r.titleChunk=r.title),r.titleTemplate&&"%s"!==r.titleTemplate&&I({component:t,contentKeyName:"title"},r,r.titleTemplate,r.titleChunk||""),r.base&&(r.base=Object.keys(r.base).length?[r.base]:[]);const a={doEscape:e=>o.reduce((e,[t,n])=>e.replace(t,n),e)};return g.forEach((e,t)=>{if(0===t)s(r,e);else if(1===t)for(const t in r[e])s(r[e],t);a[e]=r[e]}),r=function e(t,o,r){const{tagIDKeyName:a}=o,{doEscape:s=(e=>e),escapeKeys:u}=r,c={};for(const l in t){const f=t[l];if(j(b,l)){c[l]=f;continue}let[d]=g;if(r[d]&&j(r[d],l)){c[l]=f;continue}const h=t[a];if(h&&(d=g[1],r[d]&&r[d][h]&&j(r[d][h],l)))c[l]=f;else if("string"==typeof f?c[l]=s(f):n(f)?c[l]=f.map(t=>i(t)?e(t,o,{...r,escapeKeys:!0}):s(t)):i(f)?c[l]=e(f,o,{...r,escapeKeys:!0}):c[l]=f,u){const e=s(l);l!==e&&(c[e]=c[l],delete c[l])}}return c}(r,e,a)}function V(e,t){return e[t]||(e[t]=document.getElementsByTagName(t)[0]),e[t]}function q({body:e,pbody:t}){return e?"body":t?"pbody":"head"}function W(e,{appId:t,attribute:n,type:o,tagIDKeyName:r},i={}){const a=[`${o}[${n}="${t}"]`,`${o}[data-${r}]`].map(e=>{for(const t in i){const n=i[t];e+=`[data-${t}${n&&!0!==n?`="${n}"`:""}]`}return e});return O(e.querySelectorAll(a.join(", ")))}const G=[];function J(e,t){1===arguments.length&&(t=e,e=""),G.push([e,t])}function R({tagIDKeyName:e},t,n,o){let r=!1;for(const o of n)o[e]&&o.callback&&(r=!0,J(`${t}[data-${e}="${o[e]}"]`,o.callback));return o&&r?F():r}function F(){!function(e=document){return"complete"===e.readyState}()?document.onreadystatechange=()=>{Q()}:Q()}function Q(e){for(const[t,n]of G){const o=`${t}[onload="this.__vm_l=1"]`;let r=[];e||(r=O(document.querySelectorAll(o))),e&&e.matches(o)&&(r=[e]);for(const e of r){if(e.__vm_cb)continue;const t=()=>{e.__vm_cb=!0,e.removeAttribute("onload"),n(e)};e.__vm_l?t():e.__vm_ev||(e.__vm_ev=!0,e.addEventListener("load",t))}}}function U({attribute:e}={},t,o){const r=o.getAttribute(e),i=r?r.split(","):[],a=O(i),s=[];for(const e in t)if(t.hasOwnProperty(e)){const r=j(A,e)?"":n(t[e])?t[e].join(" "):t[e];o.setAttribute(e,r||""),j(i,e)||i.push(e),s.push(a.indexOf(e))}const u=a.filter((e,t)=>!j(s,t)).reduce((e,t)=>(o.removeAttribute(t),e+1),0);i.length===u?o.removeAttribute(e):o.setAttribute(e,i.sort().join(","))}function X(e,t={},n,o,r,i){const{attribute:a,tagIDKeyName:s}=t,u=[s,...M],c=[],l={appId:e,attribute:a,type:n,tagIDKeyName:s},f={head:W(r,l),pbody:W(i,l,{pbody:!0}),body:W(i,l,{body:!0})};if(o.length>1){const e=[];o=o.filter(t=>{const n=JSON.stringify(t),o=!j(e,n);return e.push(n),o})}if(o.length)for(const t of o){if(t.skip)continue;const o=document.createElement(n);o.setAttribute(a,e);for(const e in t){if(!t.hasOwnProperty(e))continue;if("innerHTML"===e){o.innerHTML=t.innerHTML;continue}if("json"===e){o.innerHTML=JSON.stringify(t.json);continue}if("cssText"===e){o.styleSheet?o.styleSheet.cssText=t.cssText:o.appendChild(document.createTextNode(t.cssText));continue}if("callback"===e){o.onload=()=>t[e](o);continue}const n=j(u,e)?`data-${e}`:e,r=j(A,e);if(r&&!t[e])continue;const i=r?"":t[e];o.setAttribute(n,i)}const r=f[q(t)];let i;r.some((e,t)=>(i=t,o.isEqualNode(e)))&&(i||0===i)?r.splice(i,1):c.push(o)}let d=[];for(const e of Object.values(f))d=[...d,...e];for(const e of d)e.parentNode.removeChild(e);for(const e of c)e.hasAttribute("data-body")?i.appendChild(e):e.hasAttribute("data-pbody")?i.insertBefore(e,i.firstChild):r.appendChild(e);return{oldTags:d,newTags:c}}function Y(e={}){return function(){const t=B(e,this.$root,S),o=function(e,t={},o){const{ssrAttribute:r,ssrAppId:i}=t,a={},s=V(a,"html");if(e===i&&s.hasAttribute(r)){s.removeAttribute(r);let e=!1;for(const n of _)o[n]&&R(t,n,o[n])&&(e=!0);return e&&F(),!1}const u={},c={};for(const r in o){if(j(b,r))continue;if("title"===r){((l=o.title)||""===l)&&(document.title=l);continue}if(j($,r)){const e=r.substr(0,4);U(t,o[r],V(a,e));continue}if(!n(o[r]))continue;const{oldTags:i,newTags:s}=X(e,t,r,o[r],V(a,"head"),V(a,"body"));s.length&&(u[r]=s,c[r]=i)}var l;return{addedTags:u,removedTags:c}}(this.$root._vueMeta.appId,e,t);return o&&a(t.changed)&&t.changed(t,o.addedTags,o.removedTags),{vm:this,metaInfo:t,tags:o}}}function Z(e,n={}){e.__vuemeta_installed||(e.__vuemeta_installed=!0,n=function(e){e=r(e)?e:{};for(const t in v)e[t]||(e[t]=v[t]);return e}(n),e.prototype.$meta=function(e={}){const t=Y(e),n=()=>{};return function(){return this.$root._vueMeta?{getOptions:()=>(function(e){const t={};for(const n in e)t[n]=e[n];return t})(e),refresh:t.bind(this),inject:n,pause:T.bind(this),resume:N.bind(this)}:{getOptions:p,refresh:p,inject:p,pause:p,resume:p}}}(n),e.mixin(function(e,n){const r=["activated","deactivated","beforeMount"];return{beforeCreate(){if(Object.defineProperty(this,"_hasMetaInfo",{configurable:!0,get(){return e.config.devtools&&!this.$root._vueMeta.hasMetaInfoDeprecationWarningShown&&(h("VueMeta DeprecationWarning: _hasMetaInfo has been deprecated and will be removed in a future version. Please use hasMetaInfo(vm) instead"),this.$root._vueMeta.hasMetaInfoDeprecationWarningShown=!0),c(this)}}),!o(this.$options[n.keyName])&&null!==this.$options[n.keyName]){if(this.$root._vueMeta||(this.$root._vueMeta={appId:m},m++),!this._vueMeta){this._vueMeta=!0;let e=this.$parent;for(;e&&e!==this.$root;)o(e._vueMeta)&&(e._vueMeta=!1),e=e.$parent}a(this.$options[n.keyName])&&(this.$options.computed||(this.$options.computed={}),this.$options.computed.$metaInfo=this.$options[n.keyName],this.$isServer||u(this.$options,"created",()=>{this.$watch("$metaInfo",function(){t(this,"watcher")})})),o(this.$root._vueMeta.initialized)&&(this.$root._vueMeta.initialized=this.$isServer,this.$root._vueMeta.initialized||(u(this.$options,"beforeMount",()=>{this.$root.$el&&this.$root.$el.hasAttribute&&this.$root.$el.hasAttribute("data-server-rendered")&&(this.$root._vueMeta.appId=n.ssrAppId)}),u(this.$options,"mounted",()=>{this.$root._vueMeta.initialized||(this.$root._vueMeta.initializing=!0,this.$nextTick(function(){const{tags:e,metaInfo:o}=this.$root.$meta().refresh();!1===e&&null===this.$root._vueMeta.initialized&&this.$nextTick(()=>t(this,"initializing")),this.$root._vueMeta.initialized=!0,delete this.$root._vueMeta.initializing,!n.refreshOnceOnNavigation&&o.afterNavigation&&l(this)}))}),n.refreshOnceOnNavigation&&l(this))),this.$isServer||(r.forEach(e=>{u(this.$options,e,()=>t(this,e))}),u(this.$options,"destroyed",()=>{const e=setInterval(()=>{this.$el&&null!==this.$el.offsetParent||(clearInterval(e),this.$parent&&t(this,"destroyed"))},50)}))}}}}(e,n)))}return o(window)||o(window.Vue)||Z(window.Vue),{version:"2.1.0",install:Z,hasMetaInfo:c}});
|