UNPKG

vue-cookie-accept-decline

Version:

Show a banner with text, a decline button, and an accept button on your page.

2 lines (1 loc) 6.04 kB
var VueCookieAcceptDecline=function(e,t){"use strict";function o(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function n(e){var t=e.charAt(e.length-1),o=parseInt(e,10),n=new Date;switch(t){case"Y":n.setFullYear(n.getFullYear()+o);break;case"M":n.setMonth(n.getMonth()+o);break;case"D":n.setDate(n.getDate()+o);break;case"h":n.setHours(n.getHours()+o);break;case"m":n.setMinutes(n.getMinutes()+o);break;case"s":n.setSeconds(n.getSeconds()+o);break;default:n=new Date(e)}return n}function c(e,t,c,i){void 0===c&&(c=encodeURIComponent),"object"==typeof c&&null!==c&&(i=c,c=encodeURIComponent);var s=function(e){var t="";for(var c in e)if(o(e,c))if(/^expires$/i.test(c)){var i=e[c];"object"!=typeof i&&(i=n(i+="number"==typeof i?"D":"")),t+=";"+c+"="+i.toUTCString()}else/^secure$/.test(c)?e[c]&&(t+=";"+c):t+=";"+c+"="+e[c];return o(e,"path")||(t+=";path=/"),t}(i||{}),a=e+"="+("function"==typeof c?c(t):t)+s;document.cookie=a}var i={name:"vue-cookie-accept-decline",props:{elementId:{type:String,required:!0},debug:{type:Boolean,default:!1},disableDecline:{type:Boolean,default:!1},position:{type:String,default:"bottom-left"},type:{type:String,default:"floating"},transitionName:{type:String,default:"slideFromBottom"},showPostponeButton:{type:Boolean,default:!1},forceCookies:{type:Boolean,default:!1}},data:function(){return{status:null,supportsLocalStorage:!0,isOpen:!1}},computed:{containerPosition:function(){return"cookie--"+this.position},containerType:function(){return"cookie--"+this.type}},mounted:function(){this.checkLocalStorageFunctionality(),this.init()},methods:{init:function(){var e=this.getCookieStatus();!e||"accept"!==e&&"decline"!==e&&"postpone"!==e||(this.isOpen=!1),e||(this.isOpen=!0),this.status=e,this.$emit("status",e)},checkLocalStorageFunctionality:function(){if(this.forceCookies)this.supportsLocalStorage=!1;else try{var e="__vue-cookie-accept-decline-check-localStorage";window.localStorage.setItem(e,e),window.localStorage.removeItem(e)}catch(e){console.error("Local storage is not supported, falling back to cookie use"),this.supportsLocalStorage=!1}},setCookieStatus:function(e){this.supportsLocalStorage?("accept"===e&&localStorage.setItem("vue-cookie-accept-decline-"+this.elementId,"accept"),"decline"===e&&localStorage.setItem("vue-cookie-accept-decline-"+this.elementId,"decline"),"postpone"===e&&localStorage.setItem("vue-cookie-accept-decline-"+this.elementId,"postpone")):("accept"===e&&c("vue-cookie-accept-decline-"+this.elementId,"accept"),"decline"===e&&c("vue-cookie-accept-decline-"+this.elementId,"decline"),"postpone"===e&&c("vue-cookie-accept-decline-"+this.elementId,"postpone"))},getCookieStatus:function(){return this.supportsLocalStorage?localStorage.getItem("vue-cookie-accept-decline-"+this.elementId):function(e,t){if(void 0===t&&(t=decodeURIComponent),"string"!=typeof e||!e)return null;var o=new RegExp("(?:^|; )"+(e.replace(/[.*+?^$|[\](){}\\-]/g,"\\$&")+"(?:=([^;]*))?(?:;|$)")).exec(document.cookie);return null===o?null:"function"==typeof t?t(o[1]):o[1]}("vue-cookie-accept-decline-"+this.elementId)},accept:function(){this.debug||this.setCookieStatus("accept"),this.status="accept",this.isOpen=!1,this.$emit("clicked-accept")},decline:function(){this.debug||this.setCookieStatus("decline"),this.status="decline",this.isOpen=!1,this.$emit("clicked-decline")},postpone:function(){this.debug||this.setCookieStatus("postpone"),this.status="postpone",this.isOpen=!1,this.$emit("clicked-postpone")},removeCookie:function(){localStorage.removeItem("vue-cookie-accept-decline-"+this.elementId),this.status=null,this.$emit("removed-cookie")}}},s=["id"],a=t.createTextVNode("×"),l=t.createTextVNode(" We use cookies to ensure you get the best experience on our website. "),r=t.createElementVNode("a",{href:"https://cookiesandyou.com/",target:"_blank"},"Learn More...",-1),u=t.createTextVNode("Opt Out"),p=t.createTextVNode("Got It!");function d(e){d.installed||(d.installed=!0,e.component("VueCookieAcceptDecline",i))}i.render=function(e,o,n,c,i,d){return t.openBlock(),t.createBlock(t.Transition,{appear:"",name:n.transitionName},{default:t.withCtx((function(){return[i.isOpen?(t.openBlock(),t.createElementBlock("div",{key:0,class:t.normalizeClass(["cookie",["cookie__"+n.type,"cookie__"+n.type+"--"+n.position]]),id:n.elementId},[t.createElementVNode("div",{class:t.normalizeClass("cookie__"+n.type+"__wrap")},[!0===n.showPostponeButton?(t.openBlock(),t.createElementBlock("div",{key:0,onClick:o[0]||(o[0]=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return d.postpone&&d.postpone.apply(d,e)}),class:t.normalizeClass("cookie__"+n.type+"__postpone-button"),title:"Close"},[t.renderSlot(e.$slots,"postponeContent",{},(function(){return[a]}))],2)):t.createCommentVNode("v-if",!0),t.createElementVNode("div",{class:t.normalizeClass("cookie__"+n.type+"__content")},[t.renderSlot(e.$slots,"message",{},(function(){return[l,r]}))],2),t.createElementVNode("div",{class:t.normalizeClass("cookie__"+n.type+"__buttons")},[!1===n.disableDecline?(t.openBlock(),t.createElementBlock("button",{key:0,onClick:o[1]||(o[1]=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return d.decline&&d.decline.apply(d,e)}),class:t.normalizeClass(["cookie__"+n.type+"__buttons__button","cookie__"+n.type+"__buttons__button--decline"])},[t.renderSlot(e.$slots,"declineContent",{},(function(){return[u]}))],2)):t.createCommentVNode("v-if",!0),t.createElementVNode("button",{onClick:o[2]||(o[2]=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return d.accept&&d.accept.apply(d,e)}),class:t.normalizeClass(["cookie__"+n.type+"__buttons__button","cookie__"+n.type+"__buttons__button--accept"])},[t.renderSlot(e.$slots,"acceptContent",{},(function(){return[p]}))],2)],2)],2)],10,s)):t.createCommentVNode("v-if",!0)]})),_:3},8,["name"])},i.__file="src/vue-cookie-accept-decline.vue";var k={install:d},m=null;return"undefined"!=typeof window?m=window.Vue:"undefined"!=typeof global&&(m=global.Vue),m&&"use"in m&&m.use(k),e.default=i,e.install=d,e}({},Vue);