Page refresh - Enter, arrow keys not working in table cells - html

This problem is happening both in IE and Chrome.
This is an HTML 5 with Angular JS 4 application. I am using TypeScript. My main aim in a table row I need to make enter, right, and left arrows of keyboard work and set focus on corresponding table cells
I have a page which has a dropdown. It has names of students. I also have a table with single row like below with multiple columns
When I launch by default a student is selected in dropdown which populates students details in table. I made each cell in row as editable. So when user clicks on any cell in the row and presses enter key I move to next cell and so the functions for right arrow and left arrow are working fine on first load/launch of my application.
But when I change another student from my dropdown, I refresh the page to show new selected student details. From this point if I select a cell in the row my enter key, left,
and right arrows are not working. Please see below for my code
Thank you very much in advance.
<table>
<tr>
<td *ngFor="let item of student.classes " >
<!-- I need to use div to make cell editable in IE. -->
<div name='TotalDistributableQtyCell' [id]="item.cellId"
[contentEditable]='!isDisabled' [textContent]="item.value"
(keydown)="onKeydown($event, item)"
style="line-height:1.8em;">{{item.value}}</div>
</td>
</tr>
<table>
MyComponent.ts
totalDistributableQtyCells: HTMLElement[];
ngAfterViewChecked() {
const distCells = document.getElementsByName('TotalDistributableQtyCell');
if(distCells !== null && distCells !== undefined && distCells.length > 0) {
if(this.totalDistributableQtyCells === null || this.totalDistributableQtyCells === undefined) {
this.totalDistributableQtyCells = [];
}
for ( let i = 0 ; i < distCells.length; i++) {
const index = this.totalDistributableQtyCells.findIndex( x => x.id !== undefined && x.id === distCells[i].id);
if(index === -1) {
this.totalDistributableQtyCells.push(distCells[i]);
}
}
}
}
private onKeydown(event: any, item: ITotalDistributableQuantity) {
const key_code = (window.event) ? event.keyCode : event.which;
if (this.deviceInfo.browser === 'ie') {
console.log('ie: event.keyCode ' + event.keyCode);
switch(event.keyCode.toString()) {
case '13': //enter
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
if(item.sequenceNumber === 2) {
this.totalDistributableQtyCells[1].focus();
} else if ( item.sequenceNumber === this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[0].focus();
} else if (item.sequenceNumber > 2 && item.sequenceNumber < this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[item.sequenceNumber - 1].focus();
}
break;
case '39': //right
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
if(item.sequenceNumber === 2) {
this.totalDistributableQtyCells[1].focus();
} else if ( item.sequenceNumber === this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[0].focus();
} else if (item.sequenceNumber > 2 && item.sequenceNumber < this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[item.sequenceNumber - 1].focus();
}
break;
case '37': //left
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
if(item.sequenceNumber === 2) {
this.totalDistributableQtyCells[this.totalDistributableQtyCells.length - 1 ].focus();
} else if ( item.sequenceNumber === this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[this.totalDistributableQtyCells.length - 2 ].focus();
} else if (item.sequenceNumber > 2 && item.sequenceNumber < 11) {
this.totalDistributableQtyCells[item.sequenceNumber - 3].focus();
}
break;
}
}else if ( this.deviceInfo.browser === 'chrome') {
console.log('chrome: event.key ' + event.key);
switch(event.key) {
case 'Enter':
event.preventDefault();
if(item.sequenceNumber === 2) {
this.totalDistributableQtyCells[1].focus();
} else if ( item.sequenceNumber === this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[0].focus();
} else if (item.sequenceNumber > 2 && item.sequenceNumber < this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[item.sequenceNumber - 1].focus();
}
break;
case 'ArrowRight':
event.preventDefault();
if(item.sequenceNumber === 2) {
this.totalDistributableQtyCells[1].focus();
} else if ( item.sequenceNumber === this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[0].focus();
} else if (item.sequenceNumber > 2 && item.sequenceNumber < this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[item.sequenceNumber - 1].focus();
}
break;
case 'ArrowLeft':
event.preventDefault();
if(item.sequenceNumber === 2) {
this.totalDistributableQtyCells[this.totalDistributableQtyCells.length - 1 ].focus();
} else if ( item.sequenceNumber === this.totalDistributableQtyCells.length + 1) {
this.totalDistributableQtyCells[this.totalDistributableQtyCells.length - 2 ].focus();
} else if (item.sequenceNumber > 2 && item.sequenceNumber < 11) {
this.totalDistributableQtyCells[item.sequenceNumber - 3].focus();
}
break;
}
}
}

Related

PrimeFaces Extension Sheet Restrict Decimal Places For Input

