Skip to content
Snippets Groups Projects
Console.js 2.5 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: {

        },

        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){
            this._writeOutput(string + "<br/>");
        },

        warn: function(string){
            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){

        },

        _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("");
            this.write(command);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        }

    });

    return new Console();