search-wp.js 3.54 KB


loadTermData = function (currentBodyViewId, languageArray) {

    console.log('search-wp, loadTermData, time: ' + new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1"));
    console.log('languageArray: ' + languageArray.length);

    var termNoJsonPath = '~/../content/data/json/da/body-views/' + currentBodyViewId + '/da_dat_tm_sg_' + currentBodyViewId + '.json';

    var TermNumberData;
    var AJAX_req = new XMLHttpRequest();
    AJAX_req.open("GET", termNoJsonPath, true);
    AJAX_req.setRequestHeader("Content-type", "application/json");

    AJAX_req.onreadystatechange = function () {
        if (AJAX_req.readyState == 4 && AJAX_req.status == 200) {
            TermNumberData = JSON.parse(AJAX_req.responseText);
            console.log("data returned: " + TermNumberData);

            loadTermTextData(TermNumberData, languageArray);
            // loadTermTextData(languageArray);
        }
    }
    AJAX_req.send();


}


loadTermTextData = function (TermNumberData, arr) {
    console.log(arr.length);
    var DataArray = [];
    var a;
    for (var j = 0; j <= arr.length - 1; j++) {

        //1. 
        var a = arr[j].language;
        var termTextJsonPath = '~/../content/data/json/da/vocab/' + arr[j].language + '/cm_dat_vocabterm_' + arr[j].id + '.json';
        console.log(termTextJsonPath);

        var VocabTermData;
        var AJAX_req = new XMLHttpRequest();
        AJAX_req.open("GET", termTextJsonPath, true);
        AJAX_req.setRequestHeader("Content-type", "application/json");

        AJAX_req.onreadystatechange = function () {
            if (AJAX_req.readyState == 4 && AJAX_req.status == 200) {
                VocabTermData = JSON.parse(AJAX_req.responseText);
                console.log("data returned FOR VocabTermData: " + VocabTermData);

                var vocabTermTxt = [];

                TermNumberData.TermData.Term.forEach(function (value1, key1) {
                    
                    console.log("data returned FOR VocabTermData1: " + VocabTermData.VocabTerms);
                    VocabTermData.VocabTerms.Term.forEach(function (value2, key2) {

                        if (value1._ActualTermNumber === value2._ActualTermNumber) {
                            vocabTermTxt.push(
                                {
                                    "_ActualTermNumber": value2._ActualTermNumber,
                                    "_TermText": value2._TermText,
                                    "_cdId": value2._cdId
                                }
                                );
                        }
                    })

                })
                DataArray.push({ "language": a, "VocabTermData": VocabTermData, "vocabTermTxt": vocabTermTxt });
                self.postMessage({
                    'vocabTermTxt': vocabTermTxt,
                    'TermNumberData': TermNumberData,
                    'VocabTermData': VocabTermData,
                    'DataArray': DataArray

                });

            }
        }



    }
    AJAX_req.send();

    //  var termTextJsonPath = '~/../content/data/json/da/vocab/english/cm_dat_vocabterm_1.json';

    // readTextFile(termTextJsonPath, function (text) {




}


self.onmessage = function (e) {
    console.log("search");
    //console.log('search-wp onmessage time: ' + new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1"));
    loadTermData(e.data.currentBodyViewId, e.data.languageArray);
    //  loadTermData(e.data.languageArray);

}