Skip to content

Instantly share code, notes, and snippets.

@paceaux
Last active January 28, 2022 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paceaux/7d68c324de79867fde0b81ab79805b19 to your computer and use it in GitHub Desktop.
Save paceaux/7d68c324de79867fde0b81ab79805b19 to your computer and use it in GitHub Desktop.
/**
* @class Locker
* @description who doesn't like Konami?
* @example const locker = new Locker(); locker.initialize();
*
*/
// I'm not going to use Babel for a tiny static site
// eslint-disable-next-line no-unused-vars
class Locker {
/**
* @param {number[]} [keys=up up down down...] change these numbers to whatever code sequence you want
*/
constructor(keys = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65]) {
this.keys = keys;
this.keyIndex = 0;
}
/**
* @description actions to take when the keys have been pressed
* @example add a class to the body that reads "is-unlocked", and save that to localStorage
* @static
*/
static unlock() {
document.body.classList.add('is-unlocked');
window.sessionStorage.setItem('locker-unlocked', new Date());
}
/**
* @description actions to take to revert whatever pressing the keys did
* @example remove the is-unlocked class, remove from storage
* @static
*/
static lock() {
document.body.classList.remove('is-unlocked');
window.sessionStorage.removeItem('locker-unlocked');
}
/**
* @description the callback for the keydown/up events
* @param {Event} event the event callback
*/
trackerCallback(event) {
const currentKey = this.keys[this.keyIndex];
if (event.keyCode === currentKey) {
this.keyIndex += 1;
if (this.keyIndex === this.keys.length) {
document.removeEventListener('keydown', this.trackerCallback);
Locker.unlock();
}
}
}
/**
* @description Binds any and all events needed
*/
bindEvents() {
document.addEventListener('keydown', this.trackerCallback.bind(this));
}
/**
* @description do all the things (like bind events)
*/
initialize() {
this.bindEvents();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment