juejin-vue-meta
Version:
manage page meta info in Vue 2.0 server-rendered apps
11 lines • 9.19 kB
JavaScript
/**
* vue-meta v1.0.4
* (c) 2017 Declan de Wet
* @license MIT
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.VueMeta=e()}(this,function(){"use strict";function t(t){if(null===t||void 0===t)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(t)}function e(){try{if(!Object.assign)return!1;var t=new String("abc");if(t[5]="de","5"===Object.getOwnPropertyNames(t)[0])return!1;for(var e={},n=0;n<10;n++)e["_"+String.fromCharCode(n)]=n;var r=Object.getOwnPropertyNames(e).map(function(t){return e[t]});if("0123456789"!==r.join(""))return!1;var i={};return"abcdefghijklmnopqrst".split("").forEach(function(t){i[t]=t}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},i)).join("")}catch(t){return!1}}function n(t,e){return e={exports:{}},t(e,e.exports),e.exports}function r(t){var e=!1;if(null!=t&&"function"!=typeof t.toString)try{e=!!(t+"")}catch(t){}return e}function i(t,e){return function(n){return t(e(n))}}function o(t){return!!t&&"object"==typeof t}function a(t){if(!o(t)||P.call(t)!=E||r(t))return!1;var e=D(t);if(null===e)return!0;var n=I.call(e,"constructor")&&e.constructor;return"function"==typeof n&&n instanceof n&&C.call(n)==_}function u(t){return Array.isArray?Array.isArray(t):"[object Array]"===Object.prototype.toString.call(t)}function c(t,e){void 0===e&&(e={});var n=t.component,r=t.option,i=t.deep,o=t.arrayMerge,a=n.$options;if("undefined"!=typeof a[r]&&null!==a[r]){var u=a[r];"function"==typeof u&&(u=u.call(n)),e="object"==typeof u?$(e,u,{clone:!0,arrayMerge:o}):u}return i&&n.$children.length&&n.$children.forEach(function(t){e=c({component:t,option:r,deep:i,arrayMerge:o},e)}),e}function f(t){void 0===t&&(t={});var e=t.keyName,n=t.tagIDKeyName;return function(t){var r={title:"",titleChunk:"",titleTemplate:"%s",htmlAttrs:{},bodyAttrs:{},meta:[],base:[],link:[],style:[],script:[],noscript:[],__dangerouslyDisableSanitizers:[]},i=c({component:t,option:e,deep:!0,arrayMerge:function(t,e){var r=[];for(var i in t){var o=t[i],a=!1;for(var u in e){var c=e[u];if(o[n]&&o[n]===c[n]){a=!0;break}}a||r.push(o)}return r.concat(e)}});i.title=i.title||"",i.title&&(i.titleChunk=i.title),i.titleTemplate&&i.titleChunk&&(i.title=i.titleTemplate.replace(/%s/g,i.titleChunk)),i.base&&(i.base=Object.keys(i.base).length?[i.base]:[]);var o=function(t){return Object.keys(t).reduce(function(e,n){var r=t.__dangerouslyDisableSanitizers,i=r&&r.indexOf(n)>-1,a=t[n];return e[n]=a,"__dangerouslyDisableSanitizers"===n?e:(i?e[n]=a:"string"==typeof a?e[n]=H(a):q(a)?e[n]=o(a):u(a)?e[n]=a.map(o):e[n]=a,e)},{})};return i=$(r,i),i=o(i)}}function s(t){void 0===t&&(t={});var e=t.attribute;return function(t,n){return{text:function(){return"<"+t+" "+e+'="true">'+n+"</"+t+">"}}}}function l(t){void 0===t&&(t={});var e=t.attribute;return function(t,n){return{text:function(){var t="",r=[];for(var i in n)n.hasOwnProperty(i)&&(r.push(i),t+=("undefined"!=typeof n[i]?i+'="'+n[i]+'"':i)+" ");return t+=e+'="'+r.join(",")+'"',t.trim()}}}}function p(t){void 0===t&&(t={});var e=t.attribute;return function(n,r){return{text:function(){return r.reduce(function(r,i){var o=Object.keys(i).reduce(function(e,n){switch(n){case"innerHTML":case"cssText":case"once":return e;default:return n===t.tagIDKeyName?e+" data-"+n+'="'+i[n]+'"':"undefined"==typeof i[n]?e+" "+n:e+" "+n+'="'+i[n]+'"'}},"").trim(),a=i.innerHTML||i.cssText||"",u=["noscript","script","style"].indexOf(n)===-1,c=i.once?"":e+'="true" ';return u?r+"<"+n+" "+c+o+"/>":r+"<"+n+" "+c+o+">"+a+"</"+n+">"},"")}}}}function d(t){return void 0===t&&(t={}),function(e,n){switch(e){case"title":return s(t)(e,n);case"htmlAttrs":case"bodyAttrs":return l(t)(e,n);default:return p(t)(e,n)}}}function y(t){return void 0===t&&(t={}),function(){var e=f(t)(this.$root);for(var n in e)e.hasOwnProperty(n)&&"titleTemplate"!==n&&"titleChunk"!==n&&(e[n]=d(t)(n,e[n]));return e}}function h(){return function(t){void 0===t&&(t=document.title),document.title=t}}function v(t){void 0===t&&(t={});var e=t.attribute;return function(t,n){var r=n.getAttribute(e),i=r?r.split(","):[],o=[].concat(i);for(var a in t)if(t.hasOwnProperty(a)){var u=t[a]||"";n.setAttribute(a,u),i.indexOf(a)===-1&&i.push(a);var c=o.indexOf(a);c!==-1&&o.splice(c,1)}for(var f=o.length-1;f>=0;f--)n.removeAttribute(o[f]);i.length===o.length?n.removeAttribute(e):n.setAttribute(e,i.join(","))}}function b(t){void 0===t&&(t={});var e=t.attribute;return function(n,r,i){var o,a=i.querySelectorAll(n+"["+e+"]"),u=L(a),c=[];if(r.length>1){var f=[];r=r.map(function(t){var e=JSON.stringify(t);if(f.indexOf(e)<0)return f.push(e),t}).filter(function(t){return t})}return r&&r.length&&r.forEach(function(r){var i=document.createElement(n);for(var a in r)if(r.hasOwnProperty(a))if("innerHTML"===a)i.innerHTML=r.innerHTML;else if("cssText"===a)i.styleSheet?i.styleSheet.cssText=r.cssText:i.appendChild(document.createTextNode(r.cssText));else if(a===t.tagIDKeyName){var f="data-"+a,s="undefined"==typeof r[a]?"":r[a];i.setAttribute(f,s)}else{var l="undefined"==typeof r[a]?"":r[a];i.setAttribute(a,l)}i.setAttribute(e,"true"),u.some(function(t,e){return o=e,i.isEqualNode(t)})?u.splice(o,1):c.push(i)}),u.forEach(function(t){return t.parentNode.removeChild(t)}),c.forEach(function(t){return i.appendChild(t)}),{oldTags:u,newTags:c}}}function g(t){void 0===t&&(t={});var e=t.ssrAttribute;return function(n){var r=document.getElementsByTagName("html")[0];if(null===r.getAttribute(e)){var i={},o={};Object.keys(n).forEach(function(e){switch(e){case"title":h(t)(n.title);break;case"htmlAttrs":case"bodyAttrs":v(t)(n[e],"htmlAttrs"===e?r:document.getElementsByTagName("body")[0]);break;case"titleChunk":case"titleTemplate":case"changed":case"__dangerouslyDisableSanitizers":break;default:var a=b(t)(e,n[e],document.getElementsByTagName("head")[0]),u=a.oldTags,c=a.newTags;c.length&&(i[e]=c,o[e]=u)}}),"function"==typeof n.changed&&n.changed(n,i,o)}else r.removeAttribute(e)}}function m(t){return void 0===t&&(t={}),function(){var e=f(t)(this.$root);return g(t)(e),e}}function O(t){return void 0===t&&(t={}),function(){return{inject:y(t).bind(this),refresh:m(t).bind(this)}}}function j(t,e){return z(t),K(function(){t=null,e()})}function A(t,e){void 0===e&&(e={});var n={keyName:B,attribute:V,ssrAttribute:F,tagIDKeyName:J};e=x(n,e),t.prototype.$meta=O(e);var r=null;t.mixin({beforeCreate:function(){"undefined"!=typeof this.$options[e.keyName]&&(this._hasMetaInfo=!0),"function"==typeof this.$options[e.keyName]&&("undefined"==typeof this.$options.computed&&(this.$options.computed={}),this.$options.computed.$metaInfo=this.$options[e.keyName])},created:function(){var t=this;this.$metaInfo&&this.$watch("$metaInfo",function(){r=j(r,function(){return t.$meta().refresh()})})},beforeMount:function(){var t=this;this._hasMetaInfo&&(r=j(r,function(){return t.$meta().refresh()}))},destroyed:function(){var t=this;this.$isServer||this._hasMetaInfo&&(r=j(r,function(){return t.$meta().refresh()}))}})}/*
object-assign
(c) Sindre Sorhus
@license MIT
*/
var w=Object.getOwnPropertySymbols,T=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable,x=e()?Object.assign:function(e,n){for(var r,i,o=arguments,a=t(e),u=1;u<arguments.length;u++){r=Object(o[u]);for(var c in r)T.call(r,c)&&(a[c]=r[c]);if(w){i=w(r);for(var f=0;f<i.length;f++)k.call(r,i[f])&&(a[i[f]]=r[i[f]])}}return a},S="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},$=n(function(t,e){!function(e,n){t.exports=n()}(S,function(){function t(t){var e=t&&"object"==typeof t;return e&&"[object RegExp]"!==Object.prototype.toString.call(t)&&"[object Date]"!==Object.prototype.toString.call(t)}function e(t){return Array.isArray(t)?[]:{}}function n(n,r){var i=r&&r.clone===!0;return i&&t(n)?o(e(n),n,r):n}function r(e,r,i){var a=e.slice();return r.forEach(function(r,u){"undefined"==typeof a[u]?a[u]=n(r,i):t(r)?a[u]=o(e[u],r,i):e.indexOf(r)===-1&&a.push(n(r,i))}),a}function i(e,r,i){var a={};return t(e)&&Object.keys(e).forEach(function(t){a[t]=n(e[t],i)}),Object.keys(r).forEach(function(u){t(r[u])&&e[u]?a[u]=o(e[u],r[u],i):a[u]=n(r[u],i)}),a}function o(t,e,o){var a=Array.isArray(e),u=o||{arrayMerge:r},c=u.arrayMerge||r;return a?Array.isArray(t)?c(t,e,o):n(e,o):i(t,e,o)}return o.all=function(t,e){if(!Array.isArray(t)||t.length<2)throw new Error("first argument should be an array with at least two elements");return t.reduce(function(t,n){return o(t,n,e)})},o})}),E="[object Object]",N=Function.prototype,M=Object.prototype,C=N.toString,I=M.hasOwnProperty,_=C.call(Object),P=M.toString,D=i(Object.getPrototypeOf,Object),q=a,H=function(t){return"undefined"==typeof window?String(t).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"):String(t).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,'"').replace(/'/g,"'")},L=Function.prototype.call.bind(Array.prototype.slice),z=clearTimeout,K=function(t){return setTimeout(t,0)},B="metaInfo",V="data-vue-meta",F="data-vue-meta-server-rendered",J="vmid";"undefined"!=typeof Vue&&Vue.use(A);var R="1.0.4";return A.version=R,A});