Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean indentation mainjs #36

Merged
merged 3 commits into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
267 changes: 119 additions & 148 deletions JS/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// Global variable to keep track of CapsLock
// Global variable to keep track of CapsLock
var caps = false;

var allData ;

var reqKeys = []

var typewriter;

$(document).ready(function() {
//$("#retryButton").toggleClass("on");
//alert($('li[data-keycode="test"]').attr('id'));
$.getJSON( "JS/shortcuts.json", function( data ) {
allData = data;
if(sessionStorage.getItem("questionNo")==null){
sessionStorage.setItem("questionNo", "1");
sessionStorage.setItem("totalCount", Object.keys(allData).length);
}
// Call readText()
readText()
})
});

function nextQuestion(){
if(sessionStorage.getItem("questionNo")!=null){
if(parseInt(sessionStorage.getItem("questionNo"))<parseInt(sessionStorage.getItem("totalCount"))){
Expand All @@ -16,7 +27,6 @@ function nextQuestion(){
}
// Un-Highlight the prompt keys.
$.each( reqKeys, function( index, key ){

if($($('li[data-keycode="'+key+'"]')[0]).hasClass('prompt')){
promptKey2(key)
}
Expand All @@ -27,10 +37,8 @@ function nextQuestion(){
if(key.toLowerCase()=='alt')
$("#optionleft").toggleClass("prompt"); */
});

// Reset the reqKeys
reqKeys = [];

readText();
}

Expand All @@ -40,71 +48,48 @@ function retry(){
readText();
}

$(document).ready(function() {
//$("#retryButton").toggleClass("on");

//alert($('li[data-keycode="test"]').attr('id'));


$.getJSON( "JS/shortcuts.json", function( data ) {
allData = data;
if(sessionStorage.getItem("questionNo")==null){
sessionStorage.setItem("questionNo", "1");
sessionStorage.setItem("totalCount", Object.keys(allData).length);
}
// Call readText()
readText()
})

});

// Function called on KeyDown to show Pressed key by adding class = 'pressed'
function handle(e) {
// Function called on KeyDown to show Pressed key by adding class = 'pressed'
function handle(e) {
var text1 = e.type +
' key=' + e.key +
' code=' + e.code

if(e.code.toLowerCase()=="space"){
$("#space").toggleClass("pressed");
}

if((e.which>=186 && e.which<=192)|| (e.which>=219 && e.which<=222)){
$("#"+e.code.toLowerCase()).toggleClass("pressed");
}

if(e.key.toLowerCase()=="alt" || e.key.toLowerCase()=="shift" || e.key.toLowerCase()=="meta"){
$("#"+e.code.toLowerCase()).toggleClass("pressed");
}

if(e.key.toLowerCase()=="capslock" && caps==false){
caps= true;
$("#"+e.key.toLowerCase()).toggleClass("pressed");
$('.letter').toggleClass('uppercase');
}
else if(e.key.toLowerCase()=="capslock" && caps==true) {
$("#"+e.key.toLowerCase()).toggleClass("pressed");
$('.letter').toggleClass('uppercase');
caps=false;
}
else $("#"+e.key.toLowerCase() ).addClass("pressed");
' code=' + e.code

if(e.code.toLowerCase()=="space"){
$("#space").toggleClass("pressed");
}
if((e.which>=186 && e.which<=192)|| (e.which>=219 && e.which<=222)){
$("#"+e.code.toLowerCase()).toggleClass("pressed");
}
if(e.key.toLowerCase()=="alt" || e.key.toLowerCase()=="shift" || e.key.toLowerCase()=="meta"){
$("#"+e.code.toLowerCase()).toggleClass("pressed");
}
if(e.key.toLowerCase()=="capslock" && caps==false){
caps= true;
$("#"+e.key.toLowerCase()).toggleClass("pressed");
$('.letter').toggleClass('uppercase');
}
else if(e.key.toLowerCase()=="capslock" && caps==true) {
$("#"+e.key.toLowerCase()).toggleClass("pressed");
$('.letter').toggleClass('uppercase');
caps=false;
}
else $("#"+e.key.toLowerCase() ).addClass("pressed");
}

// Function called on KeyUp to reset the key by removing class = 'pressed'
function release(e) {

if((e.which>=186 && e.which<=192)|| (e.which>=219 && e.which<=222)){
$("#"+e.code.toLowerCase()).removeClass("pressed");
}

if(e.key.toLowerCase()=="alt" || e.key.toLowerCase()=="shift" || e.key.toLowerCase()=="meta"){
$("#"+e.code.toLowerCase()).removeClass("pressed");
}
if(e.code.toLowerCase()=="space"){
$("#space").removeClass("pressed");
}
if(e.key.toLowerCase()=="capslock")return
else{
$("#"+e.key.toLowerCase() ).removeClass("pressed");
if((e.which>=186 && e.which<=192)|| (e.which>=219 && e.which<=222)){
$("#"+e.code.toLowerCase()).removeClass("pressed");
}
if(e.key.toLowerCase()=="alt" || e.key.toLowerCase()=="shift" || e.key.toLowerCase()=="meta"){
$("#"+e.code.toLowerCase()).removeClass("pressed");
}
if(e.code.toLowerCase()=="space"){
$("#space").removeClass("pressed");
}
if(e.key.toLowerCase()=="capslock") return
else{
$("#"+e.key.toLowerCase() ).removeClass("pressed");
}
}

Expand All @@ -118,17 +103,15 @@ function highlightNextKey(params){

function promptKey2(key){
//if($('li[data-keycode="'+key+'"]'[0]).hasClass('prompt')){

$($('li[data-keycode="'+key+'"]')[0]).toggleClass("prompt")
//}
}

// Function to highlight any key passed as input
function promptKey(key){
// Handling all key types
if(key.length==1)
$("#"+key.toLowerCase()).toggleClass("prompt");
else{
// Handling all key types
if(key.length==1) $("#"+key.toLowerCase()).toggleClass("prompt");
else {
if(key.toLowerCase()=='ctrl'||key.toLowerCase()=='control')
$("#control").toggleClass("prompt");
if(key.toLowerCase()=='command' || key.toLowerCase()=='cmd'|| key.toLowerCase()=="meta")
Expand All @@ -154,58 +137,50 @@ function promptKey(key){
}
}



// Function to read the next combination of keys and highlight it on keyboard
function readText(){

quesNo = sessionStorage.getItem("questionNo")
if(quesNo!=null){
commandText = allData[parseInt(quesNo)-1].answer
answerkeys = allData[parseInt(quesNo)-1].keys

//commandText = "A+Control" //$("#textdiv").text(); // Will be taken from some other list type of a source.
//Each command will have an associated question text used in writeQuestion
var speed = 50

var i = 0;

// Call writeQuestion to add question on the top textarea
writeQuestion(allData[parseInt(sessionStorage.getItem("questionNo"))-1].question)

$.each(answerkeys , function(index, val) {
reqKeys.push(val)
// Highlight the prompt keys
promptKey2(val)

});

/* commandText.split('+').forEach(function(c) {
if(c.toLowerCase()=="command"){
reqKeys.push("meta")
}else if(c.toLowerCase()=="option"){
reqKeys.push("alt")
}
else{
reqKeys.push(c)
}
// Highlight the prompt keys
promptKey(c)

}); */

// When the reqKeys combination is pressed, onSuccess function is called
runOnKeys(
{
onSuccess: () => onSuccess(...reqKeys),
onIncorrect: () => onIncorrect()
},
quesNo,
...reqKeys
);

//key(commandText, function(){ onSuccess(...reqKeys)});

//commandText = "A+Control" //$("#textdiv").text(); // Will be taken from some other list type of a source.
//Each command will have an associated question text used in writeQuestion
var speed = 50
var i = 0;

// Call writeQuestion to add question on the top textarea
writeQuestion(allData[parseInt(sessionStorage.getItem("questionNo"))-1].question)

$.each(answerkeys , function(index, val) {
reqKeys.push(val)
// Highlight the prompt keys
promptKey2(val)
});

/* commandText.split('+').forEach(function(c) {
if(c.toLowerCase()=="command"){
reqKeys.push("meta")
}else if(c.toLowerCase()=="option"){
reqKeys.push("alt")
}
else{
reqKeys.push(c)
}
// Highlight the prompt keys
promptKey(c)

}); */

// When the reqKeys combination is pressed, onSuccess function is called
runOnKeys(
{
onSuccess: () => onSuccess(...reqKeys),
onIncorrect: () => onIncorrect()
},
quesNo,
...reqKeys
);
//key(commandText, function(){ onSuccess(...reqKeys)});
} // END IF for sessionStorage check
}

Expand All @@ -221,8 +196,7 @@ function writeQuestion(question) {
typingSpeed: 2
});

typewriter.typeString(question)
.start();
typewriter.typeString(question).start();
}

function clearIncorrectIndication() {
Expand All @@ -245,47 +219,44 @@ function onSuccess(...keys){
setTimeout(nextQuestion,1000);
}

// Function to keep track when correct keys are pressed with a call back Success function as onSuccess()
// Function to keep track when correct keys are pressed with a call back Success function as onSuccess()
function runOnKeys(callbacks, quesNo, ...keySet) {
let pressed = new Set();
document.addEventListener('keydown', function(event) {
event.preventDefault();
clearIncorrectIndication();
let pressed = new Set();

document.addEventListener('keydown', function(event) {
event.preventDefault();
clearIncorrectIndication();
if(sessionStorage.getItem("questionNo")!=null){
if(quesNo!=sessionStorage.getItem("questionNo")){
return;
}
}
pressed.add(event.keyCode);

pressed.add(event.keyCode);
handle(event);
for (let key of keySet) { // are all required keys pressed?
if (!pressed.has(key)) {
if (pressed.size > 1) {
callbacks.onIncorrect();
}
return;
}
for (let key of keySet) { // are all required keys pressed?
if (!pressed.has(key)) {
if (pressed.size > 1) {
callbacks.onIncorrect();
}
return;
}
}

// All the required keys are pressed
pressed.clear();

callbacks.onSuccess();
});
// All the required keys are pressed
pressed.clear();
callbacks.onSuccess();
});

document.addEventListener('keyup', function(event) {
event.preventDefault();
if(sessionStorage.getItem("questionNo")!=null){
if(quesNo!=sessionStorage.getItem("questionNo")){
document.addEventListener('keyup', function(event) {
event.preventDefault();
if(sessionStorage.getItem("questionNo")!=null){
if(quesNo!=sessionStorage.getItem("questionNo")) {
return;
}
}
pressed.delete(event.keyCode);
pressed.delete(event.keyCode);
release(event);
});

}
});

//})
}