I want to restrict decimal places up to 1. User shouldnt type multiple dots(1..99) and (1.99). I want 1.9 2.6 for ex. Not 2.66 then convert into 2.7 etc. I have to use pe:sheetcolumn. I tried to add p:inputnumber and other p: components than pe extension. But pe:sheetcolumn have to be. With below approach it just let user to type multiple dots and multiple decimal places. It just convert to 1 decimal after user entered value on screen with #0.0. But i want to restrict on input to type multiple decimal places than 1 and multiple dots. I thought about javascript keyup event but i think it would be bad approach. How can i achive that
<pe:sheetcolumn headerText="SOME" value="#{SOME.some}" colWidth="200"
colType="numeric" numericPattern="#0.0" >
</pe:sheetcolumn>
For example for p:inputNumber as you can see on image user cant type multiple dots and they cant add decimal places more than 6.
Example
I want to do same thing with pe:sheetColumn. How can i do that
My JSF VERSION : 2.2.1 PRÄ°MEFACES AND PRIMEFACES EXTENSION VERSION : 6.2
If you install this MonkeyPatch you can then tweak the output to do whatever your want. I am pretty sure you can get the current cell with var currentValue = this.getDataAtCell(row , col) If you add this JS to your app you can then tweak how it handles keypresses and validation.
I added this line for you
var currentValue = this.getDataAtCell(row , col); // VALUE HERE!
So you can validate whatever your want with your code and if there is already a "." don't accept another "." etc.
if (PrimeFaces.widget.ExtSheet) {
PrimeFaces.widget.ExtSheet.prototype.handleHotBeforeKeyDown = function(e) {
var selectedLast = this.getSelectedLast();
if (!selectedLast) {
return;
}
var row = selectedLast[0];
var col = selectedLast[1];
var celltype = this.getCellMeta(row, col).type;
var currentValue = this.getDataAtCell(row , col); // VALUE HERE!
var evt = e || window.event; // IE support
var key = evt.charCode || evt.keyCode || 0;
var shiftDown = e.shiftKey;
// #740 tab on last cell should focus this next component
if (this.allowTabOffSheet && key == 9) {
var lastRow = this.countRows() - 1;
var lastCol = this.countCols() - 1;
if ((!shiftDown && row === lastRow && col === lastCol) ||
(shiftDown && row === 0 && col === 0)) {
e.stopImmediatePropagation();
this.unlisten();
this.deselectCell();
//add all elements we want to include in our selection
var focusableElements = 'a:not([disabled]), button:not([disabled]), input[type=text]:not([disabled]):not([hidden]):not([aria-hidden="true"]), [tabindex]:not([disabled]):not([tabindex="-1"]):not([aria-hidden="true"])';
if (document.activeElement && document.activeElement.form) {
var focusable = Array.prototype.filter.call(document.activeElement.form.querySelectorAll(focusableElements),
function(element) {
//check for visibility while always include the current activeElement
return element.offsetWidth > 0 || element.offsetHeight > 0 || element === document.activeElement
});
var index = focusable.indexOf(document.activeElement);
if (index > -1) {
var nextElement = focusable[index + 1] || focusable[0];
nextElement.focus();
}
}
}
return;
}
// prevent Alpha chars in numeric sheet cells
if (celltype === "numeric") {
// #766 do not block if just CTRL or SHIFT key
if (key === 16 || key === 17) {
return;
}
// #739 allow navigation
var ctrlDown = evt.ctrlKey || evt.metaKey; // Mac support
if (shiftDown || ctrlDown) {
// navigation keys
if (key == 9 || (key >= 35 && key <= 40)) {
return;
}
}
// check for cut and paste
var isClipboard = false;
// Check for Alt+Gr (http://en.wikipedia.org/wiki/AltGr_key)
if (ctrlDown && evt.altKey) isClipboard = false;
// Check for ctrl+c, v and x
else if (ctrlDown && key == 65) isClipboard = true; // a (select all)
else if (ctrlDown && key == 67) isClipboard = true; // c
else if (ctrlDown && key == 86) isClipboard = true; // v
else if (ctrlDown && key == 88) isClipboard = true; // x
// allow backspace, tab, delete, enter, arrows, numbers and keypad numbers
// ONLY home, end, F5, F12, minus (-), period (.)
// console.log('Key: ' + key + ' Shift: ' + e.shiftKey + ' Clipboard: ' + isClipboard);
var isNumeric = ((key == 8) || (key == 9) || (key == 13) ||
(key == 46) || (key == 110) || (key == 116) ||
(key == 123) || (key == 188) || (key == 189) ||
(key == 190) || ((key >= 35) && (key <= 40)) ||
((key >= 48) && (key <= 57)) || ((key >= 96) && (key <= 105)));
if ((!isNumeric && !isClipboard) || shiftDown) {
// prevent alpha characters
e.stopImmediatePropagation();
e.preventDefault();
}
}
}
}

Encountering an error while attempting to create a game

