common.js 3.1 KB
Newer Older
S
Sercan 已提交
1 2 3
/**
 * Created by RSercan on 26.12.2015.
 */
S
Sercan 已提交
4 5 6 7
Template.strSessionConnection = 'connection';
Template.strSessionCollectionNames = 'collectionNames';
Template.strSessionSelectedCollection = 'selectedCollection';
Template.strSessionSelectedQuery = 'selectedQuery';
8
Template.strSessionSelectedOptions = "selectedOptions";
S
Sercan 已提交
9

S
Sercan 已提交
10 11 12 13
Template.clearSessions = function () {
    Session.set(Template.strSessionCollectionNames, undefined);
    Session.set(Template.strSessionConnection, undefined);
    Session.set(Template.strSessionSelectedCollection, undefined);
S
Sercan 已提交
14 15
};

S
Sercan 已提交
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
Template.setOptionsComboboxChangeEvent = function (cmb) {
    cmb.on('change', function (evt, params) {
        var array = Session.get(Template.strSessionSelectedOptions);
        if (params.deselected) {
            array.remove(params.deselected);
        }
        else {
            array.push(params.selected);
        }
        Session.set(Template.strSessionSelectedOptions, array);
    });
};

Template.getParentTemplateName = function (levels) {
    var view = Blaze.currentView;
    if (typeof levels === "undefined") {
        levels = 1;
    }
    while (view) {
        if (view.name.indexOf("Template.") != -1 && !(levels--)) {
            return view.name.substring(view.name.indexOf('.') + 1);
        }
        view = view.parentView;
    }
};

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

        // remove newlines in pasted text
        editor.on("paste", function (e) {
            e.text = e.text.replace(/[\r\n]+/g, " ");
        });
        // make mouse position clipping nicer
        editor.renderer.screenToTextCoordinates = function (x, y) {
            var pos = this.pixelToScreenCoordinates(x, y);
            return this.session.screenToDocumentPosition(
                Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
                Math.max(pos.column, 0)
            );
        };
        // disable Enter Shift-Enter keys
        editor.commands.bindKey("Enter|Shift-Enter", evt);
    });
};

Template.registerHelper('isSelected', function (option) {
    return $.inArray(option, Session.get(Template.strSessionSelectedOptions)) != -1;
});

S
Sercan 已提交
74
Template.registerHelper('getConnection', function () {
S
Sercan 已提交
75 76
    if (Session.get(Template.strSessionConnection)) {
        return Connections.findOne({_id: Session.get(Template.strSessionConnection)});
S
Sercan 已提交
77 78 79
    }
});

S
Sercan 已提交
80
Template.registerHelper('getCollectionNames', function () {
S
Sercan 已提交
81
    return Session.get(Template.strSessionCollectionNames);
S
Sercan 已提交
82 83
});

S
Sercan 已提交
84
Template.registerHelper('getSelectedCollection', function () {
S
Sercan 已提交
85
    return Session.get(Template.strSessionSelectedCollection);
S
Sercan 已提交
86
});
S
Sercan 已提交
87

S
Sercan 已提交
88 89 90
/**
 * Adds remove by value functionality to arrays. e.x. myArray.remove('myValue');
 * */
S
Sercan 已提交
91 92 93 94 95 96 97 98 99 100
Array.prototype.remove = function () {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};