[IMP] runbot: convert basic javascript module

To odoo modules.
stats.js not converted because it would make more sense as an owl
component.
This commit is contained in:
William Braeckman 2024-12-09 09:23:07 +01:00
parent dd25f8d8b1
commit 47a5480d8a
4 changed files with 46 additions and 39 deletions

View File

@ -1,39 +1,46 @@
(function($) { import publicWidget from "@web/legacy/js/public/public_widget";
"use strict";
$(function () {
$(document).on('click', '[data-runbot]', function (e) { publicWidget.registry.RunbotPage = publicWidget.Widget.extend({
e.preventDefault(); // This selector should not be so broad.
var data = $(this).data(); selector: 'body',
var operation = data.runbot; events: {
'click [data-runbot]': '_onClickDataRunbot',
'click [data-runbot-clipboard]': '_onClickRunbotCopy',
},
_onClickDataRunbot: async (event) => {
const { currentTarget: target } = event;
if (!target) {
return;
}
event.preventDefault();
const { runbot: operation, runbotBuild } = target.dataset;
if (!operation) { if (!operation) {
return; return;
} }
var xhr = new XMLHttpRequest(); let url = target.href;
var url = e.target.href if (runbotBuild) {
if (data.runbotBuild) { url = `/runbot/build/${runbotBuild}/${operation}`
url = '/runbot/build/' + data.runbotBuild + '/' + operation
} }
var elem = e.target const response = await fetch(url, {
xhr.addEventListener('load', function () { method: 'POST',
if (operation == 'rebuild' && window.location.href.split('?')[0].endsWith('/build/' + data.runbotBuild)){ });
window.location.href = window.location.href.replace('/build/' + data.runbotBuild, '/build/' + xhr.responseText); if (operation == 'rebuild' && window.location.href.split('?')[0].endsWith(`/build/${runbotBuild}`)) {
window.location.href = window.location.href.replace('/build/' + runbotBuild, '/build/' + await response.text());
} else if (operation == 'action') { } else if (operation == 'action') {
elem.parentElement.innerText = this.responseText target.parentElement.innerText = await response.text();
} else { } else {
window.location.reload(); window.location.reload();
} }
}); },
xhr.open('POST', url);
xhr.send();
});
});
})(jQuery);
_onClickRunbotCopy: ({ currentTarget: target }) => {
function copyToClipboard(text) { if (!navigator.clipboard || !target) {
if (!navigator.clipboard) {
console.error('Clipboard not supported');
return; return;
} }
navigator.clipboard.writeText(text); navigator.clipboard.writeText(
} target.dataset.runbotClipboard
);
}
});

View File

@ -51,7 +51,7 @@ config.options.onClick = function(event, activeElements) {
}; };
function fetch(path, data, then) { function _fetch(path, data, then) {
const xhttp = new XMLHttpRequest(); const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
@ -173,7 +173,7 @@ function fetchUpdateChart() {
chart_spinner.style.visibility = 'visible'; chart_spinner.style.visibility = 'visible';
fetch_params = compute_fetch_params(); fetch_params = compute_fetch_params();
console.log('fetch') console.log('fetch')
fetch('/runbot/stats/', fetch_params, function(result) { _fetch('/runbot/stats/', fetch_params, function(result) {
config.result = result; config.result = result;
Object.values(config.result).forEach(v => v['Aggregate Sum'] = Object.values(v).reduce((a, b) => a + b, 0)) Object.values(config.result).forEach(v => v['Aggregate Sum'] = Object.values(v).reduce((a, b) => a + b, 0))
Object.values(config.result).forEach(v => v['Aggregate Average'] = Object.values(v).reduce((a, b) => a + b, 0)/Object.values(v).length) Object.values(config.result).forEach(v => v['Aggregate Average'] = Object.values(v).reduce((a, b) => a + b, 0)/Object.values(v).length)

View File

@ -384,7 +384,7 @@
</template> </template>
<template id="runbot.branch_copy_button"> <template id="runbot.branch_copy_button">
<button t-attf-class="btn btn-default {{btn_size or 'btn-ssm'}}" title="Copy Bundle name" aria-label="Copy Bundle name" t-attf-onclick="copyToClipboard('{{ bundle.name.split(':')[-1] }}')"> <button t-attf-class="btn btn-default {{btn_size or 'btn-ssm'}}" title="Copy Bundle name" aria-label="Copy Bundle name" t-att-data-runbot-clipboard="bundle.name.split(':')[-1]">
<i t-attf-class="fa fa-clipboard"/> <i t-attf-class="fa fa-clipboard"/>
</button> </button>
</template> </template>