Current File : /home/pacjaorg/www/km/media/com_akeebabackup/js/WebPush.min.js |
function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _classPrivateMethodInitSpec(a,b){_checkPrivateRedeclaration(a,b),b.add(a)}function _checkPrivateRedeclaration(a,b){if(b.has(a))throw new TypeError("Cannot initialize the same private elements twice on an object")}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _classPrivateMethodGet(a,b,c){if(!b.has(a))throw new TypeError("attempted to get private field on non-instance");return c}((a,b,c)=>{function d(a){for(var c="=".repeat((4-a.length%4)%4),d=(a+c).replace(/-/g,"+").replace(/_/g,"/"),e=b.atob(d),f=new Uint8Array(e.length),g=0;g<e.length;++g)f[g]=e.charCodeAt(g);return f}var e,f=new WeakSet,g=function(){"use strict";function e(a){_classCallCheck(this,e),_classPrivateMethodInitSpec(this,f),_defineProperty(this,"options",{}),this.options=a}return _createClass(e,[{key:"disableInterface",value:function disableInterface(){a.querySelector(this.options.subscribeButton).classList.add("d-none","disabled"),a.querySelector(this.options.unsubscribeButton).classList.add("d-none","disabled"),a.querySelector(this.options.unavailableInfo).classList.remove("d-none"),a.getElementById("webPushDetails").classList.add("d-none")}},{key:"registerServiceWorker",value:function registerServiceWorker(){console.debug("[Web Push] Preparing to register a Service Worker.");var a=this.options.workerUri;return a?c.serviceWorker.register(a).then(a=>(console.log("[Web Push] Service worker successfully registered."),a)).catch(a=>(console.error("[Web Push] Unable to register Service Worker.",a),null)):(console.error("[Web Push] The backend provided no Service Worker URI. Cannot proceed."),null)}},{key:"askPermission",value:function askPermission(){return new Promise((a,b)=>{console.debug("[Web Push] Requesting permission.");var c=Notification.requestPermission(b=>{a(b)});c&&c.then(a,b)}).then(a=>{if("granted"!==a)throw console.error("[Web Push] Permission for push notifications was NOT granted."),new Error("We were not granted permission.");return console.log("[Web Push] Permission for push notifications was granted."),!0})}},{key:"subscribeUserToPush",value:function subscribeUserToPush(){var a;console.debug("[Web Push] Preparing to subscribe the user.");var b=this.options.workerUri,e=null===(a=this.options.vapidKeys)||void 0===a?void 0:a.publicKey;return b?(e||console.error("[Web Push] The backend provided no VAPID public Key. Cannot proceed."),c.serviceWorker.register(b).then(a=>{var b={userVisibleOnly:!0,applicationServerKey:_classPrivateMethodGet(this,f,d).call(this,e)};return a.pushManager.subscribe(b)}).then(a=>(console.log("[Web Push] Received PushSubscription"),console.debug(JSON.stringify(a)),a))):(console.error("[Web Push] The backend provided no Service Worker URI. Cannot proceed."),null)}},{key:"saveUserSubscription",value:function saveUserSubscription(a){console.log("[Web Push] About to send the user subscription information to the backend.");var b=this.options.subscribeUri;if(!b)return console.error("[Web Push] The backend provided no subscription registration URL. Cannot proceed."),null;var c=new FormData;return c.append("subscription",JSON.stringify(a)),fetch(b,{method:"POST",headers:{"X-CSRF-Token":Joomla.getOptions("csrf.token")},body:c}).then(a=>{if(!a.ok)throw console.error(`[Web Push] Server returned HTTP error ${a.status}. Cannot proceed.`),new Error(`Server returned HTTP error ${a.status}.`);return a.json()}).then(a=>{if(!a||!a.success){var b;console.error(`[Web Push] Server returned invalid data: ${a}. Cannot proceed.`);var c=null!==(b=null===a||void 0===a?void 0:a.error)&&void 0!==b?b:"";throw new Error("Bad response from server. "+c)}})}},{key:"isUserSubscribed",value:function isUserSubscribed(a){a.pushManager.getSubscription().then(a=>{var c;c=a?new CustomEvent("onAkeebaBackupWebPushSubscribed",{detail:{subscription:a}}):new CustomEvent("onAkeebaBackupWebPushNotSubscribed"),b.dispatchEvent(c)}).catch(a=>c.appVersion.includes("Safari")?(console.error("[Web Push] Safari does not yet support Web Push (even if it is enabled as an experimental feature)"),void this.disableInterface()):void console.error("[Web Push] Cannot get push subscription status",a))}},{key:"onSubscribeClick",value:function onSubscribeClick(a){a.target.classList.add("disabled");var c;this.askPermission().then(()=>this.subscribeUserToPush()).then(a=>null===a?null:(c=a,this.saveUserSubscription(a))).then(()=>{a.target.classList.remove("disabled");var d=new CustomEvent("onAkeebaBackupWebPushSubscribed",{detail:{subscription:c}});b.dispatchEvent(d)}).catch(b=>{a.target.classList.remove("disabled"),Joomla.renderMessages({error:[b.message]})})}},{key:"onUnsubscribeClick",value:function onUnsubscribeClick(a){function c(a){console.log("[Web Push] About to send the user unsubscription information to the backend.");var c=d.options.unsubscribeUri;if(!c)return console.log(d.options),console.error("[Web Push] The backend provided no unsubscription registration URL. Cannot proceed."),null;var f=JSON.stringify(a),g=new FormData;return g.append("subscription",f),fetch(c,{method:"POST",headers:{"X-CSRF-Token":Joomla.getOptions("csrf.token")},body:g}).then(a=>{if(!a.ok)throw console.error(`[Web Push] Server returned HTTP error ${a.status}. Cannot proceed.`),new Error(`Server returned HTTP error ${a.status}.`);return a.json()}).then(c=>{if(!c||!c.success)throw console.error(`[Web Push] Server returned invalid data: ${c}. Cannot proceed.`),new Error("Bad response from server.");e.classList.remove("disabled");var d=new CustomEvent("onAkeebaBackupWebPushNotSubscribed",{detail:{subscription:a}});b.dispatchEvent(d)}).catch(a=>{e.classList.remove("disabled"),Joomla.renderMessages({error:[a.message]})})}var d=this,e=a.target;this.registerServiceWorker().then(a=>{e.classList.add("disabled"),a.pushManager.getSubscription().then(a=>{a.unsubscribe().then(b=>b?void c(a):(e.classList.remove("disabled"),a))}).catch(a=>{e.classList.remove("disabled"),Joomla.renderMessages({error:[a.message]})})})}},{key:"init",value:function init(){console.debug("[Web Push] Initialising"),this.options.subscribeButton||console.warn("[Web Push] The backend provided no element to trigger the user subscription process. Abort.");var d=a.querySelector(this.options.subscribeButton);return d?"serviceWorker"in c&&"PushManager"in b?(b.addEventListener("onAkeebaBackupWebPushSubscribed",()=>{console.debug("[Web Push] The user is already subscribed.");var b=a.querySelector(this.options.unsubscribeButton);d&&d.classList.add("d-none","disabled"),b&&(b.classList.remove("d-none","disabled"),b.addEventListener("click",a=>{this.onUnsubscribeClick(a)}))}),b.addEventListener("onAkeebaBackupWebPushNotSubscribed",()=>{console.debug("[Web Push] The user is not yet subscribed.");var b=a.querySelector(this.options.unsubscribeButton);b&&b.classList.add("d-none","disabled"),d&&(d.classList.remove("d-none","disabled"),d.addEventListener("click",a=>{this.onSubscribeClick(a)}))}),this.registerServiceWorker().then(a=>{this.isUserSubscribed(a)}),this):(console.warn("[Web Push] The browser is incompatible with the Web Push standard."),void this.disableInterface()):void console.info("[Web Push] The element to trigger the user subscription process does not exist.")}}]),e}();if(null!==(e=Joomla.getOptions("akeeba.webPush"))&&void 0!==e?e:{}){var h,i=new g(null!==(h=Joomla.getOptions("com_akeebabackup.webPush"))&&void 0!==h?h:{});i.init()}})(document,window,navigator);
//# sourceMappingURL=WebPush.min.js.map