From b40c858b1fe8db8f8e97c942efaa2357eb2a9ec3 Mon Sep 17 00:00:00 2001 From: Michael Bukachi Date: Tue, 1 Jun 2021 22:22:19 +0300 Subject: [PATCH] fix: Correct bug when firing events --- .gitignore | 4 +++- CHANGELOG.md | 3 +++ babel.config.js | 3 +++ flask_wire/static/js/bundle.js | 2 +- js/index.js | 2 +- setup.py | 2 +- 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ba08e54..4a3f7b2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ wire.db *.pyc build dist -.eggs \ No newline at end of file +.eggs +debug.html +debug.py \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 0167044..538902d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [1.0.4] - 2021-06-01 +### Fixed +- Fixed a bug when firing events during a mutation ## [1.0.3] - 2021-03-09 ### Added - Added `loader_use_html`, `error_use_html` parameters to the `wire_frame` macro - diff --git a/babel.config.js b/babel.config.js index ac1e294..0937ce7 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,4 +1,7 @@ // babel.config.js module.exports = { presets: [['@babel/preset-env', {targets: {node: 'current'}}]], + plugins: [ + "@babel/plugin-proposal-class-properties" + ] }; diff --git a/flask_wire/static/js/bundle.js b/flask_wire/static/js/bundle.js index d8a2b5f..715cf75 100644 --- a/flask_wire/static/js/bundle.js +++ b/flask_wire/static/js/bundle.js @@ -1 +1 @@ -!function(){var t={};Object.defineProperty(t,"__esModule",{value:!0}),t.fetch=O;var e="undefined"!=typeof globalThis&&globalThis||"undefined"!=typeof self&&self||void 0!==e&&e,r="URLSearchParams"in e,i="Symbol"in e&&"iterator"in Symbol,s="FileReader"in e&&"Blob"in e&&function(){try{return new Blob,!0}catch(t){return!1}}(),n="FormData"in e,o="ArrayBuffer"in e;if(o)var a=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],l=ArrayBuffer.isView||function(t){return t&&a.indexOf(Object.prototype.toString.call(t))>-1};function h(t){if("string"!=typeof t&&(t=String(t)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(t)||""===t)throw new TypeError("Invalid character in header field name");return t.toLowerCase()}function u(t){return"string"!=typeof t&&(t=String(t)),t}function c(t){var e={next:function(){var e=t.shift();return{done:void 0===e,value:e}}};return i&&(e[Symbol.iterator]=function(){return e}),e}function d(t){this.map={},t instanceof d?t.forEach((function(t,e){this.append(e,t)}),this):Array.isArray(t)?t.forEach((function(t){this.append(t[0],t[1])}),this):t&&Object.getOwnPropertyNames(t).forEach((function(e){this.append(e,t[e])}),this)}function f(t){if(t.bodyUsed)return Promise.reject(new TypeError("Already read"));t.bodyUsed=!0}function p(t){return new Promise((function(e,r){t.onload=function(){e(t.result)},t.onerror=function(){r(t.error)}}))}function y(t){var e=new FileReader,r=p(e);return e.readAsArrayBuffer(t),r}function m(t){if(t.slice)return t.slice(0);var e=new Uint8Array(t.byteLength);return e.set(new Uint8Array(t)),e.buffer}function b(){return this.bodyUsed=!1,this._initBody=function(t){var e;this.bodyUsed=this.bodyUsed,this._bodyInit=t,t?"string"==typeof t?this._bodyText=t:s&&Blob.prototype.isPrototypeOf(t)?this._bodyBlob=t:n&&FormData.prototype.isPrototypeOf(t)?this._bodyFormData=t:r&&URLSearchParams.prototype.isPrototypeOf(t)?this._bodyText=t.toString():o&&s&&(e=t)&&DataView.prototype.isPrototypeOf(e)?(this._bodyArrayBuffer=m(t.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):o&&(ArrayBuffer.prototype.isPrototypeOf(t)||l(t))?this._bodyArrayBuffer=m(t):this._bodyText=t=Object.prototype.toString.call(t):this._bodyText="",this.headers.get("content-type")||("string"==typeof t?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):r&&URLSearchParams.prototype.isPrototypeOf(t)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},s&&(this.blob=function(){var t=f(this);if(t)return t;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){return this._bodyArrayBuffer?f(this)||(ArrayBuffer.isView(this._bodyArrayBuffer)?Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength)):Promise.resolve(this._bodyArrayBuffer)):this.blob().then(y)}),this.text=function(){var t,e,r,i=f(this);if(i)return i;if(this._bodyBlob)return t=this._bodyBlob,r=p(e=new FileReader),e.readAsText(t),r;if(this._bodyArrayBuffer)return Promise.resolve(function(t){for(var e=new Uint8Array(t),r=new Array(e.length),i=0;i-1?i:r),this.mode=e.mode||this.mode||null,this.signal=e.signal||this.signal,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&s)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(s),!("GET"!==this.method&&"HEAD"!==this.method||"no-store"!==e.cache&&"no-cache"!==e.cache)){var n=/([?&])_=[^&]*/;n.test(this.url)?this.url=this.url.replace(n,"$1_="+(new Date).getTime()):this.url+=(/\?/.test(this.url)?"&":"?")+"_="+(new Date).getTime()}}function v(t){var e=new FormData;return t.trim().split("&").forEach((function(t){if(t){var r=t.split("="),i=r.shift().replace(/\+/g," "),s=r.join("=").replace(/\+/g," ");e.append(decodeURIComponent(i),decodeURIComponent(s))}})),e}function E(t,e){if(!(this instanceof E))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');e||(e={}),this.type="default",this.status=void 0===e.status?200:e.status,this.ok=this.status>=200&&this.status<300,this.statusText="statusText"in e?e.statusText:"",this.headers=new d(e.headers),this.url=e.url||"",this._initBody(t)}w.prototype.clone=function(){return new w(this,{body:this._bodyInit})},b.call(w.prototype),b.call(E.prototype),E.prototype.clone=function(){return new E(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new d(this.headers),url:this.url})},E.error=function(){var t=new E(null,{status:0,statusText:""});return t.type="error",t};var A=[301,302,303,307,308];E.redirect=function(t,e){if(-1===A.indexOf(e))throw new RangeError("Invalid status code");return new E(null,{status:e,headers:{location:t}})};var T=e.DOMException;try{new T}catch(q){(T=function(t,e){this.message=t,this.name=e;var r=Error(t);this.stack=r.stack}).prototype=Object.create(Error.prototype),T.prototype.constructor=T}function O(t,r){return new Promise((function(i,n){var a=new w(t,r);if(a.signal&&a.signal.aborted)return n(new T("Aborted","AbortError"));var l=new XMLHttpRequest;function h(){l.abort()}l.onload=function(){var t,e,r={status:l.status,statusText:l.statusText,headers:(t=l.getAllResponseHeaders()||"",e=new d,t.replace(/\r?\n[\t ]+/g," ").split("\r").map((function(t){return 0===t.indexOf("\n")?t.substr(1,t.length):t})).forEach((function(t){var r=t.split(":"),i=r.shift().trim();if(i){var s=r.join(":").trim();e.append(i,s)}})),e)};r.url="responseURL"in l?l.responseURL:r.headers.get("X-Request-URL");var s="response"in l?l.response:l.responseText;setTimeout((function(){i(new E(s,r))}),0)},l.onerror=function(){setTimeout((function(){n(new TypeError("Network request failed"))}),0)},l.ontimeout=function(){setTimeout((function(){n(new TypeError("Network request failed"))}),0)},l.onabort=function(){setTimeout((function(){n(new T("Aborted","AbortError"))}),0)},l.open(a.method,function(t){try{return""===t&&e.location.href?e.location.href:t}catch(r){return t}}(a.url),!0),"include"===a.credentials?l.withCredentials=!0:"omit"===a.credentials&&(l.withCredentials=!1),"responseType"in l&&(s?l.responseType="blob":o&&a.headers.get("Content-Type")&&-1!==a.headers.get("Content-Type").indexOf("application/octet-stream")&&(l.responseType="arraybuffer")),!r||"object"!=typeof r.headers||r.headers instanceof d?a.headers.forEach((function(t,e){l.setRequestHeader(e,t)})):Object.getOwnPropertyNames(r.headers).forEach((function(t){l.setRequestHeader(t,u(r.headers[t]))})),a.signal&&(a.signal.addEventListener("abort",h),l.onreadystatechange=function(){4===l.readyState&&a.signal.removeEventListener("abort",h)}),l.send(void 0===a._bodyInit?null:a._bodyInit)}))}O.polyfill=!0,e.fetch||(e.fetch=O,e.Headers=d,e.Request=w,e.Response=E);var P={};Object.defineProperty(P,"__esModule",{value:!0}),P.DomUtils=void 0;class B{static selectElementsWithAttribute(t,e){const r=new RegExp(e),i=[],s=t.children;for(let n=0;nencodeURIComponent(e)+"="+encodeURIComponent(t[e])).join("&")}}U.BrowserUtils=D;var F={};Object.defineProperty(F,"__esModule",{value:!0}),F.dispatch=function(t,e=null,r=!1){const i=document.createEvent("Events");if(i.initEvent(t,!0,!0===r),i.cancelable&&!L){const{preventDefault:t}=i;i.preventDefault=function(){this.defaultPrevented||(Object.defineProperty(this,"defaultPrevented",()=>!0),t.call(this))}}return(e||document).dispatchEvent(i),i};const L=(()=>{const t=document.createEvent("Events");return t.initEvent("test",!0,!0),t.preventDefault(),t.defaultPrevented})();var x={};function R(t){if(t.status>=200&&t.status<400)return t;{const e=new Error(t.statusText);throw e.response=t,e}}Object.defineProperty(x,"__esModule",{value:!0}),x.checkStatus=R;class k{constructor(t,e,r=null){this.id=t,this.initial=r,this.source=null,this.element=null,this.errorBody=null,this.loader=null,this.loaderDisplayType="block",this.body=null,this.init(e)}init(t){t.children.length&&(this.errorBody=P.DomUtils.selectFirstElementWithAttribute(t,"wire:error-body"),this.loader=P.DomUtils.selectFirstElementWithAttribute(t,"wire:loader.*")),this.prepareLoader(),this.hideLoader(),this.hideError(),this.element=t}updateBody(t){document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:body='']`).innerHTML=t,(0,F.dispatch)("wire:frame_load"),(0,F.dispatch)("wire:frame_load "+this.id)}updateSource(t,e=null){this.fetchDataFromSource(t,e)}stringToElement(t){const e=document.createElement("template");return e.innerHTML=t.trim(),e.content.firstChild}fetchDataFromSource(e,r=null){this.hideError(),this.showLoader(),(0,t.fetch)(e).then(R).then(t=>t.text()).then(t=>{if(this.updateBody(t),this.source=e,this.showBody(),this.hideError(),null!==r){const e=this.stringToElement(t);r(e)}}).catch(t=>{console.log(t),this.showError()}).finally(()=>{this.hideLoader()})}prepareLoader(){const t=["flex","inline-flex","grid","inline","inline-block","table"];if(null!==this.loader){const e=P.DomUtils.getAttributeNames(this.loader).find(t=>t.startsWith("wire:loader")),r=P.DomUtils.getModifiers(e);if(r.length)for(let i of r)if(t.includes(i)){this.loaderDisplayType=i;break}}}hideLoader(){this.loader&&(P.DomUtils.selectFirstElementWithAttribute(document.querySelector(`div[wire\\:frame='${this.id}']`),"wire:loader.*").style.display="none")}showLoader(){this.loader&&(P.DomUtils.selectFirstElementWithAttribute(document.querySelector(`div[wire\\:frame='${this.id}']`),"wire:loader.*").style.display=this.loaderDisplayType)}hideError(){this.errorBody&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:error-body='']`).style.display="none")}showError(){this.errorBody&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:error-body='']`).style.display="block",this.hideBody())}showBody(){this.body&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:body='']`).style.display="block")}hideBody(){this.body&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:body='']`).style.display="none")}refresh(){null!==this.source&&this.updateSource(this.source)}}class C{constructor(t){this.name=t,this.listeners={}}addListener(t){t.id in this.listeners||(this.listeners[t.id]=t)}removeListener(t){t in this.listeners&&delete this.listeners[t]}fire(){Object.keys(this.listeners).forEach(t=>this.listeners[t].refresh())}}class I{constructor(t,e,r=!1){this.target=t,this.source=e,this.persist=r}fire(){if(this.target){let t=this.source;if(this.target.updateSource(t),this.persist){const e=U.BrowserUtils.getParamsFromUrl(t),r=U.BrowserUtils.addParamsToUrl(location.pathname,e);U.BrowserUtils.updateBrowserUrl(new URL(r,`${location.protocol}//${location.host}`))}}}}class M{constructor(t,e,r,i=null,s=null,n=null){this.url=t,this.method=e,this.data=r,this.target=i,this.eventsToFire=s,this.eventsHandler=n}mutate(){this.target&&this.target.showLoader(),this.submitForm(this.url,this.data,this.method,t=>{if(this.target)this.target.updateBody(t),this.target.hideLoader();else if(this.eventsToFire&&this.eventsHandler){const t=this.eventsToFire.split(",");this.eventsHandler(t)}},()=>{this.target&&this.target.showError()})}submitForm(e,r,i,s=null,n=null){if("GET"===i.toUpperCase()){const o=new URLSearchParams(r).toString();(0,t.fetch)(`${e}?${o}`,{method:i}).then(R).then((function(t){return t.text()})).then((function(t){null!==s&&s(t)})).catch((function(t){console.log(t),null!==n&&n()}))}else(0,t.fetch)(e,{method:i,body:r}).then(R).then((function(t){return t.text()})).then((function(t){null!==s&&s(t)})).catch((function(t){console.log(t),null!==n&&n()}))}}class W{constructor(){this.frames={},this.events={}}load(){this.loadFrames(),this.prepareTriggers(),this.setupDocumentListeners()}registerForEvent(t,e){t in this.events||(this.events[t]=new C(t)),this.events[t].addListener(e)}fireEvents(t){for(let e of t)e in this.events&&this.events[e].fire()}loadFrames(t=document){this.getAllWireFrames(t).forEach(t=>{const e=t.getAttribute("wire:frame"),r=t.getAttribute("wire:init"),i=t.getAttribute("wire:on-event");e in this.frames||(this.frames[e]=new k(e,t,r),null!==i&&i.split(",").forEach(t=>this.registerForEvent(t,this.frames[e])),null!==r&&this.frames[e].updateSource(r,t=>{this.loadFrames(t),this.prepareTriggers(t)}))}),this.doCleanup()}prepareTriggers(t=document){this.getAllWireTriggers(t).forEach(t=>{if(null===t.getAttribute("wire:target"))throw Error('All wire triggers must have a "wire:target" attribute.');"A"===t.tagName&&t.setAttribute("href","javascript:")})}interceptClickEvent(){const t=this,e=e=>{const r=e.target,i=r.getAttribute("wire:target");if("A"===r.tagName&&null!==i){let e=P.DomUtils.getAttribute(r,"wire:source.*");if(null===e)throw Error("Trigger is missing the 'wire:source' attribute");{let s=r.getAttribute(e.name);new I(t.frames[i],s,"wire:source.persist"===e.name).fire()}}};document.addEventListener?document.addEventListener("click",e):document.attachEvent&&document.attachEvent("onclick",e)}interceptSubmitEvent(){const t=this,e=e=>{const r=e.target;if("FORM"===r.tagName&&null!==r.getAttribute("wire:mutate")){const i=r.getAttribute("wire:target"),s=r.getAttribute("wire:event");e.preventDefault&&e.preventDefault();const n=r.getAttribute("action")||window.location.pathname,o=r.getAttribute("method")||"POST",a=new FormData(r);return new M(n,o,a,t.frames[i],s,t.fireEvents).mutate(),!1}};document.addEventListener?document.addEventListener("submit",e):document.attachEvent&&document.attachEvent("submit",e)}doCleanup(){const t=new Set(Object.keys(this.frames)),e=new Set(this.getAllWireFrames().map(t=>t.getAttribute("wire:frame")));[...t].filter(t=>!e.has(t)).forEach(t=>{Object.values(this.events).forEach(e=>e.removeListener(t)),delete this.frames[t]})}setupDocumentListeners(){this.interceptClickEvent(),this.interceptSubmitEvent()}getElementsWithAttribute(t,e){return[].filter.call(t.getElementsByTagName("*"),t=>null!==t.getAttribute(e))}getAllWireFrames(t=document){return this.getElementsWithAttribute(t,"wire:frame")}getAllWireTriggers(t=document){return this.getElementsWithAttribute(t,"wire:trigger")}}window.addEventListener("DOMContentLoaded",(function(){(new W).load()}))}(); \ No newline at end of file +!function(){var t={};Object.defineProperty(t,"__esModule",{value:!0}),t.fetch=B;var e="undefined"!=typeof globalThis&&globalThis||"undefined"!=typeof self&&self||void 0!==e&&e,r="URLSearchParams"in e,i="Symbol"in e&&"iterator"in Symbol,s="FileReader"in e&&"Blob"in e&&function(){try{return new Blob,!0}catch(t){return!1}}(),n="FormData"in e,o="ArrayBuffer"in e;if(o)var a=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],h=ArrayBuffer.isView||function(t){return t&&a.indexOf(Object.prototype.toString.call(t))>-1};function l(t){if("string"!=typeof t&&(t=String(t)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(t)||""===t)throw new TypeError("Invalid character in header field name");return t.toLowerCase()}function u(t){return"string"!=typeof t&&(t=String(t)),t}function c(t){var e={next:function(){var e=t.shift();return{done:void 0===e,value:e}}};return i&&(e[Symbol.iterator]=function(){return e}),e}function d(t){this.map={},t instanceof d?t.forEach((function(t,e){this.append(e,t)}),this):Array.isArray(t)?t.forEach((function(t){this.append(t[0],t[1])}),this):t&&Object.getOwnPropertyNames(t).forEach((function(e){this.append(e,t[e])}),this)}function f(t){if(t.bodyUsed)return Promise.reject(new TypeError("Already read"));t.bodyUsed=!0}function y(t){return new Promise((function(e,r){t.onload=function(){e(t.result)},t.onerror=function(){r(t.error)}}))}function p(t){var e=new FileReader,r=y(e);return e.readAsArrayBuffer(t),r}function m(t){if(t.slice)return t.slice(0);var e=new Uint8Array(t.byteLength);return e.set(new Uint8Array(t)),e.buffer}function b(){return this.bodyUsed=!1,this._initBody=function(t){var e;this.bodyUsed=this.bodyUsed,this._bodyInit=t,t?"string"==typeof t?this._bodyText=t:s&&Blob.prototype.isPrototypeOf(t)?this._bodyBlob=t:n&&FormData.prototype.isPrototypeOf(t)?this._bodyFormData=t:r&&URLSearchParams.prototype.isPrototypeOf(t)?this._bodyText=t.toString():o&&s&&(e=t)&&DataView.prototype.isPrototypeOf(e)?(this._bodyArrayBuffer=m(t.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):o&&(ArrayBuffer.prototype.isPrototypeOf(t)||h(t))?this._bodyArrayBuffer=m(t):this._bodyText=t=Object.prototype.toString.call(t):this._bodyText="",this.headers.get("content-type")||("string"==typeof t?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):r&&URLSearchParams.prototype.isPrototypeOf(t)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},s&&(this.blob=function(){var t=f(this);if(t)return t;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){return this._bodyArrayBuffer?f(this)||(ArrayBuffer.isView(this._bodyArrayBuffer)?Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength)):Promise.resolve(this._bodyArrayBuffer)):this.blob().then(p)}),this.text=function(){var t,e,r,i=f(this);if(i)return i;if(this._bodyBlob)return t=this._bodyBlob,r=y(e=new FileReader),e.readAsText(t),r;if(this._bodyArrayBuffer)return Promise.resolve(function(t){for(var e=new Uint8Array(t),r=new Array(e.length),i=0;i-1?i:r),this.mode=e.mode||this.mode||null,this.signal=e.signal||this.signal,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&s)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(s),!("GET"!==this.method&&"HEAD"!==this.method||"no-store"!==e.cache&&"no-cache"!==e.cache)){var n=/([?&])_=[^&]*/;n.test(this.url)?this.url=this.url.replace(n,"$1_="+(new Date).getTime()):this.url+=(/\?/.test(this.url)?"&":"?")+"_="+(new Date).getTime()}}function v(t){var e=new FormData;return t.trim().split("&").forEach((function(t){if(t){var r=t.split("="),i=r.shift().replace(/\+/g," "),s=r.join("=").replace(/\+/g," ");e.append(decodeURIComponent(i),decodeURIComponent(s))}})),e}function E(t,e){if(!(this instanceof E))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');e||(e={}),this.type="default",this.status=void 0===e.status?200:e.status,this.ok=this.status>=200&&this.status<300,this.statusText="statusText"in e?e.statusText:"",this.headers=new d(e.headers),this.url=e.url||"",this._initBody(t)}w.prototype.clone=function(){return new w(this,{body:this._bodyInit})},b.call(w.prototype),b.call(E.prototype),E.prototype.clone=function(){return new E(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new d(this.headers),url:this.url})},E.error=function(){var t=new E(null,{status:0,statusText:""});return t.type="error",t};var A=[301,302,303,307,308];E.redirect=function(t,e){if(-1===A.indexOf(e))throw new RangeError("Invalid status code");return new E(null,{status:e,headers:{location:t}})};var T=e.DOMException;try{new T}catch(I){(T=function(t,e){this.message=t,this.name=e;var r=Error(t);this.stack=r.stack}).prototype=Object.create(Error.prototype),T.prototype.constructor=T}function B(t,r){return new Promise((function(i,n){var a=new w(t,r);if(a.signal&&a.signal.aborted)return n(new T("Aborted","AbortError"));var h=new XMLHttpRequest;function l(){h.abort()}h.onload=function(){var t,e,r={status:h.status,statusText:h.statusText,headers:(t=h.getAllResponseHeaders()||"",e=new d,t.replace(/\r?\n[\t ]+/g," ").split("\r").map((function(t){return 0===t.indexOf("\n")?t.substr(1,t.length):t})).forEach((function(t){var r=t.split(":"),i=r.shift().trim();if(i){var s=r.join(":").trim();e.append(i,s)}})),e)};r.url="responseURL"in h?h.responseURL:r.headers.get("X-Request-URL");var s="response"in h?h.response:h.responseText;setTimeout((function(){i(new E(s,r))}),0)},h.onerror=function(){setTimeout((function(){n(new TypeError("Network request failed"))}),0)},h.ontimeout=function(){setTimeout((function(){n(new TypeError("Network request failed"))}),0)},h.onabort=function(){setTimeout((function(){n(new T("Aborted","AbortError"))}),0)},h.open(a.method,function(t){try{return""===t&&e.location.href?e.location.href:t}catch(r){return t}}(a.url),!0),"include"===a.credentials?h.withCredentials=!0:"omit"===a.credentials&&(h.withCredentials=!1),"responseType"in h&&(s?h.responseType="blob":o&&a.headers.get("Content-Type")&&-1!==a.headers.get("Content-Type").indexOf("application/octet-stream")&&(h.responseType="arraybuffer")),!r||"object"!=typeof r.headers||r.headers instanceof d?a.headers.forEach((function(t,e){h.setRequestHeader(e,t)})):Object.getOwnPropertyNames(r.headers).forEach((function(t){h.setRequestHeader(t,u(r.headers[t]))})),a.signal&&(a.signal.addEventListener("abort",l),h.onreadystatechange=function(){4===h.readyState&&a.signal.removeEventListener("abort",l)}),h.send(void 0===a._bodyInit?null:a._bodyInit)}))}B.polyfill=!0,e.fetch||(e.fetch=B,e.Headers=d,e.Request=w,e.Response=E);var U={};Object.defineProperty(U,"__esModule",{value:!0}),U.DomUtils=void 0;class _{static selectElementsWithAttribute(t,e){const r=new RegExp(e),i=[],s=t.children;for(let n=0;nencodeURIComponent(e)+"="+encodeURIComponent(t[e])).join("&")}}P.BrowserUtils=O;var S={};Object.defineProperty(S,"__esModule",{value:!0}),S.dispatch=function(t,e=null,r=!1){const i=document.createEvent("Events");if(i.initEvent(t,!0,!0===r),i.cancelable&&!j){const{preventDefault:t}=i;i.preventDefault=function(){this.defaultPrevented||(Object.defineProperty(this,"defaultPrevented",()=>!0),t.call(this))}}return(e||document).dispatchEvent(i),i};const j=(()=>{const t=document.createEvent("Events");return t.initEvent("test",!0,!0),t.preventDefault(),t.defaultPrevented})();var D={};function F(t){if(t.status>=200&&t.status<400)return t;{const e=new Error(t.statusText);throw e.response=t,e}}Object.defineProperty(D,"__esModule",{value:!0}),D.checkStatus=F;class L{constructor(t,e,r=null){this.id=t,this.initial=r,this.source=null,this.element=null,this.errorBody=null,this.loader=null,this.loaderDisplayType="block",this.body=null,this.init(e)}init(t){t.children.length&&(this.errorBody=U.DomUtils.selectFirstElementWithAttribute(t,"wire:error-body"),this.loader=U.DomUtils.selectFirstElementWithAttribute(t,"wire:loader.*")),this.prepareLoader(),this.hideLoader(),this.hideError(),this.element=t}updateBody(t){document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:body='']`).innerHTML=t,(0,S.dispatch)("wire:frame_load"),(0,S.dispatch)("wire:frame_load "+this.id)}updateSource(t,e=null){this.fetchDataFromSource(t,e)}stringToElement(t){const e=document.createElement("template");return e.innerHTML=t.trim(),e.content.firstChild}fetchDataFromSource(e,r=null){this.hideError(),this.showLoader(),(0,t.fetch)(e).then(F).then(t=>t.text()).then(t=>{if(this.updateBody(t),this.source=e,this.showBody(),this.hideError(),null!==r){const e=this.stringToElement(t);r(e)}}).catch(t=>{console.log(t),this.showError()}).finally(()=>{this.hideLoader()})}prepareLoader(){const t=["flex","inline-flex","grid","inline","inline-block","table"];if(null!==this.loader){const e=U.DomUtils.getAttributeNames(this.loader).find(t=>t.startsWith("wire:loader")),r=U.DomUtils.getModifiers(e);if(r.length)for(let i of r)if(t.includes(i)){this.loaderDisplayType=i;break}}}hideLoader(){this.loader&&(U.DomUtils.selectFirstElementWithAttribute(document.querySelector(`div[wire\\:frame='${this.id}']`),"wire:loader.*").style.display="none")}showLoader(){this.loader&&(U.DomUtils.selectFirstElementWithAttribute(document.querySelector(`div[wire\\:frame='${this.id}']`),"wire:loader.*").style.display=this.loaderDisplayType)}hideError(){this.errorBody&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:error-body='']`).style.display="none")}showError(){this.errorBody&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:error-body='']`).style.display="block",this.hideBody())}showBody(){this.body&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:body='']`).style.display="block")}hideBody(){this.body&&(document.querySelector(`div[wire\\:frame='${this.id}'] > div[wire\\:body='']`).style.display="none")}refresh(){null!==this.source&&this.updateSource(this.source)}}class x{constructor(t){this.name=t,this.listeners={}}addListener(t){t.id in this.listeners||(this.listeners[t.id]=t)}removeListener(t){t in this.listeners&&delete this.listeners[t]}fire(){Object.keys(this.listeners).forEach(t=>this.listeners[t].refresh())}}class R{constructor(t,e,r=!1){this.target=t,this.source=e,this.persist=r}fire(){if(this.target){let t=this.source;if(this.target.updateSource(t),this.persist){const e=P.BrowserUtils.getParamsFromUrl(t),r=P.BrowserUtils.addParamsToUrl(location.pathname,e);P.BrowserUtils.updateBrowserUrl(new URL(r,`${location.protocol}//${location.host}`))}}}}class k{constructor(t,e,r,i=null,s=null,n=null){this.url=t,this.method=e,this.data=r,this.target=i,this.eventsToFire=s,this.eventsHandler=n}mutate(){this.target&&this.target.showLoader(),this.submitForm(this.url,this.data,this.method,t=>{if(this.target)this.target.updateBody(t),this.target.hideLoader();else if(this.eventsToFire&&this.eventsHandler){const t=this.eventsToFire.split(",");this.eventsHandler(t)}},()=>{this.target&&this.target.showError()})}submitForm(e,r,i,s=null,n=null){if("GET"===i.toUpperCase()){const o=new URLSearchParams(r).toString();(0,t.fetch)(`${e}?${o}`,{method:i}).then(F).then((function(t){return t.text()})).then((function(t){null!==s&&s(t)})).catch((function(t){console.log(t),null!==n&&n()}))}else(0,t.fetch)(e,{method:i,body:r}).then(F).then((function(t){return t.text()})).then((function(t){null!==s&&s(t)})).catch((function(t){console.log(t),null!==n&&n()}))}}class C{constructor(){var t,e;e=t=>{console.log(this.events);for(let e of t)e in this.events&&this.events[e].fire()},(t="fireEvents")in this?Object.defineProperty(this,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):this[t]=e,this.frames={},this.events={}}load(){this.loadFrames(),this.prepareTriggers(),this.setupDocumentListeners()}registerForEvent(t,e){console.log(this.events),t in this.events||(this.events[t]=new x(t)),this.events[t].addListener(e)}loadFrames(t=document){this.getAllWireFrames(t).forEach(t=>{const e=t.getAttribute("wire:frame"),r=t.getAttribute("wire:init"),i=t.getAttribute("wire:on-event");e in this.frames||(this.frames[e]=new L(e,t,r),null!==i&&i.split(",").forEach(t=>this.registerForEvent(t,this.frames[e])),null!==r&&this.frames[e].updateSource(r,t=>{this.loadFrames(t),this.prepareTriggers(t)}))}),this.doCleanup()}prepareTriggers(t=document){this.getAllWireTriggers(t).forEach(t=>{if(null===t.getAttribute("wire:target"))throw Error('All wire triggers must have a "wire:target" attribute.');"A"===t.tagName&&t.setAttribute("href","javascript:")})}interceptClickEvent(){const t=this,e=e=>{const r=e.target,i=r.getAttribute("wire:target");if("A"===r.tagName&&null!==i){let e=U.DomUtils.getAttribute(r,"wire:source.*");if(null===e)throw Error("Trigger is missing the 'wire:source' attribute");{let s=r.getAttribute(e.name);new R(t.frames[i],s,"wire:source.persist"===e.name).fire()}}};document.addEventListener?document.addEventListener("click",e):document.attachEvent&&document.attachEvent("onclick",e)}interceptSubmitEvent(){const t=this,e=e=>{const r=e.target;if("FORM"===r.tagName&&null!==r.getAttribute("wire:mutate")){const i=r.getAttribute("wire:target"),s=r.getAttribute("wire:event");e.preventDefault&&e.preventDefault();const n=r.getAttribute("action")||window.location.pathname,o=r.getAttribute("method")||"POST",a=new FormData(r);return new k(n,o,a,t.frames[i],s,t.fireEvents).mutate(),!1}};document.addEventListener?document.addEventListener("submit",e):document.attachEvent&&document.attachEvent("submit",e)}doCleanup(){const t=new Set(Object.keys(this.frames)),e=new Set(this.getAllWireFrames().map(t=>t.getAttribute("wire:frame")));[...t].filter(t=>!e.has(t)).forEach(t=>{Object.values(this.events).forEach(e=>e.removeListener(t)),delete this.frames[t]})}setupDocumentListeners(){this.interceptClickEvent(),this.interceptSubmitEvent()}getElementsWithAttribute(t,e){return[].filter.call(t.getElementsByTagName("*"),t=>null!==t.getAttribute(e))}getAllWireFrames(t=document){return this.getElementsWithAttribute(t,"wire:frame")}getAllWireTriggers(t=document){return this.getElementsWithAttribute(t,"wire:trigger")}}window.addEventListener("DOMContentLoaded",(function(){(new C).load()}))}(); \ No newline at end of file diff --git a/js/index.js b/js/index.js index 3ef384b..c45eb9c 100644 --- a/js/index.js +++ b/js/index.js @@ -290,7 +290,7 @@ class WireManager { this.events[eventName].addListener(listener) } - fireEvents(events) { + fireEvents = (events) => { for (let event of events) { if (event in this.events) { this.events[event].fire() diff --git a/setup.py b/setup.py index 0541271..98a7b45 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ import codecs -version = '1.0.3' +version = '1.0.4' setup( name='Flask-Wire',