cartodb-4.42/lib/assets/javascripts/cartodb/common/router.js

61 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-04-06 13:25:13 +08:00
var _ = require('underscore-cdb-v3');
var Backbone = require('backbone-cdb-v3');
/**
* @extends http://backbonejs.org/#Router With some common functionality in the context of this app.
*/
var Router = Backbone.Router.extend({
/**
* Placeholder, is replaced by enableAfterMainView().
*/
navigate: function() {
throw new Error('router.enableAfterMainView({ ... }) must be called before you can navigate');
},
/**
* Enable router to monitor and manage browser URL and history.
* Expected to be called after main view as the function name indicates,
*/
enableAfterMainView: function() {
/**
* @override http://backbonejs.org/#Router-navigate Allow
* @param fragmentOrUrl {String} Either a fragment (e.g. '/dashboard/datasets') or a full URL
* (e.g. http://user.carto.com/dashboard/datasets), the navigate method takes care to route correctly.
*/
this.navigate = function(fragmentOrUrl, opts) {
Backbone.Router.prototype.navigate.call(this, this.normalizeFragmentOrUrl(fragmentOrUrl), opts);
};
Backbone.history.start({
pushState: true,
root: this.rootPath() + '/' //Yes, this trailing slash is necessary for the router to update the history state properly.
});
},
rootPath: function() {
throw new Error('implement rootPath in child router (no trailing slash)');
},
/**
* Normalise a given fragment or URL for navigation mechanisms to work.
* Typically, remove the leading base URL from the given fragment or URL.
*
* @param {String} fragmentOrUrl
* @return {String}
*/
normalizeFragmentOrUrl: function(fragmentOrUrl) {
throw new Error('implement normalizeFragmentOrUrl in child router');
}
});
Router.supportTrailingSlashes = function(obj) {
return _.reduce(obj, function(res, val, key) {
res[key] = val;
res[key + '/'] = val;
return res;
}, {});
};
module.exports = Router;