From 620380e9324a91551e75dfadf394a9c8fdce9445 Mon Sep 17 00:00:00 2001 From: dcore94 Date: Fri, 14 Apr 2023 11:51:08 +0200 Subject: [PATCH] add code generation trigger --- ccp/js/executionformcontroller.js | 60 ++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/ccp/js/executionformcontroller.js b/ccp/js/executionformcontroller.js index a941de6..e989161 100644 --- a/ccp/js/executionformcontroller.js +++ b/ccp/js/executionformcontroller.js @@ -75,22 +75,25 @@ class CCPExecutionForm extends HTMLElement{ - - + @@ -168,6 +171,25 @@ class CCPExecutionForm extends HTMLElement{ }).catch(err => {alert("Unable to call execute: " + err); this.unlockRender()}) } + generateCode(mime, filename){ + const url = this.#serviceurl + "/methods/" + this.#method + "/code" + const req = this.buildRequest() + this.#boot.secureFetch( + url, { method : "POST", body : JSON.stringify(req), headers : { "Content-Type" : "application/json", "Accept" : mime}} + ).then(reply=>{ + if(reply.status !== 200) throw "Error while requesting code:"; + return reply.blob() + }).then(blob => { + const objectURL = URL.createObjectURL(blob) + var tmplnk = document.createElement("a") + tmplnk.download = filename + tmplnk.href = objectURL + document.body.appendChild(tmplnk) + tmplnk.click() + document.body.removeChild(tmplnk) + }).catch(err=>{ alert(err)}) + } + buildRequest(){ let request = { inputs : {}, outputs : {}, response : "raw"} @@ -338,6 +360,18 @@ class CCPExecutionForm extends HTMLElement{ return false; } }, + { + target: "button[name=codegen]", + on_click : ev=>{ + ev.preventDefault() + ev.stopPropagation() + const langsel = ev.target.parentElement.querySelector("select[name=language-selector]") + const lang = langsel.value + const ext = langsel.selectedOptions[0].getAttribute("data-ext") + this.generateCode(lang, `ccprequest.${ext}`) + return false + } + }, ] }