I'm trying to create a crossword puzzle game using Actionscript 3.0 in Adobe
Animate. For the game coding, I followed an online tutorial and now I'm encountering an error. The error occurs whenever I'm in the actual game scene, no errors are encountered on the home page or instruction page. can anyone help me? my coding skills are below basic.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at CROSSWORD_CODE_fla::MainTimeline/entFrame().
stop();
quitbtn.addEventListener(MouseEvent.CLICK, quitt);
function quitt(evt: MouseEvent): void {
gotoAndStop("start");
}
var hippopotamus: Number = 0;
var horse: Number = 0;
var eel: Number = 0;
var frog: Number = 0;
var ant: Number = 0;
var turtle: Number = 0;
var tiger: Number = 0;
var bird: Number = 0;
var penguin: Number = 0;
var orca: Number = 0;
var octopus: Number = 0;
var owl: Number = 0;
var snail: Number = 0;
var duck: Number = 0;
var kangaroo: Number = 0;
var iguana: Number = 0;
var ALLwords:Number = 0;
var Total:Number = 0;
{
stage.addEventListener(Event.ENTER_FRAME, entFrame);
function entFrame(evt:Event):void {
if ((hypoH.text == ("h").toString())
&& (hypo1.text == ("i").toString())
&& (hypoP.text == ("p").toString())
&& (hypoP2.text == ("p").toString())
&& (hypoO.text == ("o").toString())
&& (hypoP3.text == ("p").toString())
&& (hypoO2.text == ("o").toString())
&& (hypoT.text == ("t").toString())
&& (hypoA.text == ("a").toString())
&& (hypoM.text == ("m").toString())
&& (hypoU.text == ("u").toString())
&& (hypoS.text == ("s").toString()))
{
hippopotamus = 1;
trace("hippopotamus");
}
}
{
if ((hrsO.text == ("o").toString())
&& (hrsR.text == ("r").toString())
&& (hrsS.text == ("s").toString())
&& (hrsE.text == ("e").toString()))
{
horse = 1;
trace("horse");
}
}
{
if ((eelE.text == ("e").toString())
&& (eel1.text == ("l").toString()))
{
eel = 1;
trace("eel");
}
}
{
if ((penP.text == ("p").toString())
&& (penE.text == ("e").toString())
&& (penN.text == ("n").toString())
&& (penG.text == ("g").toString())
&& (penU.text == ("u").toString())
&& (pen1.text == ("i").toString())
&& (penN2.text == ("n").toString()))
{
penguin = 1;
trace("penguin");
}
}
{
if ((brdB.text == ("b").toString())
&& (brdR.text == ("r").toString())
&& (brdD.text == ("d").toString()))
{
bird = 1;
trace("bird");
}
}
{
if ((octO.text == ("o").toString())
&& (octC.text == ("c").toString())
&& (octT.text == ("t").toString())
&& (octO2.text == ("0").toString())
&& (octP.text == ("p").toString())
&& (octU.text == ("u").toString()))
{
octopus = 1;
trace("octopus");
}
}
{
if ((orcR.text == ("r").toString())
&& (orcC.text == ("c").toString())
&& (orcA.text == ("a").toString()))
{
orca = 1;
trace("orca");
}
}
{
if ((owlO.text == ("o").toString())
&& (owlW.text == ("w").toString()))
{
owl = 1;
trace("owl");
}
}
{
if ((nailS.text == ("s").toString())
&& (nailA.text == ("a").toString())
&& (nail1.text == ("i").toString())
&& (nailL.text == ("l").toString()))
{
snail = 1;
trace("snail");
}
}
{
if ((dkD.text == ("d").toString())
&& (dkU.text == ("u").toString())
&& (dkC.text == ("c").toString())
&& (dkK.text == ("k").toString()))
{
duck = 1;
trace("duck");
}
}
{
if ((groA.text == ("a").toString())
&& (groN.text == ("n").toString())
&& (groG.text == ("g").toString())
&& (groA2.text == ("a").toString())
&& (groR.text == ("r").toString())
&& (groO.text == ("o").toString()))
{
kangaroo = 1;
trace("kangaroo");
}
}
{
if ((frgF.text == ("f").toString())
&& (frgR.text == ("r").toString())
&& (frgG.text == ("g").toString()))
{
frog = 1;
trace("frog");
}
}
{
if ((ignG.text == ("g").toString())
&& (ignU.text == ("u").toString())
&& (ignA.text == ("a").toString())
&& (ignN.text == ("n").toString()))
{
iguana = 1;
trace("iguana");
}
}
{
if ((antA.text == ("a").toString())
&& (antN.text == ("n").toString()))
{
ant = 1;
trace("ant");
}
}
{
if ((trtT.text == ("t").toString())
&& (trtU.text == ("u").toString())
&& (trtR.text == ("r").toString())
&& (trtT2.text == ("t").toString())
&& (trtL.text == ("l").toString()))
{
turtle = 1;
trace("turtle");
}
}
{
if ((tgr1.text == ("i").toString())
&& (tgrG.text == ("g").toString())
&& (tgrE.text == ("e").toString()))
{
tiger = 1;
trace("tiger");
}
}
ALLwords = hippopotamus + horse + eel + frog + tiger + octopus + penguin + bird + orca +
turtle + duck + snail + ant + kangaroo + iguana + owl;
Total = 16 - ALLwords;
if (Total == 0)
{
trace("Gameover");
}
incorrectTXT.text = ("you have" + Total+ " incorrect words remaining").toString();
}

How to add pattern='\d*' for an input which contains spaces?

I have some inputs in the form which from the visual point of view accept numbers only, for example input for "salary" or "phone number". However, I'd like to add some sort of input mask and make formatting for those inputs.
For example for "salary" input, I'd like the value to look like this 9 999 if user entered 4 numbers or like this 99 999 if users entered 5 numbers.
But at the same time I want to have a pattern='\d'* for these inputs in order for the user to "open" keyboard with the numbers only when user touches the input from smartphone.
If I do this, it doesn't allow me to submit the form, even though I trim all spaces before submission, it still says "Please match the format requested".
The question: is there any workaround to achieve what I need?
Here is a codesandbox with the example, please open from a smartphone to see the "keyboard" I want to "show" to the user.
https://codesandbox.io/s/masked-input-with-different-currency-patterns-forked-v8f.
Using the pattern attribute to tell mobile browsers which keyboard layout you prefer might not work reliably. Thankfully, there's a dedicated attribute, inputmode, for this that should achieve exactly what you want.
$('.money > div').click(function() {
$('.money > input:eq('+$('.money > div').index(this)+')').focus();
});
$('.numberOnly').on('keydown', function(e) {
if (this.selectionStart || this.selectionStart == 0) {
// selectionStart won't work in IE < 9
var key = e.which;
var prevDefault = true;
var thouSep = " "; // your seperator for thousands
var deciSep = ","; // your seperator for decimals
var deciNumber = 2; // how many numbers after the comma
var thouReg = new RegExp(thouSep,"g");
var deciReg = new RegExp(deciSep,"g");
function spaceCaretPos(val, cPos) {
/// get the right caret position without the spaces
if (cPos > 0 && val.substring((cPos-1),cPos) == thouSep)
cPos = cPos-1;
if (val.substring(0,cPos).indexOf(thouSep) >= 0) {
cPos = cPos - val.substring(0,cPos).match(thouReg).length;
}
return cPos;
}
function spaceFormat(val, pos) {
/// add spaces for thousands
if (val.indexOf(deciSep) >= 0) {
var comPos = val.indexOf(deciSep);
var int = val.substring(0,comPos);
var dec = val.substring(comPos);
} else{
var int = val;
var dec = "";
}
var ret = [val, pos];
if (int.length > 3) {
var newInt = "";
var spaceIndex = int.length;
while (spaceIndex > 3) {
spaceIndex = spaceIndex - 3;
newInt = thouSep+int.substring(spaceIndex,spaceIndex+3)+newInt;
if (pos > spaceIndex) pos++;
}
ret = [int.substring(0,spaceIndex) + newInt + dec, pos];
}
return ret;
}
$(this).on('keyup', function(ev) {
if (ev.which == 8) {
// reformat the thousands after backspace keyup
var value = this.value;
var caretPos = this.selectionStart;
caretPos = spaceCaretPos(value, caretPos);
value = value.replace(thouReg, '');
var newValues = spaceFormat(value, caretPos);
this.value = newValues[0];
this.selectionStart = newValues[1];
this.selectionEnd = newValues[1];
}
});
if ((e.ctrlKey && (key == 65 || key == 67 || key == 86 || key == 88 || key == 89 || key == 90)) ||
(e.shiftKey && key == 9)) // You don't want to disable your shortcuts!
prevDefault = false;
if ((key < 37 || key > 40) && key != 8 && key != 9 && prevDefault) {
e.preventDefault();
if (!e.altKey && !e.shiftKey && !e.ctrlKey) {
var value = this.value;
if ((key > 95 && key < 106)||(key > 47 && key < 58) ||
(deciNumber > 0 && (key == 110 || key == 188 || key == 190))) {
var keys = { // reformat the keyCode
48: 0, 49: 1, 50: 2, 51: 3, 52: 4, 53: 5, 54: 6, 55: 7, 56: 8, 57: 9,
96: 0, 97: 1, 98: 2, 99: 3, 100: 4, 101: 5, 102: 6, 103: 7, 104: 8, 105: 9,
110: deciSep, 188: deciSep, 190: deciSep
};
var caretPos = this.selectionStart;
var caretEnd = this.selectionEnd;
if (caretPos != caretEnd) // remove selected text
value = value.substring(0,caretPos) + value.substring(caretEnd);
caretPos = spaceCaretPos(value, caretPos);
value = value.replace(thouReg, '');
var before = value.substring(0,caretPos);
var after = value.substring(caretPos);
var newPos = caretPos+1;
if (keys[key] == deciSep && value.indexOf(deciSep) >= 0) {
if (before.indexOf(deciSep) >= 0) newPos--;
before = before.replace(deciReg, '');
after = after.replace(deciReg, '');
}
var newValue = before + keys[key] + after;
if (newValue.substring(0,1) == deciSep) {
newValue = "0"+newValue;
newPos++;
}
while (newValue.length > 1 && newValue.substring(0,1) == "0" && newValue.substring(1,2) != deciSep) {
newValue = newValue.substring(1);
newPos--;
}
if (newValue.indexOf(deciSep) >= 0) {
var newLength = newValue.indexOf(deciSep)+deciNumber+1;
if (newValue.length > newLength) {
newValue = newValue.substring(0,newLength);
}
}
newValues = spaceFormat(newValue, newPos);
this.value = newValues[0];
this.selectionStart = newValues[1];
this.selectionEnd = newValues[1];
}
}
}
$(this).on('blur', function(e) {
if (deciNumber > 0) {
var value = this.value;
var noDec = "";
for (var i = 0; i < deciNumber; i++) noDec += "0";
if (value == "0" + deciSep + noDec) {
this.value = ""; //<-- put your default value here
} else if(value.length > 0) {
if (value.indexOf(deciSep) >= 0) {
var newLength = value.indexOf(deciSep) + deciNumber + 1;
if (value.length < newLength) {
while (value.length < newLength) value = value + "0";
this.value = value.substring(0,newLength);
}
}
else this.value = value + deciSep + noDec;
}
}
});
}
});
$('.price > input:eq(0)').focus();
body{
margin:50px;
}
.money{
display:inline-block;
border:1px solid #ababab;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
}
.money > div {
display:inline-block;
padding:8px 8px 8px 8px;
font-size:14px;
cursor:text;
color:#666;
/* pointer-events: none; */
}
.money > input{
width:200px;
border:0;
padding:8px 4px 8px 8px;
margin:0;
font-size:14px;
color:#666;
cursor:text;
text-align:left;
outline:none;
}
.money > input::-ms-clear{
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="money">
<input type="text" class="numberOnly" autocomplete="off" maxlength="4" /><div>$</div>
</div>
<br><br>
<div class="money">
<input type="text" class="numberOnly" autocomplete="off" maxlength="6" /><div>$</div>
</div>
<br><br>
<div class="money">
<input type="text" class="numberOnly" autocomplete="off" maxlength="50" /><div>$</div>
</div>

AS3 Cipher Decoder & Encoder (better answer?)

Last night, my little brother asked me if it were possible to make a program that would substitute every letter for another letter in the alphabet, to turn it into a code, and also turn it back to its normal state.
So I made this: http://www.skyetheguy.com/novatranslationtool
And by all accounts, it does the job, but I feel like, code-wise, it's clunky as all get-out, and I'd really love to improve my knowledge on things like loops...
EDIT: The above has since been updated with DodgerThud's amazing answer and works like a dream. : ) Dreams DO come true!
Also, this is very pie-in-the-sky, but it'd be amazing to get this to work with longer-than-two-characters things. My code definitely wouldn't do that at all. But, you know, all those fun codes with apostrophes in them... good stuff!
stop();
// SET-UP STUFF
inputText.alpha = .5;
outputText.alpha = .5;
inputText.borderColor = 0xCCCCCC;
outputText.borderColor = 0xCCCCCC;
var textinProgress:String;
textinProgress = inputText.text;
if (textinProgress.charAt(0) == "\r") {
inputText.text = "";
}
stage.addEventListener(Event.ENTER_FRAME, refresh_frame1);
function refresh_frame1(e:Event):void {
inputScroll.update();
outputScroll.update();
}
// ORIGINAL ALPHABET
var letters_aArray:Array = new Array();
letters_aArray[0] = "a";
letters_aArray[1] = "b";
letters_aArray[2] = "c";
letters_aArray[3] = "d";
letters_aArray[4] = "e";
letters_aArray[5] = "f";
letters_aArray[6] = "g";
letters_aArray[7] = "h";
letters_aArray[8] = "i";
letters_aArray[9] = "j";
letters_aArray[10] = "k";
letters_aArray[11] = "l";
letters_aArray[12] = "m";
letters_aArray[13] = "n";
letters_aArray[14] = "o";
letters_aArray[15] = "p";
letters_aArray[16] = "q";
letters_aArray[17] = "r";
letters_aArray[18] = "s";
letters_aArray[19] = "t";
letters_aArray[20] = "u";
letters_aArray[21] = "v";
letters_aArray[22] = "w";
letters_aArray[23] = "x";
letters_aArray[24] = "y";
letters_aArray[25] = "z";
// NEW ALPHABET
var letters_bArray:Array = new Array();
letters_bArray[0] = "m";
letters_bArray[1] = "n";
letters_bArray[2] = "b";
letters_bArray[3] = "v";
letters_bArray[4] = "c";
letters_bArray[5] = "x";
letters_bArray[6] = "z";
letters_bArray[7] = "l";
letters_bArray[8] = "k";
letters_bArray[9] = "j";
letters_bArray[10] = "h";
letters_bArray[11] = "g";
letters_bArray[12] = "f";
letters_bArray[13] = "d";
letters_bArray[14] = "s";
letters_bArray[15] = "a";
letters_bArray[16] = "p";
letters_bArray[17] = "o";
letters_bArray[18] = "i";
letters_bArray[19] = "u";
letters_bArray[20] = "y";
letters_bArray[21] = "t";
letters_bArray[22] = "r";
letters_bArray[23] = "e";
letters_bArray[24] = "w";
letters_bArray[25] = "q";
// ENCODE BUTTON
encode_btn.addEventListener(MouseEvent.CLICK, encode_btnclick);
function encode_btnclick(event:MouseEvent):void
{
textinProgress = inputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_aArray[0]) == 0) {
outputText.appendText(letters_bArray[0]);
} else if (textinProgress.charAt(b).search(letters_aArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[1]) == 0) {
outputText.appendText(letters_bArray[1]);
} else if (textinProgress.charAt(b).search(letters_aArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[2]) == 0) {
outputText.appendText(letters_bArray[2]);
} else if (textinProgress.charAt(b).search(letters_aArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[3]) == 0) {
outputText.appendText(letters_bArray[3]);
} else if (textinProgress.charAt(b).search(letters_aArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[4]) == 0) {
outputText.appendText(letters_bArray[4]);
} else if (textinProgress.charAt(b).search(letters_aArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[5]) == 0) {
outputText.appendText(letters_bArray[5]);
} else if (textinProgress.charAt(b).search(letters_aArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[6]) == 0) {
outputText.appendText(letters_bArray[6]);
} else if (textinProgress.charAt(b).search(letters_aArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[7]) == 0) {
outputText.appendText(letters_bArray[7]);
} else if (textinProgress.charAt(b).search(letters_aArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[8]) == 0) {
outputText.appendText(letters_bArray[8]);
} else if (textinProgress.charAt(b).search(letters_aArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[9]) == 0) {
outputText.appendText(letters_bArray[9]);
} else if (textinProgress.charAt(b).search(letters_aArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[10]) == 0) {
outputText.appendText(letters_bArray[10]);
} else if (textinProgress.charAt(b).search(letters_aArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[11]) == 0) {
outputText.appendText(letters_bArray[11]);
} else if (textinProgress.charAt(b).search(letters_aArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[12]) == 0) {
outputText.appendText(letters_bArray[12]);
} else if (textinProgress.charAt(b).search(letters_aArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[13]) == 0) {
outputText.appendText(letters_bArray[13]);
} else if (textinProgress.charAt(b).search(letters_aArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[14]) == 0) {
outputText.appendText(letters_bArray[14]);
} else if (textinProgress.charAt(b).search(letters_aArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[15]) == 0) {
outputText.appendText(letters_bArray[15]);
} else if (textinProgress.charAt(b).search(letters_aArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[16]) == 0) {
outputText.appendText(letters_bArray[16]);
} else if (textinProgress.charAt(b).search(letters_aArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[17]) == 0) {
outputText.appendText(letters_bArray[17]);
} else if (textinProgress.charAt(b).search(letters_aArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[18]) == 0) {
outputText.appendText(letters_bArray[18]);
} else if (textinProgress.charAt(b).search(letters_aArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[19]) == 0) {
outputText.appendText(letters_bArray[19]);
} else if (textinProgress.charAt(b).search(letters_aArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[20]) == 0) {
outputText.appendText(letters_bArray[20]);
} else if (textinProgress.charAt(b).search(letters_aArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[21]) == 0) {
outputText.appendText(letters_bArray[21]);
} else if (textinProgress.charAt(b).search(letters_aArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[22]) == 0) {
outputText.appendText(letters_bArray[22]);
} else if (textinProgress.charAt(b).search(letters_aArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[23]) == 0) {
outputText.appendText(letters_bArray[23]);
} else if (textinProgress.charAt(b).search(letters_aArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[24]) == 0) {
outputText.appendText(letters_bArray[24]);
} else if (textinProgress.charAt(b).search(letters_aArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_aArray[25]) == 0) {
outputText.appendText(letters_bArray[25]);
} else if (textinProgress.charAt(b).search(letters_aArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_bArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
// DECODE BUTTON
decode_btn.addEventListener(MouseEvent.CLICK, decode_btnclick);
function decode_btnclick(event:MouseEvent):void
{
textinProgress = inputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
outputText.appendText(letters_aArray[0]);
} else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
outputText.appendText(letters_aArray[1]);
} else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
outputText.appendText(letters_aArray[2]);
} else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
outputText.appendText(letters_aArray[3]);
} else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
outputText.appendText(letters_aArray[4]);
} else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
outputText.appendText(letters_aArray[5]);
} else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
outputText.appendText(letters_aArray[6]);
} else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
outputText.appendText(letters_aArray[7]);
} else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
outputText.appendText(letters_aArray[8]);
} else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
outputText.appendText(letters_aArray[9]);
} else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
outputText.appendText(letters_aArray[10]);
} else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
outputText.appendText(letters_aArray[11]);
} else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
outputText.appendText(letters_aArray[12]);
} else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
outputText.appendText(letters_aArray[13]);
} else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
outputText.appendText(letters_aArray[14]);
} else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
outputText.appendText(letters_aArray[15]);
} else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
outputText.appendText(letters_aArray[16]);
} else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
outputText.appendText(letters_aArray[17]);
} else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
outputText.appendText(letters_aArray[18]);
} else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
outputText.appendText(letters_aArray[19]);
} else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
outputText.appendText(letters_aArray[20]);
} else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
outputText.appendText(letters_aArray[21]);
} else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
outputText.appendText(letters_aArray[22]);
} else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
outputText.appendText(letters_aArray[23]);
} else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
outputText.appendText(letters_aArray[24]);
} else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
outputText.appendText(letters_aArray[25]);
} else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
// RECODE BUTTON (scrambles, takes what's in the output and applies the cipher again)
recode_btn.addEventListener(MouseEvent.CLICK, recode_btnclick);
function recode_btnclick(event:MouseEvent):void
{
textinProgress = outputText.text;
outputText.text = "";
for (var b=0; b<textinProgress.length; b++) {
if (textinProgress.charAt(b).search(letters_bArray[0]) == 0) {
outputText.appendText(letters_aArray[0]);
} else if (textinProgress.charAt(b).search(letters_bArray[0].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[0].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[1]) == 0) {
outputText.appendText(letters_aArray[1]);
} else if (textinProgress.charAt(b).search(letters_bArray[1].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[1].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[2]) == 0) {
outputText.appendText(letters_aArray[2]);
} else if (textinProgress.charAt(b).search(letters_bArray[2].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[2].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[3]) == 0) {
outputText.appendText(letters_aArray[3]);
} else if (textinProgress.charAt(b).search(letters_bArray[3].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[3].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[4]) == 0) {
outputText.appendText(letters_aArray[4]);
} else if (textinProgress.charAt(b).search(letters_bArray[4].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[4].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[5]) == 0) {
outputText.appendText(letters_aArray[5]);
} else if (textinProgress.charAt(b).search(letters_bArray[5].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[5].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[6]) == 0) {
outputText.appendText(letters_aArray[6]);
} else if (textinProgress.charAt(b).search(letters_bArray[6].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[6].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[7]) == 0) {
outputText.appendText(letters_aArray[7]);
} else if (textinProgress.charAt(b).search(letters_bArray[7].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[7].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[8]) == 0) {
outputText.appendText(letters_aArray[8]);
} else if (textinProgress.charAt(b).search(letters_bArray[8].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[8].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[9]) == 0) {
outputText.appendText(letters_aArray[9]);
} else if (textinProgress.charAt(b).search(letters_bArray[9].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[9].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[10]) == 0) {
outputText.appendText(letters_aArray[10]);
} else if (textinProgress.charAt(b).search(letters_bArray[10].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[10].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[11]) == 0) {
outputText.appendText(letters_aArray[11]);
} else if (textinProgress.charAt(b).search(letters_bArray[11].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[11].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[12]) == 0) {
outputText.appendText(letters_aArray[12]);
} else if (textinProgress.charAt(b).search(letters_bArray[12].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[12].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[13]) == 0) {
outputText.appendText(letters_aArray[13]);
} else if (textinProgress.charAt(b).search(letters_bArray[13].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[13].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[14]) == 0) {
outputText.appendText(letters_aArray[14]);
} else if (textinProgress.charAt(b).search(letters_bArray[14].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[14].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[15]) == 0) {
outputText.appendText(letters_aArray[15]);
} else if (textinProgress.charAt(b).search(letters_bArray[15].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[15].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[16]) == 0) {
outputText.appendText(letters_aArray[16]);
} else if (textinProgress.charAt(b).search(letters_bArray[16].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[16].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[17]) == 0) {
outputText.appendText(letters_aArray[17]);
} else if (textinProgress.charAt(b).search(letters_bArray[17].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[17].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[18]) == 0) {
outputText.appendText(letters_aArray[18]);
} else if (textinProgress.charAt(b).search(letters_bArray[18].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[18].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[19]) == 0) {
outputText.appendText(letters_aArray[19]);
} else if (textinProgress.charAt(b).search(letters_bArray[19].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[19].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[20]) == 0) {
outputText.appendText(letters_aArray[20]);
} else if (textinProgress.charAt(b).search(letters_bArray[20].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[20].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[21]) == 0) {
outputText.appendText(letters_aArray[21]);
} else if (textinProgress.charAt(b).search(letters_bArray[21].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[21].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[22]) == 0) {
outputText.appendText(letters_aArray[22]);
} else if (textinProgress.charAt(b).search(letters_bArray[22].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[22].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[23]) == 0) {
outputText.appendText(letters_aArray[23]);
} else if (textinProgress.charAt(b).search(letters_bArray[23].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[23].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[24]) == 0) {
outputText.appendText(letters_aArray[24]);
} else if (textinProgress.charAt(b).search(letters_bArray[24].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[24].toUpperCase());
} else if (textinProgress.charAt(b).search(letters_bArray[25]) == 0) {
outputText.appendText(letters_aArray[25]);
} else if (textinProgress.charAt(b).search(letters_bArray[25].toUpperCase()) == 0) {
outputText.appendText(letters_aArray[25].toUpperCase());
} else if (textinProgress.charAt(b) == "\r") {
outputText.appendText("\n");
} else {
outputText.appendText(textinProgress.charAt(b));
}
}
}
Just gonna tell you why I did all the incredibly arbitrary and loopy stuff that I did...
If you do any of the other normal replace things other people have talked about here (from what I've FOUND, at least), you end up replacing your own work at a certain point. For example if you want B to be A, but then you want A to be C, then if you type "BA", you get back "CC" instead of "AC". So my code actually takes each character and spits them all out one at a time.
I did all that tedious ".toUpperCase()" stuff to make sure that this tool was case-sensitive-- so that if I typed in a lowercase or uppercase key, it would always generate the proper one in its place. That's why RegExp didn't quite work for me.
It also takes into account things like line breaks and random symbols not included in the alphabet.
I know for SURE there's simpler, more energy-efficient ways to code this, but I just have no idea how.
I'm gonna expand on my comment and show you how can simplify your code and cut out a lot of it.
First off, you don't need to use Arrays. String objects in essence already are Arrays of byte values. So instead of creating big arrays, you can do this
var alphabet:String = "abcdefghijklmnopqrstuvwxyz";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewq";
In order to access a specific character, you can simply call the indexOf method on the string.
var indexOfJ = alphabet.indexOf("j");
trace(indexOfJ); //puts out 9
And you already now how to get the character of a string from a specific index.
var charAtIndex7:String = alphabet.charAt(7);
trace(charAtIndex7); //puts out "h"
Edit fom here:
Looks like I was only half-awake when I wrote that answer last night. The issue with the double for-loop is simply that it will always run the whole way through, even if it found an if clause that gets accepted. You already figured out on
your own that the last else-clause causes the input character to be printed 25 times. So, lets fix it.
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var anyHit = false;
for(var i=0;i<alphabet.length;i++){
if(textinProgress.charAt(b).search(alphabet.charAt(i)) == 0){
result += substituton.charAt(i);
anyHit = true;
break;
}
else if(textinProgress.charAt(b).search(alphabet.charAt(i).toUpperCase()) == 0){
result += substituton.charAt(i).toUpperCase();
anyHit = true;
break;
}
else if(textinProgress.charAt(b) == "\r"){
result += "\n";
anyHit = true;
break;
}
}
if(anyHit == false){
result += textinProgress.charAt(b);
}
}
output.text = result;
}
I had a coworker test this code for me earlier today, and it should work as intended.
But that's not all we can do to simplify your code. We can cut out the second if clause by defining our alphabet and substitution variables with both lowercase and uppercase letters.
var alphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "mnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
You should also be able to put in special symbols into your alphabet and substitution string, like \r for example.
var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
I'm not entirely sure if this will work, but it should.
Now you have already considerably shortened your code, but we can even shorten it further. Is the inner for loop really neccessary? Because our alphabet and our substitution are the same length, maybe we can simplify this even more.
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var index:int = alphabet.indexOf(textinProgress.charAt(b));
if(index >= 0){
result += substitution.charAt(index);
}else{
result += textinProgress.charAt(b);
}
}
output.text = result;
}
Putting it all together.
var alphabet:String = "\rabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var substitution:String = "\nmnbvcxzlkjhgfdsapoiuytrewqMNBVCXZLKJHGFDSAPOIUYTREWQ";
function cipher(input:Textfield, output:Textfield, alphabet:String, substitution:String){
textinProgress = input.text;
var result:String = "";
for(var b=0;b<textinProgress.length;b++)
{
var index:int = alphabet.indexOf(textinProgress.charAt(b));
if(index >= 0){
result += substitution.charAt(index);
}else{
result += textinProgress.charAt(b);
}
}
output.text = result;
}
function encode_btnclick(event:MouseEvent):void{
cipher(inputText, outputText, alphabet, substitution);
}
function decode_btnclick(event:MouseEvent):void{
cipher(inputText, outputText, substitution, alphabet);
}
function recode_btnclick(event:MouseEvent):void{
cipher(outputText, outputText, alphabet, substitution);
}
This should be able to handle
Text of any length
Line breaks
Upper- and Lowercase letters
Undefined characters
You should search for Substitution ciphers, e.g., one of the simplest ones is the Caesar cipher.

JQuery SVG making objects droppable

I am trying to build a seating reservation web app using SVG. Imagine, I've created rectangles in the svg, which represents an empty seat. I want to allow user to drop an html "image" element on the "rect" to reserve the seat.
However, I couldn't get the droppable to work on the SVG elemnets. Any one has any idea why this is so? Here is the code:
$('#target').svg();
var svg = $("#target").svg('get');
svg.rect(20, 10, 100, 50, 10, 10, { fill: '#666', class: "droppable" });
$('rect')
.droppable({
accept: '.product',
tolerance: 'touch',
drop: function (event, ui) {
alert('df');
}
}
I looked in to the jQuery-ui source and figured out why it wasn't working with SVGs. jQuery thinks they have a width and height of 0px, so the intersection test fails.
I wrapped $.ui.intersect and set the correct size information before passing the arguments through to the original function. There may be more proportion objects that need fixing but the two I did here are enough to fix my :
$.ui.intersect_o = $.ui.intersect;
$.ui.intersect = function(draggable, droppable, toleranceMode) {
//Fix helper
if (draggable.helperProportions && draggable.helperProportions.width === 0 && draggable.helperProportions.height === 0) {
draggable.helperProportionsBBox = draggable.helperProportionsBBox || $(draggable.element).get(0).getBBox();
draggable.helperProportions = draggable.helperProportionsBBox;
}
//Fix droppable
if (droppable.proportions && droppable.proportions.width === 0 && droppable.proportions.height === 0) {
droppable.proportionsBBox = droppable.proportionsBBox || $(droppable.element).get(0).getBBox();
droppable.proportions = droppable.proportionsBBox;
}
return $.ui.intersect_o(draggable, droppable, toleranceMode);
};
With jQuery UI 1.11.4 I had to change Eddie's solution to the following, as draggable.proportions is now a function:
$.ui.intersect_o = $.ui.intersect;
$.ui.intersect = function (draggable, droppable, toleranceMode, event) {
//Fix helper
if (draggable.helperProportions && draggable.helperProportions.width === 0 && draggable.helperProportions.height === 0) {
draggable.helperProportionsBBox = draggable.helperProportionsBBox || $(draggable.element).get(0).getBBox();
draggable.helperProportions = draggable.helperProportionsBBox;
}
if (droppable.proportions && !droppable.proportions().width && !droppable.proportions().height)
if (typeof $(droppable.element).get(0).getBBox === "function") {
droppable.proportionsBBox = droppable.proportionsBBox || $(droppable.element).get(0).getBBox();
droppable.proportions = function () {
return droppable.proportionsBBox;
};
}
return $.ui.intersect_o(draggable, droppable, toleranceMode, event);
};
If you want to restrict the drop on svg elements to hit on visible content only (for example in polygons) you might want to use this addition to Peter Baumann's suggestion:
$.ui.intersect_o = $.ui.intersect;
$.ui.intersect = function (draggable, droppable, toleranceMode, event) {
//Fix helper
if (draggable.helperProportions && draggable.helperProportions.width === 0 && draggable.helperProportions.height === 0) {
draggable.helperProportionsBBox = draggable.helperProportionsBBox || $(draggable.element).get(0).getBBox();
draggable.helperProportions = draggable.helperProportionsBBox;
}
if (droppable.proportions && !droppable.proportions().width && !droppable.proportions().height)
if (typeof $(droppable.element).get(0).getBBox === "function") {
droppable.proportionsBBox = droppable.proportionsBBox || $(droppable.element).get(0).getBBox();
droppable.proportions = function () {
return droppable.proportionsBBox;
};
}
var intersect = $.ui.intersect_o(draggable, droppable, toleranceMode, event);
if (intersect) {
var dropTarget = $(droppable.element).get(0);
if (dropTarget.ownerSVGElement && (typeof (dropTarget.isPointInFill) === 'function') && (typeof (dropTarget.isPointInStroke) === 'function')) {
var x = ( draggable.positionAbs || draggable.position.absolute ).left + draggable.margins.left + draggable.helperProportions.width / 2,
y = ( draggable.positionAbs || draggable.position.absolute ).top + draggable.margins.top + draggable.helperProportions.height / 2,
p = dropTarget.ownerSVGElement.createSVGPoint();
p.x = x;
p.y = y;
p = p.matrixTransform(dropTarget.getScreenCTM().inverse());
if(!(dropTarget.isPointInFill(p) || dropTarget.isPointInStroke(p)))
intersect = false;
}
}
return intersect;
};
In case if anyone has the same question in mind, droppable doesn't work in jquery SVG. So in the end, I did the following to create my own droppable event:
In draggable, set the target currently dragged been dragged to draggedObj ,
In the mouse up event, check if the draggedObj is null, if it's not null, then drop the item according to the current position. ( let me know if you need help on detecting the current position)