Index: gc_lang/fr/webext/background.js ================================================================== --- gc_lang/fr/webext/background.js +++ gc_lang/fr/webext/background.js @@ -153,11 +153,11 @@ if (oData.hasOwnProperty("dictionaries")) { if (oData.dictionaries.hasOwnProperty("__personal__")) { setDictionary("personal", oData.dictionaries["__personal__"]); } if (oData.dictionaries.hasOwnProperty("__community__")) { - setDictionary("personal", oData.dictionaries["__community__"]); + setDictionary("community", oData.dictionaries["__community__"]); } } } function init () { Index: gc_lang/fr/webext/manifest.json ================================================================== --- gc_lang/fr/webext/manifest.json +++ gc_lang/fr/webext/manifest.json @@ -114,10 +114,12 @@ "grammalecte/fr/tests_data.json", "img/logo-16.png" ], "permissions": [ + "*://localhost/*", + "*://dic.grammalecte.net/*", "activeTab", "contextMenus", "downloads", "storage" ], Index: gc_lang/fr/webext/panel/dictionaries.js ================================================================== --- gc_lang/fr/webext/panel/dictionaries.js +++ gc_lang/fr/webext/panel/dictionaries.js @@ -33,10 +33,11 @@ showError(e); } } + class Table { constructor (sNodeId, lColumn, sProgressBarId, sResultId="", bDeleteButtons=true, bActionButtons) { this.sNodeId = sNodeId; @@ -46,11 +47,12 @@ this.xProgressBar = document.getElementById(sProgressBarId); this.xNumEntry = document.getElementById(sResultId); this.iEntryIndex = 0; this.lEntry = []; this.nEntry = 0; - this.aSelectedDict = new Map(); + this.dSelectedDict = new Map(); + this.dDict = new Map(); this.bDeleteButtons = bDeleteButtons; this.bActionButtons = bActionButtons; this._createHeader(); this.listen(); } @@ -99,10 +101,56 @@ this._addRow(lData); } this.nEntry += lFlex.length; this.showEntryNumber(); } + + getDictionarieslist () { + fetch("http://localhost/dictionaries/") + .then((response) => { + if (response.ok) { + return response.json(); + } else { + return null; + } + }) + .then((response) => { + if (response) { + this.fill(response); + } else { + // todo + } + }) + .catch((e) => { + showError(e); + }); + } + + getDictionary (sId, sName) { + console.log("get: "+sName); + fetch("http://localhost/download/"+sName) + .then((response) => { + if (response.ok) { + return response.json(); + } else { + console.log("dictionary not loaded: " + sName); + return null; + } + }) + .then((response) => { + if (response) { + this.selectEntry(sId, sName); + this.dDict.set(sName, response); + browser.storage.local.set({ "stored_dictionaries": this.dDict }); + } else { + // + } + }) + .catch((e) => { + showError(e); + }); + } showEntryNumber () { if (this.xNumEntry) { this.xNumEntry.textContent = this.nEntry; } @@ -135,12 +183,16 @@ onTableClick (xEvent) { try { let xElem = xEvent.target; if (xElem.className) { switch (xElem.className) { - case "delete_entry": this.deleteRow(xElem.dataset.id_entry, xElem.dataset.dict_name); break; - case "select_entry": this.selectEntry(xElem.dataset.id_entry, xElem.dataset.dict_name); break; + case "delete_entry": + this.deleteRow(xElem.dataset.id_entry, xElem.dataset.dict_name); + break; + case "select_entry": + this.getDictionary(xElem.dataset.id_entry, xElem.dataset.dict_name); + break; } } } catch (e) { showError(e); @@ -159,16 +211,16 @@ } } selectEntry (nEntryId, sDicName) { let sRowId = this.sNodeId + "_row_" + nEntryId; - if (!this.aSelectedDict.has(sDicName)) { - this.aSelectedDict.set(sDicName, nEntryId); + if (!this.dSelectedDict.has(sDicName)) { + this.dSelectedDict.set(sDicName, nEntryId); document.getElementById(sRowId).style.backgroundColor = "hsl(120, 50%, 90%)"; } else { - this.aSelectedDict.delete(sDicName); + this.dSelectedDict.delete(sDicName); document.getElementById(sRowId).style.backgroundColor = ""; } this.showSelectedDict(); } @@ -180,24 +232,24 @@ } showSelectedDict () { this.clearSelectedDict(); let xDicList = document.getElementById("dictionaries_list"); - if (this.aSelectedDict.size === 0) { + if (this.dSelectedDict.size === 0) { xDicList.textContent = "[Aucun]"; return; } - for (let [sName, nDicId] of this.aSelectedDict) { + for (let [sName, nDicId] of this.dSelectedDict) { xDicList.appendChild(this.createDictLabel(nDicId, sName)); } } createDictLabel (nDicId, sLabel) { let xLabel = createNode("div", {className: "dic_button"}); let xCloseButton = createNode("div", {className: "dic_button_close", textContent: "×"}, {id_entry: nDicId}); xCloseButton.addEventListener("click", () => { - this.aSelectedDict.delete(sLabel); + this.dSelectedDict.delete(sLabel); document.getElementById(this.sNodeId+"_row_"+nDicId).style.backgroundColor = ""; xLabel.style.display = "none"; }); xLabel.appendChild(xCloseButton); xLabel.appendChild(createNode("div", {className: "dic_button_label", textContent: sLabel})); @@ -206,12 +258,6 @@ } const oDicTable = new Table("dictionaries_table", ["Id", "Nom", "par", "Entrées", "Description"], "wait_progress", "num_dic", false, true); -oDicTable.fill([ - [1, "Ambre", "Inconnu", "240", "Univers des Princes d’Ambre (de Roger Zelazny)"], - [2, "Malaz", "Inconnu", "2340", "Univers du Livre des Martyrs (de Steven Erikson)"], - [3, "Poudlard", "Inconnu", "1440", "Univers d’Harry Potter (de J. K. Rowlings)"], - [4, "Dune", "Inconnu", "2359", "Univers de Dune (de Frank Herbert)"], - [5, "StarWars", "Inconnu", "4359", "Univers de Star Wars (de George Lucas)"] -]); +oDicTable.getDictionarieslist();