Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shawn Allen
committed
Dec 13, 2011
1 parent
841c133
commit 95e7a24
Showing
5 changed files
with
239 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
|
||
var HAS_HASHCHANGE = (function() { | ||
var doc_mode = window.documentMode; | ||
return ('onhashchange' in window) | ||
&& (doc_mode === undefined || doc_mode > 7); | ||
})(); | ||
|
||
MM.Hash = function(map) { | ||
this.onMapMove = MM.bind(this.onMapMove, this); | ||
this.onHashChange = MM.bind(this.onHashChange, this); | ||
if (map) { | ||
this.init(map); | ||
} | ||
}; | ||
|
||
MM.Hash.prototype = { | ||
map: null, | ||
lastHash: null, | ||
|
||
parseHash: function(hash) { | ||
var args = hash.split("/"); | ||
if (args.length == 3) { | ||
var zoom = parseInt(args[0]), | ||
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("/"); | ||
}, | ||
|
||
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) { | ||
if (this.movingMap) { | ||
return false; | ||
} | ||
var hash = this.formatHash(map); | ||
if (this.lastHash != hash) { | ||
location.replace(hash); | ||
this.lastHash = hash; | ||
} | ||
}, | ||
|
||
onHashChange: 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.onMapMove(this.map); | ||
} | ||
}, | ||
|
||
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; | ||
} | ||
}; |