metamaps--metamaps/app/assets/javascripts/metamaps/Metamaps.Router.js

163 lines
5.4 KiB
JavaScript
Raw Normal View History

2014-08-02 02:50:23 +00:00
(function () {
2014-08-10 17:06:58 +00:00
Metamaps.currentPage = "";
2014-08-02 02:50:23 +00:00
var Router = Backbone.Router.extend({
routes: {
"": "home", // #home
"explore/:section": "explore", // #explore/active
"maps/:id": "maps" // #maps/7
},
2014-08-06 14:09:01 +00:00
home: function () {
2014-08-10 17:06:58 +00:00
if (Metamaps.Active.Mapper) document.title = 'My Maps | Metamaps';
else document.title = 'Home | Metamaps';
Metamaps.currentSection = "";
Metamaps.currentPage = "";
$('.wrapper').removeClass('mapPage');
var classes = Metamaps.Active.Mapper ? "homePage explorePage" : "homePage";
$('.wrapper').addClass(classes);
// all this only for the logged in home page
if (Metamaps.Active.Mapper) {
2014-08-10 23:13:25 +00:00
Metamaps.Famous.yield.hide();
2014-08-10 17:06:58 +00:00
Metamaps.Famous.explore.set('mine');
Metamaps.Famous.explore.show();
$('.mapsWrapper').fadeIn(300);
Metamaps.GlobalUI.Search.open();
Metamaps.GlobalUI.Search.lock();
Metamaps.Views.exploreMaps.setCollection( Metamaps.Maps.Mine );
if (Metamaps.Maps.Mine.length === 0) {
Metamaps.Maps.Mine.getMaps(); // this will trigger an explore maps render
}
else {
Metamaps.Views.exploreMaps.render();
}
}
// logged out home page
else {
2014-08-10 23:13:25 +00:00
Metamaps.Famous.yield.show();
2014-08-10 17:06:58 +00:00
Metamaps.Famous.explore.hide();
Metamaps.GlobalUI.Search.unlock();
Metamaps.GlobalUI.Search.close(0, true);
$('.mapsWrapper').fadeOut(300);
}
Metamaps.Famous.viz.hide();
Metamaps.Active.Map = null;
setTimeout(function(){
Metamaps.Router.navigate("");
}, 500);
2014-08-06 14:09:01 +00:00
},
2014-08-02 02:50:23 +00:00
explore: function (section) {
2014-08-06 14:09:01 +00:00
var capitalize = section.charAt(0).toUpperCase() + section.slice(1);
document.title = 'Explore ' + capitalize + ' Maps | Metamaps';
2014-08-10 17:06:58 +00:00
$('.wrapper').removeClass('homePage mapPage');
$('.wrapper').addClass('explorePage');
Metamaps.currentSection = "explore";
Metamaps.currentPage = section;
Metamaps.Views.exploreMaps.setCollection( Metamaps.Maps[capitalize] );
if (Metamaps.Maps[capitalize].length === 0) {
Metamaps.Maps[capitalize].getMaps(); // this will trigger an explore maps render
}
else {
Metamaps.Views.exploreMaps.render();
}
Metamaps.GlobalUI.Search.open();
Metamaps.GlobalUI.Search.lock();
2014-08-10 23:13:25 +00:00
Metamaps.Famous.yield.hide();
2014-08-10 17:06:58 +00:00
$('.mapsWrapper').fadeIn(300);
Metamaps.Famous.explore.set(section);
Metamaps.Famous.explore.show();
Metamaps.Famous.viz.hide();
Metamaps.Active.Map = null;
setTimeout(function(){
Metamaps.Router.navigate("/explore/" + section);
}, 500);
2014-08-02 02:50:23 +00:00
},
maps: function (id) {
2014-08-06 14:09:01 +00:00
document.title = 'Map ' + id + ' | Metamaps';
2014-08-10 17:06:58 +00:00
Metamaps.currentSection = "map";
Metamaps.currentPage = id;
$('.wrapper').removeClass('homePage explorePage');
$('.wrapper').addClass('mapPage');
2014-08-10 23:13:25 +00:00
Metamaps.Famous.yield.hide();
2014-08-10 17:06:58 +00:00
$('.mapsWrapper').fadeOut(300);
Metamaps.Famous.explore.hide();
// clear the visualization, if there was one, before showing its div again
if (Metamaps.Visualize.mGraph) {
Metamaps.Visualize.mGraph.graph.empty();
Metamaps.Visualize.mGraph.plot();
Metamaps.JIT.centerMap();
}
Metamaps.Famous.viz.show();
Metamaps.GlobalUI.Search.unlock();
Metamaps.GlobalUI.Search.close(0, true);
Metamaps.Map.launch(id);
2014-08-02 02:50:23 +00:00
}
});
2014-08-10 17:06:58 +00:00
2014-08-02 02:50:23 +00:00
Metamaps.Router = new Router();
2014-08-10 17:06:58 +00:00
2014-08-02 02:50:23 +00:00
Metamaps.Router.init = function () {
2014-08-06 14:09:01 +00:00
Backbone.history.start({
2014-08-10 17:06:58 +00:00
silent: true,
2014-08-02 02:50:23 +00:00
pushState: true,
2014-08-06 14:09:01 +00:00
root: '/'
2014-08-02 02:50:23 +00:00
});
$(document).on("click", "a:not([data-bypass])", function (evt) {
2014-08-10 17:06:58 +00:00
var segments;
2014-08-02 02:50:23 +00:00
var href = {
prop: $(this).prop("href"),
attr: $(this).attr("href")
};
var root = location.protocol + "//" + location.host + Backbone.history.options.root;
2014-08-06 14:09:01 +00:00
if (href.prop && href.prop === root) href.attr = ""
2014-08-02 02:50:23 +00:00
if (href.prop && href.prop.slice(0, root.length) === root) {
evt.preventDefault();
2014-08-10 17:06:58 +00:00
segments = href.attr.split('/');
segments.splice(0,1); // pop off the element created by the first /
if (href.attr === "") Metamaps.Router.home();
else {
Metamaps.Router[segments[0]](segments[1]);
}
2014-08-02 02:50:23 +00:00
}
2014-08-06 14:09:01 +00:00
});
2014-08-02 02:50:23 +00:00
}
2014-08-02 17:49:51 +00:00
})();