Permalink
Browse files

Improved hash

  • Loading branch information...
1 parent a22b6f2 commit c68e61a56c9a03eccd88ad8ede0fe2c81abc2ac0 @dhcole dhcole committed May 23, 2012
Showing with 49 additions and 32 deletions.
  1. +3 −2 ext/wax/wax.ext.js
  2. +4 −4 index.html
  3. +42 −26 script.js
View
5 ext/wax/wax.ext.js
@@ -95,8 +95,9 @@ wax.mm = wax.mm || {};
wax.g.share =
wax.mm.share = function(map, tilejson) {
tilejson = tilejson || {};
- tilejson.webpage = window.location.toString().split('#')[0];
- tilejson.embed = window.location.toString().split('#')[0] + '#embed';
+ var l = window.location;
+ tilejson.webpage = l.href;
+ tilejson.embed = (l.hash) ? l.href + '?embed' : l.href + '#/?embed';
var link = document.createElement('a');
var close = document.createElement('a');
View
8 index.html
@@ -78,7 +78,7 @@
zoom: 13,
ease: 1000
},
- group: 1
+ group: 0
},
recipients: {
api: 'http://a.tiles.mapbox.com/v3/awidercircle.awc-addresses-rec.jsonp',
@@ -88,7 +88,7 @@
zoom: 14,
ease: 2000
},
- group: 1
+ group: 0
},
both: {
api: 'http://a.tiles.mapbox.com/v3/awidercircle.awc-addresses-don,awidercircle.awc-addresses-rec.jsonp',
@@ -98,7 +98,7 @@
zoom: 12,
ease: 2000
},
- group: 2
+ group: 1
},
zip: {
api: 'http://a.tiles.mapbox.com/v3/awidercircle.awc_don_rec_zip08.jsonp',
@@ -108,7 +108,7 @@
zoom: 11,
ease: 1000
},
- group: 2
+ group: 1
}
});
</script>
View
68 script.js
@@ -154,28 +154,32 @@
};
Map.parseHash = function() {
+ var pattern = /(?:#([^\?]*))?(?:\?(.*))?$/,
+ components = window.location.href.match(pattern);
- // TODO: update the embed hash and website url
-
- var hashes = window.location.hash.split('#');
- $.each(hashes, function(index, hash) {
- if (hash === 'embed') {
- $('body').removeClass().addClass('embed');
- } else if (hash.substring(0,1) === '!') {
- var ids = decodeURIComponent(hash.substring(1)).split('/');
- $.each(ids, function(i, layer) {
- if (layer !== '-') {
- Map.layerGroups[i] = {
- id: layer,
- api: layers[layer].api
- };
- }
- });
- }
- });
- if (cleanArray(Map.layerGroups).length > 0) {
- Map.setOverlay();
+ if (components && components[2] === 'embed') {
+ $('body').removeClass().addClass('embed');
+ window.location.replace(window.location.href.split('?')[0]);
+ }
+
+ if (components && components[1]) {
+ var hash = components[1];
+ if (hash.substr(0, 1) === '/') hash = hash.substring(1);
+ if (hash.substr(hash.length - 1, 1) === '/') hash = hash.substr(0, hash.length - 1);
+
+ ids = decodeURIComponent(hash).split('/');
+
+
+ $.each(ids, function(i, layer) {
+ if (layer !== '-' && layers[layer]) {
+ Map.layerGroups[i] = {
+ id: layer,
+ api: layers[layer].api
+ };
+ }
+ });
}
+ if (cleanArray(Map.layerGroups).length > 0) Map.setOverlay();
};
Map.setHash = function() {
@@ -187,13 +191,25 @@
});
var l = window.location,
- state = '#!' + hash.join('/');
+ baseUrl = l.href,
+ state = (hash.length > 0) ? '#/' + hash.join('/') : '';
- if (l.hash) {
- l.replace(l.toString().split('#')[0] + state);
- } else {
- l.replace(l.toString() + state);
- }
+ if (baseUrl.indexOf('?') >= 0) baseUrl = baseUrl.split('?')[0];
+ if (baseUrl.indexOf('#') >= 0) baseUrl = baseUrl.split('#')[0];
+
+ l.replace(baseUrl + state);
+
+ // Update share urls
+ var webpage = l.href;
+ var embed = (l.hash) ? l.href + '?embed' : l.href + '#/?embed';
+
+ $('.wax-share textarea.embed').val(
+ '<iframe width="500" height="300" frameBorder="0" src="{{embed}}"></iframe>'
+ .replace('{{embed}}', embed));
+ $('.wax-share a.twitter').attr('href', 'http://twitter.com/intent/tweet?status='
+ + encodeURIComponent(document.title + ' (' + webpage + ')'));
+ $('.wax-share a.facebook').attr('href', 'https://www.facebook.com/sharer.php?u='
+ + encodeURIComponent(webpage) + '&t=' + encodeURIComponent(document.title));
};
root.Map = Map;

0 comments on commit c68e61a

Please sign in to comment.