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 {
content: "help";
}
.service[data-service="corpus"]:before {
content: "book";
}
.service[data-service="merge_images"]:before {
content: "burst_mode";
}

View File

@ -1,106 +1,25 @@
class CorpusList extends List {
constructor(idOrElement, subscriberList, options={}) {
super(idOrElement, {...CorpusList.DEFAULT_OPTIONS, ...options});
class RessourceList extends List {
constructor(idOrElement, subscriberList, dataMapper=null, options={}) {
super(idOrElement, {...RessourceList.options, ...options});
this.dataMapper = dataMapper;
subscriberList.push(this);
}
_init(corpora) {
this.addCorpora(Object.values(corpora));
_init(ressources) {
this.addRessources(Object.values(ressources));
}
_update(patch) {
let item, corpusStatusElement, operation, pathArray;
let item, pathArray;
for (operation of patch) {
/* "/corpusId/valueName" -> ["corpusId", "valueName"] */
for (let operation of patch) {
/* "/ressourceId/valueName" -> ["ressourceId", "valueName"] */
pathArray = operation.path.split("/").slice(1);
switch(operation.op) {
case "add":
this.addCorpora([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]);
this.addRessources([operation.value]);
break;
case "remove":
this.remove("id", pathArray[0]);
@ -121,20 +40,29 @@ class JobList extends List {
}
addJobs(jobs) {
let data = [];
for (let job of jobs) {
data.push({description: job.description,
id: job.id,
link: `/jobs/${job.id}`,
service: job.service,
status: job.status,
title: job.title});
addRessources(ressources) {
if (this.dataMapper) {
this.add(ressources.map(x => this.dataMapper(x)));
} else {
this.add(ressources);
}
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>
<td>
<a class="btn-floating disabled">

View File

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