Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var CONTROLS = {
- UP: 38,
- DOWN: 40,
- ENTER: 13
- };
- var USER_COMMANDS = {
- 'SHOW_CONTACT': 'show contact',
- 'SHOW_EXPERIENCE': 'show experience',
- 'SHOW_BIO': 'show bio',
- 'CLEAR': 'clear',
- 'SINPAI_HELP': 'sinpai help'
- };
- var THEMES = {
- DRACULA: 'dracula',
- WINDOWS: 'windows'
- };
- function SinpaiConsole(config) {
- this.value = '';
- this.theme = config.theme;
- this.fontSize = config.fontSize || '12px';
- this.commands = [];
- this.commandIndex = this.commands.length - 1;
- this.input = document.getElementById('console-input');
- this.sinpaiApp = document.getElementsByTagName('body')[0];
- this.controls = CONTROLS;
- this.userCommands = USER_COMMANDS;
- return this;
- }
- SinpaiConsole.prototype.clearValue = function () {
- this.input.value = '';
- };
- SinpaiConsole.prototype.clearConsole = function () {
- var consoleContainer = this.input.parentNode;
- while (consoleContainer.children[0].tagName === 'P') {
- consoleContainer.removeChild(consoleContainer.firstChild);
- }
- };
- SinpaiConsole.prototype.showHelp = function () {
- for (var i = 0; i < CLI_COMMANDS.length; i++) {
- var CLICommand = document.createElement('p');
- CLICommand.innerHTML = CLI_COMMANDS[i].command.toUpperCase();
- var CLICommandDescription = document.createElement('span');
- CLICommandDescription.innerHTML = CLI_COMMANDS[i].description;
- CLICommandDescription.classList.add('command-description');
- CLICommand.appendChild(CLICommandDescription);
- this.input.parentNode.insertBefore(CLICommand, this.input);
- }
- };
- SinpaiConsole.prototype.getCommand = function () {
- return this.input.value;
- };
- SinpaiConsole.prototype.getPreviousCommand = function () {
- if (this.isCommandsListEmpty()) {
- return;
- }
- var prevIndex = this.commandIndex--;
- if (this.isFirstCommand()) {
- this.resetCommandIndex();
- }
- this.input.value = this.commands[prevIndex];
- };
- SinpaiConsole.prototype.isFirstCommand = function () {
- return this.commandIndex <= 0;
- };
- SinpaiConsole.prototype.isLastCommand = function () {
- return this.commandIndex >= this.commands.length - 1;
- };
- SinpaiConsole.prototype.getNextCommand = function () {
- if (this.isCommandsListEmpty()) {
- return;
- }
- var nextIndex = ++this.commandIndex - 1;
- if (this.isLastCommand()) {
- this.setDefaultCommandIndex();
- }
- this.input.value = this.commands[nextIndex];
- };
- SinpaiConsole.prototype.resetCommandIndex = function () {
- this.commandIndex = 0;
- };
- SinpaiConsole.prototype.setDefaultCommandIndex = function () {
- this.commandIndex = this.commands.length - 1;
- };
- SinpaiConsole.prototype.isCommandsListEmpty = function () {
- return this.commands.length === 0;
- };
- SinpaiConsole.prototype.setCommand = function () {
- this.commands.push(this.input.value);
- this.showSection();
- this.resetCommandIndex();
- this.displayLastCommand();
- };
- SinpaiConsole.prototype.displayLastCommand = function () {
- var commandNode;
- this.isCommandFound() ? commandNode = this.getCorrectCommandNode() : commandNode = this.getErrorCommandNode();
- this.input.parentNode.insertBefore(commandNode, this.input);
- };
- SinpaiConsole.prototype.getCorrectCommandNode = function () {
- var command = document.createElement('p');
- command.innerHTML = this.commands[this.commands.length - 1];
- return command;
- };
- SinpaiConsole.prototype.getErrorCommandNode = function () {
- var errorCommand = document.createElement('p');
- errorCommand.innerHTML = this.input.value + ' command not found! use SINPAI HELP to get command list';
- errorCommand.classList.add('error');
- return errorCommand;
- };
- SinpaiConsole.prototype.isCommandFound = function () {
- var userCommand = this.input.value.toUpperCase().replace(' ', '_');
- return USER_COMMANDS.hasOwnProperty(userCommand);
- };
- SinpaiConsole.prototype.setControls = function () {
- var self = this;
- this.input.addEventListener('keyup', function(e) {
- var key = e.which || e.keyCode;
- if (key === self.controls.UP) {
- self.getPreviousCommand();
- }
- if (key === self.controls.DOWN) {
- self.getNextCommand();
- }
- if (key === self.controls.ENTER) {
- self.setCommand();
- self.clearValue();
- }
- });
- return this;
- };
- SinpaiConsole.prototype.showSection = function () {
- switch(this.input.value) {
- case this.userCommands.SHOW_BIO:
- this.showBio();
- break;
- case this.userCommands.SHOW_EXPERIENCE:
- this.showExperience();
- break;
- case this.userCommands.SHOW_CONTACT:
- this.showContact();
- break;
- case this.userCommands.CLEAR:
- this.clearConsole();
- break;
- case this.userCommands.SINPAI_HELP:
- this.showHelp();
- break;
- default:
- return;
- }
- };
- SinpaiConsole.prototype.showBio = function () {
- return new DOMSection(SECTIONS.BIO).createHeader();
- };
- SinpaiConsole.prototype.showExperience = function () {
- return new DOMSection(SECTIONS.EXPERIENCE).createHeader();
- };
- SinpaiConsole.prototype.showContact = function () {
- return new DOMSection(SECTIONS.CONTACT).createHeader();
- };
- SinpaiConsole.prototype.setTheme = function () {
- this.sinpaiApp.classList.add(this.theme.toLowerCase());
- return this;
- };
- SinpaiConsole.prototype.setFontSize = function () {
- this.sinpaiApp.style.fontSize = this.fontSize;
- return this;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement