nopaque/web/app/static/js/nopaque.lists.new.js
2020-12-04 14:16:11 +01:00

69 lines
1.5 KiB
JavaScript

class RessourceList extends List {
constructor(idOrElement, options) {
super(idOrElement, {...RessourceList.options['default'], ...(options ? options : {})});
}
_init(ressources) {
this.clear();
this._add(Object.values(ressources));
this.sort("id", {order: "desc"});
}
_update(patch) {
let item, pathArray;
for (let operation of patch) {
/*
* '/{ressourceName}/{ressourceId}/...' -> ['{ressourceId}', ...]
* Example: '/jobs/1/status' -> ['1', 'status']
*/
pathArray = operation.path.split("/").slice(2);
switch(operation.op) {
case "add":
this.add_handler([operation.value]);
break;
case "remove":
this.remove_handler(pathArray[0]);
break;
case "replace":
this.replace_handler(pathArray[0], pathArray[1], operation.value);
break;
default:
break;
}
}
}
add_handler(values, callback) {
if (this.hasOwnProperty('add_')) {
this.add_(values, callback);
} else {
this.add(values, callback);
}
}
remove_handler(id) {
if (this.hasOwnProperty('remove_')) {
this.remove_(id);
} else {
this.remove(id);
}
}
replace_handler(id, valueName, newValue) {
let item = this.get('id', id);
if (this.hasOwnProperty('add_'))
item.values({valueName: operation.value});
}
}
RessourceList.options = {
// default RessourceList options
default: {page: 5, pagination: [{innerWindow: 4, outerWindow: 1}]},
};
export { RessourceList, };