-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
326 lines (293 loc) · 10.5 KB
/
index.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="modestmaps.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" src="jquery.ba-hashchange.min.js"></script>
<script type="text/javascript">
// <!--
var hash = decodeURI(location.hash.substring(1)),
coords,
map,
mm = com.modestmaps,
default_location = {
center: new mm.Location(37.76491, -122.41946),
zoom:13
},
url_delimiter = "!",
layer,
url = "http://tile.stamen.com/toner/{Z}/{X}/{Y}.png",
tileseturl = url;
$(function() {
$("#revealDialog").click(function(){
console.info("revealDialog clicked", url);
showDialog("Insert a mustache syntax URL here", url);
});
$("#close").click(function(){
$("#dialog").hide();
$("#revealDialog").show();
});
if (hash && hash.indexOf(url_delimiter) != -1) {
var args = hash.split(url_delimiter);
coords = args[0];
url = args[1];
} else {
console.info("hash", hash, hash.indexOf(url_delimiter));
url = "http://tile.stamen.com/toner/{Z}/{X}/{Y}.png";
showDialog("No URL yet so we're using Toner. Want to add one?", url);
//loadMap(url, default_location);
//return;
}
// parse url and test for validity
if (!isValidURL(url)) {
showDialog("Our URL Validatron says your URL isn't valid. Want to fix it?", url);
//return;
} else if (!isValidMustache(url)) {
url = url || "http://tile.stamen.com/toner/{Z}/{X}/{Y}.png";
showDialog("We use mustaches here. You should too (ie. replace {Z} for zoom, {X} for latitude, and {Y} for longitude. An example is for Toner which is the default tileset: <em>http://tile.stamen.com/toner/{Z}/{X}/{Y}.png</em> Want to fix it?", url);
//return;
} else {
if (coords) {
var cc = coords.split("/"),
loc = {center:new mm.Location(cc[1],cc[2]),
zoom:cc[0]};
loadMap(url,loc);
} else {
loadMap(url, default_location);
}
}
});
function loadMap(url, loc) {
console.info("loadMap()", url, loc, map, layer);
//loc = loc || map.getCenter() || {center:new mm.Location(37.811530, -122.2666097), zoom:13};
//console.info("loc", loc);
if (!loc && map) {
loc = {
center: map.getCenter(),
zoom: map.getZoom()
};
}
console.info("loc", loc);
loc = typeof loc !== "undefined" ?
loc :
default_location;
//console.info("loc",loc);
if (map && layer && typeof map !== "undefined" && map !== "null") {
console.info("update layer", layer, url);
layer.setProvider(new mm.TemplatedMapProvider(url));
//var layer = new mm.Layer(new mm.TemplatedMapProvider(url));
//map.removeLayerAt(0);
//map.insertLayerAt(0,layer);
} else {
console.info("create new layer", map, layer);
layer = new mm.Layer(new mm.TemplatedMapProvider(url));
/*
var interaction = [
new MM.DragHandler(map),
new MM.DoubleClickHandler(map)
];
*/
console.info("layer", layer);
map = new mm.Map("map", layer, null);
}
map.setCenterZoom(loc.center, loc.zoom);
var hashhash = new mm.MyHash(map);
tileseturl = url;
}
function showDialog(text, url) {
console.info("showDialog()", text, url);
$("#dialog input.textinput").attr("value", url);
$("#dialog > span.text").html(text);
$("#dialog").show();
$("#revealDialog").hide();
}
function submitclicked() {
console.info("submit clicked");
url = unescape($("input.textinput").val());
// validate url
if (isValidURL(url) && isValidMustache(url)) {
console.info("valid url:", url);
loadMap(url, undefined);
$("#dialog").hide();
$("#revealDialog").show();
} else {
console.info("not valid url:", url);
//show alert with error.
}
}
function isValidURL(url){
return /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(url);
}
function isValidMustache(url) {
return (url.indexOf("{Z}") > 0 &&
url.indexOf("{Y}") > 0 &&
url.indexOf("{X}") > 0);
}
// -->
</script>
<style type="text/css">
body {font-family:"Helvetica Neue", Helvetica, sans-serif; margin:0;}
#map {width:100%; height:100%; position:absolute; overflow:hidden; top:0; left:0;}
#dialog {width:600px; margin:0 auto; background:#06F; text-align:left; display:none; padding:15px; position:relative; color:white;}
#dialog .title {font-size:36px; font-weight:bold; margin-bottom:10px;}
#dialog span.text {font-weight:bold; }
#dialog input.textinput {width:500px; font-size:14px; font-weight:bold; color:#666; border:0; padding:7px 10px; margin-top:10px;}
#dialog button.submitbutton {background-color:#06D; border:2px solid white; padding:5px 8px; font-size:14px; cursor:pointer; color:white;}
#dialog button.submitbutton:hover {color:#333; border-color:#333;}
#dialog #close {float:right; cursor:pointer; font-size:40px; margin-top:-15px;}
#dialog #close:hover {color:#333;}
#revealDialog {position:fixed; top:0; left:0; color:white; padding:0px 20px 3px 17px; font-size:40px; background-color:#06F; opacity:0.5; cursor:pointer;}
#revealDialog:hover {opacity:1; color:#333;}
</style>
</head>
<body defer>
<div id="map"></div>
<div id="dialog">
<span id="close">×</span>
<div class="title">TilesetViewer</div>
<span class="text">message here</span><br>
<form>
<input class="textinput" type="text" value="" />
<button class="submitbutton" onClick="submitclicked()">Map it!</button>
</form>
</div>
<div id="revealDialog">⁝</div>
</body>
<script type="text/javascript">
// namespacing!
if (!com) {
var com = {};
}
if (!com.modestmaps) {
com.modestmaps = {};
}
(function(MM) {
var HAS_HASHCHANGE = (function() {
var doc_mode = window.documentMode;
return ("onhashchange" in window) &&
(doc_mode === undefined || doc_mode > 7);
})();
MM.MyHash = function(map) {
this.onMapMove = MM.bind(this.onMapMove, this);
this.onHashChange = MM.bind(this.onHashChange, this);
if (map) {
this.init(map);
}
};
MM.MyHash.prototype = {
map: null,
lastHash: null,
parseHash: function(hash) {
var args = hash.split("/");
if (args.length == 3) {
var zoom = parseInt(args[0], 10),
lat = parseFloat(args[1]),
lon = parseFloat(args[2]);
if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) {
return false;
} else {
return {
center: new MM.Location(lat, lon),
zoom: zoom
};
}
} else {
return false;
}
},
formatHash: function(map) {
var center = map.getCenter(),
zoom = map.getZoom(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
return "#" + [zoom,
center.lat.toFixed(precision),
center.lon.toFixed(precision)
].join("/") + "!" + tileseturl;
},
init: function(map) {
this.map = map;
this.map.addCallback("drawn", this.onMapMove);
// reset the hash
this.lastHash = null;
this.onHashChange();
if (!this.isListening) {
this.startListening();
}
},
remove: function() {
this.map = null;
if (this.isListening) {
this.stopListening();
}
},
onMapMove: function(map) {
// bail if we're moving the map (updating from a hash),
// or if the map has no zoom set
if (this.movingMap || this.map.zoom === 0) {
return false;
}
var hash = this.formatHash(map);
if (this.lastHash != hash) {
location.replace(hash);
this.lastHash = hash;
}
},
movingMap: false,
update: function() {
var hash = location.hash;
if (hash === this.lastHash) {
// console.info("(no change)");
return;
}
var sansHash = hash.substr(1),
parsed = this.parseHash(sansHash);
if (parsed) {
// console.log("parsed:", parsed.zoom, parsed.center.toString());
this.movingMap = true;
this.map.setCenterZoom(parsed.center, parsed.zoom);
this.movingMap = false;
} else {
// console.warn("parse error; resetting:", this.map.getCenter(), this.map.getZoom());
this.onMapMove(this.map);
}
},
// defer hash change updates every 100ms
changeDefer: 100,
changeTimeout: null,
onHashChange: function() {
// throttle calls to update() so that they only happen every
// `changeDefer` ms
if (!this.changeTimeout) {
var that = this;
this.changeTimeout = setTimeout(function() {
that.update();
that.changeTimeout = null;
}, this.changeDefer);
}
},
isListening: false,
hashChangeInterval: null,
startListening: function() {
if (HAS_HASHCHANGE) {
window.addEventListener("hashchange", this.onHashChange, false);
} else {
clearInterval(this.hashChangeInterval);
this.hashChangeInterval = setInterval(this.onHashChange, 50);
}
this.isListening = true;
},
stopListening: function() {
if (HAS_HASHCHANGE) {
window.removeEventListener("hashchange", this.onHashChange);
} else {
clearInterval(this.hashChangeInterval);
}
this.isListening = false;
}
};
})(com.modestmaps);
</script>
</html>