-
Notifications
You must be signed in to change notification settings - Fork 1
/
phoney.js
147 lines (130 loc) · 4.42 KB
/
phoney.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
;(function(window, undefined) {
//before we do anything, we have to ask if the device is touch capable. if it is, we're going to
//user Modernizr to load toucher.js to handle all the weirdness with touching.
//TODO - touch devices. stuff looks like it works fine with iphone4, but more superphone
//testing is needed.
/*all our testing will be done through Modernizr, because it's already there so why not? */
//TODO - add tests for for app installed
Modernizr.addTest('ipad', function () {
return !!navigator.userAgent.match(/iPad/i);
});
Modernizr.addTest('iphone', function () {
return !!navigator.userAgent.match(/iPhone/i);
});
Modernizr.addTest('ipod', function () {
return !!navigator.userAgent.match(/iPod/i);
});
Modernizr.addTest('webkit', function () {
return !!navigator.userAgent.match(/webkit/i);
});
Modernizr.addTest('appleios', function () {
return (Modernizr.ipad || Modernizr.ipod || Modernizr.iphone);
});
/*function groupValidate(groupName, groupChecks){
console.log(groupName);
console.log(groupChecks);
for (i = 0; i < groupChecks.length; i++){
prop = groupChecks[i].toLowerCase();
if (Modernizr[prop] !== true){
console.log('break');
return false;
}
}
console.log('r gname');
return groupName;
}*/
//break apart the groups and determine which group are we in
function determineGroup(groups){
console.log(groups);
var activeGroup = 'default';
for (var key in groups){
console.log(key);
if (key !== 'default'){
groupProps = groups[key].split(' ');
console.log(groupProps.length);
console.log(groupProps);
var thisGroupCount = 0;
for (i = 0; i < groupProps.length; i++){
prop = groupProps[i].toLowerCase();
if (Modernizr[prop] !== true){
console.log('group ' + key + ' out based on ' + prop );
}
else{
thisGroupCount++;
console.log(thisGroupCount);
}
}
if (thisGroupCount === groupProps.length){
console.log(thisGroupCount + '==' + groupProps.length);
activeGroup = key;
console.log(activeGroup);
return activeGroup;
}
}//endif key !default
}
//if we've gotten here, that means nothing matched, so
return activeGroup;
}
function replaceHref(oldHref, router){
console.log(oldHref);
// protocol match - /((?:[a-z0-9_\-]{1,5})(?::\/\/))/g,
var href = oldHref,
rKey,
nKey;
//handle 1:1 router matches only, no wilds
if (router[href]){
console.log(router[href]);
return router[href];
}
//ok the easy part's over. no matches direct in route
else{
//oldProtocol = href.match(protocol);
//console.log(oldProtocol);
//href = href.replace(protocol, '');
//sanitize url, removing beginning and ending slashes
//href = href.replace(/^\/|\/$/g, '');
//href = href.split('/');
//console.log(href);
//run thru looking for matches
for (var key in router){
rKey = new RegExp(key);
console.log(rKey);
if (href.match(rKey)){
nKey = router[key];
href = href.replace(rKey, nKey);
href = href.replace(/^\/|\/$/g, '');
//if (oldProtocol){
// href = oldProtocol + href;
//}
console.log(href);
return href;
}
}
//so if we haven't returned by now that means there's no match. so return oldie
return oldHref;
}
}
this.Phoney = function(config) { //pass in objects here
if(!(this instanceof Phoney)) return new Phoney(config);
console.log(this);
this.activeGroup = determineGroup(config.groups);
console.log(this.activeGroup);
this.activeRouter = config.routers[this.activeGroup];
this.startListening = function(){
var activeRouter = this.activeRouter;
console.log(activeRouter);
window.onclick = function(event){
console.log(event);
element = event.srcElement;
if (element.nodeName === "A") {
element.setAttribute('href', replaceHref(element.getAttribute('href'), activeRouter));
alert(element); //remove when done testing
}
event.preventDefault(); //remove me when u're done testing
}
console.log('listening now');
}//startlistening
console.log(this.activeRouter);
return this;
}
})(this);