Skip to content

Commit

Permalink
Bugfixes concerning reuse of autogenerate and parallel use of multiple
Browse files Browse the repository at this point in the history
managers
  • Loading branch information
lucidBrot committed Sep 11, 2018
2 parents a103b9d + 5d65250 commit a9348fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ Overwrites `leave_f_mode`, so if for some reason, you're overwriting that as wel

### disable

Does not unregister the listener to hotkeys.js - it simply does not act on the events until activated again using `enable_f_mode` with either true or false as parameter. (True if you want F_Mode).
Does not unregister the listener to hotkeys.js - it simply does not act on the events until activated again using `enable_f_mode` with either true or false as parameter. (True if you want F_Mode).

If you're disabling the manager with a user-typed-word-reaction, you want to also `manager.leave_f_mode()`, else the link hints remain visible.

### hotkeys_init

Expand Down
28 changes: 22 additions & 6 deletions brotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class HotkeyManager {

// internal config. It doesn't matter what you set here
this.AUTOGEN_LINKHINT_ATTRIBUTE = "brotkeysid"; // used for counting all anchors. Throwaway property.
// and then we add a UID to it so it works even with multiple HotkeyManagers
this.UID = HotkeyManager.genHotkeyManagerUID();
this.AUTOGEN_LINKHINT_ATTRIBUTE += this.UID;
this.SWAP_CLASS_NAME_DEFAULT = "LB-SS-swap1"; // used for all link hints in order to swap them on and off if no other are provided

// fake enum for adding more options later, for autogeneration of link hints
Expand Down Expand Up @@ -54,6 +57,10 @@ class HotkeyManager {

// this was not yet loaded
this.__loadNeededJSCSSForStyleSwapping_alreadyLoaded = false;


// set this to 0 if you need autogenerate to start overwriting previously generated link hints
this.global_index = 0;

this.hotkeys_init();
}
Expand Down Expand Up @@ -270,18 +277,16 @@ class HotkeyManager {
const num_elems_to_gen_for = elems_to_gen.length + this.wordMap.size; // need to fit at least this many link hints
let letters = this.computeLettersArray();
let min_len = this.computeMinLength(num_elems_to_gen_for, letters);
let brotkeys_elem_id = 0;
// For each element, create a tag
[...elems_to_gen].forEach(function(item, index){
const curr_bk_elem_id = brotkeys_elem_id;
// noinspection JSPotentiallyInvalidUsageOfClassThis
let link_hint_text = this.generateLinkHintText(item, min_len, letters); // generate link hint
item.setAttribute(this.AUTOGEN_LINKHINT_ATTRIBUTE, index); // give it a unique id based on index
let f = new Function("document.querySelector(\"["+this.AUTOGEN_LINKHINT_ATTRIBUTE+"='"+curr_bk_elem_id+"']\").click();");
item.setAttribute(this.AUTOGEN_LINKHINT_ATTRIBUTE, this.global_index); // give it a unique id based on index
let f = new Function("document.querySelector(\"["+this.AUTOGEN_LINKHINT_ATTRIBUTE+"='"+this.global_index+"']\").click();");
this.wordMap.set(link_hint_text, f); // current value in wordMap is there, but action is undefined. Set up action.
// noinspection JSPotentiallyInvalidUsageOfClassThis
this.addBeautifulLinkHint(item, link_hint_text, swap_class); // add the graphics
brotkeys_elem_id++;
this.global_index++; // increase global index that persists over multiple autogenerations
}.bind(this));
HotkeyManager.showKeys(false, swap_class); // set display to none, even if the css class was not loaded before
}
Expand Down Expand Up @@ -525,6 +530,17 @@ class HotkeyManager {
}
}.bind(this);
}

// generate an identifier unique among HotkeyManagers
static genHotkeyManagerUID(){
let prev = HotkeyManager.latest_uid
if (prev == undefined) {
prev = -1;
}
let uid = prev + 1;
HotkeyManager.latest_uid = uid;
return uid;
}

}

Expand Down Expand Up @@ -617,4 +633,4 @@ function brotkeys_autogenerate_manager_for_class_tag(css_class_name){
TODO: make link hints overlay if possible, instead of shifting content.
TODO: make sure link hints also show over images
TODO: make sure loading with a nonexistent css class like LB-Swap-Class still works. And get rid of the fact that every successive call seems to load eric-reverse yet again.
*/
*/
Loading

0 comments on commit a9348fc

Please sign in to comment.