|
| 1 | +const BLACKLISTED_KEY_CODES = [38, 40, 37, 39, 18, 20, 17, 16, 9, 27, 144]; |
| 2 | +//List of commands |
| 3 | +const COMMANDS = { |
| 4 | + help: 'The page you want to visit does not exist, or it may have been deleted, or the wrong address was entered. To see the commands, enter the word <span class="red"> commands</span>', |
| 5 | + exit: '', |
| 6 | + report: "<span class='green'>This page report has been successfully sent to support.</span>", |
| 7 | + commands: 'List of commands: <span class="red"> help</span> , <span class="red"> report</span> ,<span class="red"> exit</span>\n', |
| 8 | + cls: '', |
| 9 | +}; |
| 10 | + |
| 11 | +let userInput; |
| 12 | +let terminalOutput; |
| 13 | +let Terminal; |
| 14 | +let Keyboard; |
| 15 | +const app = () => { |
| 16 | + userInput = document.getElementById('userInput'); |
| 17 | + terminalOutput = document.getElementById('code'); |
| 18 | + Terminal = document.getElementById('Terminal'); |
| 19 | + Keyboard = document.getElementById('Keyboard'); |
| 20 | + Keyboard.focus(); |
| 21 | + if (screen.width < 991) { |
| 22 | + Keyboard.addEventListener('keyup', key); |
| 23 | + } else { |
| 24 | + document.addEventListener('keypress', key); |
| 25 | + } |
| 26 | + document.addEventListener('keydown', backSpace); |
| 27 | +}; |
| 28 | + |
| 29 | +//When the user click the 'Enter' key |
| 30 | +const execute = function executeCommand(input) { |
| 31 | + let output; |
| 32 | + |
| 33 | + if (input.length === 0) { |
| 34 | + return; |
| 35 | + } |
| 36 | + //If what the user entered is not in the command list |
| 37 | + if (!COMMANDS.hasOwnProperty(input)) { |
| 38 | + output = `<p>The command entered is not correct</p>`; |
| 39 | + } |
| 40 | + //If user enter the word cls |
| 41 | + else if (input === 'cls') { |
| 42 | + terminalOutput.innerHTML = ''; |
| 43 | + return; |
| 44 | + } |
| 45 | + //If the user enters one of the words 'exit' and 'close' |
| 46 | + else if (input === 'close' || input === 'exit') { |
| 47 | + document.location.href = '../../index.html'; // The link that the user enters after sending the exit |
| 48 | + return; |
| 49 | + } |
| 50 | + //If the user enters the word report |
| 51 | + else if (input === 'report') { |
| 52 | + terminalOutput.innerHTML = `${terminalOutput.innerHTML}<p>${COMMANDS[input]}</p>`; |
| 53 | + |
| 54 | + return; |
| 55 | + } |
| 56 | + //Otherwise, the text of the command will be displayed as output |
| 57 | + else { |
| 58 | + output = COMMANDS[input]; |
| 59 | + } |
| 60 | + |
| 61 | + terminalOutput.innerHTML = `${terminalOutput.innerHTML}<p class="out_code">${output}</p>`; |
| 62 | + Terminal.scrollTop = terminalOutput.scrollHeight; |
| 63 | +}; |
| 64 | +let str = ''; |
| 65 | +//when user click any key |
| 66 | +const key = function keyEvent(event) { |
| 67 | + let currentKey = event.key; |
| 68 | + Keyboard.focus(); |
| 69 | + Keyboard.innerHTML = event.target.value; |
| 70 | + if (BLACKLISTED_KEY_CODES.includes(event.keyCode)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + if (!currentKey || currentKey === 'Unidentified' || screen.width < 991) { |
| 74 | + currentKey = event.target.value; |
| 75 | + } |
| 76 | + if (event.key === 'Enter') { |
| 77 | + execute(userInput.innerHTML); |
| 78 | + userInput.innerHTML = ''; |
| 79 | + currentKey = ''; |
| 80 | + event.target.value = ''; |
| 81 | + str = ''; |
| 82 | + } else { |
| 83 | + if (screen.width < 991) { |
| 84 | + str = currentKey; |
| 85 | + } else { |
| 86 | + str += currentKey; |
| 87 | + } |
| 88 | + event.preventDefault(); |
| 89 | + userInput.innerHTML = str; |
| 90 | + } |
| 91 | +}; |
| 92 | +//when user click 'BackSpace' key |
| 93 | +const backSpace = function backSpace(e) { |
| 94 | + //if user click backspace |
| 95 | + if (e.keyCode === 8) { |
| 96 | + userInput.innerHTML = userInput.innerHTML.slice(0, userInput.innerHTML.length - 1); |
| 97 | + str = str.slice(0, str.length - 1); |
| 98 | + } |
| 99 | +}; |
| 100 | +//When the user clicks on a control buttons |
| 101 | +const BTNS = function MenuBTN(t) { |
| 102 | + switch (t) { |
| 103 | + case 'max': |
| 104 | + if (document.getElementById('body').className !== 'max') { |
| 105 | + document.getElementById('body').className = 'max'; |
| 106 | + } else { |
| 107 | + document.getElementById('body').className = ''; |
| 108 | + } |
| 109 | + break; |
| 110 | + case 'min': |
| 111 | + if (document.getElementById('body').className === 'max') { |
| 112 | + document.getElementById('body').className = 'max min'; |
| 113 | + } else if (document.getElementById('body').className !== 'max') { |
| 114 | + document.getElementById('body').className = 'min'; |
| 115 | + } |
| 116 | + break; |
| 117 | + case 're': |
| 118 | + if (document.getElementById('body').className === 'max min') { |
| 119 | + document.getElementById('body').className = 'max'; |
| 120 | + } |
| 121 | + if (document.getElementById('body').className === 'min') { |
| 122 | + document.getElementById('body').className = ''; |
| 123 | + } |
| 124 | + break; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +document.addEventListener('DOMContentLoaded', app); |
0 commit comments