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.

app.js 6.0 KiB

13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. if(!console) {
  4. var console = {
  5. log: function(txt) {
  6. // suppress
  7. }
  8. }
  9. }
  10. $(document).ready(function() {
  11. wn.assets.check();
  12. wn.provide('wn.app');
  13. $.extend(wn.app, new wn.Application());
  14. });
  15. wn.Application = Class.extend({
  16. init: function() {
  17. this.load_startup();
  18. },
  19. load_startup: function() {
  20. var me = this;
  21. if(window.app) {
  22. return wn.call({
  23. method: 'startup',
  24. callback: function(r, rt) {
  25. wn.provide('wn.boot');
  26. wn.boot = r;
  27. if(wn.boot.profile.name==='Guest' || wn.boot.profile.user_type==="Website User") {
  28. window.location = 'index';
  29. return;
  30. }
  31. me.startup();
  32. }
  33. });
  34. } else {
  35. this.startup();
  36. }
  37. },
  38. startup: function() {
  39. // load boot info
  40. this.load_bootinfo();
  41. // page container
  42. this.make_page_container();
  43. // navbar
  44. this.make_nav_bar();
  45. // favicon
  46. this.set_favicon();
  47. if(user!="Guest") this.set_user_display_settings();
  48. this.setup_keyboard_shortcuts();
  49. // control panel startup code
  50. this.run_startup_js();
  51. if(wn.boot) {
  52. // route to home page
  53. wn.route();
  54. }
  55. // trigger app startup
  56. $(document).trigger('startup');
  57. this.start_notification_updates();
  58. $(document).trigger('app_ready');
  59. },
  60. set_user_display_settings: function() {
  61. wn.ui.set_user_background(wn.boot.profile.background_image);
  62. },
  63. load_bootinfo: function() {
  64. if(wn.boot) {
  65. wn.control_panel = wn.boot.control_panel;
  66. wn.modules = wn.boot.modules;
  67. this.check_metadata_cache_status();
  68. this.set_globals();
  69. this.sync_pages();
  70. } else {
  71. this.set_as_guest();
  72. }
  73. },
  74. check_metadata_cache_status: function() {
  75. if(wn.boot.metadata_version != localStorage.metadata_version) {
  76. localStorage.clear();
  77. console.log("Cleared Cache - New Metadata");
  78. wn.assets.init_local_storage();
  79. }
  80. },
  81. start_notification_updates: function() {
  82. var me = this;
  83. setInterval(function() {
  84. me.refresh_notifications();
  85. }, 30000);
  86. // first time loaded in boot
  87. $(document).trigger("notification-update");
  88. // refresh notifications if user is back after sometime
  89. $(document).on("session_alive", function() {
  90. me.refresh_notifications();
  91. })
  92. },
  93. refresh_notifications: function() {
  94. if(wn.session_alive) {
  95. return wn.call({
  96. method: "webnotes.core.doctype.notification_count.notification_count.get_notifications",
  97. callback: function(r) {
  98. if(r.message) {
  99. $.extend(wn.boot.notification_info, r.message);
  100. $(document).trigger("notification-update");
  101. }
  102. },
  103. no_spinner: true
  104. });
  105. }
  106. },
  107. set_globals: function() {
  108. // for backward compatibility
  109. profile = wn.boot.profile;
  110. user = wn.boot.profile.name;
  111. user_fullname = wn.user_info(user).fullname;
  112. user_defaults = profile.defaults;
  113. user_roles = profile.roles;
  114. user_email = profile.email;
  115. sys_defaults = wn.boot.sysdefaults;
  116. },
  117. sync_pages: function() {
  118. // clear cached pages if timestamp is not found
  119. if(localStorage["page_info"]) {
  120. wn.boot.allowed_pages = [];
  121. page_info = JSON.parse(localStorage["page_info"]);
  122. $.each(wn.boot.page_info, function(name, modified) {
  123. if(page_info[name]!=modified) {
  124. delete localStorage["_page:" + name];
  125. }
  126. wn.boot.allowed_pages.push(name);
  127. });
  128. } else {
  129. wn.boot.allowed_pages = keys(wn.boot.page_info);
  130. }
  131. localStorage["page_info"] = JSON.stringify(wn.boot.page_info);
  132. },
  133. set_as_guest: function() {
  134. // for backward compatibility
  135. profile = {name:'Guest'};
  136. user = 'Guest';
  137. user_fullname = 'Guest';
  138. user_defaults = {};
  139. user_roles = ['Guest'];
  140. user_email = '';
  141. sys_defaults = {};
  142. },
  143. make_page_container: function() {
  144. if($("#body_div").length) {
  145. $(".splash").remove();
  146. wn.temp_container = $("<div id='temp-container' style='display: none;'>")
  147. .appendTo("body");
  148. wn.container = new wn.views.Container();
  149. }
  150. },
  151. make_nav_bar: function() {
  152. // toolbar
  153. if(wn.boot) {
  154. wn.container.wntoolbar = new wn.ui.toolbar.Toolbar();
  155. }
  156. },
  157. logout: function() {
  158. var me = this;
  159. me.logged_out = true;
  160. return wn.call({
  161. method:'logout',
  162. callback: function(r) {
  163. if(r.exc) {
  164. console.log(r.exc);
  165. return;
  166. }
  167. me.redirect_to_login();
  168. }
  169. })
  170. },
  171. redirect_to_login: function() {
  172. window.location.href = 'index.html';
  173. },
  174. set_favicon: function() {
  175. var link = $('link[type="image/x-icon"]').remove().attr("href");
  176. $('<link rel="shortcut icon" href="' + link + '" type="image/x-icon">').appendTo("head");
  177. $('<link rel="icon" href="' + link + '" type="image/x-icon">').appendTo("head");
  178. },
  179. setup_keyboard_shortcuts: function() {
  180. $(document)
  181. .keydown("meta+g ctrl+g", function(e) {
  182. wn.ui.toolbar.search.show();
  183. return false;
  184. })
  185. .keydown("meta+s ctrl+s", function(e) {
  186. e.preventDefault();
  187. if(cur_frm) {
  188. cur_frm.save_or_update();
  189. } else if(wn.container.page.save_action) {
  190. wn.container.page.save_action();
  191. }
  192. return false;
  193. })
  194. .keydown("esc", function(e) {
  195. var open_row = $(".grid-row-open");
  196. if(open_row.length) {
  197. var grid_row = open_row.data("grid_row");
  198. grid_row.toggle_view(false);
  199. }
  200. return false;
  201. })
  202. .keydown("ctrl+down meta+down", function(e) {
  203. var open_row = $(".grid-row-open");
  204. if(open_row.length) {
  205. var grid_row = open_row.data("grid_row");
  206. grid_row.toggle_view(false, function() { grid_row.open_next() });
  207. return false;
  208. }
  209. })
  210. .keydown("ctrl+up meta+up", function(e) {
  211. var open_row = $(".grid-row-open");
  212. if(open_row.length) {
  213. var grid_row = open_row.data("grid_row");
  214. grid_row.toggle_view(false, function() { grid_row.open_prev() });
  215. return false;
  216. }
  217. })
  218. .keydown("ctrl+n meta+n", function(e) {
  219. var open_row = $(".grid-row-open");
  220. if(open_row.length) {
  221. var grid_row = open_row.data("grid_row");
  222. grid_row.toggle_view(false, function() { grid_row.grid.add_new_row(grid_row.doc.idx, null, true); });
  223. return false;
  224. }
  225. })
  226. },
  227. run_startup_js: function() {
  228. if(wn.boot.startup_js)
  229. eval(wn.boot.startup_js);
  230. }
  231. })