Skip to content

Commit

Permalink
Improved hash
Browse files Browse the repository at this point in the history
  • Loading branch information
dhcole committed May 23, 2012
1 parent a22b6f2 commit c68e61a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
5 changes: 3 additions & 2 deletions ext/wax/wax.ext.js
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions index.html
Expand Up @@ -78,7 +78,7 @@ <h2>See your neighborhood</h2>
zoom: 13,
ease: 1000
},
group: 1
group: 0
},
recipients: {
api: 'http://a.tiles.mapbox.com/v3/awidercircle.awc-addresses-rec.jsonp',
Expand All @@ -88,7 +88,7 @@ <h2>See your neighborhood</h2>
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',
Expand All @@ -98,7 +98,7 @@ <h2>See your neighborhood</h2>
zoom: 12,
ease: 2000
},
group: 2
group: 1
},
zip: {
api: 'http://a.tiles.mapbox.com/v3/awidercircle.awc_don_rec_zip08.jsonp',
Expand All @@ -108,7 +108,7 @@ <h2>See your neighborhood</h2>
zoom: 11,
ease: 1000
},
group: 2
group: 1
}
});
</script>
Expand Down
68 changes: 42 additions & 26 deletions script.js
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down

0 comments on commit c68e61a

Please sign in to comment.