Home > Forum > Rules, JS, SQL > Open first attachment from all atachment tab

Open first attachment from all atachment tab
0

Based on Daniels blog (https://daniels-notes.de/posts/2021/javascript-form-rule-execution-on-page-load#show-all-attachments-after-page-load) it is posible to open first attachments after page load but from all attachments tab (becouse attachment is in parent workflow)?

MVP

Hi Marcin,

you can use the below code inside a form rule which is set to JavaScript mode. The form rule in turn is executed OnPage load / behavior tab of the form.

What the script does:
1. Show all attachments tab, which triggers loading the related attachment.
2. Once the loading is triggered showFirstAttachment is executed 10 times. This will test whether an attachment is available and "click" on it.
I shamelessly copied the simulateClick function from:
https://stackoverflow.com/questions/40091000/simulate-click-event-on-react-element

Best regards,
Daniel


window.ccls = window.ccls || {};
ccls.showAllAttachments = {};
ccls.showAllAttachments.Timeout = 0;
ccls.showAllAttachments.TimeoutMax = 4;
ccls.showFirstAttachment= {};
ccls.showFirstAttachment.Timeout = 0;
ccls.showFirstAttachment.TimeoutMax = 10;

ccls.showAllAttachments.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = document.getElementsByClassName("all-attachments-link");
// verify that attachments are avialable
if (items == null || items.length != 1 ){
if (ccls.showAllAttachments.Timeout<= ccls.showAllAttachments.TimeoutMax){
ccls.showAllAttachments.Timeout ++;
setTimeout(function (){ccls.showAllAttachments.execute();},333)
}
return;
}
items[0].click();
ccls.showFirstAttachment.execute();
}


ccls.showFirstAttachment.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = $(".attachment-name",$("#all-attachments"))
if (items == null || items.length != 1 ){
if (ccls.showFirstAttachment.Timeout<= ccls.showFirstAttachment.TimeoutMax){
ccls.showFirstAttachment.Timeout ++;
setTimeout(function (){ccls.showFirstAttachment.execute();},333)
}
return;
}
simulateMouseClick(items [0]);
}

const mouseClickEvents = ['mousedown', 'click', 'mouseup'];
function simulateMouseClick(element){
debugger;
mouseClickEvents.forEach(mouseEventType =>
element.dispatchEvent(
new MouseEvent(mouseEventType, {
view: window,
bubbles: true,
cancelable: true,
buttons: 1
})
)
);
}

ccls.showAllAttachments.execute();

In reply to: Daniel Krüger (Cosmo Consult)

Hi Marcin,

you can use the below code inside a form rule which is set to JavaScript mode. The form rule in turn is executed OnPage load / behavior tab of the form.

What the script does:
1. Show all attachments tab, which triggers loading the related attachment.
2. Once the loading is triggered showFirstAttachment is executed 10 times. This will test whether an attachment is available and "click" on it.
I shamelessly copied the simulateClick function from:
https://stackoverflow.com/questions/40091000/simulate-click-event-on-react-element

Best regards,
Daniel


window.ccls = window.ccls || {};
ccls.showAllAttachments = {};
ccls.showAllAttachments.Timeout = 0;
ccls.showAllAttachments.TimeoutMax = 4;
ccls.showFirstAttachment= {};
ccls.showFirstAttachment.Timeout = 0;
ccls.showFirstAttachment.TimeoutMax = 10;

ccls.showAllAttachments.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = document.getElementsByClassName("all-attachments-link");
// verify that attachments are avialable
if (items == null || items.length != 1 ){
if (ccls.showAllAttachments.Timeout<= ccls.showAllAttachments.TimeoutMax){
ccls.showAllAttachments.Timeout ++;
setTimeout(function (){ccls.showAllAttachments.execute();},333)
}
return;
}
items[0].click();
ccls.showFirstAttachment.execute();
}


ccls.showFirstAttachment.execute = function (){
// Start debugger, if debug parameter is set and dev tools are started.
if (new URLSearchParams(document.location.search).get("debug") == 1) {
debugger;
}

var items = $(".attachment-name",$("#all-attachments"))
if (items == null || items.length != 1 ){
if (ccls.showFirstAttachment.Timeout<= ccls.showFirstAttachment.TimeoutMax){
ccls.showFirstAttachment.Timeout ++;
setTimeout(function (){ccls.showFirstAttachment.execute();},333)
}
return;
}
simulateMouseClick(items [0]);
}

const mouseClickEvents = ['mousedown', 'click', 'mouseup'];
function simulateMouseClick(element){
debugger;
mouseClickEvents.forEach(mouseEventType =>
element.dispatchEvent(
new MouseEvent(mouseEventType, {
view: window,
bubbles: true,
cancelable: true,
buttons: 1
})
)
);
}

ccls.showAllAttachments.execute();

Thank you Daniel.