var BrsConsole; (function() { BrsConsole = (function() { function BrsConsole(oCoreInst){ var _this = this; _this.jsBase = oCoreInst; _this.jqObj = $("#debug_console"); // Create class remover extension $.fn.removeClassStartingWith = function (filter) { $(this).removeClass(function (index, className) { return (className.match(new RegExp("\\S*" + filter + "\\S*", 'g')) || []).join(' ') }); return this; }; if(_this.jqObj.length){ _this.jqLineCnt = _this.jqObj.find("ul#console-lines"); _this.jqLine = _this.jqObj.find("li.sample"); _this.jqInput = _this.jqObj.find("#cmd-input"); _this.jqScrollCnt = _this.jqObj.find(".scroll"); _this.iLineNr = _this.jqLineCnt.find(">li").length; _this.initClass(); } } // Settings BrsConsole.prototype = { sUrl : "/ajax/ac:console", jsBase : {}, jqObj : {}, jqInput : {}, jqLine :{}, jqLineCnt : {}, jqScrollCnt : {}, iLineNr : 0, bEnabled : false, vTypes : { C_TYPES_JS : "JS", C_TYPES_PHP : "PHP" }, vKeys : { C_TYPE : "t", C_FUNCTION : "f", C_LINE_DATA : "l", C_RESPONSE_DATA : "r" } }; BrsConsole.prototype.initClass = function(){ var _this = this; _this.bEnabled = true; // Append all server side PR's var jqLogerOutputs = $("pre.logger"); if(jqLogerOutputs.length){ jqLogerOutputs.each(function(){ _this.addNewLine(" ", "PR( )", false, $(this).detach().show()); }); } _this.jqObj.append(""); var sc = _this.jqObj.find(".scroll"); // Scroll to bottom sc.scrollTop(sc[0].scrollHeight); // sc.click(function(){ // _this.jqObj.find("input").focus(); // }); // Add action marker sc.find(".sql").closest("li").addClass("e"); sc.find(".sql").closest("li").dblclick(function(){ _this.handleSqlLineClick($(this)); }); sc.find("span.wpc").dblclick(function(){ _this.handleWrappedLineClick($(this)); }); if(_this.jqInput && _this.jqInput.length){ _this.jqInput.bind({ keyup : function(event){ var v = $(this).val(); if(event.which == _this.jsBase.iKeys.ENTER){ $(this).val(""); _this.handleCommand(v); }else if(event.which == _this.jsBase.iKeys.ESC){ $(this).val(""); } } }); } // Bind ESC button $(document).bind('keydown', function(event){ if(event.which == _this.jsBase.iKeys.ESC){ if(_this.bEnabled){ if(_this.jqObj.hasClass("show")){ _this.jqObj.removeClass("show"); $("body").removeClass("oh"); }else if(event.ctrlKey || event.metaKey){ _this.jqObj.show().addClass("show"); $("body").addClass("oh"); _this.jqObj.find("input").focus(); } } } }); }; BrsConsole.prototype.handleSqlLineClick = function(jqLi, bExpOnly, bCollOnly){ var _this = this; var jqSql = jqLi.find(".sql"); bExpOnly = defaultVal(bExpOnly, false); bCollOnly = defaultVal(bCollOnly, false); if(jqSql.parent().is("pre")){ if(!bExpOnly){ jqSql.html(jqSql.unwrap().html()); jqLi.removeClassStartingWith("t"); } }else{ if(!bCollOnly){ jqSql.html(jqSql.wrap("
").html().replace(/^\n+|\n+ *$/g, ''));
                    _this.jqObj.append("");
                    jqLi.addClass("t" + jqSql.height());
                }
            }
            if(!bExpOnly && !bCollOnly) _this.moveScrollToBottom(jqLi);
        };
        BrsConsole.prototype.handleWrappedLineClick = function(jqSpan){
            var _this = this;

            var jqLi = jqSpan.closest("li");
            jqSpan.toggleClass("auto-wrap");

            if(jqSpan.hasClass("auto-wrap")){
                jqSpan.find("pre").html(jqSpan.find("pre").html().replace(/^\n+|\n+ *$/g, ''));
                _this.jqObj.append("");
                jqLi.addClass("t" + jqSpan.height());
            }else{
                jqLi.removeClassStartingWith("t");
            }

            _this.moveScrollToBottom(jqLi);
        };
        BrsConsole.prototype.handleCommand = function(sCommand){
            var _this = this;
            if(sCommand.length){
                var oData = {
                    action : "check_command",
                    data : sCommand
                };
                _this.jsBase.ajax(oData, _this.sUrl, function(d){
                    _this.handleServerResponse(d);
                })
            }
        };
        BrsConsole.prototype.addNewLine = function(sTime, sType, sText, jqObj){
            var _this = this;

            sTime = defaultVal(sTime, " ");
            sType = defaultVal(sType, "DEF");
            sText = defaultVal(sText, " ");
            jqObj = defaultVal(jqObj, false);

            var jqLi = _this.jqLine.clone(true, true).removeClass("sample");
            var jqP = jqLi.find("p");

            jqP.find(".nr").html(_this.iLineNr++ + ".");

            var jqSpan = $('');
            jqP.append(jqSpan.clone().append(sTime));
            jqP.append(jqSpan.clone().append(sType));
            if(jqObj.length){
                var c = jqSpan.clone().addClass("wpc");
                pr(c);
                jqP.append(c.append(jqObj));
            }else
                jqP.append(jqSpan.clone().html(sText));

            _this.jqLineCnt.append(jqLi.show());
        };
        BrsConsole.prototype.moveScrollToBottom = function(jqElement){
            var _this = this, iTop = 0;
            jqElement = defaultVal(jqElement, false);
            if(_this.jqScrollCnt.length){
                iTop = _this.jqScrollCnt[0].scrollHeight;
                if(jqElement.length) iTop = (jqElement.offset().top - _this.jqLineCnt.offset().top - 30)
                _this.jqScrollCnt.stop().animate({scrollTop : iTop}, 500, 'swing');
            }
        };
        BrsConsole.prototype.handleServerResponse = function(oResponse){
            var _this = this;
            if(oResponse && oResponse[_this.vKeys.C_TYPE]){
                // Switch & handle actions
                switch(oResponse[_this.vKeys.C_TYPE]){
                    case _this.vTypes.C_TYPES_JS:
                        // If callback function exists - exec
                        if(oResponse[_this.vKeys.C_FUNCTION]){
                            if(oResponse[_this.vKeys.C_FUNCTION] in _this){
                                var vData = (oResponse[_this.vKeys.C_RESPONSE_DATA] ? oResponse[_this.vKeys.C_RESPONSE_DATA] : false);
                                _this[oResponse[_this.vKeys.C_FUNCTION]](vData);
                            }
                        }
                        break;

                    case _this.vTypes.C_TYPES_PHP:

                        break;
                }

                // Add line data if line data passed
                if(oResponse[_this.vKeys.C_LINE_DATA]){
                    var qjLines = $(oResponse[_this.vKeys.C_LINE_DATA]);
                    if(qjLines.length){
                        qjLines.find(".nr").each(function(){
                            $(this).html(_this.iLineNr++ + ".");
                        });
                        _this.jqLineCnt.append(qjLines);
                        _this.moveScrollToBottom();
                    }
                }
            }
        };
        BrsConsole.prototype.expandAllSqlQueries = function(vData, bExpandCollapse){
            var _this = this;

            var jqLi = _this.jqLineCnt.find(".sql").closest("li");
            if(jqLi.length){
                jqLi.each(function(){
                    _this.handleSqlLineClick($(this), true, false);
                    _this.moveScrollToBottom();
                });
            }
        };


        return BrsConsole;

    })();

}).call(this);