2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-10 16:12:25 +03:00
Files
vue-meta/dist/vue-meta.esm.browser.min.js
T
2019-07-24 16:09:24 +02:00

2 lines
11 KiB
JavaScript

import t from"deepmerge";let e=null;function n(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 o(t){return Array.isArray(t)}function i(t){return void 0===t}function a(t){return"object"==typeof t}function r(t){return"object"==typeof t&&null!==t}function s(t){return"function"==typeof t}function u(t,e){return e&&a(t)?(o(t[e])||(t[e]=[]),t):o(t)?t:[]}function c(t,e,n){u(t,e),t[e].push(n)}function l(t=this){return t&&(!0===t._vueMeta||a(t._vueMeta))}function d(t){if(t.$root._vueMeta.navGuards||!t.$root.$router)return;t.$root._vueMeta.navGuards=!0;const e=t.$root.$router,n=t.$root.$meta();e.beforeEach((t,e,o)=>{n.pause(),o()}),e.afterEach(()=>{const{metaInfo:t}=n.resume();t&&t.afterNavigation&&s(t.afterNavigation)&&t.afterNavigation(t)})}const f=function(){try{return!i(window)}catch(t){return!1}}()?window:global,h=f.console=f.console||{};function p(...t){h&&h.warn&&h.warn(...t)}const m=()=>p("This vue app/component has no vue-meta configuration");let v=1;const y={title:void 0,titleChunk:"",titleTemplate:"%s",htmlAttrs:{},bodyAttrs:{},headAttrs:{},base:[],link:[],meta:[],style:[],script:[],noscript:[],__dangerouslyDisableSanitizers:[],__dangerouslyDisableSanitizersByTagID:{}},$={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"],M=["link","style","script"],T=["body","pbody"],N=["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 I(t=!0){return this.$root._vueMeta.paused=!0,()=>A(t)}function A(t=!0){if(this.$root._vueMeta.paused=!1,t)return this.$root.$meta().refresh()}function w({component:t,metaTemplateKeyName:e,contentKeyName:n},o,a,r){return i(a)&&(a=o[e],delete o[e]),!!a&&(i(r)&&(r=o[n]),o[n]=s(a)?a.call(t,r):a.replace(/%s/g,r),!0)}function K(t,e){return t.findIndex(e,arguments[2])}function k(t){return Array.from(t)}function z(t,e){return t.includes(e)}const D=[[/&/g,"&"],[/</g,"<"],[/>/g,">"],[/"/g,'"'],[/'/g,"'"]];function O(e,n,o={}){return n.hasOwnProperty("title")&&void 0===n.title&&delete n.title,_.forEach(t=>{if(n[t])for(const e in n[t])n[t].hasOwnProperty(e)&&void 0===n[t][e]&&(z(N,e)&&p("VueMeta: Please note that since v2 the value undefined is not used to indicate boolean attributes anymore, see migration guide for details"),delete n[t][e])}),t(e,n,{arrayMerge:(t,e)=>(function({component:t,tagIDKeyName:e,metaTemplateKeyName:n,contentKeyName:o},i,a){const r=[];return i.forEach((i,s)=>{if(!i[e])return void r.push(i);const u=K(a,t=>t[e]===i[e]),c=a[u];if(-1===u)return void r.push(i);if(c.hasOwnProperty(o)&&void 0===c[o]||c.hasOwnProperty("innerHTML")&&void 0===c.innerHTML)return r.push(i),void a.splice(u,1);if(null===c[o]||null===c.innerHTML)return void a.splice(u,1);const l=i[n];l&&(c[n]?c[o]||w({component:t,metaTemplateKeyName:n,contentKeyName:o},c,void 0,i[o]):w({component:t,metaTemplateKeyName:n,contentKeyName:o},c,l))}),r.concat(a)})(o,t,e)})}function S(t={},e,n={}){const{keyName:o,metaTemplateKeyName:r,tagIDKeyName:u}=t,{$options:c,$children:l}=e;if(e._inactive)return n;if(c[o]){let i=c[o];if(s(i)&&(i=i.call(e)),!a(i))return n;n=O(n,i,t)}return l.length&&l.forEach(e=>{(function(t=this){return t&&!i(t._vueMeta)})(e)&&(n=S(t,e,n))}),r&&n.meta&&(n.meta.forEach(e=>w(t,e)),n.meta=n.meta.filter((t,e,n)=>!t.hasOwnProperty(u)||e===K(n,e=>e[u]===t[u]))),n}function E(t={},e,n=[]){let i=S(t,e,y);i.title&&(i.titleChunk=i.title),i.titleTemplate&&"%s"!==i.titleTemplate&&w({component:e,contentKeyName:"title"},i,i.titleTemplate,i.titleChunk||""),i.base&&(i.base=Object.keys(i.base).length?[i.base]:[]);const a={doEscape:t=>n.reduce((t,[e,n])=>t.replace(e,n),t)};return g.forEach((t,e)=>{if(0===e)u(i,t);else if(1===e)for(const e in i[t])u(i[t],e);a[t]=i[t]}),i=function t(e,n,i){const{tagIDKeyName:a}=n,{doEscape:s=(t=>t),escapeKeys:u}=i,c={};for(const l in e){const d=e[l];if(z(b,l)){c[l]=d;continue}let[f]=g;if(i[f]&&z(i[f],l)){c[l]=d;continue}const h=e[a];if(h&&(f=g[1],i[f]&&i[f][h]&&z(i[f][h],l)))c[l]=d;else if("string"==typeof d?c[l]=s(d):o(d)?c[l]=d.map(e=>r(e)?t(e,n,{...i,escapeKeys:!0}):s(e)):r(d)?c[l]=t(d,n,{...i,escapeKeys:!0}):c[l]=d,u){const t=s(l);l!==t&&(c[t]=c[l],delete c[l])}}return c}(i,t,a)}function j(t,e){return t[e]||(t[e]=document.getElementsByTagName(e)[0]),t[e]}function x({body:t,pbody:e}){return t?"body":e?"pbody":"head"}function P(t,{appId:e,attribute:n,type:o,tagIDKeyName:i},a={}){const r=[`${o}[${n}="${e}"]`,`${o}[data-${i}]`].map(t=>{for(const e in a){const n=a[e];t+=`[data-${e}${n&&!0!==n?`="${n}"`:""}]`}return t});return k(t.querySelectorAll(r.join(", ")))}const C=[];function L(t,e){1===arguments.length&&(e=t,t=""),C.push([t,e])}function H({tagIDKeyName:t},e,n,o){let i=!1;for(const o of n)o[t]&&o.callback&&(i=!0,L(`${e}[data-${t}="${o[t]}"]`,o.callback));return o&&i?B():i}function B(){!function(t=document){return"complete"===t.readyState}()?document.onreadystatechange=()=>{q()}:q()}function q(t){for(const[e,n]of C){const o=`${e}[onload="this.__vm_l=1"]`;let i=[];t||(i=k(document.querySelectorAll(o))),t&&t.matches(o)&&(i=[t]);for(const t of i){if(t.__vm_cb)continue;const e=()=>{t.__vm_cb=!0,t.removeAttribute("onload"),n(t)};t.__vm_l?e():t.__vm_ev||(t.__vm_ev=!0,t.addEventListener("load",e))}}}function V({attribute:t}={},e,n){const i=n.getAttribute(t),a=i?i.split(","):[],r=k(a),s=[];for(const t in e)if(e.hasOwnProperty(t)){const i=z(N,t)?"":o(e[t])?e[t].join(" "):e[t];n.setAttribute(t,i||""),z(a,t)||a.push(t),s.push(r.indexOf(t))}const u=r.filter((t,e)=>!z(s,e)).reduce((t,e)=>(n.removeAttribute(e),t+1),0);a.length===u?n.removeAttribute(t):n.setAttribute(t,a.sort().join(","))}function W(t,e={},n,o,i,a){const{attribute:r,tagIDKeyName:s}=e,u=[s,...T],c=[],l={appId:t,attribute:r,type:n,tagIDKeyName:s},d={head:P(i,l),pbody:P(a,l,{pbody:!0}),body:P(a,l,{body:!0})};if(o.length>1){const t=[];o=o.filter(e=>{const n=JSON.stringify(e),o=!z(t,n);return t.push(n),o})}if(o.length)for(const e of o){if(e.skip)continue;const o=document.createElement(n);o.setAttribute(r,t);for(const t in e){if(!e.hasOwnProperty(t))continue;if("innerHTML"===t){o.innerHTML=e.innerHTML;continue}if("json"===t){o.innerHTML=JSON.stringify(e.json);continue}if("cssText"===t){o.styleSheet?o.styleSheet.cssText=e.cssText:o.appendChild(document.createTextNode(e.cssText));continue}if("callback"===t){o.onload=()=>e[t](o);continue}const n=z(u,t)?`data-${t}`:t,i=z(N,t);if(i&&!e[t])continue;const a=i?"":e[t];o.setAttribute(n,a)}const i=d[x(e)];let a;i.some((t,e)=>(a=e,o.isEqualNode(t)))&&(a||0===a)?i.splice(a,1):c.push(o)}let f=[];for(const t of Object.values(d))f=[...f,...t];for(const t of f)t.parentNode.removeChild(t);for(const t of c)t.hasAttribute("data-body")?a.appendChild(t):t.hasAttribute("data-pbody")?a.insertBefore(t,a.firstChild):i.appendChild(t);return{oldTags:f,newTags:c}}function G(t={}){return function(){const e=E(t,this.$root,D),n=function(t,e={},n){const{ssrAttribute:i,ssrAppId:a}=e,r={},s=j(r,"html");if(t===a&&s.hasAttribute(i)){s.removeAttribute(i);let t=!1;for(const o of M)n[o]&&H(e,o,n[o])&&(t=!0);return t&&B(),!1}const u={},c={};for(const i in n){if(z(b,i))continue;if("title"===i){((l=n.title)||""===l)&&(document.title=l);continue}if(z(_,i)){const t=i.substr(0,4);V(e,n[i],j(r,t));continue}if(!o(n[i]))continue;const{oldTags:a,newTags:s}=W(t,e,i,n[i],j(r,"head"),j(r,"body"));s.length&&(u[i]=s,c[i]=a)}var l;return{addedTags:u,removedTags:c}}(this.$root._vueMeta.appId,t,e);return n&&s(e.changed)&&e.changed(e,n.addedTags,n.removedTags),{vm:this,metaInfo:e,tags:n}}}function J(t,e={}){t.__vuemeta_installed||(t.__vuemeta_installed=!0,e=function(t){t=a(t)?t:{};for(const e in $)t[e]||(t[e]=$[e]);return t}(e),t.prototype.$meta=function(t={}){const e=G(t),n=()=>{};return function(){return this.$root._vueMeta?{getOptions:()=>(function(t){const e={};for(const n in t)e[n]=t[n];return e})(t),refresh:e.bind(this),inject:n,pause:I.bind(this),resume:A.bind(this)}:{getOptions:m,refresh:m,inject:m,pause:m,resume:m}}}(e),t.mixin(function(t,e){const o=["activated","deactivated","beforeMount"];return{beforeCreate(){if(Object.defineProperty(this,"_hasMetaInfo",{configurable:!0,get(){return t.config.devtools&&!this.$root._vueMeta.hasMetaInfoDeprecationWarningShown&&(p("VueMeta DeprecationWarning: _hasMetaInfo has been deprecated and will be removed in a future version. Please use hasMetaInfo(vm) instead"),this.$root._vueMeta.hasMetaInfoDeprecationWarningShown=!0),l(this)}}),!i(this.$options[e.keyName])&&null!==this.$options[e.keyName]){if(this.$root._vueMeta||(this.$root._vueMeta={appId:v},v++),!this._vueMeta){this._vueMeta=!0;let t=this.$parent;for(;t&&t!==this.$root;)i(t._vueMeta)&&(t._vueMeta=!1),t=t.$parent}s(this.$options[e.keyName])&&(this.$options.computed||(this.$options.computed={}),this.$options.computed.$metaInfo=this.$options[e.keyName],this.$isServer||c(this.$options,"created",()=>{this.$watch("$metaInfo",function(){n(this,"watcher")})})),i(this.$root._vueMeta.initialized)&&(this.$root._vueMeta.initialized=this.$isServer,this.$root._vueMeta.initialized||(c(this.$options,"beforeMount",()=>{this.$root.$el&&this.$root.$el.hasAttribute&&this.$root.$el.hasAttribute("data-server-rendered")&&(this.$root._vueMeta.appId=e.ssrAppId)}),c(this.$options,"mounted",()=>{this.$root._vueMeta.initialized||(this.$root._vueMeta.initializing=!0,this.$nextTick(function(){const{tags:t,metaInfo:o}=this.$root.$meta().refresh();!1===t&&null===this.$root._vueMeta.initialized&&this.$nextTick(()=>n(this,"initializing")),this.$root._vueMeta.initialized=!0,delete this.$root._vueMeta.initializing,!e.refreshOnceOnNavigation&&o.afterNavigation&&d(this)}))}),e.refreshOnceOnNavigation&&d(this))),this.$isServer||(o.forEach(t=>{c(this.$options,t,()=>n(this,t))}),c(this.$options,"destroyed",()=>{const t=setInterval(()=>{this.$el&&null!==this.$el.offsetParent||(clearInterval(t),this.$parent&&n(this,"destroyed"))},50)}))}}}}(t,e)))}i(window)||i(window.Vue)||J(window.Vue);var F={version:"2.1.0",install:J,hasMetaInfo:l};export default F;