2015-04-25 23:02:05 -04:00
|
|
|
/**
|
|
|
|
* Our main Javascript file.
|
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
|
2015-04-27 20:48:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if we have a function that returns cryptographically random
|
|
|
|
* values. False otherwise.
|
|
|
|
*/
|
|
|
|
function i_can_has_good_crypto() {
|
|
|
|
|
|
|
|
if (window.crypto && window.crypto.getRandomValues) {
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
} // End of i_can_has_good_crypto()
|
|
|
|
|
|
|
|
|
2015-04-25 23:02:05 -04:00
|
|
|
/**
|
|
|
|
* Roll a die.
|
|
|
|
*
|
|
|
|
* @return integer A random number between 1 and 6, inclusive.
|
|
|
|
*/
|
|
|
|
function die_roll() {
|
2015-04-27 20:48:57 -04:00
|
|
|
|
|
|
|
var retval;
|
|
|
|
|
|
|
|
if (i_can_has_good_crypto()) {
|
|
|
|
var a = new Uint32Array(1);
|
|
|
|
window.crypto.getRandomValues(a);
|
|
|
|
retval = (a[0] % 6) + 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Fall back to something way less secure. The user has already
|
|
|
|
// been warned.
|
|
|
|
//
|
|
|
|
retval = Math.floor(Math.random() * 6) + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
|
|
|
|
} // End of die_roll()
|
2015-04-25 23:02:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-26 02:08:58 -04:00
|
|
|
* Roll a die 5 times.
|
2015-04-25 23:02:05 -04:00
|
|
|
*
|
2015-04-26 02:08:58 -04:00
|
|
|
* @return array an Array of 5 dice rolls
|
2015-04-25 23:02:05 -04:00
|
|
|
*/
|
|
|
|
function roll_dice() {
|
2015-04-26 02:08:58 -04:00
|
|
|
var retval = new Array();
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
2015-04-25 23:02:05 -04:00
|
|
|
return(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up a word from our wordlist.
|
|
|
|
*
|
|
|
|
* @param object wordlist Our hash table of dice rolls and their corresponding words.
|
|
|
|
* @param integer index
|
|
|
|
*
|
|
|
|
* @return string The word from the dicelist
|
|
|
|
*/
|
|
|
|
function get_word(wordlist, index) {
|
|
|
|
var retval = wordlist[index];
|
|
|
|
return(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-26 00:01:29 -04:00
|
|
|
//
|
|
|
|
// Handler to mark the clicked number of dice button as active.
|
|
|
|
//
|
|
|
|
jQuery(".dice_button").on("click", function(e) {
|
|
|
|
jQuery(".dice_button").removeClass("active");
|
|
|
|
jQuery(e.target).addClass("active");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-04-26 15:32:56 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function displays each dice roll.
|
|
|
|
*
|
2015-04-26 16:02:56 -04:00
|
|
|
* @param array rows Array of rows of dice rolls that we had.
|
|
|
|
* @param object cb Our callback to fire when done
|
2015-04-26 15:32:56 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function display_row(rows, cb) {
|
|
|
|
|
2015-04-27 21:34:14 -04:00
|
|
|
var fadein_duration = 250;
|
|
|
|
var fadeout_delay = 750;
|
2015-04-26 16:02:56 -04:00
|
|
|
|
2015-04-26 15:32:56 -04:00
|
|
|
if (rows.length) {
|
|
|
|
//
|
2015-04-27 21:34:14 -04:00
|
|
|
// Grab a row, and hide each of the dice and the word in it.
|
2015-04-26 15:32:56 -04:00
|
|
|
//
|
|
|
|
var row = rows.shift();
|
2015-04-27 21:34:14 -04:00
|
|
|
var html = row.hide().appendTo(".results");
|
|
|
|
html.find(".dice_element").each(function(i, value) {
|
|
|
|
jQuery(value).hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// Now show the row, and loop through each element, fading in
|
|
|
|
// the dice and the word in sequence.
|
|
|
|
//
|
|
|
|
html.show(fadein_duration, function() {
|
|
|
|
|
|
|
|
jQuery(this).find(".dice_element").each(function(i, value) {
|
|
|
|
var delay = i * 100;
|
|
|
|
setTimeout(function() {
|
|
|
|
jQuery(value).show();
|
|
|
|
}, delay);
|
|
|
|
|
|
|
|
});
|
2015-04-27 20:57:03 -04:00
|
|
|
|
2015-04-27 21:34:14 -04:00
|
|
|
//
|
|
|
|
// Now fade out the entire row, and call ourselves again
|
|
|
|
// so we can repeat with the next row.
|
|
|
|
//
|
|
|
|
jQuery(this).delay(fadeout_delay)
|
|
|
|
.fadeOut(fadeout_delay, function() {
|
|
|
|
display_row(rows, cb);
|
2015-04-27 20:57:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2015-04-26 15:32:56 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// All done with displaying rows, fire our callback and get outta here.
|
|
|
|
//
|
|
|
|
cb();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of display_row()
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the actual results.
|
2015-04-26 17:53:43 -04:00
|
|
|
*
|
|
|
|
* @param cb object Optional callback to fire when done
|
2015-04-26 15:32:56 -04:00
|
|
|
*/
|
2015-04-26 17:53:43 -04:00
|
|
|
function display_results(cb) {
|
2015-04-26 15:32:56 -04:00
|
|
|
|
|
|
|
jQuery(".results_words_key").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results_words_value").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results").append("<br clear=\"all\" />");
|
|
|
|
jQuery(".results_phrase_key").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results_phrase_value").hide().clone().appendTo(".results");
|
|
|
|
|
2015-04-26 17:53:43 -04:00
|
|
|
jQuery(".results .results_words_key").fadeIn(500, function() {
|
|
|
|
jQuery(".results .results_words_value").fadeIn(500, function() {
|
|
|
|
jQuery(".results .results_phrase_key").fadeIn(500, function() {
|
|
|
|
jQuery(".results .results_phrase_value").fadeIn(500, function() {
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
2015-04-26 15:32:56 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} // End of display_results()
|
|
|
|
|
|
|
|
|
2015-04-26 17:53:43 -04:00
|
|
|
/**
|
|
|
|
* Return the width of the browser window.
|
|
|
|
*/
|
|
|
|
function get_width() {
|
|
|
|
return(jQuery(window).width());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-26 17:20:56 -04:00
|
|
|
/**
|
|
|
|
* Return true if we are running on a mobile screen.
|
|
|
|
*/
|
|
|
|
function is_mobile() {
|
|
|
|
|
2015-04-26 17:53:43 -04:00
|
|
|
if (get_width() <= 480) {
|
2015-04-26 17:20:56 -04:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
} // End of is_mobile()
|
|
|
|
|
|
|
|
|
2015-04-26 00:01:29 -04:00
|
|
|
//
|
|
|
|
// Handler when the "Roll Dice" button is clicked. It gets the
|
|
|
|
// passphrase and updates the HTML with it.
|
|
|
|
//
|
|
|
|
jQuery("#roll_dice").on("click", function(e) {
|
|
|
|
|
2015-04-27 20:57:03 -04:00
|
|
|
var target_height = 200;
|
2015-04-26 17:53:43 -04:00
|
|
|
if (is_mobile()) {
|
2015-04-27 21:34:14 -04:00
|
|
|
target_height = 400;
|
2015-04-26 17:53:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
jQuery(".results").animate({height: target_height}, 400);
|
|
|
|
|
2015-04-26 17:20:56 -04:00
|
|
|
//
|
|
|
|
// If we're running on an iPhone or similar, scroll down so that we can
|
|
|
|
// see the dice rolls and passphrase.
|
|
|
|
//
|
|
|
|
if (is_mobile()) {
|
2015-04-26 17:53:43 -04:00
|
|
|
var aTag = $("a[name='roll_dice_button']");
|
|
|
|
$("html,body").animate({scrollTop: aTag.offset().top}, "slow");
|
2015-04-26 17:20:56 -04:00
|
|
|
}
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
|
|
|
// Remove any old results
|
|
|
|
//
|
|
|
|
jQuery(".results").empty();
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
|
|
|
// Make our dice rolls
|
|
|
|
//
|
2015-04-26 00:01:29 -04:00
|
|
|
var num_dice = jQuery(".dice_button.active").html();
|
|
|
|
var passphrase = new Array();
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
var rolls = new Array();
|
2015-04-26 00:01:29 -04:00
|
|
|
for (var i=0; i<num_dice; i++) {
|
2015-04-26 16:29:43 -04:00
|
|
|
|
|
|
|
var roll = {};
|
|
|
|
roll.dice = roll_dice();
|
|
|
|
roll.word = get_word(wordlist, roll.dice.join(""));
|
2015-04-26 02:08:58 -04:00
|
|
|
rolls.push(roll);
|
2015-04-26 16:29:43 -04:00
|
|
|
passphrase.push(roll.word);
|
|
|
|
|
2015-04-26 00:01:29 -04:00
|
|
|
}
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
2015-04-26 16:29:43 -04:00
|
|
|
// Populate our results by cloning the hidden base rows which
|
|
|
|
// represent each die.
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
2015-04-26 00:01:29 -04:00
|
|
|
jQuery(".results_words_value").html(passphrase.join(" "));
|
|
|
|
jQuery(".results_phrase_value").html(passphrase.join(""));
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
var rows = new Array();
|
|
|
|
for (key in rolls) {
|
|
|
|
|
|
|
|
var roll = rolls[key];
|
|
|
|
var row = jQuery("<div></div>");
|
|
|
|
|
2015-04-26 16:29:43 -04:00
|
|
|
//
|
|
|
|
// Clone and append specific dice to this row.
|
|
|
|
//
|
|
|
|
for (key2 in roll.dice) {
|
|
|
|
var die = roll.dice[key2];
|
2015-04-26 02:08:58 -04:00
|
|
|
var classname = ".source .dice" + die;
|
2015-04-26 16:29:43 -04:00
|
|
|
var tmp = jQuery(classname).clone().appendTo(row);
|
2015-04-26 02:08:58 -04:00
|
|
|
}
|
|
|
|
|
2015-04-26 16:29:43 -04:00
|
|
|
//
|
|
|
|
// Now append the word
|
|
|
|
//
|
|
|
|
var dice_word = jQuery(".dice_word").clone();
|
|
|
|
dice_word.html("\"" + roll.word + "\"");
|
|
|
|
row.append(dice_word);
|
2015-04-26 17:14:21 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// And clear to the next line
|
|
|
|
//
|
|
|
|
row.append("<br clear=\"all\" />");
|
2015-04-26 16:29:43 -04:00
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
rows.push(row);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-26 16:29:43 -04:00
|
|
|
//
|
|
|
|
// Now display those rows.
|
|
|
|
//
|
2015-04-26 17:53:43 -04:00
|
|
|
display_row(rows, function() {
|
2015-04-27 21:34:14 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// And then display the results
|
|
|
|
//
|
2015-04-26 17:53:43 -04:00
|
|
|
display_results(function() {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set the height of this back to auto so we don't have unused space.
|
|
|
|
// I'm amazed that we don't see a "flash" of the results div
|
|
|
|
// temporarily shrinking, but this seems to work as per what I saw
|
|
|
|
// at http://stackoverflow.com/questions/5003220/javascript-jquery-animate-to-auto-height
|
|
|
|
//
|
|
|
|
// Well then.
|
|
|
|
//
|
|
|
|
var height = jQuery(".results").height();
|
|
|
|
jQuery(".results").css("height", "auto");
|
|
|
|
var new_height = jQuery(".results").height();
|
|
|
|
jQuery(".results").height(height);
|
|
|
|
jQuery(".results").animate({height: new_height}, 400);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
2015-04-26 00:01:29 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-04-27 20:48:57 -04:00
|
|
|
|
2015-04-26 18:58:55 -04:00
|
|
|
//
|
|
|
|
// If we're not on a mobile, bring in the GitHub ribbon.
|
|
|
|
//
|
|
|
|
if (!is_mobile()) {
|
|
|
|
jQuery("#github_ribbon").fadeIn(1000);
|
|
|
|
}
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2015-04-27 21:34:14 -04:00
|
|
|
|
2015-04-27 20:48:57 -04:00
|
|
|
if (!i_can_has_good_crypto()) {
|
|
|
|
jQuery(".source .bad_crypto").clone().hide().fadeIn(800).appendTo(".message");
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:34:14 -04:00
|
|
|
|
2015-04-26 00:01:29 -04:00
|
|
|
//
|
|
|
|
// Load our wordlist.
|
|
|
|
//
|
2015-04-25 23:02:05 -04:00
|
|
|
jQuery.getScript("./wordlist.js").done(
|
|
|
|
function(data) {
|
2015-04-27 21:38:22 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// If "debug" is set in the GET data, roll the dice on page load.
|
|
|
|
// Speed up my development a bit. :-)
|
|
|
|
//
|
|
|
|
if (location.search.indexOf("debug") != -1) {
|
|
|
|
jQuery("#roll_dice").click(); // Debugging
|
|
|
|
}
|
2015-04-25 23:02:05 -04:00
|
|
|
|
|
|
|
}).fail(
|
|
|
|
function(jqxhr, settings, exception) {
|
|
|
|
console.log("Error loading Javascript:", jqxhr.status, settings, exception);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|