Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Devcon = new (function() {
  2.     var publicMethods = {
  3.         commandLogs: [],
  4.         actualCommand: -1,
  5.  
  6.         toggle: function() {
  7.             $(".devcon").addClass("toggle");
  8.         },
  9.  
  10.         bindInputActions: function() {
  11.             var thisReference = this;
  12.  
  13.             $('.devcon input[type="text"]').keydown(function(event) {
  14.                 switch(event.keyCode) {
  15.                     case 38:
  16.                         //Go forward in history
  17.                         thisReference.actualCommand++;
  18.  
  19.                         if(thisReference.commandLogs[thisReference.actualCommand] == undefined) {
  20.                             thisReference.actualCommand--;
  21.                         }
  22.  
  23.                         var inputText = thisReference.commandLogs[thisReference.actualCommand];
  24.  
  25.                 if(inputText == undefined) {
  26.                             return true;
  27.                         }
  28.  
  29.                         $('.devcon input[type="text"]').val(inputText);
  30.                     break;
  31.  
  32.                     case 40:
  33.                         //Go backward in history
  34.                         thisReference.actualCommand--;
  35.  
  36.                 if(thisReference.commandLogs[thisReference.actualCommand] == undefined && thisReference.actualCommand != -1) {
  37.                             thisReference.actualCommand++;
  38.                         }
  39.  
  40.                         var inputText = thisReference.commandLogs[thisReference.actualCommand];
  41.  
  42.                         if(thisReference.actualCommand == -1) {
  43.               inputText = '';
  44.                         } else if(inputText == undefined) {
  45.                             return true;
  46.                         }
  47.  
  48.                         $('.devcon input[type="text"]').val(inputText);
  49.                     break;
  50.                 }
  51.             });
  52.         },
  53.  
  54.  
  55.     executeUserCommand: function(commandInput) {
  56.             var command = this.parseCommand(commandInput);
  57.  
  58.             this.log("> "+commandInput);
  59.             this.saveCommand(commandInput);
  60.  
  61.             if(this.commandExists(command.commandName)) {
  62.                 this[command.commandName](command.arguments);
  63.             } else {
  64.                 this.errorLog("Command "+command.commandName+" doesn't exist!");
  65.             }
  66.         },
  67.  
  68.  
  69.     parseCommand: function(command) {
  70.             var commandArgs = Array(),
  71.                 prevCommand = command,
  72.               startedTextString = false,
  73.               isOpenString = false,
  74.               isCloseString = false;
  75.  
  76.           command += " ";
  77.  
  78.           while(prevCommand != command) {
  79.             prevCommand = command;
  80.  
  81.             command = command.replace(/^.*? /igm, function(match) {
  82.               var argument = match.replace(" ", "");
  83.  
  84.               isOpenString = false;
  85.               isCloseString = false;
  86.  
  87.               if((/^"/).test(match)) {
  88.                 startedTextString = true;
  89.                 isOpenString = true;
  90.  
  91.                 match = match.replace("\"", "");
  92.               }
  93.  
  94.                     if((/(" |")$/).test(match)) {
  95.                 startedTextString = false;
  96.                 isCloseString = true;
  97.  
  98.                 match = match.replace(/(" |")$/, "");
  99.               }
  100.  
  101.                     if(startedTextString || isCloseString) {
  102.                 if(isOpenString) {
  103.                     var lastKey = commandArgs.length;
  104.                 } else {
  105.                     var lastKey = commandArgs.length - 1;
  106.                 }
  107.  
  108.                 if(commandArgs[lastKey] == undefined) {
  109.                     commandArgs[lastKey] = "";
  110.                 }
  111.  
  112.                 commandArgs[lastKey] += match;
  113.               } else {
  114.                 commandArgs.push(argument);
  115.                     }
  116.  
  117.               return '';
  118.             });
  119.             }
  120.  
  121.           for(var c in commandArgs) {
  122.             if(parseFloat(commandArgs[c]) == commandArgs[c]) {
  123.                 commandArgs[c] = parseFloat(commandArgs[c]);
  124.             } else if((/^\{.*?\}$/).test(commandArgs[c]) == true) {
  125.                 commandArgs[c] = JSON.parse(commandArgs[c]);
  126.             }
  127.           }
  128.  
  129.                 return {
  130.                     commandName: commandArgs.shift(),
  131.                     arguments: commandArgs
  132.                 }
  133.         },
  134.  
  135.  
  136.         commandExists: function(commandName) {
  137.             return (this[commandName] != undefined);
  138.         },
  139.  
  140.  
  141.         log: function(message) {
  142.             if(typeof message == "object") {
  143.                 message = message[0];
  144.             }
  145.  
  146.             $(".devcon .log").html(function(n, current) {
  147.                 return current + message+"<br>";
  148.             });
  149.         },
  150.  
  151.  
  152.     errorLog: function(message) {
  153.             if(typeof message == "object") {
  154.                 message = message[0];
  155.             }
  156.  
  157.             this.log('<font color="#ff5555">'+message+'</font>');
  158.         },
  159.  
  160.  
  161.         warningLog: function(message) {
  162.             if(typeof message == "object") {
  163.                 message = message[0];
  164.             }
  165.  
  166.             this.log('<font color="#fdff55">'+message+'</font>');
  167.         },
  168.  
  169.  
  170.         infoLog: function(message) {
  171.             if(typeof message == "object") {
  172.                 message = message[0];
  173.             }
  174.  
  175.             this.log('<font color="#55e5ff">'+message+'</font>');
  176.         },
  177.  
  178.  
  179.         successLog: function(message) {
  180.             if(typeof message == "object") {
  181.                 message = message[0];
  182.             }
  183.  
  184.             this.log('<font color="#43ff24">'+message+'</font>');
  185.         },
  186.  
  187.  
  188.         saveCommand: function(input) {
  189.             this.commandLogs.unshift(input);
  190.             this.actualCommand = -1;
  191.         },
  192.  
  193.  
  194.         createCommand: function(commandName, callback, overwrite) {
  195.             if(this.commandExists(commandName) && overwrite || !this.commandExists(commandName)) {
  196.                 this[commandName] = callback
  197.             } else {
  198.                 this.errorLog("Command "+commandName+" actually exists!");
  199.             }
  200.         }
  201.     };
  202.  
  203.     for(var c in publicMethods) {
  204.         this[c] = publicMethods[c];
  205.     }
  206. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement