/* Describe al escritor */ function Writer() { // La sección del documento sobre la cual el escritor esta focalizado this.documentSection = null; // Determina si el escritor va a escribir o no en mayusculas this.capitalStatus = false; // Ordena al escritor a focalizarse en una sección particular del documento this.inDocumentSection = function(element) { this.documentSection = new DocumentSection(element); } // Ordena al escritor a escribir un numero en la sección del documento en la que este trabajando this.writeNumber = function(number) { this.write(function(documentSection, capitalStatus) {number.writeOn(documentSection);}); } // Ordena al escritor a escribir una letra en la sección del documento en la que este trabajando this.writeLetter = function(letter) { this.write(function(documentSection, capitalStatus) {letter.writeOn(documentSection, capitalStatus);}); } // Ordena al escritor a escribir en mayusculas this.inCapitalLetter = function() { this.capitalStatus = !this.capitalStatus; } // Ordena al escritor a que borre la ultima letra que escribio this.erase = function() { this.documentSection.deleteBackward(); } // Realiza la escritura en la sección del documento this.write = function(callback) { if (this.documentSection != null) { try { callback(this.documentSection, this.capitalStatus); } catch (e) { alert(e.message); } } else { alert("Seleccione primero un elemento que permita texto donde pueda escribir."); } } } /* Describe una sección particular de un documento */ function DocumentSection(element) { // Representa la sección concreta del documento this.element = element; // Concatena un caracter a la sección this.append = function(character) { if (this.isWriteable()) { this.element.value += character; } else { throw "No es posible escribir sobre un elemento del tipo: " + this.element.type + ". Por favor seleccione un elemento donde escribir que permita el ingreso de texto."; } } // Borra de la sección la ultima letra escrita this.deleteBackward = function() { var value = this.element.value; this.element.value = value.slice(0, value.length - 1); } // Deduce si la sección admite ser escrita this.isWriteable = function() { var writeable = false; writeable |= this.element.type == 'text'; writeable |= this.element.type == 'textarea'; writeable |= this.element.type == 'password'; return writeable; } } /** Describe un numero */ function Number(character) { // El caracter que representa en valor del numero this.character = character; // El numero se escribe sobre una sección de un documento this.writeOn = function(documentSection) { documentSection.append(this.character); } } /** Describe una letra */ function Letter(character) { // El caracter que representa en valor de la letra en miniscula this.character = character; // El caracter que representa en valor de la letra pero en mayuscula this.capitalCharacter = String.fromCharCode(character.charCodeAt(0) - 32); // Escribe la letra sobre una sección de un documento this.writeOn = function(documentSection, capital) { if (this.character == "espacio") { documentSection.append(' '); } else { if (capital) { documentSection.append(this.capitalCharacter); } else { documentSection.append(this.character); } } } } /** Algoritmo de utilidad que mezcla las posiciones de un array */ function ArrayUtils() {} ArrayUtils.mixContents = function(array) { var result = new Array(array.length); for (var i = 0; i < array.length; i++) { var randomIndex = Math.floor(Math.random() * array.length); while (result[randomIndex] != null) { randomIndex = Math.floor(Math.random() * array.length); } result[randomIndex] = array[i]; } return result; } function Builder() { var writer = new Writer(); var factory = new DOMFactory(); this.closeKeyboardCallback = null; this.form = null; this.parent = null; this.doMix = false; this.letters = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "ñ", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"); this.numbers = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); var keys = new Array(this.letters.length + this.numbers.length); var keyIndex = 0; /* Writing Actions */ var writeInDocumentSectionAction = function() {writer.inDocumentSection(this);}; var writeInCapitalLettersAction = function() {writer.inCapitalLetter();}; var deleteLastTypedCharacterAction = function() {writer.erase();}; var writeLetterAction = function() {writer.writeLetter(new Letter(this.fixedValue))}; var writeNumberAction = function() {writer.writeNumber(new Number(this.fixedValue))}; var hideKeyValueAction = function() {this.value = "#";}; var restoreKeyValueAction = function() {this.value = this.fixedValue;}; var hideKeyValuesAction = function() {for (var index = 0; index < keys.length; index++) { keys[index].value = "#"; }}; var restoreKeyValuesAction = function() {for (var index = 0; index < keys.length; index++) {keys[index].value = keys[index].fixedValue;}}; var NOPAction = function() {}; this.closeKeyboardCallback = function(callback) { this.closeKeyboardCallback = callback; return this; } this.givenForm = function(form) { this.form = form; return this; } this.mixLettersAndNumbers = function() { this.doMix = true; return this; } this.drawOnParent = function(parent) { this.parent = parent; return this; } this.doBuild = function() { this.mixLettersAndNumbersIfNecessary(); this.initializeOnFocusEventForFormElements(); this.drawLettersAndNumbers(); return this; } this.initializeOnFocusEventForFormElements = function() { var inputElements = this.form.getElementsByTagName("input"); for (var i = 0; i < inputElements.length; i++) { inputElements[i].onfocus = writeInDocumentSectionAction; } } this.mixLettersAndNumbersIfNecessary = function() { if (this.doMix) { this.letters = ArrayUtils.mixContents(this.letters); this.numbers = ArrayUtils.mixContents(this.numbers); } } this.createKeyboardForNumbers = function(characters, breakIndex, onlickCallback) { var tbl = factory.createTable(); var tblBody = factory.createTableBody(); var row = factory.createTableRow(); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[0], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[1], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[2], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); tblBody.appendChild(row); var row = factory.createTableRow(); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[3], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[4], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[5], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); tblBody.appendChild(row); var row = factory.createTableRow(); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[6], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[7], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[8], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); tblBody.appendChild(row); var row = factory.createTableRow(); row.appendChild(factory.createTableCell()); var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[9], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); row.appendChild(factory.createTableCell()); tblBody.appendChild(row); tbl.appendChild(tblBody); return tbl; } this.createKeyboardForLetters = function(characters, breakIndex, onlickCallback) { var tbl = factory.createTable(); var tblBody = factory.createTableBody(); var row = factory.createTableRow(); for (var index = 0; index < characters.length; index++) { var cell = factory.createTableCell(); var keyButton = factory.createLetterNumberKey(characters[index], onlickCallback, hideKeyValueAction, restoreKeyValueAction, hideKeyValuesAction, restoreKeyValuesAction); keys[keyIndex++] = keyButton; cell.appendChild(keyButton); row.appendChild(cell); if (((index + 1) % breakIndex) == 0) { tblBody.appendChild(row); if (index + 1 < characters.length) { row = factory.createTableRow(); } } } tblBody.appendChild(row); tbl.appendChild(tblBody); return tbl; } this.createControlBoard = function() { var tbl = factory.createTable(); var tblBody = factory.createTableBody(); row = factory.createTableRow(); cell = factory.createTableCell(); var capslockButton = factory.createLetterNumberKey( /*String.fromCharCode(8593)*/"MAY", writeInCapitalLettersAction, NOPAction, NOPAction, NOPAction, NOPAction); capslockButton.style.width = "62px"; capslockButton.style.height = "23px"; cell.appendChild(capslockButton); row.appendChild(cell); cell = factory.createTableCell(); var capslockButton = factory.createLetterNumberKey( "espacio", writeLetterAction, NOPAction, NOPAction, NOPAction, NOPAction); capslockButton.style.width = "95px"; capslockButton.style.height = "23px"; cell.appendChild(capslockButton); row.appendChild(cell); var cell = factory.createTableCell(); var backspaceButton = factory.createLetterNumberKey( /*String.fromCharCode(8592)*/"borrar", deleteLastTypedCharacterAction, NOPAction, NOPAction, NOPAction, NOPAction); backspaceButton.style.width = "62px"; backspaceButton.style.height = "23px"; cell.appendChild(backspaceButton); row.appendChild(cell); tblBody.appendChild(row); tbl.appendChild(tblBody); return tbl; } this.drawEntireKeyboard = function() { var tbl = factory.createTable(); var tblBody = factory.createTableBody(); var row = factory.createTableRow(); var cell = factory.createTableCell(); cell.appendChild(this.createKeyboardForLettersWithControls()); row.appendChild(cell); cell = factory.createTableCell(); cell.appendChild(this.createKeyboardForNumbers(this.numbers, 3, writeNumberAction)); row.appendChild(cell); tblBody.appendChild(row); tbl.appendChild(tblBody); return tbl; } this.drawWindowHeaderElements = function() { var tbl = factory.createTable(); tbl.style.emptyCells = "show"; var tblBody = factory.createTableBody(); var row = factory.createTableRow(); var cell = factory.createTableCell(); cell.style.width = "280px"; row.appendChild(cell); var cell = factory.createTableCell(); var inputButton = document.createElement("input"); inputButton.id = 'X'; inputButton.type = 'button'; inputButton.value = 'X'; inputButton.onclick = this.closeKeyboardCallback; inputButton.style.backgroundColor = "white"; inputButton.style.fontSize = "15"; inputButton.style.fontWeight = "bolder"; inputButton.style.color = "Black"; inputButton.style.width = "23px"; inputButton.style.height = "23px"; inputButton.style.margin= "auto"; inputButton.style.marginRight= "auto"; inputButton.style.marginLeft= "auto"; inputButton.style.marginBottom= "auto"; inputButton.style.marginTop= "auto"; inputButton.style.textTransform = "uppercase"; inputButton.style.fontFamily="tahoma"; cell.appendChild(inputButton); row.appendChild(cell); tblBody.appendChild(row); tbl.appendChild(tblBody); return tbl; } this.drawLettersAndNumbers = function() { var tbl = factory.createTable(); tbl.id = "keyboardTable"; var tblBody = factory.createTableBody(); var row = factory.createTableRow(); var cell = factory.createTableCell(); cell.appendChild(this.drawWindowHeaderElements()); row.appendChild(cell); tblBody.appendChild(row); row = factory.createTableRow(); cell = factory.createTableCell(); cell.appendChild(this.drawEntireKeyboard()); row.appendChild(cell); tblBody.appendChild(row); tbl.appendChild(tblBody); this.parent.appendChild(tbl); } this.createKeyboardForLettersWithControls = function() { var tbl = factory.createTable(); var tblBody = factory.createTableBody(); var row = factory.createTableRow(); cell = factory.createTableCell(); cell.appendChild(this.createKeyboardForLetters(this.letters, 9, writeLetterAction)); row.appendChild(cell); tblBody.appendChild(row); var row = factory.createTableRow(); var cell = factory.createTableCell(); cell.appendChild(this.createControlBoard()); row.appendChild(cell); tblBody.appendChild(row); tbl.appendChild(tblBody); return tbl; } } function DOMFactory() { this.createTable = function() { var table = document.createElement("table"); table.style.borderSpacing = "0px"; table.style.borderCollapse = "collapse"; table.style.backgroundColor="#F0F0F0"; return table; } this.createTableBody = function() { return document.createElement("tbody"); } this.createTableRow = function() { return document.createElement("tr"); } this.createTableCell = function() { return document.createElement("td"); } this.createLetterNumberKey = function(value, onclickCallback, onmouseoverCallback, onmouseoutCallback, onmousedownCallback, onmouseupCallback) { var cellButton = document.createElement("input"); cellButton.id = value + 'Id'; cellButton.type = 'button'; cellButton.value = value; cellButton.fixedValue = value; cellButton.onclick = onclickCallback; cellButton.onmouseover = onmouseoverCallback; cellButton.onmouseout = onmouseoutCallback; cellButton.onmousedown = onmousedownCallback; cellButton.onmouseup = onmouseupCallback; cellButton.style.backgroundColor = "#E8E8E8"; cellButton.style.fontSize = "10"; cellButton.style.fontWeight = "bolder"; cellButton.style.color = "Black"; cellButton.style.width = "23px"; cellButton.style.height = "23px"; cellButton.style.margin= "auto"; cellButton.style.marginRight= "auto"; cellButton.style.marginLeft= "auto"; cellButton.style.marginBottom= "auto"; cellButton.style.marginTop= "auto"; cellButton.style.textTransform = "uppercase"; cellButton.style.fontFamily="tahoma"; //cellButton.style.borderTop = "thin solid Gray"; //cellButton.style.borderRight = "thin solid Gray"; cellButton.style.borderLeft = "thin solid Gray"; cellButton.style.borderBottom = "medium solid Gray"; return cellButton; } }