Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Devcon = new (function() {
- var publicMethods = {
- commandLogs: [],
- actualCommand: -1,
- toggle: function() {
- $(".devcon").addClass("toggle");
- },
- bindInputActions: function() {
- var thisReference = this;
- $('.devcon input[type="text"]').keydown(function(event) {
- switch(event.keyCode) {
- case 38:
- //Go forward in history
- thisReference.actualCommand++;
- if(thisReference.commandLogs[thisReference.actualCommand] == undefined) {
- thisReference.actualCommand--;
- }
- var inputText = thisReference.commandLogs[thisReference.actualCommand];
- if(inputText == undefined) {
- return true;
- }
- $('.devcon input[type="text"]').val(inputText);
- break;
- case 40:
- //Go backward in history
- thisReference.actualCommand--;
- if(thisReference.commandLogs[thisReference.actualCommand] == undefined && thisReference.actualCommand != -1) {
- thisReference.actualCommand++;
- }
- var inputText = thisReference.commandLogs[thisReference.actualCommand];
- if(thisReference.actualCommand == -1) {
- inputText = '';
- } else if(inputText == undefined) {
- return true;
- }
- $('.devcon input[type="text"]').val(inputText);
- break;
- }
- });
- },
- executeUserCommand: function(commandInput) {
- var command = this.parseCommand(commandInput);
- this.log("> "+commandInput);
- this.saveCommand(commandInput);
- if(this.commandExists(command.commandName)) {
- this[command.commandName](command.arguments);
- } else {
- this.errorLog("Command "+command.commandName+" doesn't exist!");
- }
- },
- parseCommand: function(command) {
- var commandArgs = Array(),
- prevCommand = command,
- startedTextString = false,
- isOpenString = false,
- isCloseString = false;
- command += " ";
- while(prevCommand != command) {
- prevCommand = command;
- command = command.replace(/^.*? /igm, function(match) {
- var argument = match.replace(" ", "");
- isOpenString = false;
- isCloseString = false;
- if((/^"/).test(match)) {
- startedTextString = true;
- isOpenString = true;
- match = match.replace("\"", "");
- }
- if((/(" |")$/).test(match)) {
- startedTextString = false;
- isCloseString = true;
- match = match.replace(/(" |")$/, "");
- }
- if(startedTextString || isCloseString) {
- if(isOpenString) {
- var lastKey = commandArgs.length;
- } else {
- var lastKey = commandArgs.length - 1;
- }
- if(commandArgs[lastKey] == undefined) {
- commandArgs[lastKey] = "";
- }
- commandArgs[lastKey] += match;
- } else {
- commandArgs.push(argument);
- }
- return '';
- });
- }
- for(var c in commandArgs) {
- if(parseFloat(commandArgs[c]) == commandArgs[c]) {
- commandArgs[c] = parseFloat(commandArgs[c]);
- } else if((/^\{.*?\}$/).test(commandArgs[c]) == true) {
- commandArgs[c] = JSON.parse(commandArgs[c]);
- }
- }
- return {
- commandName: commandArgs.shift(),
- arguments: commandArgs
- }
- },
- commandExists: function(commandName) {
- return (this[commandName] != undefined);
- },
- log: function(message) {
- if(typeof message == "object") {
- message = message[0];
- }
- $(".devcon .log").html(function(n, current) {
- return current + message+"<br>";
- });
- },
- errorLog: function(message) {
- if(typeof message == "object") {
- message = message[0];
- }
- this.log('<font color="#ff5555">'+message+'</font>');
- },
- warningLog: function(message) {
- if(typeof message == "object") {
- message = message[0];
- }
- this.log('<font color="#fdff55">'+message+'</font>');
- },
- infoLog: function(message) {
- if(typeof message == "object") {
- message = message[0];
- }
- this.log('<font color="#55e5ff">'+message+'</font>');
- },
- successLog: function(message) {
- if(typeof message == "object") {
- message = message[0];
- }
- this.log('<font color="#43ff24">'+message+'</font>');
- },
- saveCommand: function(input) {
- this.commandLogs.unshift(input);
- this.actualCommand = -1;
- },
- createCommand: function(commandName, callback, overwrite) {
- if(this.commandExists(commandName) && overwrite || !this.commandExists(commandName)) {
- this[commandName] = callback
- } else {
- this.errorLog("Command "+commandName+" actually exists!");
- }
- }
- };
- for(var c in publicMethods) {
- this[c] = publicMethods[c];
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement