Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

203 linhas
5.2 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // Navigation Object
  23. var nav_obj = {}
  24. nav_obj.observers = [];
  25. nav_obj.add_observer = function(o) { nav_obj.observers.push(o); }
  26. nav_obj.ol = [];
  27. //
  28. // notify my history so that it gets added to the back button history
  29. //
  30. nav_obj.open_notify = function(t, dt, dn, no_history) {
  31. // last should not be this (refresh)
  32. if(nav_obj.ol.length) {
  33. var tmp = nav_obj.ol[nav_obj.ol.length-1];
  34. if(tmp && tmp[0]==t && tmp[1]==dt && tmp[2]==dn) return;
  35. }
  36. if(!no_history) {
  37. // remove from history (if exists so that we can put it back on top)
  38. var tmp = [];
  39. for(var i in nav_obj.ol)
  40. if(!(nav_obj.ol[i][0]==t && nav_obj.ol[i][1]==dt && nav_obj.ol[i][2]==dn)) tmp.push(nav_obj.ol[i]);
  41. nav_obj.ol = tmp;
  42. // add to top
  43. nav_obj.ol.push([t, dt, dn])
  44. // encode
  45. en_t = encodeURIComponent(t);
  46. en_dt = encodeURIComponent(dt);
  47. en_dn = dn ? encodeURIComponent(dn) : '';
  48. if(en_t=='Page') {
  49. var id = en_dt + (dn ? ('/'+en_dn): '')
  50. } else {
  51. var id = en_t+'/'+ en_dt + (dn ? ('/'+en_dn): '')
  52. }
  53. // option to add to analytics engine
  54. if(nav_obj.on_open)
  55. nav_obj.on_open(id);
  56. // add to "back" history
  57. // replace state (to url)
  58. if(window.location.hash!='!' + id) {
  59. window.location.hash = '!' + id;
  60. }
  61. }
  62. nav_obj.notify_observers(t, dt, dn);
  63. if(wn.boot.analytics_code) {
  64. try {
  65. eval(wn.boot.analytics_code);
  66. } catch (e) {
  67. console.log(e);
  68. }
  69. }
  70. }
  71. // Notify observers
  72. // =========================================
  73. nav_obj.notify_observers = function(t, dt, dn) {
  74. // notify observers (for menu?)
  75. for(var i=0; i<nav_obj.observers.length; i++) {
  76. var o = nav_obj.observers[i];
  77. if(o && o.notify) o.notify(t, dt, dn);
  78. }
  79. }
  80. // Remame links (for save - name change)
  81. // =========================================
  82. nav_obj.rename_notify = function(dt, oldn, newn) {
  83. for(var i=0;i<nav_obj.ol.length;i++) {
  84. var o = nav_obj.ol[i];
  85. if(o[1]==dt && o[2]==oldn) o[2]=newn;
  86. }
  87. }
  88. nav_obj.show_last_open = function() {
  89. var l = nav_obj.ol[nav_obj.ol.length-2];
  90. delete nav_obj.ol[nav_obj.ol.length-1]; // delete current open
  91. if(!l) loadpage('_home');
  92. else if(l[0]=='Page') {
  93. loadpage(l[1]);
  94. } else if(l[0]=='Report') {
  95. loadreport(l[1],l[2]);
  96. } else if(l[0]=='Form') {
  97. loaddoc(l[1],l[2]);
  98. } else if(l[0]=='DocBrowser' || l[0]=='List') {
  99. loaddocbrowser(l[1]);
  100. }
  101. }
  102. var _history_current;
  103. function history_get_name(t) {
  104. var parts = [];
  105. if(t.length>=3) {
  106. // combine all else
  107. for(var i=2; i<t.length; i++) {
  108. parts.push(t[i]);
  109. }
  110. }
  111. return parts.join('/')
  112. }
  113. //
  114. // get the page details from the location
  115. //
  116. nav_obj.get_page = function(loc) {
  117. if(!loc) loc = window.location.hash;
  118. // remove exclamation for hash-bangs
  119. if(loc.substr(0,1)=='#') { loc = loc.substr(1); }
  120. if(loc.substr(0,1)=='!') { loc = loc.substr(1); }
  121. if(!in_list(['Page/', 'Form/', 'Repor', 'DocBr', 'List/', 'List2'], loc.substr(0,5))) {
  122. loc = 'Page/' + loc;
  123. }
  124. return loc.split('/');
  125. }
  126. //
  127. // function called when page is updated
  128. //
  129. function historyChange(newLocation) {
  130. var t = nav_obj.get_page(newLocation)
  131. for(var i=0;i<t.length;i++)
  132. t[i] = decodeURIComponent(t[i]);
  133. // re-opening the same page?
  134. if(nav_obj.ol.length) {
  135. var c = nav_obj.ol[nav_obj.ol.length-1];
  136. if(t.length==2) {
  137. if(c[0]==t[0] && c[1]==t[1]) return;
  138. } else {
  139. if(c[0]==t[0] && c[1]==t[1] && c[2]==t[2]) return;
  140. }
  141. }
  142. if(t[2])
  143. var docname = history_get_name(t);
  144. if(t[0]=='Form') {
  145. _history_current = newLocation;
  146. if(docname.substr(0, 3)=='New' && !(locals[t[1]] && locals[t[1]][docname])) {
  147. newdoc(t[1]);
  148. } else {
  149. loaddoc(t[1], docname);
  150. }
  151. } else if(t[0]=='Report') {
  152. _history_current = newLocation;
  153. loadreport(t[1], docname);
  154. } else if(t[0]=='Page') {
  155. _history_current = newLocation;
  156. loadpage(t[1]);
  157. } else if(t[0]=='Application') {
  158. _history_current = newLocation;
  159. loadapp(t[1]);
  160. } else if(t[0]=='DocBrowser' || t[0]=='List') {
  161. _history_current = newLocation;
  162. loaddocbrowser(t[1]);
  163. } else if(t[0]=='List2') {
  164. _history_current = newLocation;
  165. loaddocbrowser2(t[1]);
  166. }
  167. };
  168. $(window).bind('hashchange', function() {
  169. historyChange(location.hash);
  170. });