forked from janmonschke/GeoMock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeomock.js
160 lines (157 loc) · 4.66 KB
/
geomock.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
148
149
150
151
152
153
154
155
156
157
158
159
160
// Generated by CoffeeScript 1.9.1
/*
(c) 2011 Jan Monschke
v1.0.1
GeoMock is licensed under the MIT license.
*/
(function() {
window.GeoMock = {
_backup: {},
_adding: ['delay', 'shouldFail', 'failsAt', 'errorMessage', 'waypoints', '_geoCall'],
_replacing: ['getCurrentPosition', 'watchPosition', 'clearWatch'],
_watchIds: [],
install: function() {
var attr, i, len, ref;
if (typeof navigator === "undefined" || navigator === null) {
window.navigator = {};
}
if (navigator.geolocation == null) {
window.navigator.geolocation = {};
}
ref = this._replacing;
for (i = 0, len = ref.length; i < len; i++) {
attr = ref[i];
this._backup[attr] = navigator.geolocation[attr];
}
GeoMock._geolocation = navigator.geolocation;
navigator.geolocation = {};
navigator.geolocation.delay = 1000;
navigator.geolocation.shouldFail = false;
navigator.geolocation.failsAt = -1;
navigator.geolocation.errorMessage = "There was an error retrieving the position!";
navigator.geolocation.currentTimeout = -1;
navigator.geolocation.lastPosReturned = 0;
navigator.geolocation._sanitizeLastReturned = function() {
if (this.lastPosReturned > this.waypoints.length - 1) {
return this.lastPosReturned = 0;
}
};
navigator.geolocation._geoCall = function(method, success, error) {
if (this.shouldFail && (error != null)) {
this.currentTimeout = window[method].call(null, (function(_this) {
return function() {
return error(_this.errorMessage);
};
})(this), this.delay);
return GeoMock._watchIds.push(this.currentTimeout);
} else {
if (success != null) {
this.currentTimeout = window[method].call(null, (function(_this) {
return function() {
success(_this.waypoints[_this.lastPosReturned++]);
return _this._sanitizeLastReturned();
};
})(this), this.delay);
return GeoMock._watchIds.push(this.currentTimeout);
}
}
};
navigator.geolocation.getCurrentPosition = function(success, error) {
return this._geoCall("setTimeout", success, error);
};
navigator.geolocation.watchPosition = function(success, error) {
this._geoCall("setInterval", success, error);
return this.currentTimeout;
};
navigator.geolocation.clearWatch = function(id) {
return clearInterval(id);
};
return navigator.geolocation.waypoints = [
{
coords: {
latitude: 52.5168,
longitude: 13.3889,
accuracy: 1500
}
}, {
coords: {
latitude: 52.5162,
longitude: 13.3890,
accuracy: 1334
}
}, {
coords: {
latitude: 52.5154,
longitude: 13.3890,
accuracy: 631
}
}, {
coords: {
latitude: 52.5150,
longitude: 13.3890,
accuracy: 361
}
}, {
coords: {
latitude: 52.5144,
longitude: 13.3890,
accuracy: 150
}
}, {
coords: {
latitude: 52.5138,
longitude: 13.3890,
accuracy: 65
}
}, {
coords: {
latitude: 52.5138,
longitude: 13.3895,
accuracy: 65
}
}, {
coords: {
latitude: 52.5139,
longitude: 13.3899,
accuracy: 65
}
}, {
coords: {
latitude: 52.5140,
longitude: 13.3906,
accuracy: 65
}
}, {
coords: {
latitude: 52.5140,
longitude: 13.3910,
accuracy: 65
}
}
];
},
uninstall: function() {
var attr, i, id, j, k, len, len1, len2, ref, ref1, ref2, results;
if (GeoMock._geolocation) {
ref = this._replacing;
for (i = 0, len = ref.length; i < len; i++) {
attr = ref[i];
navigator.geolocation[attr] = this._backup[attr];
}
ref1 = this._adding;
for (j = 0, len1 = ref1.length; j < len1; j++) {
attr = ref1[j];
delete navigator.geolocation[attr];
}
ref2 = this._watchIds;
results = [];
for (k = 0, len2 = ref2.length; k < len2; k++) {
id = ref2[k];
clearInterval(id);
results.push(clearTimeout(id));
}
return results;
}
}
};
}).call(this);