katalyst-kpop 4.0.1 → 4.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4fd1aee213d132edf3577efecbc1f3e9503753a545af8cf734235f0d9992ea0
4
- data.tar.gz: 92507a2dbf8bd77f5a075687f944acb4dfab278a8e767905ee1303e5ac73d409
3
+ metadata.gz: 41e97c3878b81b50b1d698ae4cc33d3bb8ed4881a5768a5977e62e9e282c46b0
4
+ data.tar.gz: 62be5f4be46723f17da8e6380465e1b0b25227751d451d3b14138543a02349b6
5
5
  SHA512:
6
- metadata.gz: 916cef4ba176e3ac8ccbadca46011fc238029f767ea6d5d52d4961fd9453c1a36d6079ced7b2975a76e97acbde5920e06d084d3c211d12f2eca204682a5959c3
7
- data.tar.gz: 81d567cf5ef19ea49e7b28d018fc2c57438c472c643af40df7961f4c83ddc4439ebbbe3e9ef76864fd33fb524cf7320964d08de70c24083d8ca1bb5d1b367136
6
+ metadata.gz: cecf864ccadd4b1e5c4b0cf224e7377fb58ec7fe58b6c186c8c44a19e9872bd8ec10ba5a6e25617d6decd8e334af81363af033950cda2c1548892bb3736ff1a2
7
+ data.tar.gz: 51cee27e61a12ad90aa1da523feaaa56a74bb67261102ab41b27362f7e4f4d19258eea7636af8712a92662e3dd7d438975393c7f8f60a901b1339cf49d89c1f0
data/README.md CHANGED
@@ -54,6 +54,17 @@ kpop
54
54
 
55
55
  Rules are regex strings matched against `location.pathname`. When `properties.context === "modal"`, the runtime sets `data-turbo-frame="kpop"` on hovered/focused links so Turbo prefetches them into the modal frame.
56
56
 
57
+ The same rules apply to Turbo visits. The runtime wraps Turbo's adapter so a visit proposed to a matching location, including one started from JavaScript with `Turbo.visit(url)`, opens in the modal frame instead of replacing the page. Callers don't need to pass `{ frame: "kpop" }`; the ruleset decides how the location shows, much like Hotwire Native's path configuration. Visits that already carry a response, such as a form submission that redirects to a matching location, still render as a page, as do refreshes and visits from pages without a kpop frame.
58
+
59
+ As in Hotwire Native, the rules are authoritative: a link with `data-turbo-frame="_top"` to a matching location still opens in the modal. To show a matching location as a page, add a later rule that overrides it, or use `data-turbo="false"` for a full browser visit:
60
+
61
+ ```js
62
+ rules: [
63
+ { patterns: ["^/modal"], properties: { context: "modal" } },
64
+ { patterns: ["^/modal/print"], properties: { context: "page" } },
65
+ ],
66
+ ```
67
+
57
68
  ## Render the frame
58
69
 
59
70
  Every layout that can host a modal needs the frame near the end of `<body>`:
@@ -742,6 +742,81 @@ function openStreamModal() {
742
742
  }
743
743
  }
744
744
 
745
+ /**
746
+ * Routes Turbo visits to locations that match a modal rule into the kpop
747
+ * frame, the way Hotwire Native routes visits with its path configuration.
748
+ * Callers use `Turbo.visit` and the ruleset decides how the location shows.
749
+ *
750
+ * Wraps Turbo's browser adapter, which Turbo does not export, so every other
751
+ * adapter call reaches the browser adapter unchanged.
752
+ */
753
+ class VisitAdapter {
754
+ started = false;
755
+
756
+ constructor(delegate) {
757
+ this.delegate = delegate;
758
+ }
759
+
760
+ start() {
761
+ if (this.started) return;
762
+
763
+ const adapter = (this.adapter = Turbo$1.session.adapter);
764
+ const visitAdapter = this;
765
+
766
+ Turbo$1.registerAdapter(
767
+ Object.assign(Object.create(adapter), {
768
+ visitProposedToLocation(location, options) {
769
+ if (visitAdapter.#shouldVisitInFrame(location, options)) {
770
+ Turbo$1.visit(location, { frame: "kpop" });
771
+ } else {
772
+ adapter.visitProposedToLocation(location, options);
773
+ }
774
+ },
775
+ }),
776
+ );
777
+
778
+ this.started = true;
779
+ }
780
+
781
+ stop() {
782
+ if (!this.started) return;
783
+
784
+ Turbo$1.registerAdapter(this.adapter);
785
+ delete this.adapter;
786
+
787
+ this.started = false;
788
+ }
789
+
790
+ /**
791
+ * Like Hotwire Native, the rules decide how a location shows, whatever the
792
+ * link that proposed it says, so `data-turbo-frame="_top"` does not opt out.
793
+ * Use a rule override or `data-turbo="false"` instead. Turbo submits
794
+ * `data-turbo-stream` links as forms, so they never reach the adapter.
795
+ */
796
+ #shouldVisitInFrame(location, options) {
797
+ // A response means Turbo has already fetched the page, e.g. after a form
798
+ // submission or a redirect, so let it render.
799
+ if (options?.response) return false;
800
+
801
+ // Refreshes and visits to the current page should not open a modal.
802
+ if (isCurrentPage(location)) return false;
803
+
804
+ // Without a frame, Turbo.visit would propose this visit again.
805
+ if (!(document.getElementById("kpop") instanceof Turbo$1.FrameElement)) {
806
+ return false;
807
+ }
808
+
809
+ return this.delegate.isModalLocation(location);
810
+ }
811
+ }
812
+
813
+ function isCurrentPage(location) {
814
+ return (
815
+ location.pathname + location.search ===
816
+ document.location.pathname + document.location.search
817
+ );
818
+ }
819
+
745
820
  const controllers = [
746
821
  { identifier: "kpop--frame", controllerConstructor: Kpop__FrameController },
747
822
  ];
@@ -758,11 +833,13 @@ class Application {
758
833
  this.ruleset = new Ruleset(rules);
759
834
  this.linkObserver = new LinkObserver(this, document);
760
835
  this.streamActions = new StreamActions();
836
+ this.visitAdapter = new VisitAdapter(this);
761
837
  }
762
838
 
763
839
  start() {
764
840
  this.streamActions.start();
765
841
  this.linkObserver.start();
842
+ this.visitAdapter.start();
766
843
 
767
844
  window.addEventListener(
768
845
  "turbo:before-fetch-request",
@@ -785,11 +862,16 @@ class Application {
785
862
  document.removeEventListener("focusin", debugFocusIn);
786
863
  document.removeEventListener("focusout", debugFocusOut);
787
864
 
865
+ this.visitAdapter.stop();
788
866
  this.streamActions.stop();
789
867
  this.linkObserver.stop();
790
868
  }
791
869
 
792
870
  isModalLink(link, location) {
871
+ return this.isModalLocation(location);
872
+ }
873
+
874
+ isModalLocation(location) {
793
875
  const properties = this.ruleset.properties(location);
794
876
  return properties.context === "modal";
795
877
  }
@@ -742,6 +742,81 @@ function openStreamModal() {
742
742
  }
743
743
  }
744
744
 
745
+ /**
746
+ * Routes Turbo visits to locations that match a modal rule into the kpop
747
+ * frame, the way Hotwire Native routes visits with its path configuration.
748
+ * Callers use `Turbo.visit` and the ruleset decides how the location shows.
749
+ *
750
+ * Wraps Turbo's browser adapter, which Turbo does not export, so every other
751
+ * adapter call reaches the browser adapter unchanged.
752
+ */
753
+ class VisitAdapter {
754
+ started = false;
755
+
756
+ constructor(delegate) {
757
+ this.delegate = delegate;
758
+ }
759
+
760
+ start() {
761
+ if (this.started) return;
762
+
763
+ const adapter = (this.adapter = Turbo$1.session.adapter);
764
+ const visitAdapter = this;
765
+
766
+ Turbo$1.registerAdapter(
767
+ Object.assign(Object.create(adapter), {
768
+ visitProposedToLocation(location, options) {
769
+ if (visitAdapter.#shouldVisitInFrame(location, options)) {
770
+ Turbo$1.visit(location, { frame: "kpop" });
771
+ } else {
772
+ adapter.visitProposedToLocation(location, options);
773
+ }
774
+ },
775
+ }),
776
+ );
777
+
778
+ this.started = true;
779
+ }
780
+
781
+ stop() {
782
+ if (!this.started) return;
783
+
784
+ Turbo$1.registerAdapter(this.adapter);
785
+ delete this.adapter;
786
+
787
+ this.started = false;
788
+ }
789
+
790
+ /**
791
+ * Like Hotwire Native, the rules decide how a location shows, whatever the
792
+ * link that proposed it says, so `data-turbo-frame="_top"` does not opt out.
793
+ * Use a rule override or `data-turbo="false"` instead. Turbo submits
794
+ * `data-turbo-stream` links as forms, so they never reach the adapter.
795
+ */
796
+ #shouldVisitInFrame(location, options) {
797
+ // A response means Turbo has already fetched the page, e.g. after a form
798
+ // submission or a redirect, so let it render.
799
+ if (options?.response) return false;
800
+
801
+ // Refreshes and visits to the current page should not open a modal.
802
+ if (isCurrentPage(location)) return false;
803
+
804
+ // Without a frame, Turbo.visit would propose this visit again.
805
+ if (!(document.getElementById("kpop") instanceof Turbo$1.FrameElement)) {
806
+ return false;
807
+ }
808
+
809
+ return this.delegate.isModalLocation(location);
810
+ }
811
+ }
812
+
813
+ function isCurrentPage(location) {
814
+ return (
815
+ location.pathname + location.search ===
816
+ document.location.pathname + document.location.search
817
+ );
818
+ }
819
+
745
820
  const controllers = [
746
821
  { identifier: "kpop--frame", controllerConstructor: Kpop__FrameController },
747
822
  ];
@@ -758,11 +833,13 @@ class Application {
758
833
  this.ruleset = new Ruleset(rules);
759
834
  this.linkObserver = new LinkObserver(this, document);
760
835
  this.streamActions = new StreamActions();
836
+ this.visitAdapter = new VisitAdapter(this);
761
837
  }
762
838
 
