class Utils { static elementFromString(string) { let tmpElement = document.createElement('div'); tmpElement.innerHTML = string.trim(); return tmpElement.firstChild; } static generateElementId(prefix='') { for (let i = 0; true; i++) { if (document.querySelector(`#${prefix}${i}`) !== null) {continue;} return `${prefix}${i}`; } } static isObject(object) { return object !== null && typeof object === 'object' && !Array.isArray(object); } static mergeObjectsDeep(...objects) { let mergedObject = {}; if (objects.length === 0) { return mergedObject; } if (objects.length === 1) { if (!Utils.isObject(objects[0])) { throw 'Cannot merge non-object'; } return Utils.mergeObjectsDeep(mergedObject, objects[0]); } if (!Utils.isObject(objects[0]) || !Utils.isObject(objects[1])) { throw 'Cannot merge non-object'; } for (let key in objects[0]) { if (objects[0].hasOwnProperty(key)) { if (objects[1].hasOwnProperty(key)) { if (Utils.isObject(objects[0][key]) && Utils.isObject(objects[1][key])) { mergedObject[key] = Utils.mergeObjectsDeep(objects[0][key], objects[1][key]); } else { mergedObject[key] = objects[1][key]; } } else { mergedObject[key] = objects[0][key]; } } } for (let key in objects[1]) { if (objects[1].hasOwnProperty(key)) { if (!objects[0].hasOwnProperty(key)) { mergedObject[key] = objects[1][key]; } } } if (objects.length === 2) { return mergedObject; } return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2)); } static buildCorpusRequest(userId, corpusId) { return new Promise((resolve, reject) => { let corpus = app.data.users[userId].corpora[corpusId]; fetch(`/corpora/${corpus.id}/build`, {method: 'POST', headers: {Accept: 'application/json'}}) .then( (response) => { if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} if (response.status === 409) {app.flash('Conflict', 'error'); reject(response);} app.flash(`Corpus "${corpus.title}" marked for building`, 'corpus'); resolve(response); }, (response) => { app.flash('Something went wrong', 'error'); reject(response); } ); }); } static deleteCorpusRequest(userId, corpusId) { return new Promise((resolve, reject) => { let corpus = app.data.users[userId].corpora[corpusId]; let modalElement = Utils.elementFromString( `