added support for code generation

This commit is contained in:
dcore94 2023-04-12 15:50:18 +02:00
parent d35af6ae61
commit 1ac7bbe3d1
1 changed files with 41 additions and 3 deletions

View File

@ -79,9 +79,9 @@ class CCPExecutionHistory extends HTMLElement {
<button data-index="0" name="reexecute1" title="Level 1 re-execution" class="btn btn-info ccp-toolbar-button ccp-toolbar-button-small">
<svg enable-background="new 0 0 24 24" viewBox="0 0 24 24"><g><rect fill="none" height="24" width="24"/><rect fill="none" height="24" width="24"/><rect fill="none" height="24" width="24"/></g><g><g/><path d="M12,5V1L7,6l5,5V7c3.31,0,6,2.69,6,6s-2.69,6-6,6s-6-2.69-6-6H4c0,4.42,3.58,8,8,8s8-3.58,8-8S16.42,5,12,5z"/></g></svg>
</button>
<button data-index="0" name="reexecute2" title="Level 2 re-execution" class="btn btn-info ccp-toolbar-button ccp-toolbar-button-small">
<!-- button data-index="0" name="reexecute2" title="Level 2 re-execution" class="btn btn-info ccp-toolbar-button ccp-toolbar-button-small">
<svg enable-background="new 0 0 24 24" viewBox="0 0 24 24"><g><rect fill="none" height="24" width="24"/></g><g><path d="M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z M12,18.5c-3.31,0-6-2.69-6-6h2c0,2.21,1.79,4,4,4 s4-1.79,4-4c0-2.24-1.85-4.09-4.16-3.99l1.57,1.57L12,11.5l-4-4l4-4l1.41,1.41l-1.6,1.6C15.28,6.4,18,9.18,18,12.5 C18,15.81,15.31,18.5,12,18.5z"/></g></svg>
</button>
</button-->
<button data-index="0" name="delete" title="Delete" class="btn btn-danger ccp-toolbar-button ccp-toolbar-button-small">
<svg viewBox="0 0 24 24">
<path d="M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z"></path>
@ -104,6 +104,17 @@ class CCPExecutionHistory extends HTMLElement {
<ul>
<li></li>
</ul>
<div class="d-flex float-right" style="gap: 3px;">
<select name="language-selector" class="form-control" style="height:inherit;padding:2px">
<option value="text/python" title="Generate plain Python3">Python 3</option>
<option value="application/vnd.jupyter+python" title="Generate Jupyter notebook with Python 3 cells">Jupyter Python3</option>
</select>
<button data-index="0" name="codegen" title="Generate code" class="btn btn-primary ccp-toolbar-button ccp-toolbar-button-small">
<svg viewBox="0 96 960 960">
<path d="m384 721 43-43-101-102 101-101-43-43-144 144.5L384 721Zm192 0 145-145-144-144-43 43 101 101-102 102 43 43ZM180 936q-24.75 0-42.375-17.625T120 876V276q0-24.75 17.625-42.375T180 216h205q5-35 32-57.5t63-22.5q36 0 63 22.5t32 57.5h205q24.75 0 42.375 17.625T840 276v600q0 24.75-17.625 42.375T780 936H180Zm0-60h600V276H180v600Zm300-617q14 0 24.5-10.5T515 224q0-14-10.5-24.5T480 189q-14 0-24.5 10.5T445 224q0 14 10.5 24.5T480 259ZM180 876V276v600Z"/>
</svg>
</button>
</div>
</details>
</li>
</ul>
@ -302,6 +313,24 @@ class CCPExecutionHistory extends HTMLElement {
}
}
generateCode(id, mime){
this.#boot.secureFetch(`${this.#serviceurl}/executions/${id}/code`,
{ method: "GET", headers : { "Accept" : mime} }).then(reply =>{
if (reply.status !== 200) {
throw "Unable to generate code for " + mime
}
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)})
}
#execution_list_bss = {
template : "#EXECUTIOM_LIST_TEMPLATE",
target : "ul[name=ccp_execution_list]",
@ -485,6 +514,15 @@ class CCPExecutionHistory extends HTMLElement {
e.classList.add(t2)
}
},
{
target : "button[name=codegen]",
apply : (e,d)=>e.setAttribute("data-index", d.id),
on_click: (ev)=>{
const id = ev.target.getAttribute("data-index")
const lang = ev.target.parentElement.querySelector("select[name=language-selector]")
this.generateCode(id, lang)
}
},
{
target : "ul",
recurse : [
@ -513,4 +551,4 @@ class CCPExecutionHistory extends HTMLElement {
]
}
}
window.customElements.define('d4s-ccp-executionhistory', CCPExecutionHistory);
window.customElements.define('d4s-ccp-executionhistory', CCPExecutionHistory);