Skip to content

Commit d3fe7bd

Browse files
submit button listener
1 parent aeaabc1 commit d3fe7bd

1 file changed

Lines changed: 118 additions & 72 deletions

File tree

Lines changed: 118 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,136 @@
11
<template>
22
<div>
33
<b-embed
4+
ref="preview"
45
:src="linkTasks"
5-
@load="loaded()"
66
:disable-interstitial="true"
7-
ref="preview"
8-
:event-parent-id="_uid" />
7+
:event-parent-id="_uid"
8+
@load="loaded()"
9+
/>
910
</div>
1011
</template>
1112

12-
1313
<script>
14-
export default {
15-
props: {
16-
taskId: {
17-
type: Number,
18-
default: null
19-
},
20-
inboxRuleData: {
21-
type: Object,
22-
default: null
23-
},
24-
propInboxQuickFill: {
25-
type: Object,
26-
default: null
27-
},
14+
export default {
15+
props: {
16+
taskId: {
17+
type: Number,
18+
default: null,
2819
},
29-
data() {
30-
return {
31-
formData: {}
32-
};
20+
inboxRuleData: {
21+
type: Object,
22+
default: null,
3323
},
34-
computed: {
35-
linkTasks() {
36-
return `/tasks/${this.taskId}/edit/preview?dispatchSubmit=1&alwaysAllowEditing=1&disableInterstitial=1`;
37-
},
38-
iframeContentWindow() {
39-
return this.$refs['preview'].firstChild.contentWindow;
40-
}
24+
propInboxQuickFill: {
25+
type: Object,
26+
default: null,
4127
},
42-
watch: {
43-
formData() {
44-
this.$emit("data", this.formData);
45-
},
46-
propInboxQuickFill() {
47-
this.formData = _.merge({}, this.formData, this.propInboxQuickFill);
48-
this.iframeContentWindow.location.reload();
49-
}
28+
},
29+
data() {
30+
return {
31+
formData: {},
32+
lastClickedButton: null,
33+
};
34+
},
35+
computed: {
36+
linkTasks() {
37+
return `/tasks/${this.taskId}/edit/preview?dispatchSubmit=1&alwaysAllowEditing=1&disableInterstitial=1`;
38+
},
39+
iframeContentWindow() {
40+
return this.$refs.preview.firstChild.contentWindow;
41+
},
42+
},
43+
watch: {
44+
formData() {
45+
this.$emit("data", this.formData);
5046
},
51-
mounted() {
52-
this.receiveEvent('dataUpdated', (data) => {
53-
this.formData = data;
47+
propInboxQuickFill() {
48+
this.formData = _.merge({}, this.formData, this.propInboxQuickFill);
49+
this.iframeContentWindow.location.reload();
50+
},
51+
},
52+
mounted() {
53+
this.receiveEvent("dataUpdated", (data) => {
54+
this.formData = data;
55+
});
56+
this.receiveEvent("formSubmit", (data) => {
57+
// Use buttonInfo from event if it's a valid object, otherwise use lastClickedButton
58+
// Note: There's a bug in screen-builder where buttonInfo arrives as `false` instead of the object
59+
let submitData = null;
60+
if (data && typeof data === "object" && !Array.isArray(data)) {
61+
submitData = data;
62+
} else if (this.lastClickedButton && this.lastClickedButton.label) {
63+
// Fallback: use button info captured from iframe click listener
64+
submitData = {
65+
name: this.lastClickedButton.name,
66+
label: this.lastClickedButton.label,
67+
value: this.lastClickedButton.value,
68+
};
69+
}
70+
this.$emit("submit", submitData);
71+
});
72+
this.receiveEvent("taskReady", () => {
73+
this.sendEvent("fillDataOverwriteExistingFields", this.inboxRuleData);
74+
// Setup click listener for submit buttons in iframe after task is ready
75+
this.$nextTick(() => {
76+
this.setupButtonClickListener();
5477
});
55-
this.receiveEvent('formSubmit', (data) => {
56-
this.$emit("submit", data);
78+
});
79+
},
80+
methods: {
81+
eraseData() {
82+
this.sendEvent("eraseData", true);
83+
},
84+
reload() {
85+
this.formData = {};
86+
this.iframeContentWindow.location.reload();
87+
},
88+
loaded() {
89+
// eslint-disable-next-line no-underscore-dangle
90+
this.iframeContentWindow.event_parent_id = this._uid;
91+
this.sendEvent("sendValidateForm", false);
92+
},
93+
sendEvent(name, data) {
94+
const event = new CustomEvent(name, {
95+
detail: data,
5796
});
58-
this.receiveEvent('taskReady', () => {
59-
this.sendEvent("fillDataOverwriteExistingFields", this.inboxRuleData);
97+
this.iframeContentWindow.dispatchEvent(event);
98+
},
99+
receiveEvent(name, callback) {
100+
window.addEventListener(name, (event) => {
101+
// eslint-disable-next-line no-underscore-dangle
102+
if (event.detail.event_parent_id !== this._uid) {
103+
return;
104+
}
105+
callback(event.detail.data);
60106
});
61107
},
62-
methods: {
63-
eraseData() {
64-
this.sendEvent("eraseData", true);
65-
},
66-
reload() {
67-
this.formData = {};
68-
this.iframeContentWindow.location.reload();
69-
},
70-
loaded() {
71-
this.iframeContentWindow.event_parent_id = this._uid;
72-
this.sendEvent("sendValidateForm", false);
73-
},
74-
sendEvent(name, data) {
75-
const event = new CustomEvent(name, {
76-
detail: data
77-
});
78-
this.iframeContentWindow.dispatchEvent(event);
79-
},
80-
receiveEvent(name, callback) {
81-
window.addEventListener(name, (event) => {
82-
if (event.detail.event_parent_id !== this._uid) {
83-
return;
108+
setupButtonClickListener() {
109+
try {
110+
const iframeDoc = this.iframeContentWindow.document;
111+
// Listen for clicks on submit buttons in the iframe
112+
iframeDoc.addEventListener("click", (event) => {
113+
const button = event.target.closest("button");
114+
if (button) {
115+
// Check if this is a submit button (has btn-primary class or is inside form-group)
116+
const isSubmitButton = button.classList.contains("btn-primary")
117+
|| button.classList.contains("btn-secondary")
118+
|| button.closest(".form-group");
119+
if (isSubmitButton) {
120+
this.lastClickedButton = {
121+
name: button.getAttribute("name") || null,
122+
label: button.textContent?.trim() || null,
123+
value: button.value || null,
124+
};
125+
}
84126
}
85-
callback(event.detail.data);
86-
});
87-
},
88-
}
89-
}
90-
</script>
127+
}, true);
128+
} catch (e) {
129+
// Cross-origin restrictions may prevent access to iframe content
130+
// eslint-disable-next-line no-console
131+
console.warn("Could not setup button click listener in iframe:", e.message);
132+
}
133+
},
134+
},
135+
};
136+
</script>

0 commit comments

Comments
 (0)