Grammalecte  Diff

Differences From Artifact [84567c7948]:

To Artifact [0ed08aafe8]:


30
31
32
33
34
35
36
37

38
39
40
41
42
43
44
30
31
32
33
34
35
36

37
38
39
40
41
42
43
44







-
+









document.getElementById("lexicon_button").addEventListener("click", () => { oWidgets.showPage("lexicon"); }, false);
document.getElementById("add_word_button").addEventListener("click", () => { oWidgets.showPage("lemma"); }, false);

document.getElementById("table").addEventListener("click", (xEvent) => { oWidgets.onTableClick(xEvent); }, false);
document.getElementById("save_button").addEventListener("click", () => { oLexicon.save(); }, false);
document.getElementById("export_button").addEventListener("click", () => { oLexicon.export(); }, false);
document.getElementById("export_button").addEventListener("click", () => { oDict.export(); }, false);

document.getElementById("editor").addEventListener("click", (xEvent) => { oWidgets.onSelectionClick(xEvent); }, false);
document.getElementById("lemma").addEventListener("keyup", () => { oWidgets.onWrite(); }, false);
document.getElementById("lemma2").addEventListener("keyup", () => { oWidgets.onWrite2(); }, false);
document.getElementById("verb_pattern").addEventListener("keyup", () => { oFlexGen.update(); }, false);
document.getElementById("flexion").addEventListener("keyup", () => { oFlexGen.update(); }, false);
document.getElementById("tags").addEventListener("keyup", () => { oFlexGen.update(); }, false);
510
511
512
513
514
515
516
517

518
519
520

521
522
523
524
525
526
527




528
529
530
531
532
533
534
510
511
512
513
514
515
516

517
518
519

520
521
522
523




524
525
526
527
528
529
530
531
532
533
534







-
+


-
+



-
-
-
-
+
+
+
+







    lFlexion: [],
    nEntries: 0,
    nDeletedEntries: 0,
    nAddedEntries: 0,

    load: function () {
        if (bChrome) {
            browser.storage.local.get("lexicon", this._load);
            browser.storage.local.get("oLexicon", this._load);
            return;
        }
        let xPromise = browser.storage.local.get("lexicon");
        let xPromise = browser.storage.local.get("oLexicon");
        xPromise.then(this._load.bind(this), showError);
    },

    _load: function (dResult) {
        if (dResult.hasOwnProperty("lexicon")) {
            this.lFlexion = dResult.lexicon.lEntry;
            oWidgets.setDictData(this.lFlexion, dResult.lexicon.sDate);    
    _load: function (oResult) {
        if (oResult.hasOwnProperty("oLexicon")) {
            this.lFlexion = oResult.oLexicon.lEntry;
            oWidgets.setDictData(this.lFlexion, oResult.oLexicon.sDate);    
            oWidgets.displayTable(this.lFlexion);
        }
        if (this.lFlexion.length > 0) {
            oWidgets.showElement("export_button");
        } else {
            oWidgets.hideElement("export_button");
        }
561
562
563
564
565
566
567
568

569
570

571
572
573
574
575
576
577
578
579

580
581
582



















583
584









585

586
587
588



589


590
591
592
593
594

595
596
597

598
599
600
601

602
603
604









605
561
562
563
564
565
566
567

568
569

570
571
572
573
574
575
576
577
578

579



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609

610
611
612

613
614
615
616
617
618
619
620
621
622

623



624
625
626
627
628
629



630
631
632
633
634
635
636
637
638
639







-
+

-
+








-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+
+
+
+
+
+
-
+


-
+
+
+

+
+




-
+
-
-
-
+




+
-
-
-
+
+
+
+
+
+
+
+
+

        let lEntry = [];
        for (let e of this.lFlexion) {
            if (e !== null) {
                lEntry.push(e);
            }
        }
        let sDate = this._getDate();
        browser.storage.local.set({ "lexicon": {"lEntry": lEntry, "sDate": sDate} });
        browser.storage.local.set({ "oLexicon": {"lEntry": lEntry, "sDate": sDate} });
        this.lFlexion = lEntry;
        this.build();
        oDict.build(lEntry);
        this.resetData();
        oWidgets.displayTable(this.lFlexion);
        oWidgets.updateData();
        oWidgets.setDictData(lEntry.length, sDate);
    },

    _getDate: function () {
        let oDate = new Date();
        let sMonth = (oDate.getMonth() + 1).toString(); // Because JS always sucks somehow.
        let sMonth = (oDate.getMonth() + 1).toString().padStart(2, "0"); // Month+1: Because JS always sucks somehow.
        if (sMonth.length == 1) sMonth =  "0" + sMonth;
        let sDay = (oDate.getDay() < 10) ? "0"+oDate.getDay() : oDate.getDay();
        return `${oDate.getFullYear()}-${sMonth}-${sDay} (${oDate.getHours()}:${oDate.getMinutes()})`;
        let sDay = (oDate.getDay()).toString().padStart(2, "0");
        let sHours = (oDate.getHours()).toString().padStart(2, "0");
        let sMinutes = (oDate.getMinutes()).toString().padStart(2, "0");
        return `${oDate.getFullYear()}-${sMonth}-${sDay}, ${sHours}:${sMinutes}`;
    }
}


const oDict = {

    oJSON: null,

    load: function () {
        if (bChrome) {
            browser.storage.local.get("oDictionary", this._load);
            return;
        }
        let xPromise = browser.storage.local.get("oDictionary");
        xPromise.then(this._load.bind(this), showError);
    },

    _load: function (oResult) {
        if (oResult.hasOwnProperty("oDictionary")) {
            this.oJSON = oResult.oDictionary;
            oWidgets.showElement("export_button");
        } else {
            oWidgets.hideElement("export_button");
        }
    },

    build: function () {
    build: function (lEntry) {
        oWidgets.showElement("build_progress");
        let xProgressNode = document.getElementById("build_progress");
        let oDAWG = new DAWG(this.lFlexion, "Français - dictionnaire personnel", "S", xProgressNode);
        let oDAWG = new DAWG(lEntry, "Français - dictionnaire personnel", "S", xProgressNode);
        this.oJSON = oDAWG.createBinary(1);
        this.save();
        oWidgets.hideElement("build_progress");
        oWidgets.showElement("export_button");
        // debug
        let lMorph = oDAWG.morph("finis");
        console.log(lMorph);
    },

    export: function () {
    save: function () {
        let xBlob = new Blob(['{ "app": "grammalecte", "data": ' + JSON.stringify(this.lFlexion) + ' }'], {type: 'application/json'}); 
        let sURL = URL.createObjectURL(xBlob);
        browser.downloads.download({ filename: "grammalecte_dictionnaire_personnel.json", url: sURL, saveAs: true });
        browser.storage.local.set({ "oDictionary": this.oJSON });
    },

    import: function () {
        // TO DO
    },
    }
}


    export: function () {
        let xBlob = new Blob([ JSON.stringify(this.oJSON) ], {type: 'application/json'}); 
        let sURL = URL.createObjectURL(xBlob);
        browser.downloads.download({ filename: "grammalecte_dictionnaire_personnel.json", url: sURL, saveAs: true });
    }
}


oLexicon.load();