You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

router.js 2.5 KiB

13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // route urls to their virtual pages
  4. // re-route map (for rename)
  5. wn.re_route = {};
  6. wn.route_titles = {};
  7. wn.route_history = [];
  8. wn.view_factory = {};
  9. wn.view_factories = [];
  10. wn.route = function() {
  11. if(wn.re_route[window.location.hash]) {
  12. // after saving a doc, for example,
  13. // "New DocType 1" and the renamed "TestDocType", both exist in history
  14. // now if we try to go back,
  15. // it doesn't allow us to go back to the one prior to "New DocType 1"
  16. // Hence if this check is true, instead of changing location hash,
  17. // we just do a back to go to the doc previous to the "New DocType 1"
  18. var re_route_val = wn.get_route_str(wn.re_route[window.location.hash]);
  19. var cur_route_val = wn.get_route_str(wn._cur_route);
  20. if (decodeURIComponent(re_route_val) === decodeURIComponent(cur_route_val)) {
  21. window.history.back();
  22. return;
  23. } else {
  24. window.location.hash = wn.re_route[window.location.hash];
  25. }
  26. }
  27. wn._cur_route = window.location.hash;
  28. route = wn.get_route();
  29. wn.route_history.push(route);
  30. if(route[0] && wn.views[route[0] + "Factory"]) {
  31. // has a view generator, generate!
  32. if(!wn.view_factory[route[0]])
  33. wn.view_factory[route[0]] = new wn.views[route[0] + "Factory"]();
  34. wn.view_factory[route[0]].show();
  35. } else {
  36. // show page
  37. wn.views.pageview.show(route[0]);
  38. }
  39. if(wn.route_titles[window.location.hash]) {
  40. document.title = wn.route_titles[window.location.hash];
  41. }
  42. }
  43. wn.get_route = function(route) {
  44. // for app
  45. return wn.get_route_str(route).split('/')
  46. }
  47. wn.get_route_str = function(route) {
  48. if(!route)
  49. route = window.location.hash;
  50. if(route.substr(0,1)=='#') route = route.substr(1);
  51. if(route.substr(0,1)=='!') route = route.substr(1);
  52. route = $.map(route.split('/'),
  53. function(r) { return decodeURIComponent(r); }).join('/');
  54. return route;
  55. }
  56. wn.set_route = function() {
  57. route = $.map(arguments, function(a) {
  58. if($.isPlainObject(a)) {
  59. wn.route_options = a;
  60. return null;
  61. } else {
  62. return a ? encodeURIComponent(a) : null;
  63. }
  64. }).join('/');
  65. window.location.hash = route;
  66. // Set favicon (app.js)
  67. wn.app.set_favicon();
  68. }
  69. wn._cur_route = null;
  70. $(window).on('hashchange', function() {
  71. // save the title
  72. wn.route_titles[wn._cur_route] = document.title;
  73. if(window.location.hash==wn._cur_route)
  74. return;
  75. // hide open dialog
  76. if(cur_dialog && cur_dialog.hide_on_page_refresh)
  77. cur_dialog.hide();
  78. wn.route();
  79. });