search-wp.js 3.36 KB


loadTermData = function (currentBodyViewId, languageName, languageID) {

    console.log('search-wp, loadTermData, time: ' + new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1"));
    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, languageName, languageID);
           
        }
    }
    AJAX_req.send();


}


loadTermTextData = function (TermNumberData, languageName, languageID) {
  
    var languageFileName;
    if(languageName == "English (Undergraduate)")
    {
        languageFileName = "English_Undergraduate";
    }
    else if (languageName == "Japanese (Kanji)") {
        languageFileName = "Japanese_Kanji";
    }
    else if (languageName == "Japanese (Yomi)")
    {
        languageFileName = "Japanese_yomi";
    }
    else
    {
        languageFileName = languageName;
    }
  
    var termTextJsonPath = '~/../content/data/json/da/vocab/' + languageFileName + '/cm_dat_vocabterm_' + languageID + '.json';
      
        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) {
                console.log('AJAX_req.readyState= ' + AJAX_req.readyState + ' and AJAX_req.status =' + AJAX_req.status);
              
                VocabTermData = JSON.parse(AJAX_req.responseText);
                var vocabTermTxt = [];
                TermNumberData.TermData.Term.forEach(function (value1, key1) {
                    VocabTermData.VocabTerms.Term.forEach(function (value2, key2) {
                        if (value1._ActualTermNumber === value2._ActualTermNumber) {
                            vocabTermTxt.push(
                                {
                                    "_ActualTermNumber": value2._ActualTermNumber,
                                    "_TermText": value2._TermText,
                                    "_cdId": value2._cdId
                                }
                                );
                        }
                    })
                })
                    self.postMessage({
                        'vocabTermTxt': vocabTermTxt,
                        'TermNumberData': TermNumberData,
                         'VocabTermData': VocabTermData,
                         'language': languageName

                    });
            }
        }
    AJAX_req.send();
}


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.languageName, e.data.languageID);
  

}