map_reduce.js 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * Created by RSercan on 3.1.2016.
 */
Template.mapReduce.onRendered(function () {
    Template.mapReduce.initializeAceEditor('aceMap');
    Template.mapReduce.initializeAceEditor('aceReduce');
    Template.mapReduce.initializeOptions();
});

Template.mapReduce.initializeOptions = function () {
    var cmb = $('#cmbMapReduceOptions');
    $.each(MAP_REDUCE_OPTIONS, function (key, value) {
        cmb.append($("<option></option>")
            .attr("value", key)
            .text(value));
    });

    cmb.chosen();
    Template.setOptionsComboboxChangeEvent(cmb);
};

Template.mapReduce.initializeAceEditor = function (id) {
    AceEditor.instance(id, {
        mode: "javascript",
        theme: 'dawn'
    }, function (editor) {
        editor.$blockScrolling = Infinity;
        editor.getSession().setOption("useWorker", false);
        editor.setOptions({
            fontSize: "11pt",
            showPrintMargin: false
        });
    });
};

Template.mapReduce.executeQuery = function () {
    Template.browseCollection.initExecuteQuery();
    var connection = Connections.findOne({_id: Session.get(Template.strSessionConnection)});
    var selectedCollection = Session.get(Template.strSessionSelectedCollection);
    var options = Template.mapReduceOptions.getOptions();
    var map = ace.edit("aceMap").getSession().getValue();
    var reduce = ace.edit("aceReduce").getSession().getValue();


S
Sercan 已提交
45
    if (map.parseFunction() == null) {
46 47 48 49 50
        toastr.error("Syntax error on map, not a valid function ");
        Ladda.stopAll();
        return;
    }

S
Sercan 已提交
51
    if (reduce.parseFunction() == null) {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        toastr.error("Syntax error on reduce, not a valid function ");
        Ladda.stopAll();
        return;
    }

    if (options["ERROR"]) {
        toastr.error(options["ERROR"]);
        Ladda.stopAll();
        return;
    }

    Meteor.call("mapReduce", connection, selectedCollection, map, reduce, options, function (err, result) {
        Template.renderAfterQueryExecution(err, result);
    });
};