763
839
  start() {
764
840
  this.streamActions.start();
765
841
  this.linkObserver.start();
842
+ this.visitAdapter.start();
766
843
 
767
844
  window.addEventListener(
768
845
  "turbo:before-fetch-request",
@@ -785,11 +862,16 @@ class Application {
785
862
  document.removeEventListener("focusin", debugFocusIn);
786
863
  document.removeEventListener("focusout", debugFocusOut);
787
864
 
865
+ this.visitAdapter.stop();
788
866
  this.streamActions.stop();
789
867
  this.linkObserver.stop();
790
868
  }
791
869
 
792
870
  isModalLink(link, location) {
871
+ return this.isModalLocation(location);
872
+ }
873
+
874
+ isModalLocation(location) {
793
875
  const properties = this.ruleset.properties(location);
794
876
  return properties.context === "modal";
795
877
  }
@@ -1,2 +1,2 @@
1
- import{Controller as e}from"@hotwired/stimulus";import{Turbo as t}from"@hotwired/turbo-rails";let s=!1;const i=function(e){return s?console.debug.bind(console,"[%s] %s",e):n},n=()=>{};Object.defineProperty(i,"enabled",{get:function(){return s},set:function(e){s=e}});class r{constructor(e,t,s=null){this.frame=e,this.element=t,this.uri=new URL(s||t.dataset.src,window.location.origin)}connect(){this.element.addEventListener("cancel",this.cancel),this.element.addEventListener("close",this.close),this.element.addEventListener("mousedown",this.scrim)}disconnect(){this.element.removeEventListener("cancel",this.cancel),this.element.removeEventListener("close",this.close),this.element.removeEventListener("mousedown",this.scrim)}get src(){return this.uri.pathname+this.uri.search+this.uri.hash}cancel=e=>{this.debug("event:cancel",e),e.preventDefault(),this.frame.dismiss({animate:!0,reason:"dialog:cancel"})};close=e=>{this.debug("event:close",e),this.frame.clear({reason:"dialog:close"})};scrim=e=>{"DIALOG"===e.target.tagName&&(this.debug("event:scrim",e),this.frame.dismiss({animate:!0,reason:"dialog:scrim"}))};async open({animate:e=!0}={}){this.debug("open-start",e),await a(this.element,e,()=>this.element.showModal()),this.debug("open-end")}async dismiss({animate:e=!0}={}){this.debug("dismiss-start",e),await a(this.element,e,()=>this.element.removeAttribute("open")),this.debug("dismiss-end"),this.element.close()}beforeVisit(e,t){this.debug("before-visit",t.detail.url),this.frame.clear()}static get debug(){return i(this.name)}get debug(){return i(this.constructor.name)}}function a(e,t,s){if(!t)return s();const i=function(e,t="0.2s"){const s=getComputedStyle(e).getPropertyValue("--animation-duration")||t,i=parseFloat(s);return s.endsWith("ms")?i:1e3*i}(e);return new Promise(n=>{const r=()=>{e.removeEventListener("animationend",r,{once:!0}),clearTimeout(a),e.toggleAttribute("animate",!1),n()};e.addEventListener("animationend",r,{once:!0});const a=setTimeout(r,i);e.toggleAttribute("animate",t),s()})}class o extends r{static connect(e,t){e.open(new o(e,t),{animate:!1})}}class c extends r{static connect(e,t,s){return this.debug("restore",s),e.open(new c(e,t,s),{animate:!1})}static load(e,t,s){return this.debug("load",s),e.open(new c(e,t,s),{animate:!0})}static visit(e,t,s,i){s.hasAttribute("busy")&&(this.debug("clearing src to cancel turbo request"),s.src=""),this.debug("navigate to",e),i()}}class d extends e{static values={open:Boolean};connect(){this.debug("connect",this.element.src),this.element.kpop=this,function(e){const t=e.element.delegate.constructor.prototype;if(t._linkClickIntercepted)return;t._linkClickIntercepted=t.linkClickIntercepted,t.linkClickIntercepted=function(e,s){const i=e?.getAttribute("data-turbo-frame")||this.element.getAttribute("target");let n=document.getElementById(i);n instanceof Turbo.FrameElement||(n=this.element),n.kpop?(n.kpop.debug("navigate-frame %s => %s",n.src,s),c.visit(s,n.kpop,n,()=>{t._linkClickIntercepted.call(this,e,s)})):t._linkClickIntercepted.call(this,e,s)}}(this);const e=this.element.querySelector("dialog");this.element.src&&e?(this.debug("new frame modal",this.element.src),c.connect(this,e,this.element.src).then(()=>{})):e?(this.debug("new content modal",e),o.connect(this,e)):(this.debug("no modal"),this.clear())}disconnect(){this.debug("disconnect",this.element.src),delete this.element.kpop,delete this.modal}openValueChanged(e){this.debug("open-changed",e)}async open(e,{animate:t=!0}={}){return this.isOpen?(this.debug("skip open as already open"),this.modal||=e,!1):(await this.dismissing,this.opening||=Promise.resolve().then(()=>(e.connect(),this.#e(e,{animate:t}))))}async dismiss({animate:e=!0,reason:t=""}={}){return this.debug("event:dismiss",t),this.isOpen?(await this.opening,this.dismissing||=this.#t({animate:e,reason:t})):(this.debug("skip dismiss as already closed"),!1)}clear({reason:e=""}={}){this.debug("event:clear",e),this.modal?.src===this.element.src&&(this.element.src=""),this.element.innerHTML="",this.openValue=!1,this.modal&&this.modal.disconnect(),delete this.modal,delete this.dismissing}beforeFrameRender(e){this.debug("before-frame-render",e.detail.newFrame.baseURI),e.detail.newFrame.id===this.element.id&&(e.preventDefault(),this.dismiss({animate:!0,reason:"before-frame-render"}).then(()=>{this.debug("resume-frame-render",e.detail.newFrame.baseURI),e.detail.resume()}))}beforeStreamRender(e){this.debug("before-stream-render",e.detail);const t=e.detail.render;e.detail.render=async e=>{await this.dismissing,this.debug("stream-render",e),await t(e)}}beforeVisit(e){if(this.debug("before-visit",e.detail.url),e.detail.url===this.element.src)return;if("/resume_historical_location"===new URL(e.detail.url.toString(),document.baseURI).pathname)return e.preventDefault(),this.dismiss();this.isOpen&&this.modal.beforeVisit(this,e)}frameLoad(e){this.debug("frame-load"),c.load(this,e.target.firstElementChild,e.target.src).then(()=>{})}beforeFetchRequest(){const e=document.activeElement;e===document.body?delete this.lastFetchFocusRef:this.lastFetchFocusRef=new WeakRef(e)}get isOpen(){return this.openValue&&!this.dismissing}async#e(e,{animate:t=!0}={}){var s;return this.debug("open-start",{animate:t}),this.previousFocusRef=document.activeElement===document.body?this.lastFetchFocusRef:new WeakRef(document.activeElement),this.debug("capture focus",this.previousFocusRef?.deref()),this.modal=e,this.openValue=!0,this.element.delegate.sourceURL=this.modal.src,await e.open({animate:t}),delete this.opening,this.debug("open-end"),(s=this.modal?.element,s?s.querySelector("[autofocus]")??s.querySelector("button:not([disabled])"):null)?.focus(),Turbo.session.view.forceReloaded&&console.error("Turbo-Frame response is incompatible with current page"),!0}async#t({animate:e=!0,reason:t=""}={}){this.debug("dismiss-start",{animate:e,reason:t}),this.element.isConnected?(this.modal||console.warn("modal missing on dismiss"),await(this.modal?.dismiss({animate:e})),this.clear(),this.previousFocusRef?.deref()?.focus(),this.debug("restore focus",this.previousFocusRef?.deref()),delete this.previousFocusRef,this.debug("dismiss-end")):this.debug("skip dismiss, element detached")}async#s(e){return new Promise(window.requestAnimationFrame).then(e)}get debug(){return i("FrameController")}}class u{started=!1;constructor(e,t){this.delegate=e,this.eventTarget=t}start(){this.started||("loading"===this.eventTarget.readyState?this.eventTarget.addEventListener("DOMContentLoaded",this.#i,{once:!0}):this.#i())}stop(){this.started&&(this.eventTarget.removeEventListener("mouseenter",this.#n,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("turbo:before-prefetch",this.#n,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("focusin",this.#n,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("mouseleave",this.#r,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("focusout",this.#r,{capture:!0,passive:!0}),this.started=!1)}#i=()=>{this.started||(this.started=!0,this.eventTarget.addEventListener("mouseenter",this.#n,{capture:!0,passive:!0}),this.eventTarget.addEventListener("focusin",this.#n,{capture:!0,passive:!0}),this.eventTarget.addEventListener("turbo:before-prefetch",this.#n,{capture:!0,passive:!0}),this.eventTarget.addEventListener("mouseleave",this.#r,{capture:!0,passive:!0}),this.eventTarget.addEventListener("focusout",this.#r,{capture:!0,passive:!0}))};#n=e=>{const t=e.target;if(t.matches&&t.matches("a[href]:not([target^=_]):not([download]):not([data-turbo-frame]")&&this.#a(t)){const e=t,s=function(e){return new URL(e.getAttribute("href").toString(),document.baseURI)}(e);this.delegate.isModalLink(e,s)&&(e.dataset.turboFrame="kpop")}};#r=e=>{const t=e.target;t.matches&&t.matches("a[href][data-turbo-frame='kpop']")&&delete t.dataset.turboFrame};#a(e){return!!e.getAttribute("href")&&(!h(e)&&(!l(e)&&(!m(e)&&!p(e))))}}const h=e=>e.origin!==document.location.origin||!["http:","https:"].includes(e.protocol)||e.hasAttribute("target"),l=e=>e.pathname+e.search===document.location.pathname+document.location.search||e.href.startsWith("#"),m=e=>"false"===e.getAttribute("data-turbo"),p=e=>{const t=e.getAttribute("data-turbo-method");return!(!t||"get"===t.toLowerCase())||(!!g(e)||(!!e.hasAttribute("data-turbo-confirm")||!!e.hasAttribute("data-turbo-stream")))},g=e=>e.hasAttribute("data-remote")||e.hasAttribute("data-behavior")||e.hasAttribute("data-confirm")||e.hasAttribute("data-method");class b{constructor(e=[]){this.rules=[],e.forEach(e=>{this.#o(e)})}properties(e){return this.rules.reduce((t,s)=>s(e,t),{})}#o({patterns:e,properties:t}){e.forEach(e=>{this.rules.push(function(e,t){return(s,i)=>e.test(s.pathname)?{...i,...t}:i}(new RegExp(e),t))})}}class f extends r{static async open(e,t){const s=!e.isOpen;await e.dismiss({animate:s,reason:"turbo-stream.kpop_open"}),e.element.append(t.templateContent);const i=e.element.querySelector("dialog"),n=i.dataset.src;await e.open(new f(e,i,n),{animate:s})}}class v{start(){t.StreamActions.kpop_open=w}stop(){delete t.StreamActions.kpop_open}}function w(){const e=this.targetElements[0]?.kpop;e&&f.open(e,this).then(()=>{})}const L=[{identifier:"kpop--frame",controllerConstructor:d}];class k{static configure(e={}){return this.instance||=new this(e),i.enabled=this.instance.debug,this.instance}constructor({rules:e=[],debug:t=!1}={}){this.config={rules:e,debug:t},this.ruleset=new b(e),this.linkObserver=new u(this,document),this.streamActions=new v}start(){return this.streamActions.start(),this.linkObserver.start(),window.addEventListener("turbo:before-fetch-request",R),this.debug&&(document.addEventListener("focusin",E),document.addEventListener("focusout",A)),this}stop(){window.removeEventListener("turbo:before-fetch-request",R),document.removeEventListener("focusin",E),document.removeEventListener("focusout",A),this.streamActions.stop(),this.linkObserver.stop()}isModalLink(e,t){return"modal"===this.ruleset.properties(t).context}get debug(){return Boolean(this.config.debug)}}const E=e=>i("Application")("focus",e.target),A=e=>i("Application")("blur",e.target),R=e=>{const t=e.detail.fetchOptions.headers;t.Accept?.includes("text/vnd.turbo-stream.html")&&(t["Kpop-Available"]="true")};export{L as controllers,k as default};
1
+ import{Controller as e}from"@hotwired/stimulus";import{Turbo as t}from"@hotwired/turbo-rails";let s=!1;const i=function(e){return s?console.debug.bind(console,"[%s] %s",e):n},n=()=>{};Object.defineProperty(i,"enabled",{get:function(){return s},set:function(e){s=e}});class r{constructor(e,t,s=null){this.frame=e,this.element=t,this.uri=new URL(s||t.dataset.src,window.location.origin)}connect(){this.element.addEventListener("cancel",this.cancel),this.element.addEventListener("close",this.close),this.element.addEventListener("mousedown",this.scrim)}disconnect(){this.element.removeEventListener("cancel",this.cancel),this.element.removeEventListener("close",this.close),this.element.removeEventListener("mousedown",this.scrim)}get src(){return this.uri.pathname+this.uri.search+this.uri.hash}cancel=e=>{this.debug("event:cancel",e),e.preventDefault(),this.frame.dismiss({animate:!0,reason:"dialog:cancel"})};close=e=>{this.debug("event:close",e),this.frame.clear({reason:"dialog:close"})};scrim=e=>{"DIALOG"===e.target.tagName&&(this.debug("event:scrim",e),this.frame.dismiss({animate:!0,reason:"dialog:scrim"}))};async open({animate:e=!0}={}){this.debug("open-start",e),await a(this.element,e,()=>this.element.showModal()),this.debug("open-end")}async dismiss({animate:e=!0}={}){this.debug("dismiss-start",e),await a(this.element,e,()=>this.element.removeAttribute("open")),this.debug("dismiss-end"),this.element.close()}beforeVisit(e,t){this.debug("before-visit",t.detail.url),this.frame.clear()}static get debug(){return i(this.name)}get debug(){return i(this.constructor.name)}}function a(e,t,s){if(!t)return s();const i=function(e,t="0.2s"){const s=getComputedStyle(e).getPropertyValue("--animation-duration")||t,i=parseFloat(s);return s.endsWith("ms")?i:1e3*i}(e);return new Promise(n=>{const r=()=>{e.removeEventListener("animationend",r,{once:!0}),clearTimeout(a),e.toggleAttribute("animate",!1),n()};e.addEventListener("animationend",r,{once:!0});const a=setTimeout(r,i);e.toggleAttribute("animate",t),s()})}class o extends r{static connect(e,t){e.open(new o(e,t),{animate:!1})}}class d extends r{static connect(e,t,s){return this.debug("restore",s),e.open(new d(e,t,s),{animate:!1})}static load(e,t,s){return this.debug("load",s),e.open(new d(e,t,s),{animate:!0})}static visit(e,t,s,i){s.hasAttribute("busy")&&(this.debug("clearing src to cancel turbo request"),s.src=""),this.debug("navigate to",e),i()}}class c extends e{static values={open:Boolean};connect(){this.debug("connect",this.element.src),this.element.kpop=this,function(e){const t=e.element.delegate.constructor.prototype;if(t._linkClickIntercepted)return;t._linkClickIntercepted=t.linkClickIntercepted,t.linkClickIntercepted=function(e,s){const i=e?.getAttribute("data-turbo-frame")||this.element.getAttribute("target");let n=document.getElementById(i);n instanceof Turbo.FrameElement||(n=this.element),n.kpop?(n.kpop.debug("navigate-frame %s => %s",n.src,s),d.visit(s,n.kpop,n,()=>{t._linkClickIntercepted.call(this,e,s)})):t._linkClickIntercepted.call(this,e,s)}}(this);const e=this.element.querySelector("dialog");this.element.src&&e?(this.debug("new frame modal",this.element.src),d.connect(this,e,this.element.src).then(()=>{})):e?(this.debug("new content modal",e),o.connect(this,e)):(this.debug("no modal"),this.clear())}disconnect(){this.debug("disconnect",this.element.src),delete this.element.kpop,delete this.modal}openValueChanged(e){this.debug("open-changed",e)}async open(e,{animate:t=!0}={}){return this.isOpen?(this.debug("skip open as already open"),this.modal||=e,!1):(await this.dismissing,this.opening||=Promise.resolve().then(()=>(e.connect(),this.#e(e,{animate:t}))))}async dismiss({animate:e=!0,reason:t=""}={}){return this.debug("event:dismiss",t),this.isOpen?(await this.opening,this.dismissing||=this.#t({animate:e,reason:t})):(this.debug("skip dismiss as already closed"),!1)}clear({reason:e=""}={}){this.debug("event:clear",e),this.modal?.src===this.element.src&&(this.element.src=""),this.element.innerHTML="",this.openValue=!1,this.modal&&this.modal.disconnect(),delete this.modal,delete this.dismissing}beforeFrameRender(e){this.debug("before-frame-render",e.detail.newFrame.baseURI),e.detail.newFrame.id===this.element.id&&(e.preventDefault(),this.dismiss({animate:!0,reason:"before-frame-render"}).then(()=>{this.debug("resume-frame-render",e.detail.newFrame.baseURI),e.detail.resume()}))}beforeStreamRender(e){this.debug("before-stream-render",e.detail);const t=e.detail.render;e.detail.render=async e=>{await this.dismissing,this.debug("stream-render",e),await t(e)}}beforeVisit(e){if(this.debug("before-visit",e.detail.url),e.detail.url===this.element.src)return;if("/resume_historical_location"===new URL(e.detail.url.toString(),document.baseURI).pathname)return e.preventDefault(),this.dismiss();this.isOpen&&this.modal.beforeVisit(this,e)}frameLoad(e){this.debug("frame-load"),d.load(this,e.target.firstElementChild,e.target.src).then(()=>{})}beforeFetchRequest(){const e=document.activeElement;e===document.body?delete this.lastFetchFocusRef:this.lastFetchFocusRef=new WeakRef(e)}get isOpen(){return this.openValue&&!this.dismissing}async#e(e,{animate:t=!0}={}){var s;return this.debug("open-start",{animate:t}),this.previousFocusRef=document.activeElement===document.body?this.lastFetchFocusRef:new WeakRef(document.activeElement),this.debug("capture focus",this.previousFocusRef?.deref()),this.modal=e,this.openValue=!0,this.element.delegate.sourceURL=this.modal.src,await e.open({animate:t}),delete this.opening,this.debug("open-end"),(s=this.modal?.element,s?s.querySelector("[autofocus]")??s.querySelector("button:not([disabled])"):null)?.focus(),Turbo.session.view.forceReloaded&&console.error("Turbo-Frame response is incompatible with current page"),!0}async#t({animate:e=!0,reason:t=""}={}){this.debug("dismiss-start",{animate:e,reason:t}),this.element.isConnected?(this.modal||console.warn("modal missing on dismiss"),await(this.modal?.dismiss({animate:e})),this.clear(),this.previousFocusRef?.deref()?.focus(),this.debug("restore focus",this.previousFocusRef?.deref()),delete this.previousFocusRef,this.debug("dismiss-end")):this.debug("skip dismiss, element detached")}async#s(e){return new Promise(window.requestAnimationFrame).then(e)}get debug(){return i("FrameController")}}class h{started=!1;constructor(e,t){this.delegate=e,this.eventTarget=t}start(){this.started||("loading"===this.eventTarget.readyState?this.eventTarget.addEventListener("DOMContentLoaded",this.#i,{once:!0}):this.#i())}stop(){this.started&&(this.eventTarget.removeEventListener("mouseenter",this.#n,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("turbo:before-prefetch",this.#n,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("focusin",this.#n,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("mouseleave",this.#r,{capture:!0,passive:!0}),this.eventTarget.removeEventListener("focusout",this.#r,{capture:!0,passive:!0}),this.started=!1)}#i=()=>{this.started||(this.started=!0,this.eventTarget.addEventListener("mouseenter",this.#n,{capture:!0,passive:!0}),this.eventTarget.addEventListener("focusin",this.#n,{capture:!0,passive:!0}),this.eventTarget.addEventListener("turbo:before-prefetch",this.#n,{capture:!0,passive:!0}),this.eventTarget.addEventListener("mouseleave",this.#r,{capture:!0,passive:!0}),this.eventTarget.addEventListener("focusout",this.#r,{capture:!0,passive:!0}))};#n=e=>{const t=e.target;if(t.matches&&t.matches("a[href]:not([target^=_]):not([download]):not([data-turbo-frame]")&&this.#a(t)){const e=t,s=function(e){return new URL(e.getAttribute("href").toString(),document.baseURI)}(e);this.delegate.isModalLink(e,s)&&(e.dataset.turboFrame="kpop")}};#r=e=>{const t=e.target;t.matches&&t.matches("a[href][data-turbo-frame='kpop']")&&delete t.dataset.turboFrame};#a(e){return!!e.getAttribute("href")&&(!u(e)&&(!l(e)&&(!m(e)&&!p(e))))}}const u=e=>e.origin!==document.location.origin||!["http:","https:"].includes(e.protocol)||e.hasAttribute("target"),l=e=>e.pathname+e.search===document.location.pathname+document.location.search||e.href.startsWith("#"),m=e=>"false"===e.getAttribute("data-turbo"),p=e=>{const t=e.getAttribute("data-turbo-method");return!(!t||"get"===t.toLowerCase())||(!!g(e)||(!!e.hasAttribute("data-turbo-confirm")||!!e.hasAttribute("data-turbo-stream")))},g=e=>e.hasAttribute("data-remote")||e.hasAttribute("data-behavior")||e.hasAttribute("data-confirm")||e.hasAttribute("data-method");class b{constructor(e=[]){this.rules=[],e.forEach(e=>{this.#o(e)})}properties(e){return this.rules.reduce((t,s)=>s(e,t),{})}#o({patterns:e,properties:t}){e.forEach(e=>{this.rules.push(function(e,t){return(s,i)=>e.test(s.pathname)?{...i,...t}:i}(new RegExp(e),t))})}}class f extends r{static async open(e,t){const s=!e.isOpen;await e.dismiss({animate:s,reason:"turbo-stream.kpop_open"}),e.element.append(t.templateContent);const i=e.element.querySelector("dialog"),n=i.dataset.src;await e.open(new f(e,i,n),{animate:s})}}class v{start(){t.StreamActions.kpop_open=L}stop(){delete t.StreamActions.kpop_open}}function L(){const e=this.targetElements[0]?.kpop;e&&f.open(e,this).then(()=>{})}class w{started=!1;constructor(e){this.delegate=e}start(){if(this.started)return;const e=this.adapter=t.session.adapter,s=this;t.registerAdapter(Object.assign(Object.create(e),{visitProposedToLocation(i,n){s.#d(i,n)?t.visit(i,{frame:"kpop"}):e.visitProposedToLocation(i,n)}})),this.started=!0}stop(){this.started&&(t.registerAdapter(this.adapter),delete this.adapter,this.started=!1)}#d(e,s){return!s?.response&&(!function(e){return e.pathname+e.search===document.location.pathname+document.location.search}(e)&&(document.getElementById("kpop")instanceof t.FrameElement&&this.delegate.isModalLocation(e)))}}const k=[{identifier:"kpop--frame",controllerConstructor:c}];class E{static configure(e={}){return this.instance||=new this(e),i.enabled=this.instance.debug,this.instance}constructor({rules:e=[],debug:t=!1}={}){this.config={rules:e,debug:t},this.ruleset=new b(e),this.linkObserver=new h(this,document),this.streamActions=new v,this.visitAdapter=new w(this)}start(){return this.streamActions.start(),this.linkObserver.start(),this.visitAdapter.start(),window.addEventListener("turbo:before-fetch-request",y),this.debug&&(document.addEventListener("focusin",A),document.addEventListener("focusout",F)),this}stop(){window.removeEventListener("turbo:before-fetch-request",y),document.removeEventListener("focusin",A),document.removeEventListener("focusout",F),this.visitAdapter.stop(),this.streamActions.stop(),this.linkObserver.stop()}isModalLink(e,t){return this.isModalLocation(t)}isModalLocation(e){return"modal"===this.ruleset.properties(e).context}get debug(){return Boolean(this.config.debug)}}const A=e=>i("Application")("focus",e.target),F=e=>i("Application")("blur",e.target),y=e=>{const t=e.detail.fetchOptions.headers;t.Accept?.includes("text/vnd.turbo-stream.html")&&(t["Kpop-Available"]="true")};export{k as controllers,E as default};
2
2
  //# sourceMappingURL=kpop.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"kpop.min.js","sources":["../../../javascript/kpop/utils/debug.js","../../../javascript/kpop/modals/modal.js","../../../javascript/kpop/modals/content_modal.js","../../../javascript/kpop/modals/frame_modal.js","../../../javascript/kpop/controllers/frame_controller.js","../../../javascript/kpop/utils/link_observer.js","../../../javascript/kpop/utils/ruleset.js","../../../javascript/kpop/modals/stream_modal.js","../../../javascript/kpop/utils/stream_actions.js","../../../javascript/kpop/application.js"],"sourcesContent":["let enabled = false;\n\nconst debug = function (receiver) {\n if (enabled) {\n return console.debug.bind(console, \"[%s] %s\", receiver);\n } else {\n return noop;\n }\n};\n\nconst noop = () => {};\n\nObject.defineProperty(debug, \"enabled\", {\n get: function () {\n return enabled;\n },\n set: function (debug) {\n enabled = debug;\n },\n});\n\nexport default debug;\n","import debug from \"../utils/debug\";\n\nexport class Modal {\n constructor(frame, dialog, src = null) {\n this.frame = frame;\n this.element = dialog;\n this.uri = new URL(src || dialog.dataset.src, window.location.origin);\n }\n\n connect() {\n this.element.addEventListener(\"cancel\", this.cancel);\n this.element.addEventListener(\"close\", this.close);\n this.element.addEventListener(\"mousedown\", this.scrim);\n }\n\n disconnect() {\n this.element.removeEventListener(\"cancel\", this.cancel);\n this.element.removeEventListener(\"close\", this.close);\n this.element.removeEventListener(\"mousedown\", this.scrim);\n }\n\n get src() {\n return this.uri.pathname + this.uri.search + this.uri.hash;\n }\n\n cancel = (e) => {\n this.debug(\"event:cancel\", e);\n\n e.preventDefault();\n\n this.frame.dismiss({ animate: true, reason: \"dialog:cancel\" });\n };\n\n close = (e) => {\n this.debug(\"event:close\", e);\n\n this.frame.clear({ reason: \"dialog:close\" });\n };\n\n scrim = (e) => {\n if (e.target.tagName === \"DIALOG\") {\n this.debug(\"event:scrim\", e);\n\n this.frame.dismiss({ animate: true, reason: \"dialog:scrim\" });\n }\n };\n\n async open({ animate = true } = {}) {\n this.debug(\"open-start\", animate);\n\n await animation(this.element, animate, () => this.element.showModal());\n\n this.debug(\"open-end\");\n }\n\n /**\n * Modals are closed by animating out the modal then removing the modal\n * element from the wrapping frame.\n *\n * @returns {Promise<void>}\n */\n async dismiss({ animate = true } = {}) {\n this.debug(\"dismiss-start\", animate);\n\n await animation(this.element, animate, () =>\n this.element.removeAttribute(\"open\"),\n );\n\n this.debug(\"dismiss-end\");\n\n this.element.close();\n }\n\n /**\n * When user navigates from inside a modal, dismiss the modal first so\n * that the modal does not appear in the history stack.\n *\n * @param frame FrameController\n * @param e Turbo navigation event\n */\n beforeVisit(frame, e) {\n this.debug(`before-visit`, e.detail.url);\n\n this.frame.clear();\n }\n\n static get debug() {\n return debug(this.name);\n }\n\n get debug() {\n return debug(this.constructor.name);\n }\n}\n\nfunction animation(el, animate, trigger) {\n if (!animate) return trigger();\n\n const duration = animationDuration(el);\n\n return new Promise((resolve) => {\n const resolver = () => {\n el.removeEventListener(\"animationend\", resolver, { once: true });\n clearTimeout(timeout);\n el.toggleAttribute(\"animate\", false);\n resolve();\n };\n\n el.addEventListener(\"animationend\", resolver, { once: true });\n const timeout = setTimeout(resolver, duration);\n\n el.toggleAttribute(\"animate\", animate);\n trigger();\n });\n}\n\nfunction animationDuration(el, defaultValue = \"0.2s\") {\n const value =\n getComputedStyle(el).getPropertyValue(\"--animation-duration\") ||\n defaultValue;\n const num = parseFloat(value);\n if (value.endsWith(\"ms\")) return num;\n return num * 1000;\n}\n","import { Modal } from \"./modal\";\n\nexport class ContentModal extends Modal {\n static connect(frame, dialog) {\n frame.open(new ContentModal(frame, dialog), { animate: false });\n }\n}\n","import { Modal } from \"./modal\";\n\nexport class FrameModal extends Modal {\n /**\n * When the FrameController detects a frame element on connect, it runs this\n * method to sanity check the frame src and restore the modal state.\n *\n * @param {Kpop__FrameController} frame\n * @param {HTMLDialogElement} dialog\n * @param {String} src\n */\n static connect(frame, dialog, src) {\n // restoration visit\n this.debug(\"restore\", src);\n return frame.open(new FrameModal(frame, dialog, src), { animate: false });\n }\n\n /**\n * When the FrameController detects a frame load event, it runs this\n * method to open the modal.\n *\n * @param {Kpop__FrameController} frame\n * @param {HTMLDialogElement} dialog\n * @param {String} src\n */\n static load(frame, dialog, src) {\n this.debug(\"load\", src);\n return frame.open(new FrameModal(frame, dialog, src), { animate: true });\n }\n\n /**\n * When a user clicks a kpop link, turbo intercepts the click and calls\n * #navigateFrame on the turbo frame controller before setting the TurboFrame\n * element's src attribute. KPOP intercepts this call and calls this method\n * first so we cancel problematic navigations that might cache invalid states.\n *\n * @param location URL requested by turbo\n * @param frame FrameController\n * @param element TurboFrame element\n * @param resolve continuation chain\n */\n static visit(location, frame, element, resolve) {\n // Ensure that turbo doesn't cache the frame in a loading state by cancelling\n // the current request (if any) by clearing the src.\n // Known issue: this won't work if the frame was previously rendering a useful src.\n if (element.hasAttribute(\"busy\")) {\n this.debug(\"clearing src to cancel turbo request\");\n element.src = \"\";\n }\n\n this.debug(\"navigate to\", location);\n resolve();\n }\n}\n","import { Controller } from \"@hotwired/stimulus\";\n\nimport { ContentModal } from \"../modals/content_modal\";\nimport { FrameModal } from \"../modals/frame_modal\";\n\nimport debug from \"../utils/debug\";\n\nexport default class Kpop__FrameController extends Controller {\n static values = {\n open: Boolean,\n };\n\n connect() {\n this.debug(\"connect\", this.element.src);\n\n this.element.kpop = this;\n\n // allow our code to intercept frame navigation requests before dom changes\n installNavigationInterception(this);\n\n const dialog = this.element.querySelector(\"dialog\");\n\n if (this.element.src && dialog) {\n this.debug(\"new frame modal\", this.element.src);\n FrameModal.connect(this, dialog, this.element.src).then(() => {});\n } else if (dialog) {\n this.debug(\"new content modal\", dialog);\n ContentModal.connect(this, dialog);\n } else {\n this.debug(\"no modal\");\n this.clear();\n }\n }\n\n disconnect() {\n this.debug(\"disconnect\", this.element.src);\n\n delete this.element.kpop;\n delete this.modal;\n }\n\n openValueChanged(open) {\n this.debug(\"open-changed\", open);\n }\n\n /**\n * Animate an attached modal into the foreground. Returns a promise that\n * resolves when the animation is complete.\n *\n * @param modal\n * @param animate\n * @returns {Promise<Boolean>}\n */\n async open(modal, { animate = true } = {}) {\n if (this.isOpen) {\n this.debug(\"skip open as already open\");\n this.modal ||= modal;\n return false;\n }\n\n await this.dismissing;\n\n return (this.opening ||= Promise.resolve().then(() => {\n modal.connect();\n return this.#open(modal, { animate });\n }));\n }\n\n /**\n * Cause a modal to hide. Returns a promise that will resolve when the\n * animation (if requested) is finished.\n *\n * If the modal is already animating out, returns the existing promise instead.\n *\n * @param {Boolean} animate\n * @param {String} reason\n * @returns {Promise}\n */\n async dismiss({ animate = true, reason = \"\" } = {}) {\n this.debug(\"event:dismiss\", reason);\n\n if (!this.isOpen) {\n this.debug(\"skip dismiss as already closed\");\n return false;\n }\n\n await this.opening;\n\n return (this.dismissing ||= this.#dismiss({ animate, reason }));\n }\n\n /**\n * Clean up after a modal is finished dismissing.\n */\n clear({ reason = \"\" } = {}) {\n this.debug(\"event:clear\", reason);\n\n // clear the src from the frame (if any)\n if (this.modal?.src === this.element.src) this.element.src = \"\";\n this.element.innerHTML = \"\";\n\n // mark the modal as hidden\n this.openValue = false;\n\n // unset modal\n if (this.modal) this.modal.disconnect();\n delete this.modal;\n delete this.dismissing;\n }\n\n // EVENTS\n\n /**\n * Incoming frame render, dismiss the current modal (if any) first.\n *\n * We're starting the actual visit\n *\n * @param event turbo:before-render\n */\n beforeFrameRender(event) {\n this.debug(\"before-frame-render\", event.detail.newFrame.baseURI);\n\n if (event.detail.newFrame.id !== this.element.id) return;\n\n event.preventDefault();\n\n this.dismiss({ animate: true, reason: \"before-frame-render\" }).then(() => {\n this.debug(\"resume-frame-render\", event.detail.newFrame.baseURI);\n event.detail.resume();\n });\n }\n\n beforeStreamRender(event) {\n this.debug(\"before-stream-render\", event.detail);\n\n const resume = event.detail.render;\n\n // Defer rendering until dismiss is complete.\n // Dismiss may change history so we need to wait for it to complete to avoid\n // losing DOM changes on restoration visits.\n event.detail.render = async (stream) => {\n await this.dismissing;\n\n this.debug(\"stream-render\", stream);\n\n await resume(stream);\n };\n }\n\n beforeVisit(e) {\n this.debug(\"before-visit\", e.detail.url);\n\n // ignore visits to the current frame, these fire when the frame navigates\n if (e.detail.url === this.element.src) return;\n\n const url = new URL(e.detail.url.toString(), document.baseURI);\n if (url.pathname === \"/resume_historical_location\") {\n e.preventDefault();\n return this.dismiss();\n }\n\n // ignore unless we're open\n if (!this.isOpen) return;\n\n this.modal.beforeVisit(this, e);\n }\n\n frameLoad(e) {\n this.debug(\"frame-load\");\n\n FrameModal.load(this, e.target.firstElementChild, e.target.src).then(\n () => {},\n );\n }\n\n /**\n * Outgoing fetch request. Capture the initiator so we can return focus if it causes a modal to show.\n */\n beforeFetchRequest() {\n const focusElement = document.activeElement;\n\n if (focusElement === document.body) {\n delete this.lastFetchFocusRef;\n } else {\n this.lastFetchFocusRef = new WeakRef(focusElement);\n }\n }\n\n get isOpen() {\n return this.openValue && !this.dismissing;\n }\n\n async #open(modal, { animate = true } = {}) {\n this.debug(\"open-start\", { animate });\n\n this.previousFocusRef =\n document.activeElement === document.body\n ? this.lastFetchFocusRef\n : new WeakRef(document.activeElement);\n this.debug(\"capture focus\", this.previousFocusRef?.deref());\n\n this.modal = modal;\n this.openValue = true;\n\n // Set turbo-frame[src] without causing a load event\n this.element.delegate.sourceURL = this.modal.src;\n\n await modal.open({ animate });\n\n delete this.opening;\n\n this.debug(\"open-end\");\n\n autofocus(this.modal?.element)?.focus();\n\n // Detect https://github.com/hotwired/turbo-rails/issues/580\n if (Turbo.session.view.forceReloaded) {\n console.error(\"Turbo-Frame response is incompatible with current page\");\n }\n\n return true;\n }\n\n async #dismiss({ animate = true, reason = \"\" } = {}) {\n this.debug(\"dismiss-start\", { animate, reason });\n\n // if this element is detached then we've experienced a turbo navigation\n if (!this.element.isConnected) {\n this.debug(\"skip dismiss, element detached\");\n return;\n }\n\n if (!this.modal) {\n console.warn(\"modal missing on dismiss\");\n }\n\n await this.modal?.dismiss({ animate });\n\n this.clear();\n\n this.previousFocusRef?.deref()?.focus();\n this.debug(\"restore focus\", this.previousFocusRef?.deref());\n delete this.previousFocusRef;\n\n this.debug(\"dismiss-end\");\n }\n\n async #nextFrame(callback) {\n return new Promise(window.requestAnimationFrame).then(callback);\n }\n\n get debug() {\n return debug(\"FrameController\");\n }\n}\n\n/**\n * Monkey patch for Turbo#FrameController.\n *\n * Intercept calls to linkClickIntercepted(element, location) and ensures\n * that src is cleared if the frame is busy so that we don't restore an\n * in-progress src on restoration visits.\n *\n * See Turbo issue: https://github.com/hotwired/turbo/issues/1055\n *\n * @param controller FrameController\n */\nfunction installNavigationInterception(controller) {\n const TurboFrameController =\n controller.element.delegate.constructor.prototype;\n\n if (TurboFrameController._linkClickIntercepted) return;\n\n TurboFrameController._linkClickIntercepted =\n TurboFrameController.linkClickIntercepted;\n TurboFrameController.linkClickIntercepted = function (element, location) {\n // #findFrameElement\n const id =\n element?.getAttribute(\"data-turbo-frame\") ||\n this.element.getAttribute(\"target\");\n let frame = document.getElementById(id);\n if (!(frame instanceof Turbo.FrameElement)) {\n frame = this.element;\n }\n\n if (frame.kpop) {\n frame.kpop.debug(\"navigate-frame %s => %s\", frame.src, location);\n FrameModal.visit(location, frame.kpop, frame, () => {\n TurboFrameController._linkClickIntercepted.call(\n this,\n element,\n location,\n );\n });\n } else {\n TurboFrameController._linkClickIntercepted.call(this, element, location);\n }\n };\n}\n\nfunction autofocus(container) {\n if (!container) return null;\n\n return (\n container.querySelector(\"[autofocus]\") ??\n container.querySelector(\"button:not([disabled])\")\n );\n}\n","/**\n * Based on Turbo's LinkObserver, checks links on mouse-over and focus to see\n * whether they should open in modals. If they should, then sets the\n * data-turbo-frame attribute so it will be prefetched and opened in the context\n * of the kpop turbo frame.\n */\nexport default class LinkObserver {\n started = false;\n\n constructor(delegate, eventTarget) {\n this.delegate = delegate;\n this.eventTarget = eventTarget;\n }\n\n start() {\n if (this.started) return;\n if (this.eventTarget.readyState === \"loading\") {\n this.eventTarget.addEventListener(\"DOMContentLoaded\", this.#enable, {\n once: true,\n });\n } else {\n this.#enable();\n }\n }\n\n stop() {\n if (!this.started) return;\n this.eventTarget.removeEventListener(\"mouseenter\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.removeEventListener(\n \"turbo:before-prefetch\",\n this.#addKpopLink,\n {\n capture: true,\n passive: true,\n },\n );\n this.eventTarget.removeEventListener(\"focusin\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.removeEventListener(\"mouseleave\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.removeEventListener(\"focusout\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n this.started = false;\n }\n\n #enable = () => {\n if (this.started) return;\n this.started = true;\n this.eventTarget.addEventListener(\"mouseenter\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.addEventListener(\"focusin\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.addEventListener(\n \"turbo:before-prefetch\",\n this.#addKpopLink,\n {\n capture: true,\n passive: true,\n },\n );\n this.eventTarget.addEventListener(\"mouseleave\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.addEventListener(\"focusout\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n };\n\n #addKpopLink = (event) => {\n const target = event.target;\n const isLink =\n target.matches &&\n target.matches(\n \"a[href]:not([target^=_]):not([download]):not([data-turbo-frame]\",\n );\n if (isLink && this.#isPrefetchable(target)) {\n const link = target;\n const location = getLocationForLink(link);\n if (this.delegate.isModalLink(link, location)) {\n link.dataset.turboFrame = \"kpop\";\n }\n }\n };\n\n #removeKpopLink = (event) => {\n const target = event.target;\n const isLink =\n target.matches && target.matches(\"a[href][data-turbo-frame='kpop']\");\n if (isLink) {\n delete target.dataset.turboFrame;\n }\n };\n\n #isPrefetchable(link) {\n const href = link.getAttribute(\"href\");\n if (!href) return false;\n if (unfetchableLink(link)) return false;\n if (linkToTheSamePage(link)) return false;\n if (linkOptsOut(link)) return false;\n if (nonSafeLink(link)) return false;\n return true;\n }\n}\n\nfunction getLocationForLink(link) {\n return new URL(link.getAttribute(\"href\").toString(), document.baseURI);\n}\n\nconst unfetchableLink = (link) =>\n link.origin !== document.location.origin ||\n ![\"http:\", \"https:\"].includes(link.protocol) ||\n link.hasAttribute(\"target\");\n\nconst linkToTheSamePage = (link) =>\n link.pathname + link.search ===\n document.location.pathname + document.location.search ||\n link.href.startsWith(\"#\");\n\nconst linkOptsOut = (link) => {\n return link.getAttribute(\"data-turbo\") === \"false\";\n};\n\nconst nonSafeLink = (link) => {\n const turboMethod = link.getAttribute(\"data-turbo-method\");\n if (turboMethod && turboMethod.toLowerCase() !== \"get\") return true;\n if (isUJS(link)) return true;\n if (link.hasAttribute(\"data-turbo-confirm\")) return true;\n if (link.hasAttribute(\"data-turbo-stream\")) return true;\n return false;\n};\n\nconst isUJS = (link) =>\n link.hasAttribute(\"data-remote\") ||\n link.hasAttribute(\"data-behavior\") ||\n link.hasAttribute(\"data-confirm\") ||\n link.hasAttribute(\"data-method\");\n","/**\n * Similar to Hotwire's PathConfiguration.json, this class compiles a list of\n * rules to check link hrefs against so that we can identify links that\n * should open in a KPOP modal.\n *\n * Unlike Hotwire Native, we can't intercept 303s in the browser before they\n * load. Browser sandbox prevents us from inspecting the location of redirect\n * requests so we can only intercept links that match modals directly.\n *\n * For posts and redirects, we need server support (flash modals, streams).\n */\nexport default class Ruleset {\n constructor(rules = []) {\n this.rules = [];\n\n rules.forEach((rule) => {\n this.#compileRule(rule);\n });\n }\n\n /**\n * Returns properties for the given URL\n *\n * @param {URL} location\n * @returns {} properties\n */\n properties(location) {\n return this.rules.reduce((c, f) => f(location, c), {});\n }\n\n #compileRule({ patterns, properties }) {\n patterns.forEach((pattern) => {\n this.rules.push(locationMatcher(new RegExp(pattern), properties));\n });\n }\n}\n\nfunction locationMatcher(re, properties) {\n return (location, accumulator) =>\n re.test(location.pathname)\n ? { ...accumulator, ...properties }\n : accumulator;\n}\n","import { Modal } from \"./modal\";\n\nexport class StreamModal extends Modal {\n /**\n * When a turbo-stream[action=kpop_open] element is rendered, it runs this\n * method to load the modal template as a StreamModal.\n *\n * @param {Kpop__FrameController} frame\n * @param {Turbo.StreamElement} action\n */\n static async open(frame, action) {\n const animate = !frame.isOpen;\n\n await frame.dismiss({ animate, reason: \"turbo-stream.kpop_open\" });\n\n frame.element.append(action.templateContent);\n\n const dialog = frame.element.querySelector(\"dialog\");\n const src = dialog.dataset.src;\n\n await frame.open(new StreamModal(frame, dialog, src), { animate });\n }\n}\n","import { Turbo } from \"@hotwired/turbo-rails\";\n\nimport { StreamModal } from \"../modals/stream_modal\";\n\nexport default class StreamActions {\n start() {\n Turbo.StreamActions.kpop_open = openStreamModal;\n }\n\n stop() {\n delete Turbo.StreamActions.kpop_open;\n }\n}\n\nfunction openStreamModal() {\n const frame = this.targetElements[0]?.kpop;\n\n if (frame) {\n StreamModal.open(frame, this).then(() => {});\n }\n}\n","import FrameController from \"../kpop/controllers/frame_controller\";\n\nimport debug from \"./utils/debug\";\nimport LinkObserver from \"./utils/link_observer\";\nimport Ruleset from \"./utils/ruleset\";\nimport StreamActions from \"./utils/stream_actions\";\n\nexport const controllers = [\n { identifier: \"kpop--frame\", controllerConstructor: FrameController },\n];\n\nexport default class Application {\n static configure(config = {}) {\n this.instance ||= new this(config);\n debug.enabled = this.instance.debug;\n return this.instance;\n }\n\n constructor({ rules = [], debug = false } = {}) {\n this.config = { rules, debug };\n this.ruleset = new Ruleset(rules);\n this.linkObserver = new LinkObserver(this, document);\n this.streamActions = new StreamActions();\n }\n\n start() {\n this.streamActions.start();\n this.linkObserver.start();\n\n window.addEventListener(\n \"turbo:before-fetch-request\",\n addKpopToRequestHeaders,\n );\n\n if (this.debug) {\n document.addEventListener(\"focusin\", debugFocusIn);\n document.addEventListener(\"focusout\", debugFocusOut);\n }\n\n return this;\n }\n\n stop() {\n window.removeEventListener(\n \"turbo:before-fetch-request\",\n addKpopToRequestHeaders,\n );\n document.removeEventListener(\"focusin\", debugFocusIn);\n document.removeEventListener(\"focusout\", debugFocusOut);\n\n this.streamActions.stop();\n this.linkObserver.stop();\n }\n\n isModalLink(link, location) {\n const properties = this.ruleset.properties(location);\n return properties.context === \"modal\";\n }\n\n get debug() {\n return Boolean(this.config.debug);\n }\n}\n\nconst debugFocusIn = (e) => debug(\"Application\")(\"focus\", e.target);\nconst debugFocusOut = (e) => debug(\"Application\")(\"blur\", e.target);\n\nconst addKpopToRequestHeaders = (e) => {\n const headers = e.detail.fetchOptions.headers;\n\n if (headers[\"Accept\"]?.includes(\"text/vnd.turbo-stream.html\")) {\n headers[\"Kpop-Available\"] = \"true\";\n }\n};\n"],"names":["enabled","debug","receiver","console","bind","noop","Object","defineProperty","get","set","Modal","constructor","frame","dialog","src","this","element","uri","URL","dataset","window","location","origin","connect","addEventListener","cancel","close","scrim","disconnect","removeEventListener","pathname","search","hash","e","preventDefault","dismiss","animate","reason","clear","target","tagName","open","animation","showModal","removeAttribute","beforeVisit","detail","url","name","el","trigger","duration","defaultValue","value","getComputedStyle","getPropertyValue","num","parseFloat","endsWith","animationDuration","Promise","resolve","resolver","once","clearTimeout","timeout","toggleAttribute","setTimeout","ContentModal","FrameModal","load","visit","hasAttribute","Kpop__FrameController","Controller","static","Boolean","kpop","controller","TurboFrameController","delegate","prototype","_linkClickIntercepted","linkClickIntercepted","id","getAttribute","document","getElementById","Turbo","FrameElement","call","installNavigationInterception","querySelector","then","modal","openValueChanged","isOpen","dismissing","opening","innerHTML","openValue","beforeFrameRender","event","newFrame","baseURI","resume","beforeStreamRender","render","async","stream","toString","frameLoad","firstElementChild","beforeFetchRequest","focusElement","activeElement","body","lastFetchFocusRef","WeakRef","container","previousFocusRef","deref","sourceURL","focus","session","view","forceReloaded","error","isConnected","warn","nextFrame","callback","requestAnimationFrame","LinkObserver","started","eventTarget","start","readyState","enable","stop","addKpopLink","capture","passive","removeKpopLink","matches","isPrefetchable","link","getLocationForLink","isModalLink","turboFrame","unfetchableLink","linkToTheSamePage","linkOptsOut","nonSafeLink","includes","protocol","href","startsWith","turboMethod","toLowerCase","isUJS","Ruleset","rules","forEach","rule","compileRule","properties","reduce","c","f","patterns","pattern","push","re","accumulator","test","locationMatcher","RegExp","StreamModal","action","append","templateContent","StreamActions","kpop_open","openStreamModal","targetElements","controllers","identifier","controllerConstructor","FrameController","Application","configure","config","instance","ruleset","linkObserver","streamActions","addKpopToRequestHeaders","debugFocusIn","debugFocusOut","context","headers","fetchOptions"],"mappings":"8FAAA,IAAIA,GAAU,EAEd,MAAMC,EAAQ,SAAUC,GACtB,OAAIF,EACKG,QAAQF,MAAMG,KAAKD,QAAS,UAAWD,GAEvCG,CAEX,EAEMA,EAAO,OAEbC,OAAOC,eAAeN,EAAO,UAAW,CACtCO,IAAK,WACH,OAAOR,CACT,EACAS,IAAK,SAAUR,GACbD,EAAUC,CACZ,IChBK,MAAMS,EACX,WAAAC,CAAYC,EAAOC,EAAQC,EAAM,MAC/BC,KAAKH,MAAQA,EACbG,KAAKC,QAAUH,EACfE,KAAKE,IAAM,IAAIC,IAAIJ,GAAOD,EAAOM,QAAQL,IAAKM,OAAOC,SAASC,OAChE,CAEA,OAAAC,GACER,KAAKC,QAAQQ,iBAAiB,SAAUT,KAAKU,QAC7CV,KAAKC,QAAQQ,iBAAiB,QAAST,KAAKW,OAC5CX,KAAKC,QAAQQ,iBAAiB,YAAaT,KAAKY,MAClD,CAEA,UAAAC,GACEb,KAAKC,QAAQa,oBAAoB,SAAUd,KAAKU,QAChDV,KAAKC,QAAQa,oBAAoB,QAASd,KAAKW,OAC/CX,KAAKC,QAAQa,oBAAoB,YAAad,KAAKY,MACrD,CAEA,OAAIb,G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IAAIlL,KAAKiL,GAC3B/L,EAAMD,QAAUe,KAAKkL,SAAShM,MACvBc,KAAKkL,QACd,CAEA,WAAAtL,EAAYuJ,MAAEA,EAAQ,GAAEjK,MAAEA,GAAQ,GAAU,IAC1Cc,KAAKiL,OAAS,CAAE9B,QAAOjK,SACvBc,KAAKmL,QAAU,IAAIjC,EAAQC,GAC3BnJ,KAAKoL,aAAe,IAAI9D,EAAatH,KAAMuE,UAC3CvE,KAAKqL,cAAgB,IAAId,CAC3B,CAEA,KAAA9C,GAcE,OAbAzH,KAAKqL,cAAc5D,QACnBzH,KAAKoL,aAAa3D,QAElBpH,OAAOI,iBACL,6BACA6K,GAGEtL,KAAKd,QACPqF,SAAS9D,iBAAiB,UAAW8K,GACrChH,SAAS9D,iBAAiB,WAAY+K,IAGjCxL,IACT,CAEA,IAAA4H,GACEvH,OAAOS,oBACL,6BACAwK,GAEF/G,SAASzD,oBAAoB,UAAWyK,GACxChH,SAASzD,oBAAoB,WAAY0K,GAEzCxL,KAAKqL,cAAczD,OACnB5H,KAAKoL,aAAaxD,MACpB,CAEA,WAAAS,CAAYF,EAAM7H,GAEhB,MAA8B,UADXN,KAAKmL,QAAQ5B,WAAWjJ,GACzBmL,OACpB,CAEA,SAAIvM,GACF,OAAO2E,QAAQ7D,KAAKiL,OAAO/L,MAC7B,EAGF,MAAMqM,EAAgBrK,GAAMhC,EAAM,cAANA,CAAqB,QAASgC,EAAEM,QACtDgK,EAAiBtK,GAAMhC,EAAM,cAANA,CAAqB,OAAQgC,EAAEM,QAEtD8J,EAA2BpK,IAC/B,MAAMwK,EAAUxK,EAAEa,OAAO4J,aAAaD,QAElCA,EAAgB,QAAG/C,SAAS,gCAC9B+C,EAAQ,kBAAoB"}
1
+ {"version":3,"file":"kpop.min.js","sources":["../../../javascript/kpop/utils/debug.js","../../../javascript/kpop/modals/modal.js","../../../javascript/kpop/modals/content_modal.js","../../../javascript/kpop/modals/frame_modal.js","../../../javascript/kpop/controllers/frame_controller.js","../../../javascript/kpop/utils/link_observer.js","../../../javascript/kpop/utils/ruleset.js","../../../javascript/kpop/modals/stream_modal.js","../../../javascript/kpop/utils/stream_actions.js","../../../javascript/kpop/utils/visit_adapter.js","../../../javascript/kpop/application.js"],"sourcesContent":["let enabled = false;\n\nconst debug = function (receiver) {\n if (enabled) {\n return console.debug.bind(console, \"[%s] %s\", receiver);\n } else {\n return noop;\n }\n};\n\nconst noop = () => {};\n\nObject.defineProperty(debug, \"enabled\", {\n get: function () {\n return enabled;\n },\n set: function (debug) {\n enabled = debug;\n },\n});\n\nexport default debug;\n","import debug from \"../utils/debug\";\n\nexport class Modal {\n constructor(frame, dialog, src = null) {\n this.frame = frame;\n this.element = dialog;\n this.uri = new URL(src || dialog.dataset.src, window.location.origin);\n }\n\n connect() {\n this.element.addEventListener(\"cancel\", this.cancel);\n this.element.addEventListener(\"close\", this.close);\n this.element.addEventListener(\"mousedown\", this.scrim);\n }\n\n disconnect() {\n this.element.removeEventListener(\"cancel\", this.cancel);\n this.element.removeEventListener(\"close\", this.close);\n this.element.removeEventListener(\"mousedown\", this.scrim);\n }\n\n get src() {\n return this.uri.pathname + this.uri.search + this.uri.hash;\n }\n\n cancel = (e) => {\n this.debug(\"event:cancel\", e);\n\n e.preventDefault();\n\n this.frame.dismiss({ animate: true, reason: \"dialog:cancel\" });\n };\n\n close = (e) => {\n this.debug(\"event:close\", e);\n\n this.frame.clear({ reason: \"dialog:close\" });\n };\n\n scrim = (e) => {\n if (e.target.tagName === \"DIALOG\") {\n this.debug(\"event:scrim\", e);\n\n this.frame.dismiss({ animate: true, reason: \"dialog:scrim\" });\n }\n };\n\n async open({ animate = true } = {}) {\n this.debug(\"open-start\", animate);\n\n await animation(this.element, animate, () => this.element.showModal());\n\n this.debug(\"open-end\");\n }\n\n /**\n * Modals are closed by animating out the modal then removing the modal\n * element from the wrapping frame.\n *\n * @returns {Promise<void>}\n */\n async dismiss({ animate = true } = {}) {\n this.debug(\"dismiss-start\", animate);\n\n await animation(this.element, animate, () =>\n this.element.removeAttribute(\"open\"),\n );\n\n this.debug(\"dismiss-end\");\n\n this.element.close();\n }\n\n /**\n * When user navigates from inside a modal, dismiss the modal first so\n * that the modal does not appear in the history stack.\n *\n * @param frame FrameController\n * @param e Turbo navigation event\n */\n beforeVisit(frame, e) {\n this.debug(`before-visit`, e.detail.url);\n\n this.frame.clear();\n }\n\n static get debug() {\n return debug(this.name);\n }\n\n get debug() {\n return debug(this.constructor.name);\n }\n}\n\nfunction animation(el, animate, trigger) {\n if (!animate) return trigger();\n\n const duration = animationDuration(el);\n\n return new Promise((resolve) => {\n const resolver = () => {\n el.removeEventListener(\"animationend\", resolver, { once: true });\n clearTimeout(timeout);\n el.toggleAttribute(\"animate\", false);\n resolve();\n };\n\n el.addEventListener(\"animationend\", resolver, { once: true });\n const timeout = setTimeout(resolver, duration);\n\n el.toggleAttribute(\"animate\", animate);\n trigger();\n });\n}\n\nfunction animationDuration(el, defaultValue = \"0.2s\") {\n const value =\n getComputedStyle(el).getPropertyValue(\"--animation-duration\") ||\n defaultValue;\n const num = parseFloat(value);\n if (value.endsWith(\"ms\")) return num;\n return num * 1000;\n}\n","import { Modal } from \"./modal\";\n\nexport class ContentModal extends Modal {\n static connect(frame, dialog) {\n frame.open(new ContentModal(frame, dialog), { animate: false });\n }\n}\n","import { Modal } from \"./modal\";\n\nexport class FrameModal extends Modal {\n /**\n * When the FrameController detects a frame element on connect, it runs this\n * method to sanity check the frame src and restore the modal state.\n *\n * @param {Kpop__FrameController} frame\n * @param {HTMLDialogElement} dialog\n * @param {String} src\n */\n static connect(frame, dialog, src) {\n // restoration visit\n this.debug(\"restore\", src);\n return frame.open(new FrameModal(frame, dialog, src), { animate: false });\n }\n\n /**\n * When the FrameController detects a frame load event, it runs this\n * method to open the modal.\n *\n * @param {Kpop__FrameController} frame\n * @param {HTMLDialogElement} dialog\n * @param {String} src\n */\n static load(frame, dialog, src) {\n this.debug(\"load\", src);\n return frame.open(new FrameModal(frame, dialog, src), { animate: true });\n }\n\n /**\n * When a user clicks a kpop link, turbo intercepts the click and calls\n * #navigateFrame on the turbo frame controller before setting the TurboFrame\n * element's src attribute. KPOP intercepts this call and calls this method\n * first so we cancel problematic navigations that might cache invalid states.\n *\n * @param location URL requested by turbo\n * @param frame FrameController\n * @param element TurboFrame element\n * @param resolve continuation chain\n */\n static visit(location, frame, element, resolve) {\n // Ensure that turbo doesn't cache the frame in a loading state by cancelling\n // the current request (if any) by clearing the src.\n // Known issue: this won't work if the frame was previously rendering a useful src.\n if (element.hasAttribute(\"busy\")) {\n this.debug(\"clearing src to cancel turbo request\");\n element.src = \"\";\n }\n\n this.debug(\"navigate to\", location);\n resolve();\n }\n}\n","import { Controller } from \"@hotwired/stimulus\";\n\nimport { ContentModal } from \"../modals/content_modal\";\nimport { FrameModal } from \"../modals/frame_modal\";\n\nimport debug from \"../utils/debug\";\n\nexport default class Kpop__FrameController extends Controller {\n static values = {\n open: Boolean,\n };\n\n connect() {\n this.debug(\"connect\", this.element.src);\n\n this.element.kpop = this;\n\n // allow our code to intercept frame navigation requests before dom changes\n installNavigationInterception(this);\n\n const dialog = this.element.querySelector(\"dialog\");\n\n if (this.element.src && dialog) {\n this.debug(\"new frame modal\", this.element.src);\n FrameModal.connect(this, dialog, this.element.src).then(() => {});\n } else if (dialog) {\n this.debug(\"new content modal\", dialog);\n ContentModal.connect(this, dialog);\n } else {\n this.debug(\"no modal\");\n this.clear();\n }\n }\n\n disconnect() {\n this.debug(\"disconnect\", this.element.src);\n\n delete this.element.kpop;\n delete this.modal;\n }\n\n openValueChanged(open) {\n this.debug(\"open-changed\", open);\n }\n\n /**\n * Animate an attached modal into the foreground. Returns a promise that\n * resolves when the animation is complete.\n *\n * @param modal\n * @param animate\n * @returns {Promise<Boolean>}\n */\n async open(modal, { animate = true } = {}) {\n if (this.isOpen) {\n this.debug(\"skip open as already open\");\n this.modal ||= modal;\n return false;\n }\n\n await this.dismissing;\n\n return (this.opening ||= Promise.resolve().then(() => {\n modal.connect();\n return this.#open(modal, { animate });\n }));\n }\n\n /**\n * Cause a modal to hide. Returns a promise that will resolve when the\n * animation (if requested) is finished.\n *\n * If the modal is already animating out, returns the existing promise instead.\n *\n * @param {Boolean} animate\n * @param {String} reason\n * @returns {Promise}\n */\n async dismiss({ animate = true, reason = \"\" } = {}) {\n this.debug(\"event:dismiss\", reason);\n\n if (!this.isOpen) {\n this.debug(\"skip dismiss as already closed\");\n return false;\n }\n\n await this.opening;\n\n return (this.dismissing ||= this.#dismiss({ animate, reason }));\n }\n\n /**\n * Clean up after a modal is finished dismissing.\n */\n clear({ reason = \"\" } = {}) {\n this.debug(\"event:clear\", reason);\n\n // clear the src from the frame (if any)\n if (this.modal?.src === this.element.src) this.element.src = \"\";\n this.element.innerHTML = \"\";\n\n // mark the modal as hidden\n this.openValue = false;\n\n // unset modal\n if (this.modal) this.modal.disconnect();\n delete this.modal;\n delete this.dismissing;\n }\n\n // EVENTS\n\n /**\n * Incoming frame render, dismiss the current modal (if any) first.\n *\n * We're starting the actual visit\n *\n * @param event turbo:before-render\n */\n beforeFrameRender(event) {\n this.debug(\"before-frame-render\", event.detail.newFrame.baseURI);\n\n if (event.detail.newFrame.id !== this.element.id) return;\n\n event.preventDefault();\n\n this.dismiss({ animate: true, reason: \"before-frame-render\" }).then(() => {\n this.debug(\"resume-frame-render\", event.detail.newFrame.baseURI);\n event.detail.resume();\n });\n }\n\n beforeStreamRender(event) {\n this.debug(\"before-stream-render\", event.detail);\n\n const resume = event.detail.render;\n\n // Defer rendering until dismiss is complete.\n // Dismiss may change history so we need to wait for it to complete to avoid\n // losing DOM changes on restoration visits.\n event.detail.render = async (stream) => {\n await this.dismissing;\n\n this.debug(\"stream-render\", stream);\n\n await resume(stream);\n };\n }\n\n beforeVisit(e) {\n this.debug(\"before-visit\", e.detail.url);\n\n // ignore visits to the current frame, these fire when the frame navigates\n if (e.detail.url === this.element.src) return;\n\n const url = new URL(e.detail.url.toString(), document.baseURI);\n if (url.pathname === \"/resume_historical_location\") {\n e.preventDefault();\n return this.dismiss();\n }\n\n // ignore unless we're open\n if (!this.isOpen) return;\n\n this.modal.beforeVisit(this, e);\n }\n\n frameLoad(e) {\n this.debug(\"frame-load\");\n\n FrameModal.load(this, e.target.firstElementChild, e.target.src).then(\n () => {},\n );\n }\n\n /**\n * Outgoing fetch request. Capture the initiator so we can return focus if it causes a modal to show.\n */\n beforeFetchRequest() {\n const focusElement = document.activeElement;\n\n if (focusElement === document.body) {\n delete this.lastFetchFocusRef;\n } else {\n this.lastFetchFocusRef = new WeakRef(focusElement);\n }\n }\n\n get isOpen() {\n return this.openValue && !this.dismissing;\n }\n\n async #open(modal, { animate = true } = {}) {\n this.debug(\"open-start\", { animate });\n\n this.previousFocusRef =\n document.activeElement === document.body\n ? this.lastFetchFocusRef\n : new WeakRef(document.activeElement);\n this.debug(\"capture focus\", this.previousFocusRef?.deref());\n\n this.modal = modal;\n this.openValue = true;\n\n // Set turbo-frame[src] without causing a load event\n this.element.delegate.sourceURL = this.modal.src;\n\n await modal.open({ animate });\n\n delete this.opening;\n\n this.debug(\"open-end\");\n\n autofocus(this.modal?.element)?.focus();\n\n // Detect https://github.com/hotwired/turbo-rails/issues/580\n if (Turbo.session.view.forceReloaded) {\n console.error(\"Turbo-Frame response is incompatible with current page\");\n }\n\n return true;\n }\n\n async #dismiss({ animate = true, reason = \"\" } = {}) {\n this.debug(\"dismiss-start\", { animate, reason });\n\n // if this element is detached then we've experienced a turbo navigation\n if (!this.element.isConnected) {\n this.debug(\"skip dismiss, element detached\");\n return;\n }\n\n if (!this.modal) {\n console.warn(\"modal missing on dismiss\");\n }\n\n await this.modal?.dismiss({ animate });\n\n this.clear();\n\n this.previousFocusRef?.deref()?.focus();\n this.debug(\"restore focus\", this.previousFocusRef?.deref());\n delete this.previousFocusRef;\n\n this.debug(\"dismiss-end\");\n }\n\n async #nextFrame(callback) {\n return new Promise(window.requestAnimationFrame).then(callback);\n }\n\n get debug() {\n return debug(\"FrameController\");\n }\n}\n\n/**\n * Monkey patch for Turbo#FrameController.\n *\n * Intercept calls to linkClickIntercepted(element, location) and ensures\n * that src is cleared if the frame is busy so that we don't restore an\n * in-progress src on restoration visits.\n *\n * See Turbo issue: https://github.com/hotwired/turbo/issues/1055\n *\n * @param controller FrameController\n */\nfunction installNavigationInterception(controller) {\n const TurboFrameController =\n controller.element.delegate.constructor.prototype;\n\n if (TurboFrameController._linkClickIntercepted) return;\n\n TurboFrameController._linkClickIntercepted =\n TurboFrameController.linkClickIntercepted;\n TurboFrameController.linkClickIntercepted = function (element, location) {\n // #findFrameElement\n const id =\n element?.getAttribute(\"data-turbo-frame\") ||\n this.element.getAttribute(\"target\");\n let frame = document.getElementById(id);\n if (!(frame instanceof Turbo.FrameElement)) {\n frame = this.element;\n }\n\n if (frame.kpop) {\n frame.kpop.debug(\"navigate-frame %s => %s\", frame.src, location);\n FrameModal.visit(location, frame.kpop, frame, () => {\n TurboFrameController._linkClickIntercepted.call(\n this,\n element,\n location,\n );\n });\n } else {\n TurboFrameController._linkClickIntercepted.call(this, element, location);\n }\n };\n}\n\nfunction autofocus(container) {\n if (!container) return null;\n\n return (\n container.querySelector(\"[autofocus]\") ??\n container.querySelector(\"button:not([disabled])\")\n );\n}\n","/**\n * Based on Turbo's LinkObserver, checks links on mouse-over and focus to see\n * whether they should open in modals. If they should, then sets the\n * data-turbo-frame attribute so it will be prefetched and opened in the context\n * of the kpop turbo frame.\n */\nexport default class LinkObserver {\n started = false;\n\n constructor(delegate, eventTarget) {\n this.delegate = delegate;\n this.eventTarget = eventTarget;\n }\n\n start() {\n if (this.started) return;\n if (this.eventTarget.readyState === \"loading\") {\n this.eventTarget.addEventListener(\"DOMContentLoaded\", this.#enable, {\n once: true,\n });\n } else {\n this.#enable();\n }\n }\n\n stop() {\n if (!this.started) return;\n this.eventTarget.removeEventListener(\"mouseenter\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.removeEventListener(\n \"turbo:before-prefetch\",\n this.#addKpopLink,\n {\n capture: true,\n passive: true,\n },\n );\n this.eventTarget.removeEventListener(\"focusin\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.removeEventListener(\"mouseleave\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.removeEventListener(\"focusout\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n this.started = false;\n }\n\n #enable = () => {\n if (this.started) return;\n this.started = true;\n this.eventTarget.addEventListener(\"mouseenter\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.addEventListener(\"focusin\", this.#addKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.addEventListener(\n \"turbo:before-prefetch\",\n this.#addKpopLink,\n {\n capture: true,\n passive: true,\n },\n );\n this.eventTarget.addEventListener(\"mouseleave\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n this.eventTarget.addEventListener(\"focusout\", this.#removeKpopLink, {\n capture: true,\n passive: true,\n });\n };\n\n #addKpopLink = (event) => {\n const target = event.target;\n const isLink =\n target.matches &&\n target.matches(\n \"a[href]:not([target^=_]):not([download]):not([data-turbo-frame]\",\n );\n if (isLink && this.#isPrefetchable(target)) {\n const link = target;\n const location = getLocationForLink(link);\n if (this.delegate.isModalLink(link, location)) {\n link.dataset.turboFrame = \"kpop\";\n }\n }\n };\n\n #removeKpopLink = (event) => {\n const target = event.target;\n const isLink =\n target.matches && target.matches(\"a[href][data-turbo-frame='kpop']\");\n if (isLink) {\n delete target.dataset.turboFrame;\n }\n };\n\n #isPrefetchable(link) {\n const href = link.getAttribute(\"href\");\n if (!href) return false;\n if (unfetchableLink(link)) return false;\n if (linkToTheSamePage(link)) return false;\n if (linkOptsOut(link)) return false;\n if (nonSafeLink(link)) return false;\n return true;\n }\n}\n\nfunction getLocationForLink(link) {\n return new URL(link.getAttribute(\"href\").toString(), document.baseURI);\n}\n\nconst unfetchableLink = (link) =>\n link.origin !== document.location.origin ||\n ![\"http:\", \"https:\"].includes(link.protocol) ||\n link.hasAttribute(\"target\");\n\nconst linkToTheSamePage = (link) =>\n link.pathname + link.search ===\n document.location.pathname + document.location.search ||\n link.href.startsWith(\"#\");\n\nconst linkOptsOut = (link) => {\n return link.getAttribute(\"data-turbo\") === \"false\";\n};\n\nconst nonSafeLink = (link) => {\n const turboMethod = link.getAttribute(\"data-turbo-method\");\n if (turboMethod && turboMethod.toLowerCase() !== \"get\") return true;\n if (isUJS(link)) return true;\n if (link.hasAttribute(\"data-turbo-confirm\")) return true;\n if (link.hasAttribute(\"data-turbo-stream\")) return true;\n return false;\n};\n\nconst isUJS = (link) =>\n link.hasAttribute(\"data-remote\") ||\n link.hasAttribute(\"data-behavior\") ||\n link.hasAttribute(\"data-confirm\") ||\n link.hasAttribute(\"data-method\");\n","/**\n * Similar to Hotwire's PathConfiguration.json, this class compiles a list of\n * rules to check link hrefs against so that we can identify links that\n * should open in a KPOP modal.\n *\n * Unlike Hotwire Native, we can't intercept 303s in the browser before they\n * load. Browser sandbox prevents us from inspecting the location of redirect\n * requests so we can only intercept links that match modals directly.\n *\n * For posts and redirects, we need server support (flash modals, streams).\n */\nexport default class Ruleset {\n constructor(rules = []) {\n this.rules = [];\n\n rules.forEach((rule) => {\n this.#compileRule(rule);\n });\n }\n\n /**\n * Returns properties for the given URL\n *\n * @param {URL} location\n * @returns {} properties\n */\n properties(location) {\n return this.rules.reduce((c, f) => f(location, c), {});\n }\n\n #compileRule({ patterns, properties }) {\n patterns.forEach((pattern) => {\n this.rules.push(locationMatcher(new RegExp(pattern), properties));\n });\n }\n}\n\nfunction locationMatcher(re, properties) {\n return (location, accumulator) =>\n re.test(location.pathname)\n ? { ...accumulator, ...properties }\n : accumulator;\n}\n","import { Modal } from \"./modal\";\n\nexport class StreamModal extends Modal {\n /**\n * When a turbo-stream[action=kpop_open] element is rendered, it runs this\n * method to load the modal template as a StreamModal.\n *\n * @param {Kpop__FrameController} frame\n * @param {Turbo.StreamElement} action\n */\n static async open(frame, action) {\n const animate = !frame.isOpen;\n\n await frame.dismiss({ animate, reason: \"turbo-stream.kpop_open\" });\n\n frame.element.append(action.templateContent);\n\n const dialog = frame.element.querySelector(\"dialog\");\n const src = dialog.dataset.src;\n\n await frame.open(new StreamModal(frame, dialog, src), { animate });\n }\n}\n","import { Turbo } from \"@hotwired/turbo-rails\";\n\nimport { StreamModal } from \"../modals/stream_modal\";\n\nexport default class StreamActions {\n start() {\n Turbo.StreamActions.kpop_open = openStreamModal;\n }\n\n stop() {\n delete Turbo.StreamActions.kpop_open;\n }\n}\n\nfunction openStreamModal() {\n const frame = this.targetElements[0]?.kpop;\n\n if (frame) {\n StreamModal.open(frame, this).then(() => {});\n }\n}\n","import { Turbo } from \"@hotwired/turbo-rails\";\n\n/**\n * Routes Turbo visits to locations that match a modal rule into the kpop\n * frame, the way Hotwire Native routes visits with its path configuration.\n * Callers use `Turbo.visit` and the ruleset decides how the location shows.\n *\n * Wraps Turbo's browser adapter, which Turbo does not export, so every other\n * adapter call reaches the browser adapter unchanged.\n */\nexport default class VisitAdapter {\n started = false;\n\n constructor(delegate) {\n this.delegate = delegate;\n }\n\n start() {\n if (this.started) return;\n\n const adapter = (this.adapter = Turbo.session.adapter);\n const visitAdapter = this;\n\n Turbo.registerAdapter(\n Object.assign(Object.create(adapter), {\n visitProposedToLocation(location, options) {\n if (visitAdapter.#shouldVisitInFrame(location, options)) {\n Turbo.visit(location, { frame: \"kpop\" });\n } else {\n adapter.visitProposedToLocation(location, options);\n }\n },\n }),\n );\n\n this.started = true;\n }\n\n stop() {\n if (!this.started) return;\n\n Turbo.registerAdapter(this.adapter);\n delete this.adapter;\n\n this.started = false;\n }\n\n /**\n * Like Hotwire Native, the rules decide how a location shows, whatever the\n * link that proposed it says, so `data-turbo-frame=\"_top\"` does not opt out.\n * Use a rule override or `data-turbo=\"false\"` instead. Turbo submits\n * `data-turbo-stream` links as forms, so they never reach the adapter.\n */\n #shouldVisitInFrame(location, options) {\n // A response means Turbo has already fetched the page, e.g. after a form\n // submission or a redirect, so let it render.\n if (options?.response) return false;\n\n // Refreshes and visits to the current page should not open a modal.\n if (isCurrentPage(location)) return false;\n\n // Without a frame, Turbo.visit would propose this visit again.\n if (!(document.getElementById(\"kpop\") instanceof Turbo.FrameElement)) {\n return false;\n }\n\n return this.delegate.isModalLocation(location);\n }\n}\n\nfunction isCurrentPage(location) {\n return (\n location.pathname + location.search ===\n document.location.pathname + document.location.search\n );\n}\n","import FrameController from \"../kpop/controllers/frame_controller\";\n\nimport debug from \"./utils/debug\";\nimport LinkObserver from \"./utils/link_observer\";\nimport Ruleset from \"./utils/ruleset\";\nimport StreamActions from \"./utils/stream_actions\";\nimport VisitAdapter from \"./utils/visit_adapter\";\n\nexport const controllers = [\n { identifier: \"kpop--frame\", controllerConstructor: FrameController },\n];\n\nexport default class Application {\n static configure(config = {}) {\n this.instance ||= new this(config);\n debug.enabled = this.instance.debug;\n return this.instance;\n }\n\n constructor({ rules = [], debug = false } = {}) {\n this.config = { rules, debug };\n this.ruleset = new Ruleset(rules);\n this.linkObserver = new LinkObserver(this, document);\n this.streamActions = new StreamActions();\n this.visitAdapter = new VisitAdapter(this);\n }\n\n start() {\n this.streamActions.start();\n this.linkObserver.start();\n this.visitAdapter.start();\n\n window.addEventListener(\n \"turbo:before-fetch-request\",\n addKpopToRequestHeaders,\n );\n\n if (this.debug) {\n document.addEventListener(\"focusin\", debugFocusIn);\n document.addEventListener(\"focusout\", debugFocusOut);\n }\n\n return this;\n }\n\n stop() {\n window.removeEventListener(\n \"turbo:before-fetch-request\",\n addKpopToRequestHeaders,\n );\n document.removeEventListener(\"focusin\", debugFocusIn);\n document.removeEventListener(\"focusout\", debugFocusOut);\n\n this.visitAdapter.stop();\n this.streamActions.stop();\n this.linkObserver.stop();\n }\n\n isModalLink(link, location) {\n return this.isModalLocation(location);\n }\n\n isModalLocation(location) {\n const properties = this.ruleset.properties(location);\n return properties.context === \"modal\";\n }\n\n get debug() {\n return Boolean(this.config.debug);\n }\n}\n\nconst debugFocusIn = (e) => debug(\"Application\")(\"focus\", e.target);\nconst debugFocusOut = (e) => debug(\"Application\")(\"blur\", e.target);\n\nconst addKpopToRequestHeaders = (e) => {\n const headers = e.detail.fetchOptions.headers;\n\n if (headers[\"Accept\"]?.includes(\"text/vnd.turbo-stream.html\")) {\n headers[\"Kpop-Available\"] = \"true\";\n }\n};\n"],"names":["enabled","debug","receiver","console","bind","noop","Object","defineProperty","get","set","Modal","constructor","frame","dialog","src","this","element","uri","URL","dataset","window","location","origin","connect","addEventListener","cancel","close","scrim","disconnect","removeEventListener","pathname","search","hash","e","preventDefault","dismiss","animate","reason","clear","target","tagName","open","animation","showModal","removeAttribute","beforeVisit","detail","url","name","el","trigger","duration","defaultValue","value","getComputedStyle","getPropertyValue","num","parseFloat","endsWith","animationDuration","Promise","resolve","resolver","once","clearTimeout","timeout","toggleAttribute","setTimeout","ContentModal","FrameModal","load","visit","hasAttribute","Kpop__FrameController","Controller","static","Boolean","kpop","controller","TurboFrameController","delegate","prototype","_linkClickIntercepted","linkClickIntercepted","id","getAttribute","document","getElementById","Turbo","FrameElement","call","installNavigationInterception","querySelector","then","modal","openValueChanged","isOpen","dismissing","opening","innerHTML","openValue","beforeFrameRender","event","newFrame","baseURI","resume","beforeStreamRender","render","async","stream","toString","frameLoad","firstElementChild","beforeFetchRequest","focusElement","activeElement","body","lastFetchFocusRef","WeakRef","container","previousFocusRef","deref","sourceURL","focus","session","view","forceReloaded","error","isConnected","warn","nextFrame","callback","requestAnimationFrame","LinkObserver","started","eventTarget","start","readyState","enable","stop","addKpopLink","capture","passive","removeKpopLink","matches","isPrefetchable","link","getLocationForLink","isModalLink","turboFrame","unfetchableLink","linkToTheSamePage","linkOptsOut","nonSafeLink","includes","protocol","href","startsWith","turboMethod","toLowerCase","isUJS","Ruleset","rules","forEach","rule","compileRule","properties","reduce","c","f","patterns","pattern","push","re","accumulator","test","locationMatcher","RegExp","StreamModal","action","append","templateContent","StreamActions","kpop_open","openStreamModal","targetElements","VisitAdapter","adapter","visitAdapter","registerAdapter","assign","create","visitProposedToLocation","options","shouldVisitInFrame","response","isCurrentPage","isModalLocation","controllers","identifier","controllerConstructor","FrameController","Application","configure","config","instance","ruleset","linkObserver","streamActions","addKpopToRequestHeaders","debugFocusIn","debugFocusOut","context","headers","fetchOptions"],"mappings":"8FAAA,IAAIA,GAAU,EAEd,MAAMC,EAAQ,SAAUC,GACtB,OAAIF,EACKG,QAAQF,MAAMG,KAAKD,QAAS,UAAWD,GAEvCG,CAEX,EAEMA,EAAO,OAEbC,OAAOC,eAAeN,EAAO,UAAW,CACtCO,IAAK,WACH,OAAOR,CACT,EACAS,IAAK,SAAUR,GACbD,EAAUC,CACZ,IChBK,MAAMS,EACX,WAAAC,CAAYC,EAAOC,EAAQC,EAAM,MAC/BC,KAAKH,MAAQA,EACbG,KAAKC,QAAUH,EACfE,KAAKE,IAAM,IAAIC,IAAIJ,GAAOD,EAAOM,QAAQL,IAAKM,OAAOC,SAASC,OAChE,CAEA,OAAAC,GACER,KAAKC,QAAQQ,iBAAiB,SAAUT,KAAKU,QAC7CV,KAAKC,QAAQQ,iBAAiB,QAAST,KAAKW,OAC5CX,KAAKC,QAAQQ,iBAAiB,YAAaT,KAAKY,MAClD,CAEA,UAAAC,GACEb,KAAKC,QAAQa,oBAAoB,SAAUd,KAAKU,QAChDV,KAAKC,QAAQa,oBAAoB,QAASd,KAAKW,OAC/CX,KAAKC,QAAQa,oBAAoB,YAAad,KAAKY,MACrD,CAEA,OAAIb,GACF,OAAOC,KAAKE,IAAIa,SAAWf,KAAKE,IAAIc,OAAShB,KAAKE,IAAIe,IACxD,CAEAP,OAAUQ,IACRlB,KAAKd,MAAM,eAAgBgC,GAE3BA,EAAEC,iBAEFnB,KAAKH,MAAMuB,QAAQ,CAAEC,SAAS,EAAMC,OAAQ,mBAG9CX,MAASO,IACPlB,KAAKd,MAAM,cAAegC,GAE1BlB,KAAKH,MAAM0B,MAAM,CAAED,OAAQ,kBAG7BV,MAASM,I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E,OACnB5H,KAAKgM,aAAapE,MACpB,CAEA,WAAAS,CAAYF,EAAM7H,GAChB,OAAON,KAAKsL,gBAAgBhL,EAC9B,CAEA,eAAAgL,CAAgBhL,GAEd,MAA8B,UADXN,KAAK+L,QAAQxC,WAAWjJ,GACzB+L,OACpB,CAEA,SAAInN,GACF,OAAO2E,QAAQ7D,KAAK6L,OAAO3M,MAC7B,EAGF,MAAMiN,EAAgBjL,GAAMhC,EAAM,cAANA,CAAqB,QAASgC,EAAEM,QACtD4K,EAAiBlL,GAAMhC,EAAM,cAANA,CAAqB,OAAQgC,EAAEM,QAEtD0K,EAA2BhL,IAC/B,MAAMoL,EAAUpL,EAAEa,OAAOwK,aAAaD,QAElCA,EAAgB,QAAG3D,SAAS,gCAC9B2D,EAAQ,kBAAoB"}
@@ -13,6 +13,8 @@ module Katalyst
13
13
  module FrameRequest
14
14
  extend ActiveSupport::Concern
15
15
 
16
+ SUPPORTED_LOCATION_MAX_LENGTH = 1024
17
+
16
18
  class_methods do
17
19
  # Sets the expectation that these actions will be wrapped in a modal.
18
20
  # Adds custom layouts, rendering, and redirect behaviours to make this
@@ -62,7 +64,11 @@ module Katalyst
62
64
 
63
65
  return if !request.get? || turbo_frame_request? || kpop_stream_request? || hotwire_native_app?
64
66
 
65
- redirect_back_or_to(kpop_fallback_location, status: :see_other, modal_location: request.fullpath)
67
+ kpop_redirect_options = { status: :see_other }.tap do |options|
68
+ options[:modal_location] = request.fullpath if request.fullpath.bytesize <= SUPPORTED_LOCATION_MAX_LENGTH
69
+ end
70
+
71
+ redirect_back_or_to(kpop_fallback_location, **kpop_redirect_options)
66
72
  end
67
73
 
68
74
  def render(...)
@@ -4,6 +4,7 @@ import debug from "./utils/debug";
4
4
  import LinkObserver from "./utils/link_observer";
5
5
  import Ruleset from "./utils/ruleset";
6
6
  import StreamActions from "./utils/stream_actions";
7
+ import VisitAdapter from "./utils/visit_adapter";
7
8
 
8
9
  export const controllers = [
9
10
  { identifier: "kpop--frame", controllerConstructor: FrameController },
@@ -21,11 +22,13 @@ export default class Application {
21
22
  this.ruleset = new Ruleset(rules);
22
23
  this.linkObserver = new LinkObserver(this, document);
23
24
  this.streamActions = new StreamActions();
25
+ this.visitAdapter = new VisitAdapter(this);
24
26
  }
25
27
 
26
28
  start() {
27
29
  this.streamActions.start();
28
30
  this.linkObserver.start();
31
+ this.visitAdapter.start();
29
32
 
30
33
  window.addEventListener(
31
34
  "turbo:before-fetch-request",
@@ -48,11 +51,16 @@ export default class Application {
48
51
  document.removeEventListener("focusin", debugFocusIn);
49
52
  document.removeEventListener("focusout", debugFocusOut);
50
53
 
54
+ this.visitAdapter.stop();
51
55
  this.streamActions.stop();
52
56
  this.linkObserver.stop();
53
57
  }
54
58
 
55
59
  isModalLink(link, location) {
60
+ return this.isModalLocation(location);
61
+ }
62
+
63
+ isModalLocation(location) {
56
64
  const properties = this.ruleset.properties(location);
57
65
  return properties.context === "modal";
58
66
  }
@@ -0,0 +1,76 @@
1
+ import { Turbo } from "@hotwired/turbo-rails";
2
+
3
+ /**
4
+ * Routes Turbo visits to locations that match a modal rule into the kpop
5
+ * frame, the way Hotwire Native routes visits with its path configuration.
6
+ * Callers use `Turbo.visit` and the ruleset decides how the location shows.
7
+ *
8
+ * Wraps Turbo's browser adapter, which Turbo does not export, so every other
9
+ * adapter call reaches the browser adapter unchanged.
10
+ */
11
+ export default class VisitAdapter {
12
+ started = false;
13
+
14
+ constructor(delegate) {
15
+ this.delegate = delegate;
16
+ }
17
+
18
+ start() {
19
+ if (this.started) return;
20
+
21
+ const adapter = (this.adapter = Turbo.session.adapter);
22
+ const visitAdapter = this;
23
+
24
+ Turbo.registerAdapter(
25
+ Object.assign(Object.create(adapter), {
26
+ visitProposedToLocation(location, options) {
27
+ if (visitAdapter.#shouldVisitInFrame(location, options)) {
28
+ Turbo.visit(location, { frame: "kpop" });
29
+ } else {
30
+ adapter.visitProposedToLocation(location, options);
31
+ }
32
+ },
33
+ }),
34
+ );
35
+
36
+ this.started = true;
37
+ }
38
+
39
+ stop() {
40
+ if (!this.started) return;
41
+
42
+ Turbo.registerAdapter(this.adapter);
43
+ delete this.adapter;
44
+
45
+ this.started = false;
46
+ }
47
+
48
+ /**
49
+ * Like Hotwire Native, the rules decide how a location shows, whatever the
50
+ * link that proposed it says, so `data-turbo-frame="_top"` does not opt out.
51
+ * Use a rule override or `data-turbo="false"` instead. Turbo submits
52
+ * `data-turbo-stream` links as forms, so they never reach the adapter.
53
+ */
54
+ #shouldVisitInFrame(location, options) {
55
+ // A response means Turbo has already fetched the page, e.g. after a form
56
+ // submission or a redirect, so let it render.
57
+ if (options?.response) return false;
58
+
59
+ // Refreshes and visits to the current page should not open a modal.
60
+ if (isCurrentPage(location)) return false;
61
+
62
+ // Without a frame, Turbo.visit would propose this visit again.
63
+ if (!(document.getElementById("kpop") instanceof Turbo.FrameElement)) {
64
+ return false;
65
+ }
66
+
67
+ return this.delegate.isModalLocation(location);
68
+ }
69
+ }
70
+
71
+ function isCurrentPage(location) {
72
+ return (
73
+ location.pathname + location.search ===
74
+ document.location.pathname + document.location.search
75
+ );
76
+ }
@@ -15,7 +15,7 @@ module Katalyst
15
15
  end
16
16
 
17
17
  def <<(matcher)
18
- matcher = matcher.new if matcher.is_a?(Class)
18
+ matcher = matcher.new if matcher.is_a?(Class)
19
19
  (@matchers ||= []) << matcher
20
20
  self
21
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-kpop
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
@@ -79,6 +79,7 @@ files:
79
79
  - app/javascript/kpop/utils/link_observer.js
80
80
  - app/javascript/kpop/utils/ruleset.js
81
81
  - app/javascript/kpop/utils/stream_actions.js
82
+ - app/javascript/kpop/utils/visit_adapter.js
82
83
  - app/views/layouts/kpop/frame.html.erb
83
84
  - app/views/layouts/kpop/stream.html.erb
84
85
  - config/importmap.rb
@@ -116,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
117
  - !ruby/object:Gem::Version
117
118
  version: '0'
118
119
  requirements: []
119
- rubygems_version: 4.0.3
120
+ rubygems_version: 4.0.20
120
121
  specification_version: 4
121
122
  summary: Modal library that uses Turbo and Stimulus.
122
123
  test_files: []