Last active
February 16, 2021 14:13
-
-
Save gravataLonga/4826c4f9ff01176cb65a7abcb72517cf to your computer and use it in GitHub Desktop.
Library to handle Hash of URL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let hashes = function () { | |
| let _this = this; | |
| let _SEPERATOR_KEY_VALUE = ":"; | |
| let _SEPERATOR_PAIR = "|"; | |
| this._OLD_HASHES = {}; | |
| /** | |
| * Add Hash event. | |
| * | |
| * @param hash | |
| * @param callback | |
| */ | |
| this.addHashEvent = function (hash, callback) { | |
| window.hashes = window.hashes || {}; | |
| window.hashes[hash] = callback; | |
| this._OLD_HASHES[hash] = null; | |
| }; | |
| /** | |
| * When hash is change we fire this method, or you can fire manually. | |
| * hash must be on the following patter: key:value;key1:value, it will transform to: | |
| * ['key:value', 'ke1:value'] for later to be dispatch to window.hashes.key => value | |
| * and window.hashes.key1 = value1; | |
| */ | |
| this.fireHashOnChange = function () { | |
| let hashes = getMapOfHash(); | |
| for (let hash in hashes) { | |
| let value = hashes[hash]; | |
| if (!window.hashes) { | |
| continue; | |
| } | |
| if (!window.hashes[hash]) { | |
| continue; | |
| } | |
| if (this._OLD_HASHES && this._OLD_HASHES[hash] && this._OLD_HASHES[hash] === value) { | |
| continue; | |
| } | |
| let callback = window.hashes[hash]; | |
| this._OLD_HASHES[hash] = value; | |
| callback.call(_this, hash, value); | |
| } | |
| }; | |
| /** | |
| * Add/Change hash on url but maintain the actual hash already added. | |
| * @param hashKey | |
| * @param hashValue | |
| */ | |
| this.addHash = function (hashKey, hashValue) { | |
| let hashes = getMapOfHash(); | |
| hashes[hashKey] = hashValue; | |
| let fullHashes = []; | |
| for (let hash in hashes) { | |
| fullHashes.push(hash + _SEPERATOR_KEY_VALUE + hashes[hash]); | |
| } | |
| window.location.hash = fullHashes.join(_SEPERATOR_PAIR); | |
| } | |
| /** | |
| * Remove Hash from hashes. | |
| * @param hashKey | |
| * @param hashValue | |
| */ | |
| this.removeHash = function (hashKey) { | |
| let hashes = getMapOfHash(); | |
| let fullHashes = []; | |
| for (let hash in hashes) { | |
| if (hash !== hashKey) { | |
| fullHashes.push(hash + _SEPERATOR_KEY_VALUE + hashes[hash]); | |
| } | |
| } | |
| window.location.hash = fullHashes.join(_SEPERATOR_PAIR); | |
| } | |
| /** | |
| * Hash on Change. | |
| */ | |
| let hashOnChange = function () { | |
| window.addEventListener("hashchange", _this.fireHashOnChange, false); | |
| }; | |
| /** | |
| * Get all hashes from url | |
| */ | |
| let getMapOfHash = function () { | |
| let hashes = location.hash.toString(); | |
| let map = {}; | |
| if (!hashes) { | |
| return map; | |
| } | |
| if (hashes.indexOf(";") >= -1) { | |
| hashes = hashes.split(_SEPERATOR_PAIR); | |
| } else { | |
| hashes = [hashes]; | |
| } | |
| for (let hash of hashes) { | |
| hash = hash.replace("#", ""); | |
| let parts = hash.split(_SEPERATOR_KEY_VALUE); | |
| map[parts[0]] = parts[1]; | |
| } | |
| return map; | |
| } | |
| hashOnChange(); | |
| }; | |
| module.exports = hashes; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UPDATE: