Remove a lot of code redundancy

This commit is contained in:
Patrick Jentsch 2020-01-31 14:14:08 +01:00
parent 87cf04a87f
commit 279cdb70a8
3 changed files with 35 additions and 104 deletions

View File

@ -90,6 +90,9 @@ indicator will show up how the column is sorted right now.; */
.service[data-service]:before { .service[data-service]:before {
content: "help"; content: "help";
} }
.service[data-service="corpus"]:before {
content: "book";
}
.service[data-service="merge_images"]:before { .service[data-service="merge_images"]:before {
content: "burst_mode"; content: "burst_mode";
} }

View File

@ -1,106 +1,25 @@
class CorpusList extends List { class RessourceList extends List {
constructor(idOrElement, subscriberList, options={}) { constructor(idOrElement, subscriberList, dataMapper=null, options={}) {
super(idOrElement, {...CorpusList.DEFAULT_OPTIONS, ...options}); super(idOrElement, {...RessourceList.options, ...options});
this.dataMapper = dataMapper;
subscriberList.push(this); subscriberList.push(this);
} }
_init(corpora) { _init(ressources) {
this.addCorpora(Object.values(corpora)); this.addRessources(Object.values(ressources));
} }
_update(patch) { _update(patch) {
let item, corpusStatusElement, operation, pathArray; let item, pathArray;
for (operation of patch) { for (let operation of patch) {
/* "/corpusId/valueName" -> ["corpusId", "valueName"] */ /* "/ressourceId/valueName" -> ["ressourceId", "valueName"] */
pathArray = operation.path.split("/").slice(1); pathArray = operation.path.split("/").slice(1);
switch(operation.op) { switch(operation.op) {
case "add": case "add":
this.addCorpora([operation.value]); this.addRessources([operation.value]);
break;
case "remove":
this.remove("id", pathArray[0]);
break;
case "replace":
item = this.get("id", pathArray[0])[0];
switch(pathArray[1]) {
case "status":
item.values({status: operation.value, _status: operation.value});
break;
default:
break;
}
default:
break;
}
}
}
addCorpora(corpora) {
let data = [];
for (let corpus of corpora) {
data.push({description: corpus.description,
id: corpus.id,
link: `/corpora/${corpus.id}`,
status: corpus.status,
title: corpus.title});
}
this.add(data);
}
}
CorpusList.DEFAULT_OPTIONS = {
item: `<tr>
<td>
<a class="btn-floating disabled">
<i class="material-icons">book</i>
</a>
</td>
<td>
<b class="title"></b><br>
<i class="description"></i>
</td>
<td>
<span class="badge new status" data-badge-caption=""></span>
</td>
<td class="right-align">
<a class="btn-small waves-effect waves-light link">View<i class="material-icons right">send</i>
</td>
</tr>`,
page: 4,
pagination: {innerWindow: 8, outerWindow: 1},
valueNames: ["description",
"title",
{data: ["id"]},
{name: "link", attr: "href"},
{name: "status", attr: "data-status"}]};
class JobList extends List {
constructor(idOrElement, subscriberList, options={}) {
super(idOrElement, {...JobList.DEFAULT_OPTIONS, ...options});
subscriberList.push(this);
}
_init(jobs) {
this.addJobs(Object.values(jobs));
}
_update(patch) {
let item, jobStatusElement, operation, pathArray;
for (operation of patch) {
/* "/jobId/valueName" -> ["jobId", "valueName"] */
pathArray = operation.path.split("/").slice(1);
switch(operation.op) {
case "add":
if (pathArray.includes("results")) {break;}
this.addJobs([operation.value]);
break; break;
case "remove": case "remove":
this.remove("id", pathArray[0]); this.remove("id", pathArray[0]);
@ -121,20 +40,29 @@ class JobList extends List {
} }
addJobs(jobs) { addRessources(ressources) {
let data = []; if (this.dataMapper) {
for (let job of jobs) { this.add(ressources.map(x => this.dataMapper(x)));
data.push({description: job.description, } else {
id: job.id, this.add(ressources);
link: `/jobs/${job.id}`,
service: job.service,
status: job.status,
title: job.title});
} }
this.add(data);
} }
} }
JobList.DEFAULT_OPTIONS = { RessourceList.dataMapper = {
corpus: corpus => ({description: corpus.description,
id: corpus.id,
link: `/corpora/${corpus.id}`,
service: "corpus",
status: corpus.status,
title: corpus.title}),
job: job => ({description: job.description,
id: job.id,
link: `/jobs/${job.id}`,
service: job.service,
status: job.status,
title: job.title})
};
RessourceList.options = {
item: `<tr> item: `<tr>
<td> <td>
<a class="btn-floating disabled"> <a class="btn-floating disabled">

View File

@ -86,7 +86,7 @@
</div> </div>
<script> <script>
var corpusList = new CorpusList("corpora", nopaque.corporaSubscribers); var corpusList = new RessourceList("corpora", nopaque.corporaSubscribers, RessourceList.dataMapper.corpus);
var jobList = new JobList("jobs", nopaque.jobsSubscribers); var jobList = new RessourceList("jobs", nopaque.jobsSubscribers, RessourceList.dataMapper.job);
</script> </script>
{% endblock %} {% endblock %}