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.
 
 
 
 
 
 

106 line
2.3 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // page container
  4. wn.provide('wn.pages');
  5. wn.provide('wn.views');
  6. wn.views.Container = Class.extend({
  7. _intro: "Container contains pages inside `#container` and manages \
  8. page creation, switching",
  9. init: function() {
  10. this.container = $('#body_div').get(0);
  11. this.page = null; // current page
  12. this.pagewidth = $('#body_div').width();
  13. this.pagemargin = 50;
  14. },
  15. add_page: function(label, onshow, onhide) {
  16. var page = $('<div class="content"></div>')
  17. .attr('id', "page-" + label)
  18. .toggle(false)
  19. .appendTo(this.container).get(0);
  20. if(onshow)
  21. $(page).bind('show', onshow);
  22. if(onshow)
  23. $(page).bind('hide', onhide);
  24. page.label = label;
  25. wn.pages[label] = page;
  26. return page;
  27. },
  28. change_to: function(label) {
  29. if(this.page && this.page.label == label) {
  30. // don't trigger double events
  31. $(this.page).trigger('show');
  32. return;
  33. }
  34. var me = this;
  35. if(label.tagName) {
  36. // if sent the div, get the table
  37. var page = label;
  38. } else {
  39. var page = wn.pages[label];
  40. }
  41. if(!page) {
  42. console.log(wn._('Page not found')+ ': ' + label);
  43. return;
  44. }
  45. // hide dialog
  46. if(cur_dialog && cur_dialog.display && !cur_dialog.keep_open) {
  47. cur_dialog.hide();
  48. }
  49. // hide current
  50. if(this.page && this.page != page) {
  51. $(this.page).toggle(false);
  52. $(this.page).trigger('hide');
  53. }
  54. // show new
  55. if(!this.page || this.page != page) {
  56. this.page = page;
  57. //$(this.page).fadeIn();
  58. $(this.page).toggle(true);
  59. }
  60. $(document).trigger("page-change");
  61. this.page._route = window.location.hash;
  62. $(this.page).trigger('show');
  63. scroll(0,0);
  64. return this.page;
  65. }
  66. });
  67. wn.views.Factory = Class.extend({
  68. init: function(opts) {
  69. $.extend(this, opts);
  70. },
  71. show: function() {
  72. var page_name = wn.get_route_str(),
  73. me = this;
  74. if(wn.pages[page_name]) {
  75. wn.container.change_to(wn.pages[page_name]);
  76. } else {
  77. var route = wn.get_route();
  78. if(route[1]) {
  79. me.make(route);
  80. } else {
  81. wn.show_not_found(route);
  82. }
  83. }
  84. },
  85. make_page: function(double_column) {
  86. var page_name = wn.get_route_str(),
  87. page = wn.container.add_page(page_name);
  88. wn.ui.make_app_page({
  89. parent: page,
  90. single_column: !double_column
  91. });
  92. wn.container.change_to(page_name);
  93. return page;
  94. }
  95. })