<commit_before>// cpserver.js - <NAME> (<EMAIL>) - Jan 2017
// License: MIT. See LICENSE for the full text.
//
// This contains the JS to drive the checkplotserver's interface.
//

//////////////
// JS BELOW //
//////////////

// this contains utility functions
var cputils = {

    // this encodes a string to base64
    // https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
    b64_encode: function (str) {
        return btoa(
            encodeURIComponent(str)
                .replace(/%([0-9A-F]{2})/g,
                         function(match, p1) {
                             return String.fromCharCode('0x' + p1);
                         }));
    },

    // this turns a base64 string into an image by updating its source
    b64_to_image: function (str, targetelem) {

        var datauri = 'data:image/png;base64,' + str;
        $(targetelem).attr('src',datauri);

    }

};


// this contains updates to current checkplots, tracks the entire project's info
// and provides functions to save everything to CSV or JSON
var cptracker = {

    // this is the actual object that will get written to JSON or CSV
    cpdata: {},

    // this is the order of columns for generating the CSV
    infocolumns: [
        'objectid','checkplot',
        'objectinfo.hatid','objectinfo.twomassid',
        'objectinfo.network','objectinfo.stations','objectinfo.ndet',
        'objectinfo.objecttags','objectinfo.bmag','objectinfo.vmag',
        'objectinfo.sdssg','objectinfo.sdssr','objectinfo.sdssi',
        'objectinfo.jmag','objectinfo.hmag','objectinfo.kmag',
        'objectinfo.bvcolor','objectinfo.ijcolor','objectinfo.jkcolor',
        'objectinfo.pmra','objectinfo.pmra_err',
        'objectinfo.pmdecl','objectinfo.pmdecl_err',
        'objectinfo.propermotion','objectinfo.reducedpropermotion',
        'varinfo.objectisvar','varinfo.varperiod','varinfo.varepoch',
        'varinfo.vartags','comments'
    ],

    // this function generates a CSV row for a single object in the
    // cptracker.cpdata object
    cpdata_get_csvrow: function (cpdkey, separator) {

        var thisdata = cptracker.cpdata[cpdkey];
        var rowarr = [];

        cptracker.infocolumns.forEach(function (e, i, a) {

            // if the key is a compound one (firstlev.secondlev)
            if (e.indexOf('.') != -1) {

                var keys = e.split('.');
                rowarr.push(thisdata[keys[0]][keys[1]]);

            }

            // otherwise, it's just in the first level
            else {

                // special case of checkplot filename
                if (e == 'checkplot') {
                    rowarr.push(cpdkey);
                }
                else {
                    rowarr.push(thisdata[e]);
                }
            }

        });

        // join the output row
        var csvrow = rowarr.join(separator);
        return csvrow

    },

    // this generates a CSV for download
    // FIXME: figure out how to do this
    cpdata_to_csv: function () {

        var csvarr = [cptracker.infocolumns.join('|')];

        for (obj in cptracker.cpdata) {
            csvarr.push(cptracker.cpdata_get_csvrow(obj,'|'));
        }

        csvarr = "data:text/csv;charset=utf-8," +
            encodeURIComponent(csvarr.join('\n'));

        $('#download-anchor').attr('href', csvarr);
        $('#download-anchor').attr('download', 'project-objectlist.csv');
        $('#download-anchor').html('get CSV for reviewed objects');
        $('#download-anchor').css({'display': 'inline'});

    },

    // this generates a JSON for download
    cpdata_to_json: function () {

        // we need to reverse the keys of the cpdata, so they're objectid first,
        // add the object's checkplot file into its own dict
        var jsonobj = {};

        for (obj in cptracker.cpdata) {

            var thisdata = cptracker.cpdata[obj];
            thisdata['checkplot'] = obj;
            jsonobj[thisdata['objectid']] = thisdata;

        }

        // generate the string
        var jsonstr = "data:text/json;charset=utf-8," +
            encodeURIComponent(JSON.stringify(jsonobj));

        $('#download-anchor').attr('href', jsonstr);
        $('#download-anchor').attr('download', 'project-objectlist.json');
        $('#download-anchor').html('get JSON for reviewed objects');
        $('#download-anchor').css({'display': 'inline'});

    }

};


