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

201 lines
7 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 topicPage');
2014-08-10 17:06:58 +00:00
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.maps.resetScroll(); // sets the scroll back to the top
2014-08-10 17:06:58 +00:00
Metamaps.Famous.explore.show();
2014-08-12 23:20:46 +00:00
Metamaps.Famous.maps.show();
2014-08-10 17:06:58 +00:00
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);
2014-08-12 23:20:46 +00:00
Metamaps.Famous.maps.hide();
2014-08-12 02:31:28 +00:00
setTimeout(function(){
Metamaps.Router.navigate("");
}, 500);
2014-08-10 17:06:58 +00:00
}
Metamaps.Famous.viz.hide();
Metamaps.Map.end();
Metamaps.Topic.end();
2014-08-10 17:06:58 +00:00
Metamaps.Active.Map = null;
2014-08-11 22:57:34 +00:00
Metamaps.Active.Topic = null;
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 topicPage');
2014-08-10 17:06:58 +00:00
$('.wrapper').addClass('explorePage');
Metamaps.currentSection = "explore";
Metamaps.currentPage = section;
Metamaps.Views.exploreMaps.setCollection( Metamaps.Maps[capitalize] );
if (Metamaps.Maps[capitalize].length === 0) {
2014-09-09 22:17:39 +00:00
Metamaps.Loading.show();
setTimeout(function(){
Metamaps.Maps[capitalize].getMaps(); // this will trigger an explore maps render
2014-08-12 22:14:04 +00:00
}, 300); // wait 300 milliseconds till the other animations are done to do the fetch
2014-08-10 17:06:58 +00:00
}
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
Metamaps.Famous.maps.resetScroll(); // sets the scroll back to the top
2014-08-12 23:20:46 +00:00
Metamaps.Famous.maps.show();
2014-08-10 17:06:58 +00:00
Metamaps.Famous.explore.set(section);
Metamaps.Famous.explore.show();
Metamaps.Famous.viz.hide();
Metamaps.Map.end();
Metamaps.Topic.end();
2014-08-10 17:06:58 +00:00
Metamaps.Active.Map = null;
2014-08-11 22:57:34 +00:00
Metamaps.Active.Topic = null;
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 topicPage');
2014-08-10 17:06:58 +00:00
$('.wrapper').addClass('mapPage');
2014-08-10 23:13:25 +00:00
Metamaps.Famous.yield.hide();
2014-08-12 23:20:46 +00:00
Metamaps.Famous.maps.hide();
2014-08-10 17:06:58 +00:00
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.Topic.end();
2014-08-11 22:57:34 +00:00
Metamaps.Active.Topic = null;
2014-08-10 17:06:58 +00:00
Metamaps.GlobalUI.Search.unlock();
Metamaps.GlobalUI.Search.close(0, true);
Metamaps.Map.end();
2014-08-10 17:06:58 +00:00
Metamaps.Map.launch(id);
2014-08-11 22:57:34 +00:00
},
topics: function (id) {
document.title = 'Topic ' + id + ' | Metamaps';
Metamaps.currentSection = "topic";
Metamaps.currentPage = id;
$('.wrapper').removeClass('homePage explorePage mapPage');
$('.wrapper').addClass('topicPage');
2014-08-11 22:57:34 +00:00
Metamaps.Famous.yield.hide();
2014-08-12 23:20:46 +00:00
Metamaps.Famous.maps.hide();
2014-08-11 22:57:34 +00:00
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.Map.end();
2014-08-11 22:57:34 +00:00
Metamaps.Active.Map = null;
Metamaps.GlobalUI.Search.unlock();
Metamaps.GlobalUI.Search.close(0, true);
Metamaps.Topic.end();
2014-08-11 22:57:34 +00:00
Metamaps.Topic.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
})();