nopaque/app/static/js/JobList.js

120 lines
4.5 KiB
JavaScript
Raw Normal View History

class JobList extends List {
2020-01-13 14:33:05 +00:00
constructor(idOrElement, subscriberList, options={}) {
super(idOrElement, {...JobList.DEFAULT_OPTIONS, ...options});
subscriberList.push(this);
2019-08-23 13:05:01 +00:00
}
_init(jobs) {
2019-08-30 11:31:00 +00:00
for (let [id, job] of Object.entries(jobs)) {
this.addJob(job);
2019-08-28 15:31:09 +00:00
}
2020-01-13 14:33:05 +00:00
this.update();
}
2019-08-30 11:31:00 +00:00
_update(patch) {
var item, jobStatusElement, newStatusColor, operation, pathArray, status,
statusColor;
2019-08-30 11:31:00 +00:00
for (operation of patch) {
2019-09-02 07:17:13 +00:00
/* "/jobId/valueName" -> ["jobId", "valueName"] */
2019-08-30 11:31:00 +00:00
pathArray = operation.path.split("/").slice(1);
switch(operation.op) {
case "add":
2019-11-18 10:21:40 +00:00
if (pathArray.includes("results")) {break;}
2019-08-30 11:31:00 +00:00
this.addJob(operation.value);
2019-08-30 12:00:08 +00:00
this.update();
2019-08-30 11:31:00 +00:00
break;
case "remove":
this.remove("id", pathArray[0]);
break;
case "replace":
item = this.get("id", pathArray[0])[0];
switch(pathArray[1]) {
2019-08-30 11:31:00 +00:00
case "status":
jobStatusElement = item.elm.querySelector(".status");
status = jobStatusElement.innerHTML;
2019-09-03 07:07:09 +00:00
statusColor = JobList.STATUS_COLORS[status]
|| JobList.STATUS_COLORS['default'];
newStatusColor = JobList.STATUS_COLORS[operation.value]
|| JobList.STATUS_COLORS['default'];
2019-08-30 11:31:00 +00:00
jobStatusElement.classList.remove(statusColor);
jobStatusElement.classList.add(newStatusColor);
jobStatusElement.innerHTML = operation.value;
2019-08-30 12:00:08 +00:00
break;
2019-08-30 11:31:00 +00:00
default:
break;
}
default:
break;
}
}
}
addJob(job) {
var jobDescriptionElement, jobElement, jobServiceElement, jobStatusElement,
2019-08-30 11:31:00 +00:00
jobTitleElement, serviceColor, serviceIcon, statusColor;
jobDescriptionElement = document.createElement("p");
jobDescriptionElement.classList.add("description");
jobDescriptionElement.innerText = job.description;
jobElement = document.createElement("a");
jobElement.classList.add("avatar", "collection-item");
jobElement.dataset.id = job.id;
jobElement.href = `/jobs/${job.id}`;
2019-09-03 07:07:09 +00:00
serviceColor = JobList.SERVICE_COLORS[job.service]
|| JobList.SERVICE_COLORS['default'];
serviceIcon = JobList.SERVICE_ICONS[job.service]
|| JobList.SERVICE_ICONS['default'];
jobServiceElement = document.createElement("i");
2019-11-18 10:21:40 +00:00
jobServiceElement.classList.add("circle", "material-icons", "service-icon",
serviceColor);
2019-08-30 11:31:00 +00:00
jobServiceElement.innerText = serviceIcon;
2019-09-03 07:07:09 +00:00
statusColor = JobList.STATUS_COLORS[job.status]
|| JobList.STATUS_COLORS['default'];
jobStatusElement = document.createElement("span");
2019-08-30 11:31:00 +00:00
jobStatusElement.classList.add("badge", "new", "status", statusColor);
jobStatusElement.dataset.badgeCaption = "";
jobStatusElement.innerText = job.status;
jobTitleElement = document.createElement("span");
jobTitleElement.classList.add("title");
jobTitleElement.innerText = job.title;
jobElement.appendChild(jobServiceElement);
jobElement.appendChild(jobStatusElement);
jobElement.appendChild(jobTitleElement);
jobElement.appendChild(jobDescriptionElement);
2019-08-30 11:31:00 +00:00
this.add(
[{description: job.description, id: job.id, title: job.title}],
2019-09-02 07:17:13 +00:00
function(items) {items[0].elm = jobElement;}
2019-08-30 11:31:00 +00:00
);
}
}
2020-01-13 14:33:05 +00:00
JobList.DEFAULT_OPTIONS = {item: `<div>
<span class="title"></span>
<span class="description"></span>
</div>`,
page: 4,
pagination: {innerWindow: 8, outerWindow: 1},
valueNames: ["description", "title", {data: ["id"]}]};
JobList.SERVICE_COLORS = {"merge_images": "amber",
"nlp": "blue",
"ocr": "green",
"default": "red"};
JobList.SERVICE_ICONS = {"merge_images": "burst_mode",
"nlp": "format_textdirection_l_to_r",
"ocr": "find_in_page",
"default": "help"};
2019-11-05 14:22:25 +00:00
JobList.STATUS_COLORS = {"submitted": "blue",
"preparing": "light-blue",
"pending": "orange",
"running": "amber",
"complete": "green",
"stopping": "orange",
"removing": "deep-orange",
"default": "red"};