// this is the container for the main functions
var cpv = {
    // these hold the current checkplot's data and filename respectively
    currentfind: 0,
    currfile: '',
    currcp: {},
    totalcps: 0,

    // these help in moving quickly through the phased LCs
    currphasedind: null,
    maxphasedind: null,

    // this function generates a spinner
    make_spinner: function (spinnermsg) {

        var spinner =
            '<div class="spinner">' +
            spinnermsg +
            '<div class="rect1"></div>' +
            '<div class="rect2"></div>' +
            '<div class="rect3"></div>' +
            '<div class="rect4"></div>' +
            '<div class="rect5"></div>' +
            '</div>';

        $('#alert-box').html(spinner);

    },

    // this function generates an alert box
    make_alert: function (alertmsg) {

        var xalert =
            '<div class="alert alert-warning alert-dismissible fade show" ' +
            'role="alert">' +
            '<button type="button" class="close" data-dismiss="alert" ' +
            'aria-label="Close">' +
            '<span aria-hidden="true">&times;</span>' +
            '</button>' +
            alertmsg +
            '</div>';

        $('#alert-box').html(xalert);

    },

    // this loads a checkplot from an image file into an HTML canvas object
    load_checkplot: function (filename) {

        // start the spinny thing
        cpv.make_spinner('loading...');
<commit_msg>cpserver.js: remove stale CSV/JSON download anchor after new cp load<commit_after>// cpserver.js - Waq<NAME> (<EMAIL>) - Jan 2017
// License: MIT. See LICENSE for the full text.
//
// This contains the JS to drive the checkplotserver's interface.
//

//////////////
// JS BELOW //
//////////////

// this contains utility functions
var cputils = {

    // this encodes a string to base64
    // https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
    b64_encode: function (str) {
        return btoa(
            encodeURIComponent(str)
                .replace(/%([0-9A-F]{2})/g,
                         function(match, p1) {
                             return String.fromCharCode('0x' + p1);
                         }));
    },

    // this turns a base64 string into an image by updating its source
    b64_to_image: function (str, targetelem) {

        var datauri = 'data:image/png;base64,' + str;
        $(targetelem).attr('src',datauri);

    }

};


// this contains updates to current checkplots, tracks the entire project's info
// and provides functions to save everything to CSV or JSON
var cptracker = {

    // this is the actual object that will get written to JSON or CSV
    cpdata: {},

    // this is the order of columns for generating the CSV
    infocolumns: [
        'objectid','checkplot',
        'objectinfo.hatid','objectinfo.twomassid',
        'objectinfo.network','objectinfo.stations','objectinfo.ndet',
        'objectinfo.objecttags','objectinfo.bmag','objectinfo.vmag',
        'objectinfo.sdssg','objectinfo.sdssr','objectinfo.sdssi',
        'objectinfo.jmag','objectinfo.hmag','objectinfo.kmag',
        'objectinfo.bvcolor','objectinfo.ijcolor','objectinfo.jkcolor',
        'objectinfo.pmra','objectinfo.pmra_err',
        'objectinfo.pmdecl','objectinfo.pmdecl_err',
        'objectinfo.propermotion','objectinfo.reducedpropermotion',
        'varinfo.objectisvar','varinfo.varperiod','varinfo.varepoch',
        'varinfo.vartags','comments'
    ],

    // this function generates a CSV row for a single object in the
    // cptracker.cpdata object
    cpdata_get_csvrow: function (cpdkey, separator) {

        var thisdata = cptracker.cpdata[cpdkey];
        var rowarr = [];

        cptracker.infocolumns.forEach(function (e, i, a) {

            // if the key is a compound one (firstlev.secondlev)
            if (e.indexOf('.') != -1) {

                var keys = e.split('.');
                rowarr.push(thisdata[keys[0]][keys[1]]);

            }

            // otherwise, it's just in the first level
            else {

                // special case of checkplot filename
                if (e == 'checkplot') {
                    rowarr.push(cpdkey);
                }
                else {
                    rowarr.push(thisdata[e]);
                }
            }

        });

        // join the output row
        var csvrow = rowarr.join(separator);
        return csvrow

    },

    // this generates a CSV for download
    // FIXME: figure out how to do this
    cpdata_to_csv: function () {

        var csvarr = [cptracker.infocolumns.join('|')];

        for (obj in cptracker.cpdata) {
            csvarr.push(cptracker.cpdata_get_csvrow(obj,'|'));
        }

        csvarr = "data:text/csv;charset=utf-8," +
            encodeURIComponent(csvarr.join('\n'));

        $('#download-anchor').attr('href', csvarr);
        $('#download-anchor').attr('download', 'project-objectlist.csv');
        $('#download-anchor').html('get CSV for reviewed objects');
        $('#download-anchor').css({'display': 'inline'});

    },

    // this generates a JSON for download
    cpdata_to_json: function () {

        // we need to reverse the keys of the cpdata, so they're objectid first,
        // add the object's checkplot file into its own dict
        var jsonobj = {};

        for (obj in cptracker.cpdata) {

            var thisdata = cptracker.cpdata[obj];
            thisdata['checkplot'] = obj;
            jsonobj[thisdata['objectid']] = thisdata;

        }

        // generate the string
        var jsonstr = "data:text/json;charset=utf-8," +
            encodeURIComponent(JSON.stringify(jsonobj));

        $('#download-anchor').attr('href', jsonstr);
        $('#download-anchor').attr('download', 'project-objectlist.json');
        $('#download-anchor').html('get JSON for reviewed objects');
        $('#download-anchor').css({'display': 'inline'});

    }

};


// this is the container for the main functions
var cpv = {
    // these hold the current checkplot's data and filename respectively
    currentfind: 0,
    currfile: '',
    currcp: {},
    totalcps: 0,

    // these help in moving quickly through the phased LCs
    currphasedind: null,
    maxphasedind: null,

    // this function generates a spinner
    make_spinner: function (spinnermsg) {

        var spinner =
            '<div class="spinner">' +
            spinnermsg +
            '<div class="rect1"></div>' +
            '<div class="rect2"></div>' +
            '<div class="rect3"></div>' +
            '<div class="rect4"></div>' +
            '<div class="rect5"></div>' +
            '</div>';

        $('#alert-box').html(spinner);

    },

    // this function generates an alert box
    make_alert: function (alertmsg) {

        var xalert =
            '<div class="alert alert-warning alert-dismissible fade show" ' +
            'role="alert">' +
            '<button type="button" class="close" data-dismiss="alert" ' +
            'aria-label="Close">' +
            '<span aria-hidden="true">&times;</span>' +
            '</button>' +
            alertmsg +
            '</div>';

        $('#alert-box').html(xalert);

    },

    // this loads a checkplot from an image file into an HTML canvas object
    load_checkplot: function (filename) {

        // remove the summary download anchor from view since it's stale now
        $('#download-anchor').css({'display': 'none'});

        // start the spinny thing
        cpv.make_spinner('loading...');

{'fraction_non_alphanumeric': 0.1438584779706275, 'fraction_numerical': 0.006008010680907877, 'mean_word_length': 2.070714834742506, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4424238', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3434, 'n_words': 846}