-
Notifications
You must be signed in to change notification settings - Fork 7
/
jquery.mobile.paramsHandler-1.4.3.js
99 lines (79 loc) · 2.63 KB
/
jquery.mobile.paramsHandler-1.4.3.js
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
$.mobile.paramsHandler = {
_pagesWithParams: [],
addPage: function (id, requiredParams, optionalParams, callback) {
var page = new PageWithParams(id, requiredParams, optionalParams, callback);
this._pagesWithParams.push(page);
},
init: function () {
$(document).on("pagebeforechange", function (e, data) {
if (typeof data.toPage !== "string") {
return;
}
var u = $.mobile.path.parseUrl(data.toPage);
var pageMatch = null;
var pages = $.mobile.paramsHandler._pagesWithParams;
for (var i in pages) {
var page = pages[i];
var re = "^#" + page.id + "(\\?|$)";
if (u.hash.search(re) !== -1) {
pageMatch = page;
}
}
var vm;
if (!pageMatch) {
return;
}
// TODO: add default functionality for if there's a parameter missing
var urlVars = getUrlVars(u.hash);
if (urlVars == null) {
return;
}
for (var j in pageMatch.requiredParams) {
if (urlVars[j]) {
pageMatch.requiredParams[j] = urlVars[j];
} else {
return;
}
}
for (var k in pageMatch.optionalParams) {
if (urlVars[k]) {
pageMatch.optionalParams[k] = urlVars[k];
}
}
pageMatch.callback(urlVars);
pageMatch.reset();
$(":mobile-pagecontainer").pagecontainer("change", "#" + pageMatch.id, data.options);
window.history.replaceState(null, null, u.href);
e.preventDefault();
});
}
};
function PageWithParams(id, requiredParams, optionalParams, callback) {
var self = this;
self.id = id;
self.callback = callback;
self.requiredParams = [];
self.optionalParams = [];
self.reset = function () {
for (var x = 0; x < requiredParams.length;x++) {
self.requiredParams[requiredParams[x]] = null;
}
for (var y = 0; y < optionalParams.length; y++) {
self.optionalParams[optionalParams[y]] = null;
}
};
self.reset();
}
function getUrlVars(url) {
var vars = [];
if (url.indexOf('?') == -1) {
return null;
}
var queryUrl = url.slice(url.lastIndexOf('?') + 1);
var hashes = queryUrl.split('&');
for (var i = 0; i < hashes.length; i++) {
url = hashes[i].split('=');
vars[url[0]] = url[1];
}
return vars;
}