@wcstack/notification 4.24.1 → 4.24.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/auto.min.js CHANGED
@@ -1,2 +1,2 @@
1
- const t={autoTrigger:!0,triggerAttribute:"data-notifytarget",tagNames:{notify:"wcs-notify"}};var e="@wcstack/notification",s="4.24.1";const i=new Set(["camera","microphone","geolocation","midi","clipboard-read","clipboard-write","screen-wake-lock","idle-detection","accelerometer","gyroscope","magnetometer","ambient-light-sensor","display-capture"]);function n(t){const e=globalThis;if(!1===e.isSecureContext)return"insecure-context";if(!i.has(t))return null;const s=e.document?.permissionsPolicy??e.document?.featurePolicy;if(void 0===s||"function"!=typeof s.allowsFeature)return null;try{return s.allowsFeature(t)?null:"policy-blocked"}catch{return null}}const r="capability-missing",o="not-allowed",a="invalid-argument",c="show-failed",h="no-service-worker",l="notify-error";class u extends EventTarget{static wcBindable={protocol:"wc-bindable",version:1,properties:[{name:"permission",event:"wcs-notify:permission-change",semantics:"state"},{name:"granted",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"granted"===t.detail},{name:"denied",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"denied"===t.detail},{name:"prompt",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"prompt"===t.detail},{name:"unsupported",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"unsupported"===t.detail},{name:"error",event:"wcs-notify:error",semantics:"state"},{name:"errorInfo",event:"wcs-notify:error-info-changed",semantics:"state"},{name:"clicked",event:"wcs-notify:click",semantics:"event",getter:t=>t.detail},{name:"closed",event:"wcs-notify:close",semantics:"event",getter:t=>t.detail},{name:"shown",event:"wcs-notify:show",semantics:"event",getter:t=>t.detail}],commands:[{name:"request",async:!0},{name:"notify"},{name:"close"},{name:"closeAll"}]};_target;_mode="auto";_permission="prompt";_error=null;_errorInfo=null;_lastClick=null;_lastClose=null;_lastShow=null;_permissionStatus=null;_permissionSubscribed=!1;_gen=0;_ready=Promise.resolve();_idSeq=0;_toldNotReady=!1;_constructed=new Map;_swTags=new Set;_channel=null;_serviceWorker=null;_clicksSubscribed=!1;_seenIds=[];constructor(t){super(),this._target=t??this}get permission(){return this._permission}get granted(){return"granted"===this._permission}get denied(){return"denied"===this._permission}get prompt(){return"prompt"===this._permission}get unsupported(){return"unsupported"===this._permission}get error(){return this._error}get errorInfo(){return this._errorInfo}get clicked(){return this._lastClick}get closed(){return this._lastClose}get shown(){return this._lastShow}get ready(){return this._ready}_setPermission(t){this._permission!==t&&(this._permission=t,"granted"===t&&(this._toldNotReady=!1),this._target.dispatchEvent(new CustomEvent("wcs-notify:permission-change",{detail:t,bubbles:!0})))}_setError(t){this._error!==t&&(this._error=t,this._commitErrorInfo(null===t?null:function(t){const{error:e,message:s}=t;switch(e){case"unsupported":return{code:r,phase:"probe",recoverable:!1,message:s};case"not-granted":return{code:o,phase:"start",recoverable:!1,message:s};case"invalid-title":return{code:a,phase:"start",recoverable:!1,message:s};case"show-failed":return{code:c,phase:"execute",recoverable:!1,message:s};case"no-service-worker":return{code:h,phase:"execute",recoverable:!1,message:s};default:return{code:l,phase:"execute",recoverable:!1,message:s}}}(t)),this._target.dispatchEvent(new CustomEvent("wcs-notify:error",{detail:t,bubbles:!0})))}_commitErrorInfo(t){this._errorInfo=t,this._target.dispatchEvent(new CustomEvent("wcs-notify:error-info-changed",{detail:t,bubbles:!0}))}_emit(t,e){"click"===t?this._lastClick=e:"close"===t?this._lastClose=e:this._lastShow=e,this._target.dispatchEvent(new CustomEvent(`wcs-notify:${t}`,{detail:e,bubbles:!0}))}observe(t="auto"){return this._mode=t,this._permissionSubscribed||(this._ready=this._initPermission(),this._subscribeClicks()),this._ready}async request(){const t=this._api();if(!t||"function"!=typeof t.requestPermission)return this._setPermission("unsupported"),this._permission;try{const e=await t.requestPermission();this._setPermission(this._normalize(e))}catch{}return this._permission}notify(t,e={}){if(!this._api()){const t=n("notifications");return this._setError(this._err("unsupported",null===t?"Notifications API is not available in this environment.":function(t,e,s){return"insecure-context"===s?`${t} needs a secure context (https:, or http://localhost), and this page is not one.`:`${t} is turned off for this page by a Permissions-Policy: the server's Permissions-Policy header, or an <iframe> without allow="${e}". The browser's site settings cannot change that.`}("Notifications","notifications",t))),""}if("granted"!==this._permission)return this._setError(this._err("not-granted","Notification permission is not granted; call request() first.")),this._warnNotReady(),"";if("string"!=typeof t)return this._setError(this._err("invalid-title","notify() requires a string title.")),"";const s="string"==typeof e.tag&&""!==e.tag?e.tag:this._nextId(),i=e.data,r={__wcsId:s,payload:i},o={...e,tag:s,data:r};return this._setError(null),this._show(t,o,s,i),s}close(t){if("string"!=typeof t||""===t)return;const e=this._constructed.get(t);e&&(e.close(),this._constructed.delete(t)),this._swTags.has(t)&&(this._closeSw(t),this._swTags.delete(t))}closeAll(){for(const t of this._constructed.values())t.close();this._constructed.clear();for(const t of this._swTags)this._closeSw(t);this._swTags.clear()}dispose(){this._permissionSubscribed=!1,this._clicksSubscribed=!1,this._gen++,this._permissionStatus&&(this._permissionStatus.removeEventListener("change",this._onPermissionChange),this._permissionStatus=null),this._channel&&(this._channel.removeEventListener("message",this._onInbound),this._channel.close(),this._channel=null),this._serviceWorker&&(this._serviceWorker.removeEventListener("message",this._onInbound),this._serviceWorker=null)}_initPermission(){const t=this._api();if(!t)return this._setPermission("unsupported"),Promise.resolve();if(this._permissionSubscribed=!0,"undefined"!=typeof navigator&&navigator.permissions&&"function"==typeof navigator.permissions.query){const e=++this._gen;return navigator.permissions.query({name:"notifications"}).then(t=>{e===this._gen&&(this._permissionStatus=t,this._setPermission(this._normalize(t.state)),t.addEventListener("change",this._onPermissionChange))},()=>{e===this._gen&&this._setPermission(this._normalize(t.permission))})}return this._setPermission(this._normalize(t.permission)),Promise.resolve()}_onPermissionChange=t=>{const e=t.target;this._setPermission(this._normalize(e.state))};_normalize(t){return"default"===t?"prompt":"granted"===t||"denied"===t||"prompt"===t?t:"prompt"}_show(t,e,s,i){if("sw"===this._mode)return void this._showViaSw(t,e,s,i);this._showViaConstructor(t,e,s,i)||("auto"===this._mode?this._showViaSw(t,e,s,i):this._setError(this._err("show-failed",'new Notification() is not usable here and mode="constructor" disallows the Service Worker fallback.')))}_showViaConstructor(t,e,s,i){const n=this._api(),r=this._gen;let o;try{o=new n(t,e)}catch(t){return!(t instanceof TypeError)&&(this._setError(this._err("show-failed","Failed to create the notification.")),!0)}return this._constructed.set(s,o),o.onshow=()=>{r===this._gen&&this._emit("show",{tag:s,data:i,action:""})},o.onclick=()=>{r===this._gen&&this._emit("click",{tag:s,data:i,action:""})},o.onclose=()=>{this._constructed.delete(s),r===this._gen&&this._emit("close",{tag:s,data:i,action:""})},o.onerror=()=>{r===this._gen&&this._setError(this._err("show-failed","The notification failed to display."))},!0}_showViaSw(t,e,s,i){const n=navigator.serviceWorker;if(!n)return void this._setError(this._err("no-service-worker","Service Worker is required to show this notification but is unavailable."));const r=this._gen;this._swTags.add(s),n.ready.then(s=>s.showNotification(t,e)).then(()=>{r===this._gen&&this._emit("show",{tag:s,data:i,action:""})}).catch(()=>{r===this._gen&&this._setError(this._err("show-failed","ServiceWorkerRegistration.showNotification() failed."))})}_closeSw(t){const e=navigator.serviceWorker;e&&e.ready.then(e=>e.getNotifications({tag:t}).then(t=>{for(const e of t)e.close()})).catch(()=>{})}_subscribeClicks(){if(this._clicksSubscribed)return;this._clicksSubscribed=!0,"function"==typeof BroadcastChannel&&(this._channel=new BroadcastChannel("wcs-notify"),this._channel.addEventListener("message",this._onInbound));const t=navigator.serviceWorker;t&&(this._serviceWorker=t,t.addEventListener("message",this._onInbound))}_onInbound=t=>{const e=t.data;e&&!0===e.__wcsNotify&&(this._isDuplicate(e.id)||this._emit("click",{tag:e.tag,data:this._unwrap(e.data),action:e.action}))};_isDuplicate(t){return!!this._seenIds.includes(t)||(this._seenIds.push(t),this._seenIds.length>50&&this._seenIds.shift(),!1)}_unwrap(t){return null!==t&&"object"==typeof t&&"__wcsId"in t?t.payload:t}_api(){const t=globalThis;return"function"==typeof t.Notification&&null===n("notifications")?t.Notification:void 0}_warnNotReady(){this._toldNotReady||(this._toldNotReady=!0,console.warn(`[@wcstack/notification] [wcs/command-not-ready] notify() was refused: the permission is "${this._permission}", so nothing is shown. It goes to \`error\` too. Ask with request() first (from a user gesture), and notify once \`permission\` is "granted". (Said once until the permission is granted again.)`,this._target))}_nextId(){return"wcs-"+ ++this._idSeq}_err(t,e){return{error:t,message:e}}}let d=!1;function g(e){const s=e.target;if(!(s instanceof Element))return;let i;try{i=s.closest(`[${t.triggerAttribute}]`)}catch{return}if(!i)return;const n=i.getAttribute(t.triggerAttribute);if(!n)return;const r=customElements.get(t.tagNames.notify),o=document.getElementById(n);if(!(r&&o instanceof r))return;const a=i.getAttribute("data-notifytitle"),c=null!==a?a:i.textContent.trim(),h=i.getAttribute("data-notifybody");o.notify(c,null!==h?{body:h}:void 0)}function _(t,e){let s=Object.getPrototypeOf(t);for(;null!==s;){const t=Object.getOwnPropertyDescriptor(s,e);if(void 0!==t)return"function"==typeof t.get||"function"==typeof t.set;s=Object.getPrototypeOf(s)}return!1}const m=new WeakSet;function f(t,e,s){if(m.has(t))return;m.add(t);const i=t.localName??"element";console.warn(`[wcs/input-as-attribute] <${i}> has the attribute ${e}="${s}": an object was written as an attribute, so the element did not receive it. A framework does that when the property is set before the element's definition loads (React 19), or when told to (Vue's .attr). Load the definition before rendering, or assign the property through a ref (Vue: .prop). (Said once per element.)`,t)}const p=new WeakMap;function b(t,e){let s=p.get(t);return void 0===s&&(s=new Map,p.set(t,s)),s.has(e)||s.set(e,t.getAttribute(e)),s.get(e)}function w(t,e,s){null===s?t.removeAttribute(e):t.setAttribute(e,s)}function y(t,e,s){const i=b(t,e);w(t,e,void 0===s?i:null===s?null:String(s))}function v(t,e,s){const i=b(t,e);w(t,e,void 0===s?i:s?"":null)}class k extends HTMLElement{static hasConnectedCallbackPromise=!0;static wcBindable={...u.wcBindable,inputs:[{name:"notice"},{name:"mode",attribute:"mode"},{name:"body",attribute:"body"},{name:"icon",attribute:"icon"},{name:"badge",attribute:"badge"},{name:"tag",attribute:"tag"},{name:"lang",attribute:"lang"},{name:"dir",attribute:"dir"},{name:"requireInteraction",attribute:"require-interaction"},{name:"silent",attribute:"silent"},{name:"renotify",attribute:"renotify"},{name:"manual",attribute:"manual"}],commands:u.wcBindable.commands};_core;_notice="";_connectedCallbackPromise=Promise.resolve();_internals=null;constructor(){super(),this._core=new u(this),this._internals=this._initInternals(),this._wireStates({"wcs-notify:permission-change":t=>({granted:"granted"===t,denied:"denied"===t,prompt:"prompt"===t,unsupported:"unsupported"===t}),"wcs-notify:error":t=>({error:null!=t})})}get debugStates(){return this._internals?[...this._internals.states]:[]}_initInternals(){try{if("function"!=typeof this.attachInternals)return null;const t=this.attachInternals();return t.states.add("wcs-probe"),t.states.delete("wcs-probe"),t}catch{return null}}_wireStates(t){if(null===this._internals)return;const e=this._internals.states;for(const[s,i]of Object.entries(t))this.addEventListener(s,t=>{const s=this.hasAttribute("debug-states");for(const[n,r]of Object.entries(i(t.detail))){try{r?e.add(n):e.delete(n)}catch{}s&&this.toggleAttribute(`data-wcs-state-${n}`,r)}})}get mode(){const t=this.getAttribute("mode");return"sw"===t||"constructor"===t?t:"auto"}set mode(t){y(this,"mode",t)}get body(){return this.getAttribute("body")??""}set body(t){y(this,"body",t)}get icon(){return this.getAttribute("icon")??""}set icon(t){y(this,"icon",t)}get badge(){return this.getAttribute("badge")??""}set badge(t){y(this,"badge",t)}get tag(){return this.getAttribute("tag")??""}set tag(t){y(this,"tag",t)}get lang(){return this.getAttribute("lang")??""}set lang(t){y(this,"lang",t)}get dir(){return this.getAttribute("dir")??""}set dir(t){y(this,"dir",t)}get requireInteraction(){return this.hasAttribute("require-interaction")}set requireInteraction(t){v(this,"require-interaction",t)}get silent(){return this.hasAttribute("silent")}set silent(t){v(this,"silent",t)}get renotify(){return this.hasAttribute("renotify")}set renotify(t){v(this,"renotify",t)}get manual(){return this.hasAttribute("manual")}set manual(t){v(this,"manual",t)}get notice(){return this._notice}set notice(t){if(null==t)return;if(this.manual)return;const e=String(t);e!==this._notice&&(this._notice=e,this.notify(e))}get permission(){return this._core.permission}get granted(){return this._core.granted}get denied(){return this._core.denied}get prompt(){return this._core.prompt}get unsupported(){return this._core.unsupported}get error(){return this._core.error}get errorInfo(){return this._core.errorInfo}get clicked(){return this._core.clicked}get closed(){return this._core.closed}get shown(){return this._core.shown}get connectedCallbackPromise(){return this._connectedCallbackPromise}request(){return this._core.request()}notify(t,e){return this._core.notify(t,{...this._options(),...e??{}})}close(t){this._core.close(t)}closeAll(){this._core.closeAll()}_options(){const t={};return""!==this.body&&(t.body=this.body),""!==this.icon&&(t.icon=this.icon),""!==this.badge&&(t.badge=this.badge),""!==this.tag&&(t.tag=this.tag),""!==this.lang&&(t.lang=this.lang),"auto"!==this.dir&&"ltr"!==this.dir&&"rtl"!==this.dir||(t.dir=this.dir),this.requireInteraction&&(t.requireInteraction=!0),this.silent&&(t.silent=!0),this.renotify&&(t.renotify=!0),t}connectedCallback(){!function(t){const e=t.constructor?.wcBindable,s=e?.inputs;if(void 0===s)return;const i=t.getAttribute;for(const e of s){const s=e.name,n="function"==typeof i?i.call(t,s.toLowerCase()):null;if(null!==n&&n.startsWith("[object ")&&f(t,s,n),!Object.prototype.hasOwnProperty.call(t,s))continue;if(!_(t,s))continue;const r=t,o=r[s];delete r[s],r[s]=o}}(this),this.style.display="none",d||(d=!0,document.addEventListener("click",g)),this._connectedCallbackPromise=this._core.observe(this.mode)}connectedMoveCallback(){}disconnectedCallback(){this._core.dispose()}}const S=Symbol.for("wcstack.package"),I=new WeakSet;function P(t,e){Object.defineProperty(t,S,{value:e,configurable:!0})}function E(t,e,s,i,n){if(void 0!==i&&(s===i||s.prototype instanceof i))return;const r=s[S];if(r===n||I.has(t))return;I.add(t);const o=n.slice(0,n.lastIndexOf("@"));console.warn(`[${o}] [wcs/package-twice] <${e}> is already defined by ${r??`another copy of ${o} that does not name its version`}; this copy, ${n}, does nothing. Load each package once.`)}function C(i=customElements){!function(t,e,s){for(const[i,n]of s){const s=t.get(i);void 0===s?(P(n,e),t.define(i,n)):E(t,i,s,n,e)}}(i,`${e}@${s}`,[[t.tagNames.notify,k]])}var A;C(A);
1
+ const t={autoTrigger:!0,triggerAttribute:"data-notifytarget",tagNames:{notify:"wcs-notify"}};var e="@wcstack/notification",s="4.24.2";const i=new Set(["camera","microphone","geolocation","midi","clipboard-read","clipboard-write","screen-wake-lock","idle-detection","accelerometer","gyroscope","magnetometer","ambient-light-sensor","display-capture"]);function n(t){const e=globalThis;if(!1===e.isSecureContext)return"insecure-context";if(!i.has(t))return null;const s=e.document?.permissionsPolicy??e.document?.featurePolicy;if(void 0===s||"function"!=typeof s.allowsFeature)return null;try{return s.allowsFeature(t)?null:"policy-blocked"}catch{return null}}const r="capability-missing",o="not-allowed",a="invalid-argument",c="show-failed",h="no-service-worker",l="notify-error";class u extends EventTarget{static wcBindable={protocol:"wc-bindable",version:1,properties:[{name:"permission",event:"wcs-notify:permission-change",semantics:"state"},{name:"granted",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"granted"===t.detail},{name:"denied",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"denied"===t.detail},{name:"prompt",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"prompt"===t.detail},{name:"unsupported",event:"wcs-notify:permission-change",semantics:"state",getter:t=>"unsupported"===t.detail},{name:"error",event:"wcs-notify:error",semantics:"state"},{name:"errorInfo",event:"wcs-notify:error-info-changed",semantics:"state"},{name:"clicked",event:"wcs-notify:click",semantics:"event",getter:t=>t.detail},{name:"closed",event:"wcs-notify:close",semantics:"event",getter:t=>t.detail},{name:"shown",event:"wcs-notify:show",semantics:"event",getter:t=>t.detail}],commands:[{name:"request",async:!0},{name:"notify"},{name:"close"},{name:"closeAll"}]};_target;_mode="auto";_permission="prompt";_error=null;_errorInfo=null;_lastClick=null;_lastClose=null;_lastShow=null;_permissionStatus=null;_permissionSubscribed=!1;_gen=0;_ready=Promise.resolve();_idSeq=0;_toldNotReady=!1;_constructed=new Map;_swTags=new Set;_channel=null;_serviceWorker=null;_clicksSubscribed=!1;_seenIds=[];constructor(t){super(),this._target=t??this}get permission(){return this._permission}get granted(){return"granted"===this._permission}get denied(){return"denied"===this._permission}get prompt(){return"prompt"===this._permission}get unsupported(){return"unsupported"===this._permission}get error(){return this._error}get errorInfo(){return this._errorInfo}get clicked(){return this._lastClick}get closed(){return this._lastClose}get shown(){return this._lastShow}get ready(){return this._ready}_setPermission(t){this._permission!==t&&(this._permission=t,"granted"===t&&(this._toldNotReady=!1),this._target.dispatchEvent(new CustomEvent("wcs-notify:permission-change",{detail:t,bubbles:!0})))}_setError(t){this._error!==t&&(this._error=t,this._commitErrorInfo(null===t?null:function(t){const{error:e,message:s}=t;switch(e){case"unsupported":return{code:r,phase:"probe",recoverable:!1,message:s};case"not-granted":return{code:o,phase:"start",recoverable:!1,message:s};case"invalid-title":return{code:a,phase:"start",recoverable:!1,message:s};case"show-failed":return{code:c,phase:"execute",recoverable:!1,message:s};case"no-service-worker":return{code:h,phase:"execute",recoverable:!1,message:s};default:return{code:l,phase:"execute",recoverable:!1,message:s}}}(t)),this._target.dispatchEvent(new CustomEvent("wcs-notify:error",{detail:t,bubbles:!0})))}_commitErrorInfo(t){this._errorInfo=t,this._target.dispatchEvent(new CustomEvent("wcs-notify:error-info-changed",{detail:t,bubbles:!0}))}_emit(t,e){"click"===t?this._lastClick=e:"close"===t?this._lastClose=e:this._lastShow=e,this._target.dispatchEvent(new CustomEvent(`wcs-notify:${t}`,{detail:e,bubbles:!0}))}observe(t="auto"){return this._mode=t,this._permissionSubscribed||(this._ready=this._initPermission(),this._subscribeClicks()),this._ready}async request(){const t=this._api();if(!t||"function"!=typeof t.requestPermission)return this._setPermission("unsupported"),this._permission;try{const e=await t.requestPermission();this._setPermission(this._normalize(e))}catch{}return this._permission}notify(t,e={}){if(!this._api()){const t=n("notifications");return this._setError(this._err("unsupported",null===t?"Notifications API is not available in this environment.":function(t,e,s){return"insecure-context"===s?`${t} needs a secure context (https:, or http://localhost), and this page is not one.`:`${t} is turned off for this page by a Permissions-Policy: the server's Permissions-Policy header, or an <iframe> without allow="${e}". The browser's site settings cannot change that.`}("Notifications","notifications",t))),""}if("granted"!==this._permission)return this._setError(this._err("not-granted","Notification permission is not granted; call request() first.")),this._warnNotReady(),"";if("string"!=typeof t)return this._setError(this._err("invalid-title","notify() requires a string title.")),"";const s="string"==typeof e.tag&&""!==e.tag?e.tag:this._nextId(),i=e.data,r={__wcsId:s,payload:i},o={...e,tag:s,data:r};return this._setError(null),this._show(t,o,s,i),s}close(t){if("string"!=typeof t||""===t)return;const e=this._constructed.get(t);e&&(e.close(),this._constructed.delete(t)),this._swTags.has(t)&&(this._closeSw(t),this._swTags.delete(t))}closeAll(){for(const t of this._constructed.values())t.close();this._constructed.clear();for(const t of this._swTags)this._closeSw(t);this._swTags.clear()}dispose(){this._permissionSubscribed=!1,this._clicksSubscribed=!1,this._gen++,this._permissionStatus&&(this._permissionStatus.removeEventListener("change",this._onPermissionChange),this._permissionStatus=null),this._channel&&(this._channel.removeEventListener("message",this._onInbound),this._channel.close(),this._channel=null),this._serviceWorker&&(this._serviceWorker.removeEventListener("message",this._onInbound),this._serviceWorker=null)}_initPermission(){const t=this._api();if(!t)return this._setPermission("unsupported"),Promise.resolve();if(this._permissionSubscribed=!0,"undefined"!=typeof navigator&&navigator.permissions&&"function"==typeof navigator.permissions.query){const e=++this._gen;return navigator.permissions.query({name:"notifications"}).then(t=>{e===this._gen&&(this._permissionStatus=t,this._setPermission(this._normalize(t.state)),t.addEventListener("change",this._onPermissionChange))},()=>{e===this._gen&&this._setPermission(this._normalize(t.permission))})}return this._setPermission(this._normalize(t.permission)),Promise.resolve()}_onPermissionChange=t=>{const e=t.target;this._setPermission(this._normalize(e.state))};_normalize(t){return"default"===t?"prompt":"granted"===t||"denied"===t||"prompt"===t?t:"prompt"}_show(t,e,s,i){if("sw"===this._mode)return void this._showViaSw(t,e,s,i);this._showViaConstructor(t,e,s,i)||("auto"===this._mode?this._showViaSw(t,e,s,i):this._setError(this._err("show-failed",'new Notification() is not usable here and mode="constructor" disallows the Service Worker fallback.')))}_showViaConstructor(t,e,s,i){const n=this._api(),r=this._gen;let o;try{o=new n(t,e)}catch(t){return!(t instanceof TypeError)&&(this._setError(this._err("show-failed","Failed to create the notification.")),!0)}return this._constructed.set(s,o),o.onshow=()=>{r===this._gen&&this._emit("show",{tag:s,data:i,action:""})},o.onclick=()=>{r===this._gen&&this._emit("click",{tag:s,data:i,action:""})},o.onclose=()=>{this._constructed.delete(s),r===this._gen&&this._emit("close",{tag:s,data:i,action:""})},o.onerror=()=>{r===this._gen&&this._setError(this._err("show-failed","The notification failed to display."))},!0}_showViaSw(t,e,s,i){const n=navigator.serviceWorker;if(!n)return void this._setError(this._err("no-service-worker","Service Worker is required to show this notification but is unavailable."));const r=this._gen;this._swTags.add(s),n.ready.then(s=>s.showNotification(t,e)).then(()=>{r===this._gen&&this._emit("show",{tag:s,data:i,action:""})}).catch(()=>{r===this._gen&&this._setError(this._err("show-failed","ServiceWorkerRegistration.showNotification() failed."))})}_closeSw(t){const e=navigator.serviceWorker;e&&e.ready.then(e=>e.getNotifications({tag:t}).then(t=>{for(const e of t)e.close()})).catch(()=>{})}_subscribeClicks(){if(this._clicksSubscribed)return;this._clicksSubscribed=!0,"function"==typeof BroadcastChannel&&(this._channel=new BroadcastChannel("wcs-notify"),this._channel.addEventListener("message",this._onInbound));const t=navigator.serviceWorker;t&&(this._serviceWorker=t,t.addEventListener("message",this._onInbound))}_onInbound=t=>{const e=t.data;e&&!0===e.__wcsNotify&&(this._isDuplicate(e.id)||this._emit("click",{tag:e.tag,data:this._unwrap(e.data),action:e.action}))};_isDuplicate(t){return!!this._seenIds.includes(t)||(this._seenIds.push(t),this._seenIds.length>50&&this._seenIds.shift(),!1)}_unwrap(t){return null!==t&&"object"==typeof t&&"__wcsId"in t?t.payload:t}_api(){const t=globalThis;return"function"==typeof t.Notification&&null===n("notifications")?t.Notification:void 0}_warnNotReady(){this._toldNotReady||(this._toldNotReady=!0,console.warn(`[@wcstack/notification] [wcs/command-not-ready] notify() was refused: the permission is "${this._permission}", so nothing is shown. It goes to \`error\` too. Ask with request() first (from a user gesture), and notify once \`permission\` is "granted". (Said once until the permission is granted again.)`,this._target))}_nextId(){return"wcs-"+ ++this._idSeq}_err(t,e){return{error:t,message:e}}}let d=!1;function g(e){const s=e.target;if(!(s instanceof Element))return;let i;try{i=s.closest(`[${t.triggerAttribute}]`)}catch{return}if(!i)return;const n=i.getAttribute(t.triggerAttribute);if(!n)return;const r=customElements.get(t.tagNames.notify),o=document.getElementById(n);if(!(r&&o instanceof r))return;const a=i.getAttribute("data-notifytitle"),c=null!==a?a:i.textContent.trim(),h=i.getAttribute("data-notifybody");o.notify(c,null!==h?{body:h}:void 0)}function _(t,e){let s=Object.getPrototypeOf(t);for(;null!==s;){const t=Object.getOwnPropertyDescriptor(s,e);if(void 0!==t)return"function"==typeof t.get||"function"==typeof t.set;s=Object.getPrototypeOf(s)}return!1}const m=new WeakSet;function f(t,e,s){if(m.has(t))return;m.add(t);const i=t.localName??"element";console.warn(`[wcs/input-as-attribute] <${i}> has the attribute ${e}="${s}": an object was written as an attribute, so the element did not receive it. A framework does that when the property is set before the element's definition loads (React 19), or when told to (Vue's .attr). Load the definition before rendering, or assign the property through a ref (Vue: .prop). (Said once per element.)`,t)}const p=new WeakMap;function b(t,e){let s=p.get(t);return void 0===s&&(s=new Map,p.set(t,s)),s.has(e)||s.set(e,t.getAttribute(e)),s.get(e)}function w(t,e,s){null===s?t.removeAttribute(e):t.setAttribute(e,s)}function y(t,e,s){const i=b(t,e);w(t,e,void 0===s?i:null===s?null:String(s))}function v(t,e,s){const i=b(t,e);w(t,e,void 0===s?i:s?"":null)}class k extends HTMLElement{static hasConnectedCallbackPromise=!0;static wcBindable={...u.wcBindable,inputs:[{name:"notice"},{name:"mode",attribute:"mode"},{name:"body",attribute:"body"},{name:"icon",attribute:"icon"},{name:"badge",attribute:"badge"},{name:"tag",attribute:"tag"},{name:"lang",attribute:"lang"},{name:"dir",attribute:"dir"},{name:"requireInteraction",attribute:"require-interaction"},{name:"silent",attribute:"silent"},{name:"renotify",attribute:"renotify"},{name:"manual",attribute:"manual"}],commands:u.wcBindable.commands};_core;_notice="";_connectedCallbackPromise=Promise.resolve();_internals=null;constructor(){super(),this._core=new u(this),this._internals=this._initInternals(),this._wireStates({"wcs-notify:permission-change":t=>({granted:"granted"===t,denied:"denied"===t,prompt:"prompt"===t,unsupported:"unsupported"===t}),"wcs-notify:error":t=>({error:null!=t})})}get debugStates(){return this._internals?[...this._internals.states]:[]}_initInternals(){try{if("function"!=typeof this.attachInternals)return null;const t=this.attachInternals();return t.states.add("wcs-probe"),t.states.delete("wcs-probe"),t}catch{return null}}_wireStates(t){if(null===this._internals)return;const e=this._internals.states;for(const[s,i]of Object.entries(t))this.addEventListener(s,t=>{const s=this.hasAttribute("debug-states");for(const[n,r]of Object.entries(i(t.detail))){try{r?e.add(n):e.delete(n)}catch{}s&&this.toggleAttribute(`data-wcs-state-${n}`,r)}})}get mode(){const t=this.getAttribute("mode");return"sw"===t||"constructor"===t?t:"auto"}set mode(t){y(this,"mode",t)}get body(){return this.getAttribute("body")??""}set body(t){y(this,"body",t)}get icon(){return this.getAttribute("icon")??""}set icon(t){y(this,"icon",t)}get badge(){return this.getAttribute("badge")??""}set badge(t){y(this,"badge",t)}get tag(){return this.getAttribute("tag")??""}set tag(t){y(this,"tag",t)}get lang(){return this.getAttribute("lang")??""}set lang(t){y(this,"lang",t)}get dir(){return this.getAttribute("dir")??""}set dir(t){y(this,"dir",t)}get requireInteraction(){return this.hasAttribute("require-interaction")}set requireInteraction(t){v(this,"require-interaction",t)}get silent(){return this.hasAttribute("silent")}set silent(t){v(this,"silent",t)}get renotify(){return this.hasAttribute("renotify")}set renotify(t){v(this,"renotify",t)}get manual(){return this.hasAttribute("manual")}set manual(t){v(this,"manual",t)}get notice(){return this._notice}set notice(t){if(null==t)return;if(this.manual)return;const e=String(t);e!==this._notice&&(this._notice=e,this.notify(e))}get permission(){return this._core.permission}get granted(){return this._core.granted}get denied(){return this._core.denied}get prompt(){return this._core.prompt}get unsupported(){return this._core.unsupported}get error(){return this._core.error}get errorInfo(){return this._core.errorInfo}get clicked(){return this._core.clicked}get closed(){return this._core.closed}get shown(){return this._core.shown}get connectedCallbackPromise(){return this._connectedCallbackPromise}request(){return this._core.request()}notify(t,e){return this._core.notify(t,{...this._options(),...e??{}})}close(t){this._core.close(t)}closeAll(){this._core.closeAll()}_options(){const t={};return""!==this.body&&(t.body=this.body),""!==this.icon&&(t.icon=this.icon),""!==this.badge&&(t.badge=this.badge),""!==this.tag&&(t.tag=this.tag),""!==this.lang&&(t.lang=this.lang),"auto"!==this.dir&&"ltr"!==this.dir&&"rtl"!==this.dir||(t.dir=this.dir),this.requireInteraction&&(t.requireInteraction=!0),this.silent&&(t.silent=!0),this.renotify&&(t.renotify=!0),t}connectedCallback(){!function(t){const e=t.constructor?.wcBindable,s=e?.inputs;if(void 0===s)return;const i=t.getAttribute;for(const e of s){const s=e.name,n="function"==typeof i?i.call(t,s.toLowerCase()):null;if(null!==n&&n.startsWith("[object ")&&f(t,s,n),!Object.prototype.hasOwnProperty.call(t,s))continue;if(!_(t,s))continue;const r=t,o=r[s];delete r[s],r[s]=o}}(this),this.style.display="none",d||(d=!0,document.addEventListener("click",g)),this._connectedCallbackPromise=this._core.observe(this.mode)}connectedMoveCallback(){}disconnectedCallback(){this._core.dispose()}}const S=Symbol.for("wcstack.package"),I=new WeakSet;function P(t,e){Object.defineProperty(t,S,{value:e,configurable:!0})}function E(t,e,s,i,n){if(void 0!==i&&(s===i||s.prototype instanceof i))return;const r=s[S];if(r===n||I.has(t))return;I.add(t);const o=n.slice(0,n.lastIndexOf("@"));console.warn(`[${o}] [wcs/package-twice] <${e}> is already defined by ${r??`another copy of ${o} that does not name its version`}; this copy, ${n}, does nothing. Load each package once.`)}function C(i=customElements){!function(t,e,s){for(const[i,n]of s){const s=t.get(i);void 0===s?(P(n,e),t.define(i,n)):E(t,i,s,n,e)}}(i,`${e}@${s}`,[[t.tagNames.notify,k]])}var A;C(A);
2
2
  //# sourceMappingURL=auto.min.js.map
package/dist/index.esm.js CHANGED
@@ -71,7 +71,7 @@ function setConfig(partialConfig) {
71
71
  frozenConfig = null;
72
72
  }
73
73
 
74
- var pkg = {"name":"@wcstack/notification","version":"4.24.1"};
74
+ var pkg = {"name":"@wcstack/notification","version":"4.24.2"};
75
75
 
76
76
  // ===========================================================================
77
77
  // AUTO-GENERATED FILE - DO NOT EDIT.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wcstack/notification",
3
- "version": "4.24.1",
3
+ "version": "4.24.2",
4
4
  "description": "Declarative desktop-notification component for Web Components. Framework-agnostic Notifications API wrapper via wc-bindable-protocol, with Service Worker support.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.esm.js",