-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.jqueryhash.js
More file actions
120 lines (117 loc) · 3.62 KB
/
jquery.jqueryhash.js
File metadata and controls
120 lines (117 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(function ($) {
// Variables
let settings;
const _jqWindow = $(window);
// Plugin methods
const methods = {
init: function (options) {
settings = $.extend(
{
hashes: [["", function () {}]],
onlyOnLoad: false,
},
options
);
// We start a events listener
methods.onCheckHash();
if (!settings.onlyOnLoad) _jqWindow.on("hashchange", methods.onCheckHash);
},
// Add one handler
onAddHandler: function (handler) {
if (typeof handler[0] === "string" && typeof handler[1] === "function") {
settings.hashes.push([handler[0], handler[1]]);
} else {
throw "Incorrect format of the handler.";
}
},
// Update one handler
onUpdateOne: function (handler) {
// Variable used to error checking
let _boolExits = false;
if (typeof handler[0] === "string") {
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === handler[0]) {
if (handler[1] && typeof handler[1] === "function") {
settings.hashes[i][1] = handler[1];
_boolExits = true;
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
}
if (!_boolExits) throw "Trying to update an unexisting handler";
}
},
// Remove only one handler
onRemoveOneHandler: function (handler) {
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === handler) {
settings.hashes.splice(i, 1);
break;
}
}
},
// Remove all handlers
onRemoveHashChange: function () {
if (!settings.onlyOnLoad) {
_jqWindow.off("hashchange", methods.onCheckHash);
settings.hashes = [["", function () {}]];
}
},
// Trigger one handler
onTriggerOne: function (handler) {
// Variable used to error checking
let _boolExits = false;
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === handler) {
if (
settings.hashes[i][1] &&
typeof settings.hashes[i][1] === "function"
) {
settings.hashes[i][1]();
_boolExits = true;
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
}
if (!_boolExits) throw "Trying to trigger an unexisting handler";
},
// Function used to check the hash and fire the function associated with it
onCheckHash: function () {
if (window.location.hash) {
let _strHash = window.location.href.split("#")[1];
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === _strHash) {
if (
settings.hashes[i][1] &&
typeof settings.hashes[i][1] === "function"
) {
settings.hashes[i][1]();
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
}
}
},
};
$.onHash = function (method) {
if (methods[method]) {
return methods[method].apply(
this,
Array.prototype.slice.call(arguments, 1)
);
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
throw "This method " + method + " does not belong to this plugin.";
}
};
})(jQuery);