Skip to content
Snippets Groups Projects
Console.js 3.4 KiB
Newer Older
Amanda Ghassaei's avatar
Amanda Ghassaei committed
/**
 * Created by aghassaei on 10/26/15.
 */


Amanda Ghassaei's avatar
Amanda Ghassaei committed
define(['jquery', 'underscore', 'backbone', 'appState'], function($, _, Backbone, appState){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    var Console = Backbone.View.extend({
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        el: "#console",

        events: {
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            "click #consoleSaveScript":                     "_saveScript"
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        },

        initialize: function(){

Amanda Ghassaei's avatar
Amanda Ghassaei committed
            _.bindAll(this, "_onKeyUp");
            $(document).bind('keyup', {}, this._onKeyUp);

Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this.listenTo(appState, "change:consoleIsVisible", this._setVisibility);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this.listenTo(appState, "change:menuIsVisible", function(){
                this._setWidth(false);
            });
            this._setWidth(false);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this._setVisibility();
        },

        _setWidth: function(immediately){
            var padding = "0";
            if (appState.get("menuIsVisible")) padding = "430px";
            if (immediately) this.$el.css({"padding-right":padding});
            else this.$el.animate({"padding-right":padding});
        },

        _setVisibility: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            if (this._isVisible()) this._show();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            else this._hide();
        },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        _isVisible: function(){
            return appState.get("consoleIsVisible");
        },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        write: function(string){//for commands
            this._writeOutput(string + "<br>");
        },

        log: function(string){//for comments
            this._writeOutput("<span class='consoleComment'>" + string + "</span><br>");
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        },

        warn: function(string){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this._writeOutput("<span class='consoleWarning'>" + string + "</span><br>");
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        error: function(string){

        },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        clear: function(){
            $("#consoleOutput").html("");
        },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        _writeOutput: function(html){
            var $output = $("#consoleOutput");
            var height = $output.height();
            $output.append(html);
            $output.height(height);
            $output.scrollTop($output.scrollTop()+$output.innerHeight());
        },

        _show: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this.$el.fadeIn();
        },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        _hide: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this.$el.fadeOut();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        },

        _onKeyUp: function(e){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            var $input = $("#consoleInput");
            if ($input.is(":focus")){
                if (e.keyCode == 27) $input.blur();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
//                if (e.keyCode == 38) $output.val(this.model.getPrevHistElem());
//                else if (e.keyCode == 40) $output.val(this.model.getNewerHistElem());
                else if (e.keyCode == 13) this._enterCommand($input);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            }
        },

        _enterCommand: function($input){
//            var command = $input.val();
            var command = "nice try, this doesn't work yet :)";
            $input.val("");
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this.log(command);
        },

        _saveScript: function(e){
            e.preventDefault();
            var self = this;
            require(['fileSaver'], function(fileSaver){
                fileSaver.saveConsoleScript(self.getConsoleData());
            })
        },

        getConsoleData: function(){
            var data = $("#consoleOutput").html().split("<br>");
            data.pop();//last line is ""
            var commands = []
            _.each(data, function(line, index){
                if (line.substr(0,5) != "<span") commands.push(line);
            });
            return commands.join('\n');
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        }

    });

    return new Console();