fix(minor): routing in various places to support v2 routing

This commit is contained in:
Rushabh Mehta 2020-11-11 10:59:50 +05:30 committed by Suraj Shetty
parent 243d5f7889
commit 029e757cc6
6 changed files with 79 additions and 39 deletions

View file

@ -29,16 +29,18 @@ page_js = {
# website
app_include_js = [
"assets/js/libs.min.js",
"assets/js/desk.min.js",
"assets/js/list.min.js",
"assets/js/form.min.js",
"assets/js/control.min.js",
"assets/js/report.min.js",
"/assets/js/libs.min.js",
"/assets/js/desk.min.js",
"/assets/js/list.min.js",
"/assets/js/form.min.js",
"/assets/js/control.min.js",
"/assets/js/report.min.js",
]
app_include_css = [
"assets/css/desk.min.css",
"assets/css/report.min.css",
"/assets/css/desk.min.css",
"/assets/css/list.min.css",
"/assets/css/form.min.css",
"/assets/css/report.min.css",
]
doctype_js = {

View file

@ -73,19 +73,9 @@ frappe.Application = Class.extend({
this.set_rtl();
if (frappe.boot) {
if (localStorage.getItem("session_last_route")) {
frappe.set_route(localStorage.getItem("session_last_route"));
localStorage.removeItem("session_last_route");
}
}
// page container
this.make_page_container();
// route to home page
frappe.route();
this.set_route();
// trigger app startup
$(document).trigger('startup');
@ -155,7 +145,7 @@ frappe.Application = Class.extend({
});
}, 300000); // check every 5 minutes
if(frappe.user.has_role("System Manager")){
if (frappe.user.has_role("System Manager")) {
setInterval(function() {
frappe.call({
method: 'frappe.core.doctype.log_settings.log_settings.has_unseen_error_log',
@ -164,7 +154,7 @@ frappe.Application = Class.extend({
},
callback: function(r) {
console.log(r);
if(r.message.show_alert){
if (r.message.show_alert) {
frappe.show_alert({
indicator: 'red',
message: r.message.message
@ -179,6 +169,19 @@ frappe.Application = Class.extend({
this.fetch_tags();
},
set_route() {
if (frappe.boot && localStorage.getItem("session_last_route")) {
frappe.set_route(localStorage.getItem("session_last_route"));
localStorage.removeItem("session_last_route");
} else if (frappe._cur_route) {
// go to the appropriate sub-path
frappe.set_route(frappe._cur_route);
} else {
// route to home page
frappe.route();
}
},
setup_frappe_vue() {
Vue.prototype.__ = window.__;
Vue.prototype.frappe = window.frappe;

View file

@ -16,22 +16,39 @@ frappe.route_hooks = {};
frappe._cur_route = null;
$(window).on('hashchange', function() {
// save the title
if (window.location.hash) {
console.log(window.location.hash);
let sub_path = frappe.get_sub_path(window.location.hash);
window.location.hash = '';
frappe.push_state(sub_path);
}
});
// routing v2, capture all clicks so that the target is managed with push-state
$('body').on('click', 'a', function(e) {
let override = (e, route) => {
e.preventDefault();
frappe.push_state(frappe.get_sub_path(route));
return false;
};
// target has "#" ,this is a v1 style route, so remake it.
if (e.target.hash) {
return override(e, e.target.hash);
}
// target has "/desk, this is a v2 style route.
if (e.target.pathname &&
(e.target.pathname.startWith('/desk') || e.target.pathname.startWith('desk'))) {
return override(e, e.target.pathname);
}
});
frappe.route = function() {
// Application is not yet initiated
if (!frappe.app) return;
let sub_path = frappe.get_route_str();
let sub_path = frappe.get_sub_path();
if (frappe.re_route[sub_path] !== undefined) {
// after saving a doc, for example,
@ -40,7 +57,7 @@ frappe.route = function() {
// it doesn't allow us to go back to the one prior to "New DocType 1"
// Hence if this check is true, instead of changing location hash,
// we just do a back to go to the doc previous to the "New DocType 1"
var re_route_val = frappe.get_route_str(frappe.re_route[sub_path]);
var re_route_val = frappe.get_sub_path(frappe.re_route[sub_path]);
if (decodeURIComponent(re_route_val) === decodeURIComponent(sub_path)) {
window.history.back();
return;
@ -105,7 +122,7 @@ frappe.route = function() {
frappe.get_route = function(route) {
// for app
route = frappe.get_raw_route_str(route).split('/');
route = frappe.get_sub_path_string(route).split('/');
route = $.map(route, frappe._decode_str);
var parts = null;
var doc_name = route[route.length - 1];
@ -150,16 +167,19 @@ frappe._decode_str = function(r) {
}
}
frappe.get_raw_route_str = function(route) {
frappe.get_sub_path_string = function(route) {
// return clean sub_path from hash or url
// supports both v1 and v2 routing
if (!route) {
route = window.location.hash;
}
if (!route && window.location.pathname.startsWith('/desk')) {
// route is without hash
route = window.location.pathname.substr(5);
if (!route) {
route = window.location.pathname;
}
if (route.substr(0, 1)=='/') route = route.substr(1);
if (route.startsWith('desk')) route = route.substr(4);
if (route.substr(0, 1)=='/') route = route.substr(1);
if (route.substr(0, 1)=='#') route = route.substr(1);
if (route.substr(0, 1)=='!') route = route.substr(1);
@ -168,8 +188,8 @@ frappe.get_raw_route_str = function(route) {
};
frappe.get_sub_path = frappe.get_route_str = function(route) {
var rawRoute = frappe.get_raw_route_str(route);
route = $.map(rawRoute.split('/'), frappe._decode_str).join('/');
var sub_path = frappe.get_sub_path_string(route);
route = $.map(sub_path.split('/'), frappe._decode_str).join('/');
return route;
};
@ -220,14 +240,23 @@ frappe.set_route = function() {
};
frappe.push_state = function (route) {
history.pushState(null, null, `/desk/${route}`);
frappe.route();
}
let url = `/desk/${route}`;
if (window.location.pathname !== url) {
// cleanup any remenants of v1 routing
window.location.hash = '';
// push state so the browser looks fine
history.pushState(null, null, url);
// now process the route
frappe.route();
}
};
frappe.set_re_route = function() {
var tmp = window.location.hash;
var tmp = frappe.get_sub_path();
frappe.set_route.apply(null, arguments);
frappe.re_route[tmp] = window.location.hash;
frappe.re_route[tmp] = frappe.get_sub_path();
};
frappe.has_route_options = function() {

View file

@ -252,6 +252,10 @@ def resolve_path(path):
if path != "index":
path = resolve_from_map(path)
if path.startswith("desk"):
path = "desk"
frappe.flags.desk_route = path[5:]
return path
def resolve_from_map(path):

View file

@ -47,6 +47,7 @@
if(!window.frappe) window.frappe = {};
frappe.boot = {{ boot }};
frappe._cur_route = "{{ desk_route }}";
frappe.csrf_token = "{{ csrf_token }}";

View file

@ -37,6 +37,7 @@ def get_context(context):
boot_json = re.sub("\<script\>[^<]*\</script\>", "", boot_json)
context.update({
"desk_route": frappe.flags.desk_route or "",
"no_cache": 1,
"build_version": get_build_version(),
"include_js": hooks["app_include_js"],