class ListManager {
currentData = [];
displayedData = [];
editableTypes = [];
type = '';
referredTable = [];
associationTable = [];
constructor(listContainer, type, editableTypes) {
this.listContainer = listContainer;
this.editableTypes = editableTypes;
this.type = type;
if (type === 'article') {
AjaxManager.getAll().then((data) => {
this.currentData = data['articles'];
this.referredTable = data['categories'];
this.associationTable = data['article_categories'];
this.generateList()
});
} else {
AjaxManager.get(type).then((data) => {
this.currentData = data;
this.generateList()
});
}
}
generateList() {
for (let i = 0; i < this.currentData.length; i++) {
this.createNewListEntry(this.currentData[i]);
}
}
createNewListEntry(item) {
let listItem = this.getListItem(item);
this.displayedData.push($(listItem));
const index = this.displayedData.length - 1;
let JQueryItem = this.displayedData[index];
this.listContainer.append(JQueryItem);
JQueryItem.on('click', () => {
this.showEditPopup(index);
});
}
deleteListEntry(id) {
$('#listItem_' + id).remove();
}
getCategoriesOfArticle(articleId) {
let filteredList = [];
for (let i = 0; i < this.associationTable.length; i++) {
if (this.associationTable[i]['article_id'] === articleId)
filteredList.push(this.associationTable[i]['category_id']);
}
return filteredList;
}
getCategoryOfId(categoryId) {
for (let i = 0; i < this.referredTable.length; i++) {
if (this.referredTable[i]['id'] === categoryId) {
return this.referredTable[i];
}
}
return "";
}
getCategoryIcons(articleId) {
let icons = "";
let categories = this.getCategoriesOfArticle(articleId);
for (let i = 0; i < categories.length; i++) {
icons += "";
}
return icons;
}
getListItem(item) {
let listItem = "
";
for (let i = 0; i < this.editableTypes.length; i++) {
listItem += "";
if (this.editableTypes[i]["type"] === "icon")
listItem += "";
else if (this.editableTypes[i]["type"] === 'checkboxes') {
listItem += this.getCategoryIcons(item['id']);
} else if (this.editableTypes[i]["type"] === 'image') {
listItem += ''
} else
listItem += item[this.editableTypes[i]["name"]] + '';
}
listItem += "
";
return listItem;
}
getCategoryCheckboxes(articleId) {
let checkboxes = "
";
let categories = [];
if (articleId !== undefined)
categories = this.getCategoriesOfArticle(articleId);
for (let i = 0; i < this.referredTable.length; i++) {
let id = this.referredTable[i]['id'];
let cat = this.getCategoryOfId(id);
let checked = categories.indexOf(id) !== -1 ? 'checked' : '';
checkboxes +=
'