2015-04-25 23:02:05 -04:00
|
|
|
/**
|
|
|
|
* Our main Javascript file.
|
|
|
|
*/
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
var Promise = require("bluebird");
|
|
|
|
var randomNumber = require("random-number-csprng");
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// This "put everything in an object" approach is a holdover from
|
|
|
|
// before I started using webpack. Yay legacy code.
|
|
|
|
//
|
2017-01-29 06:47:08 -05:00
|
|
|
var Diceware = {};
|
2015-04-25 23:02:05 -04:00
|
|
|
|
2017-02-18 16:17:25 -05:00
|
|
|
//
|
|
|
|
// How many dice per roll?
|
|
|
|
//
|
|
|
|
Diceware.num_dice_per_roll = 5;
|
|
|
|
|
2015-04-27 20:48:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if we have a function that returns cryptographically random
|
|
|
|
* values. False otherwise.
|
|
|
|
*/
|
2017-01-29 06:47:08 -05:00
|
|
|
Diceware.i_can_has_good_crypto = function() {
|
2015-04-27 20:48:57 -04:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
//return(false); // Debugging
|
2015-04-27 20:48:57 -04:00
|
|
|
if (window.crypto && window.crypto.getRandomValues) {
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
} // End of i_can_has_good_crypto()
|
|
|
|
|
|
|
|
|
2017-01-31 22:33:37 -05:00
|
|
|
/**
|
2017-11-08 23:27:42 -05:00
|
|
|
* Return a random integer between 0 and max, inclusive.
|
2017-01-31 22:33:37 -05:00
|
|
|
*/
|
|
|
|
Diceware.getRandomValue = function(max) {
|
2017-11-08 23:27:42 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2017-01-31 22:33:37 -05:00
|
|
|
|
|
|
|
if (max <= 0){
|
2017-11-08 23:27:42 -05:00
|
|
|
resolve(NaN);
|
2017-01-31 22:33:37 -05:00
|
|
|
}
|
|
|
|
|
2017-02-01 21:55:38 -05:00
|
|
|
if (Diceware.i_can_has_good_crypto()) {
|
2017-11-08 23:27:42 -05:00
|
|
|
|
|
|
|
Promise.try(function() {
|
|
|
|
return randomNumber(0, max);
|
|
|
|
|
|
|
|
}).then(function(number) {
|
|
|
|
retval = number;
|
|
|
|
resolve(retval);
|
|
|
|
|
|
|
|
}).catch({code: "RandomGenerationError"}, function(err) {
|
|
|
|
console.log("randomNumber(): Something went wrong!");
|
|
|
|
|
|
|
|
});
|
2017-02-01 21:55:38 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Fall back to something way less secure. The user has already
|
|
|
|
// been warned.
|
|
|
|
//
|
|
|
|
retval = Math.floor(Math.random() * max);
|
2017-11-08 23:27:42 -05:00
|
|
|
resolve(retval);
|
2017-02-01 21:55:38 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
}); // End of Promise()
|
2017-01-31 22:33:37 -05:00
|
|
|
} // End of getRandomValue()
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a number from base 10 into base 6.
|
|
|
|
*
|
|
|
|
* @param integer roll The random value.
|
|
|
|
* @param integer num_dice The number of dice we're returning.
|
|
|
|
*
|
|
|
|
* @return array An array of the base 6 numbers.
|
|
|
|
*/
|
|
|
|
Diceware.getBase6 = function(roll, num_dice) {
|
|
|
|
|
|
|
|
var retval = [];
|
|
|
|
|
|
|
|
//
|
|
|
|
// Sanity check
|
|
|
|
//
|
|
|
|
var max_dice_roll = Math.pow(6, num_dice) - 1;
|
|
|
|
if (roll > max_dice_roll) {
|
|
|
|
throw("Value too large!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (roll < 0) {
|
|
|
|
throw("Value cannot be negative!");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Go through each die, starting with the most significant one, and
|
|
|
|
// get its value.
|
|
|
|
//
|
|
|
|
var num_dice_left = num_dice - 1;
|
|
|
|
var dice_value_left = roll;
|
|
|
|
|
2017-02-02 20:48:33 -05:00
|
|
|
for (var i = num_dice_left; i >= 0; i--) {
|
2017-01-31 22:33:37 -05:00
|
|
|
|
|
|
|
var die_value = Math.pow(6, i);
|
|
|
|
var value = Math.floor( dice_value_left / die_value);
|
|
|
|
var left = dice_value_left % die_value;
|
|
|
|
|
|
|
|
retval.push(value);
|
|
|
|
dice_value_left = dice_value_left - (die_value * value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
|
|
|
|
} // End of getBase6()
|
|
|
|
|
|
|
|
|
2017-01-31 23:36:24 -05:00
|
|
|
/**
|
|
|
|
* Convert a base-6 number to a dice roll
|
|
|
|
*
|
|
|
|
* @param array roll An array of integers in base-6 notation
|
|
|
|
* @param integer num_dice The number of dice rolled
|
|
|
|
*
|
|
|
|
* @return array An array of integers representing dice rolls
|
|
|
|
*/
|
|
|
|
Diceware.convertBase6ToDice = function(roll, num_dice) {
|
|
|
|
|
|
|
|
var retval = [];
|
|
|
|
|
|
|
|
if (roll.length != num_dice) {
|
|
|
|
throw("Mismatch between size of roll (" + roll.length + ") and number of dice (" + num_dice + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var k in roll) {
|
|
|
|
var num = roll[k];
|
|
|
|
|
|
|
|
if (num < 0) {
|
|
|
|
throw("Value " + num + " is negative!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num > 5) {
|
|
|
|
throw("Value " + num + " is too large!");
|
|
|
|
}
|
|
|
|
|
|
|
|
num++;
|
|
|
|
retval.push(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
|
|
|
|
} // End of convertBase6ToDice()
|
|
|
|
|
|
|
|
|
2017-02-01 00:02:19 -05:00
|
|
|
/**
|
|
|
|
* Get the maximum value from the number of dice we're rolling.
|
|
|
|
* This is in a separate function so it is testable.
|
|
|
|
*/
|
|
|
|
Diceware.getNumValuesFromNumDice = function(num_dice) {
|
|
|
|
|
|
|
|
var retval;
|
|
|
|
|
|
|
|
if (num_dice == 0) {
|
|
|
|
throw("Number of dice cannot be zero!");
|
|
|
|
|
|
|
|
} else if (num_dice < 0){
|
|
|
|
throw("Number of dice is negative!");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = Math.pow(6, num_dice);
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
|
|
|
|
} // End of getNumValuesFromNumDice()
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is our main entry point for rolling dice.
|
|
|
|
*
|
|
|
|
* Get our maximum number for a random value, turn it into base-6,
|
|
|
|
* then turn it into a dice roll!
|
|
|
|
*
|
2017-02-01 21:50:33 -05:00
|
|
|
* @return object An object that contains a dice roll and the raw random value.
|
|
|
|
*
|
2017-02-01 00:02:19 -05:00
|
|
|
*/
|
|
|
|
Diceware.rollDice = function(num_dice) {
|
2017-11-08 23:27:42 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
var retval = {};
|
|
|
|
var max = Diceware.getNumValuesFromNumDice(num_dice);
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
Promise.try(function() {
|
|
|
|
|
|
|
|
return(Diceware.getRandomValue(max - 1));
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
}).then(function(random) {
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
var base6 = Diceware.getBase6(random, num_dice);
|
|
|
|
var dice = Diceware.convertBase6ToDice(base6, num_dice);
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
retval.value = random;
|
|
|
|
retval.roll = dice;
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
resolve(retval);
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
});
|
2017-02-01 00:02:19 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
}); // End of Promise()
|
2017-02-01 00:02:19 -05:00
|
|
|
} // End of rollDice()
|
|
|
|
|
|
|
|
|
2015-04-25 23:02:05 -04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-01-29 06:47:08 -05:00
|
|
|
Diceware.get_word = function(wordlist, index) {
|
2017-01-31 23:36:24 -05:00
|
|
|
|
2015-04-25 23:02:05 -04:00
|
|
|
var retval = wordlist[index];
|
2016-07-10 18:31:33 -04:00
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
retval = retval[0].toUpperCase() + retval.slice(1);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
retval = "((Word not found in wordlist)) ";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-25 23:02:05 -04:00
|
|
|
return(retval);
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2017-01-29 06:47:08 -05:00
|
|
|
}
|
2015-04-26 00:01:29 -04:00
|
|
|
|
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-11-10 23:39:31 -05:00
|
|
|
* @param integer in_fadein_duration How long before fading in a roll of dice
|
|
|
|
* @param integer in_fadeout_delay How long before fading out the diceroll
|
2015-04-26 15:32:56 -04:00
|
|
|
*
|
|
|
|
*/
|
2017-01-29 06:47:08 -05:00
|
|
|
Diceware.display_row = function(rows, cb, in_fadein_duration, in_fadeout_delay) {
|
2015-04-26 15:32:56 -04:00
|
|
|
|
2015-11-10 23:39:31 -05:00
|
|
|
var fadein_duration = in_fadein_duration || 250;
|
|
|
|
var fadeout_delay = in_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-11-10 23:39:31 -05:00
|
|
|
//
|
|
|
|
// Decrease the delays with subsequent rolls so that users
|
|
|
|
// don't get impatent.
|
|
|
|
// (I know I did when rolling 8 dice!)
|
|
|
|
//
|
|
|
|
fadein_duration -= 25;
|
|
|
|
fadeout_delay -= 50;
|
|
|
|
|
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() {
|
2017-01-29 06:47:08 -05:00
|
|
|
Diceware.display_row(rows, cb, fadein_duration, fadeout_delay);
|
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
|
|
|
*/
|
2017-01-29 06:47:08 -05:00
|
|
|
Diceware.display_results = function(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\" />");
|
2015-11-10 23:32:15 -05:00
|
|
|
|
2015-04-26 15:32:56 -04:00
|
|
|
jQuery(".results_phrase_key").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results_phrase_value").hide().clone().appendTo(".results");
|
2015-11-10 23:32:15 -05:00
|
|
|
jQuery(".results").append("<br clear=\"all\" />");
|
|
|
|
|
|
|
|
jQuery(".results_num_possible_key").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results_num_possible_value").hide().clone().appendTo(".results");
|
2015-04-26 15:32:56 -04:00
|
|
|
|
2015-04-26 17:53:43 -04:00
|
|
|
jQuery(".results .results_words_key").fadeIn(500, function() {
|
|
|
|
jQuery(".results .results_words_value").fadeIn(500, function() {
|
2015-11-10 23:32:15 -05:00
|
|
|
jQuery(".results .results_phrase_key").fadeIn(400, function() {
|
|
|
|
jQuery(".results .results_phrase_value").fadeIn(400, function() {
|
|
|
|
jQuery(".results .results_num_possible_key").fadeIn(300, function() {
|
|
|
|
jQuery(".results .results_num_possible_value").fadeIn(300, function() {
|
2015-04-26 17:53:43 -04:00
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
2015-04-26 15:32:56 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-11-10 23:32:15 -05:00
|
|
|
});
|
|
|
|
});
|
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.
|
|
|
|
*/
|
2017-01-29 06:47:08 -05:00
|
|
|
Diceware.get_width = function() {
|
2015-04-26 17:53:43 -04:00
|
|
|
return(jQuery(window).width());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-26 17:20:56 -04:00
|
|
|
/**
|
|
|
|
* Return true if we are running on a mobile screen.
|
|
|
|
*/
|
2017-01-29 06:47:08 -05:00
|
|
|
Diceware.is_mobile = function() {
|
2015-04-26 17:20:56 -04:00
|
|
|
|
2017-01-29 06:47:08 -05:00
|
|
|
if (Diceware.get_width() <= 480) {
|
2015-04-26 17:20:56 -04:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
} // End of is_mobile()
|
|
|
|
|
|
|
|
|
2017-01-29 06:47:08 -05:00
|
|
|
/**
|
2017-11-08 23:27:42 -05:00
|
|
|
* Do some preliminary work, such as clearing out results and scrolling.
|
2017-01-29 06:47:08 -05:00
|
|
|
*/
|
2017-11-08 23:27:42 -05:00
|
|
|
Diceware.rollDiceHanlderPre = function() {
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
// Clear out more space when mobile
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
// In the future, I should just use a media query in CSS
|
|
|
|
//
|
|
|
|
var target_height = 300;
|
|
|
|
if (Diceware.is_mobile()) {
|
|
|
|
target_height = 400;
|
|
|
|
}
|
2015-04-26 16:29:43 -04:00
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
jQuery(".results").animate({height: target_height}, 400);
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
// If we're running on an iPhone or similar, scroll down so that we can
|
|
|
|
// see the dice rolls and passphrase.
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
if (Diceware.is_mobile()) {
|
|
|
|
var aTag = $("a[name='roll_dice_button']");
|
|
|
|
$("html,body").animate({scrollTop: aTag.offset().top}, "slow");
|
|
|
|
}
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
//
|
|
|
|
// Remove any old results
|
|
|
|
//
|
|
|
|
jQuery(".results").empty();
|
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
} // End of rollDiceHandlerPre()
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Our handler for what to do when the "Roll Dice" button is clicked".
|
|
|
|
* It generates the passphrase and updates the HTML.
|
|
|
|
*/
|
|
|
|
Diceware.rollDiceHandler = function(e) {
|
|
|
|
|
|
|
|
Diceware.rollDiceHanlderPre();
|
|
|
|
|
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
//
|
|
|
|
// Make our dice rolls
|
|
|
|
//
|
|
|
|
var num_dice = jQuery(".dice_button.active").html();
|
2017-02-18 16:17:25 -05:00
|
|
|
var num_passwords = Number(Math.pow(6, (Diceware.num_dice_per_roll * num_dice)));
|
2017-02-17 23:40:34 -05:00
|
|
|
var passphrase = new Array();
|
|
|
|
var rolls = new Array();
|
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
//
|
|
|
|
// Create an array of empty items, since this is the only way I can
|
|
|
|
// figure out to do a loop in Bluebird at this time. Ugh.
|
|
|
|
//
|
|
|
|
var items = [];
|
|
|
|
for (i=0; i<num_dice; i++) { items.push(null); }
|
2017-02-17 23:40:34 -05:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
Promise.map(items, function(element) {
|
|
|
|
//
|
|
|
|
// Do our dice rolls all at once.
|
|
|
|
//
|
|
|
|
return(Diceware.rollDice(Diceware.num_dice_per_roll));
|
|
|
|
|
|
|
|
}).then(function(data) {
|
|
|
|
//
|
|
|
|
// Now that we have the results, get the word for each roll,
|
|
|
|
// save the roll, and push the word onto the passphrase.
|
|
|
|
//
|
|
|
|
data.forEach(function(row) {
|
|
|
|
|
|
|
|
var roll = {};
|
|
|
|
roll.dice = row;
|
|
|
|
roll.word = Diceware.get_word(wordlist, roll.dice.value);
|
|
|
|
rolls.push(roll);
|
|
|
|
passphrase.push(roll.word);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Diceware.rollDiceHanlderPost(rolls, passphrase, num_passwords);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} // End of rollDiceHandler()
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Our post work, of displaying the results of our dice rolls.
|
|
|
|
*/
|
|
|
|
Diceware.rollDiceHanlderPost = function(rolls, passphrase, num_passwords) {
|
2015-04-26 02:08:58 -04:00
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
//
|
|
|
|
// Populate our results by cloning the hidden base rows which
|
|
|
|
// represent each die.
|
|
|
|
//
|
|
|
|
jQuery(".results_words_value").html(passphrase.join(" "));
|
|
|
|
jQuery(".results_phrase_value").html(passphrase.join(""));
|
|
|
|
jQuery(".results_num_possible_value").html(num_passwords.toLocaleString("en"));
|
|
|
|
|
|
|
|
var rows = new Array();
|
|
|
|
for (var key in rolls) {
|
|
|
|
|
|
|
|
var roll = rolls[key];
|
|
|
|
var row = jQuery("<div></div>");
|
2015-04-26 02:08:58 -04:00
|
|
|
|
2015-04-26 16:29:43 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
// Clone and append specific dice to this row.
|
2015-04-26 16:29:43 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
for (var key2 in roll.dice.roll) {
|
|
|
|
var die = roll.dice.roll[key2];
|
|
|
|
var classname = ".source .dice" + die;
|
|
|
|
var tmp = jQuery(classname).clone().appendTo(row);
|
2015-04-26 02:08:58 -04:00
|
|
|
}
|
|
|
|
|
2015-04-26 16:29:43 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
// Now append the word
|
2015-04-26 16:29:43 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
var dice_word = jQuery(".dice_word").clone();
|
|
|
|
dice_word.html("\"" + roll.word + "\"");
|
|
|
|
row.append(dice_word);
|
2015-04-26 17:14:21 -04:00
|
|
|
|
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
// And clear to the next line
|
2015-04-26 17:14:21 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
row.append("<br clear=\"all\" />");
|
|
|
|
|
|
|
|
rows.push(row);
|
2015-04-26 02:08:58 -04:00
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
}
|
2015-04-27 21:34:14 -04:00
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
//
|
|
|
|
// Now display those rows.
|
|
|
|
//
|
|
|
|
Diceware.display_row(rows, function() {
|
2015-04-26 17:53:43 -04:00
|
|
|
|
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
// And then display the results
|
2015-04-26 17:53:43 -04:00
|
|
|
//
|
2017-02-17 23:40:34 -05:00
|
|
|
Diceware.display_results(function() {
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
});
|
2015-04-27 21:34:14 -04:00
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
} // End of rollDiceHandlerPost()
|
2017-01-29 06:47:08 -05:00
|
|
|
|
|
|
|
|
2017-02-18 00:00:45 -05:00
|
|
|
/**
|
|
|
|
* Turn our GET method data into an associative array.
|
|
|
|
*/
|
|
|
|
Diceware.extractGetData = function(get_data) {
|
|
|
|
|
|
|
|
var retval = {};
|
|
|
|
|
|
|
|
if (!location.search) {
|
|
|
|
return(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
var get = get_data.substring(1);
|
|
|
|
var pairs = get.split("&");
|
|
|
|
|
|
|
|
for (var k in pairs) {
|
|
|
|
var row = pairs[k];
|
|
|
|
var pair = row.split("=");
|
|
|
|
var key = pair[0];
|
|
|
|
var value = pair[1];
|
|
|
|
retval[key] = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
|
|
|
|
} // End of extractGetData()
|
|
|
|
|
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
/**
|
|
|
|
* Our main function when being used via the UI. We call this to set up our jQuery hooks.
|
|
|
|
*
|
|
|
|
* I should probably refactor this more in the future--this function came about
|
|
|
|
* when I changed the code from self-contained to contained in an external object
|
|
|
|
* in preparation fro Qunit testing...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Diceware.go = function() {
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2017-02-18 16:17:25 -05:00
|
|
|
console.log("Thanks for checking out my code! You can find the Git repo at https://github.com/dmuth/diceware");
|
|
|
|
|
2017-02-17 23:40:34 -05: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");
|
|
|
|
});
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2015-04-27 20:48:57 -04:00
|
|
|
|
2017-02-17 23:40:34 -05:00
|
|
|
jQuery("#roll_dice").on("click", Diceware.rollDiceHandler);
|
2015-04-27 21:34:14 -04:00
|
|
|
|
2015-04-27 21:38:22 -04:00
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
//
|
|
|
|
// If we're not on a mobile, bring in the GitHub ribbon.
|
|
|
|
//
|
|
|
|
if (!Diceware.is_mobile()) {
|
|
|
|
jQuery("#github_ribbon").fadeIn(1000);
|
|
|
|
}
|
2015-04-25 23:02:05 -04:00
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
if (!Diceware.i_can_has_good_crypto()) {
|
|
|
|
jQuery(".source .bad_crypto").clone().hide().fadeIn(800).appendTo(".message");
|
|
|
|
}
|
2015-04-25 23:02:05 -04:00
|
|
|
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2017-02-18 00:00:45 -05:00
|
|
|
var get_data = Diceware.extractGetData(location.search);
|
|
|
|
|
2017-02-18 00:03:43 -05:00
|
|
|
var dice = 5;
|
|
|
|
if (get_data["dice"]) {
|
|
|
|
if (get_data["dice"] >= 5 && get_data["dice"] <= 7) {
|
|
|
|
dice = get_data["dice"];
|
2017-02-18 16:17:25 -05:00
|
|
|
Diceware.num_dice_per_roll = dice;
|
2017-02-18 00:03:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:17:25 -05:00
|
|
|
console.log("Rolling " + Diceware.num_dice_per_roll + " dice per roll");
|
|
|
|
|
2017-07-09 13:37:09 -04:00
|
|
|
var file = "wordlist-" + dice + "-dice.js";
|
|
|
|
if (dice == 5) {
|
|
|
|
//
|
|
|
|
// 5 Dice? Let's use the EFF version.
|
|
|
|
//
|
|
|
|
file = "wordlist-" + dice + "-dice-eff.js";
|
|
|
|
}
|
|
|
|
|
|
|
|
var js = "./wordlist/" + file;
|
2017-02-18 00:03:43 -05:00
|
|
|
console.log("Looks like we're loading '" + js + "'!");
|
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
//
|
|
|
|
// Load our wordlist.
|
|
|
|
//
|
2017-02-18 00:03:43 -05:00
|
|
|
jQuery.getScript(js).done(
|
2017-02-17 23:41:24 -05:00
|
|
|
function(data) {
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
//
|
|
|
|
// If "debug" is set in the GET data, roll the dice on page load.
|
|
|
|
// Speed up my development a bit. :-)
|
|
|
|
//
|
|
|
|
var debug = location.search.indexOf("debug");
|
|
|
|
|
2017-02-18 00:00:45 -05:00
|
|
|
if (get_data["debug"] && get_data["debug"] > 0) {
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2017-02-18 00:00:45 -05:00
|
|
|
var num = get_data["debug"];
|
2017-02-17 23:41:24 -05:00
|
|
|
if (num < 2) {
|
|
|
|
num = 2;
|
|
|
|
} else if (num > 8) {
|
|
|
|
num = 8;
|
|
|
|
}
|
2017-01-29 13:43:25 -05:00
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
var id="#button-dice-" + num;
|
|
|
|
jQuery(id).click();
|
2017-01-29 13:43:25 -05:00
|
|
|
|
2017-02-18 16:17:25 -05:00
|
|
|
console.log("Debug mode enabled, auto-rolling " + num + " times!");
|
2017-02-17 23:41:24 -05:00
|
|
|
jQuery("#roll_dice").click();
|
2017-01-29 13:43:25 -05:00
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
}
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
}).fail(
|
|
|
|
function(jqxhr, settings, exception) {
|
|
|
|
console.log("Error loading Javascript:", jqxhr.status, settings, exception);
|
2017-01-29 06:47:08 -05:00
|
|
|
|
2017-02-17 23:41:24 -05:00
|
|
|
});
|
2015-04-25 23:02:05 -04:00
|
|
|
|
2017-01-29 06:47:08 -05:00
|
|
|
} // End of go()
|
2015-04-25 23:02:05 -04:00
|
|
|
|
|
|
|
|
2017-11-08 23:27:42 -05:00
|
|
|
//
|
|
|
|
// Run go() automatically, as that is the webpack way.
|
|
|
|
//
|
|
|
|
Diceware.go();
|
|
|
|
|
|
|
|
|