diceware/src/index.js

114 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-11-10 00:43:36 +00:00
/**
* Our main Javascript file.
*/
let Promise = require("bluebird");
2017-11-10 01:11:24 +00:00
let lib = require("./lib.js")();
2017-11-10 01:11:24 +00:00
2017-11-10 00:43:36 +00:00
//
// I'm not a huge fan of globals, but the alternative was passing this variable into
// nearly every module, which gave me headaches trying to keep track of the dependency.
// I am storing things in modules (such as the wordlist) where I can...
2017-11-10 00:43:36 +00:00
//
window.Diceware = {};
2017-11-10 00:43:36 +00:00
// Functions that relate to displaying dice
display = require("./display.js")()
2017-11-10 00:43:36 +00:00
// Wordlist handling
wordlist = require("./wordlist.js")()
2017-11-10 00:43:36 +00:00
// Misc utilities
util = require("./util.js")();
2017-11-10 00:43:36 +00:00
//
// How many dice per roll?
//
window.Diceware.num_dice_per_roll = 5;
2017-11-10 00:43:36 +00:00
/**
* Set the handlers
2017-11-10 00:43:36 +00:00
*/
function set_handlers() {
2017-11-10 00:43:36 +00: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");
});
jQuery("#roll_dice").on("click", display.rollDiceHandler);
2017-11-10 00:43:36 +00:00
} // End of set_handlers()
2017-11-10 00:43:36 +00:00
/**
* Run our pre-flight checks.
*/
function run_preflight_checks() {
2017-11-10 00:43:36 +00:00
//
// If we're not on a mobile, bring in the GitHub ribbon.
//
if (!util.is_mobile()) {
2017-11-10 00:43:36 +00:00
jQuery("#github_ribbon").fadeIn(1000);
}
2017-11-10 01:11:24 +00:00
if (!lib.iCanHasGoodCrypto()) {
2017-11-10 00:43:36 +00:00
jQuery(".source .bad_crypto").clone().hide().fadeIn(800).appendTo(".message");
}
} // End of run_preflight_checks()
2017-11-10 00:43:36 +00:00
/**
* Our main function when being used via the UI. We call this to set up our jQuery hooks.
*
*/
function go() {
2017-11-10 00:43:36 +00:00
console.log("Thanks for checking out my code! You can find the Git repo at https://github.com/dmuth/diceware, my blog at https://www.dmuth.org/, or you can bug me on Twitter at https://twitter.com/dmuth");
2017-11-10 00:43:36 +00:00
console.log("Version: $Id$");
2017-11-10 00:43:36 +00:00
//
// Set our handlers
//
set_handlers()
2017-11-10 00:43:36 +00:00
//
// Run our pre-flight checks
//
run_preflight_checks()
2017-11-10 00:43:36 +00:00
//
// Get the filename of the wordlist that we're loading.
//
let file = wordlist.get_filename()
console.log(`Looks like we're loading ${file["filename"]}!`);
2017-11-10 00:43:36 +00:00
//
// Load the wordlist.
//
let debug = location.search.indexOf("debug");
window.Diceware.get_data = util.extract_get_data(location.search);
console.log("GET Data: " + JSON.stringify(window.Diceware.get_data)); // Debugging
2017-11-10 00:43:36 +00:00
wordlist.load(file, window.Diceware.get_data, debug)
2017-11-10 00:43:36 +00:00
} // End of go()
//
// Run go() automatically, as that is the webpack way.
//
go();
2017-11-10 00:43:36 +00:00