25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.js 5.9 KiB

13 년 전
13 년 전
13 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  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.html';
  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_custom_startup_code();
  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. if(wn.boot.profile.background_image) {
  62. wn.ui.set_user_background(wn.boot.profile.background_image);
  63. } else {
  64. $("body").css("background-color", "#F5EFE6")
  65. }
  66. },
  67. load_bootinfo: function() {
  68. if(wn.boot) {
  69. wn.control_panel = wn.boot.control_panel;
  70. wn.modules = wn.boot.modules;
  71. this.set_globals();
  72. this.sync_pages();
  73. } else {
  74. this.set_as_guest();
  75. }
  76. },
  77. start_notification_updates: function() {
  78. var me = this;
  79. setInterval(function() {
  80. me.refresh_notifications();
  81. }, 30000);
  82. // first time loaded in boot
  83. $(document).trigger("notification-update");
  84. // refresh notifications if user is back after sometime
  85. $(document).on("session_alive", function() {
  86. me.refresh_notifications();
  87. })
  88. },
  89. refresh_notifications: function() {
  90. if(wn.session_alive) {
  91. return wn.call({
  92. method: "webnotes.widgets.notification.get",
  93. callback: function(r) {
  94. if(r.message) {
  95. $.extend(wn.boot.notification_info, r.message);
  96. $(document).trigger("notification-update");
  97. }
  98. },
  99. no_spinner: true
  100. });
  101. }
  102. },
  103. set_globals: function() {
  104. // for backward compatibility
  105. profile = wn.boot.profile;
  106. user = wn.boot.profile.name;
  107. user_fullname = wn.user_info(user).fullname;
  108. user_defaults = profile.defaults;
  109. user_roles = profile.roles;
  110. user_email = profile.email;
  111. sys_defaults = wn.boot.sysdefaults;
  112. },
  113. sync_pages: function() {
  114. // clear cached pages if timestamp is not found
  115. if(localStorage["page_info"]) {
  116. wn.boot.allowed_pages = [];
  117. page_info = JSON.parse(localStorage["page_info"]);
  118. $.each(wn.boot.page_info, function(name, modified) {
  119. if(page_info[name]!=modified) {
  120. delete localStorage["_page:" + name];
  121. }
  122. wn.boot.allowed_pages.push(name);
  123. });
  124. } else {
  125. wn.boot.allowed_pages = keys(wn.boot.page_info);
  126. }
  127. localStorage["page_info"] = JSON.stringify(wn.boot.page_info);
  128. },
  129. set_as_guest: function() {
  130. // for backward compatibility
  131. profile = {name:'Guest'};
  132. user = 'Guest';
  133. user_fullname = 'Guest';
  134. user_defaults = {};
  135. user_roles = ['Guest'];
  136. user_email = '';
  137. sys_defaults = {};
  138. },
  139. make_page_container: function() {
  140. if($("#body_div").length) {
  141. $(".splash").remove();
  142. wn.temp_container = $("<div id='temp-container' style='display: none;'>")
  143. .appendTo("body");
  144. wn.container = new wn.views.Container();
  145. }
  146. },
  147. make_nav_bar: function() {
  148. // toolbar
  149. if(wn.boot) {
  150. wn.container.wntoolbar = new wn.ui.toolbar.Toolbar();
  151. }
  152. },
  153. logout: function() {
  154. var me = this;
  155. me.logged_out = true;
  156. return wn.call({
  157. method:'logout',
  158. callback: function(r) {
  159. if(r.exc) {
  160. console.log(r.exc);
  161. return;
  162. }
  163. me.redirect_to_login();
  164. }
  165. })
  166. },
  167. redirect_to_login: function() {
  168. window.location.href = 'index.html';
  169. },
  170. set_favicon: function() {
  171. var link = $('link[type="image/x-icon"]').remove().attr("href");
  172. $('<link rel="shortcut icon" href="' + link + '" type="image/x-icon">').appendTo("head");
  173. $('<link rel="icon" href="' + link + '" type="image/x-icon">').appendTo("head");
  174. },
  175. setup_keyboard_shortcuts: function() {
  176. $(document)
  177. .keydown("meta+g ctrl+g", function(e) {
  178. wn.ui.toolbar.search.show();
  179. return false;
  180. })
  181. .keydown("meta+s ctrl+s", function(e) {
  182. if(cur_frm) {
  183. cur_frm.save_or_update();
  184. } else if(wn.container.page.save_action) {
  185. wn.container.page.save_action();
  186. }
  187. return false;
  188. })
  189. .keydown("esc", function(e) {
  190. var open_row = $(".grid-row-open");
  191. if(open_row.length) {
  192. var grid_row = open_row.data("grid_row");
  193. grid_row.toggle_view(false);
  194. }
  195. return false;
  196. })
  197. .keydown("ctrl+down meta+down", function(e) {
  198. var open_row = $(".grid-row-open");
  199. if(open_row.length) {
  200. var grid_row = open_row.data("grid_row");
  201. grid_row.toggle_view(false, function() { grid_row.open_next() });
  202. return false;
  203. }
  204. })
  205. .keydown("ctrl+up meta+up", function(e) {
  206. var open_row = $(".grid-row-open");
  207. if(open_row.length) {
  208. var grid_row = open_row.data("grid_row");
  209. grid_row.toggle_view(false, function() { grid_row.open_prev() });
  210. return false;
  211. }
  212. })
  213. .keydown("ctrl+n meta+n", function(e) {
  214. var open_row = $(".grid-row-open");
  215. if(open_row.length) {
  216. var grid_row = open_row.data("grid_row");
  217. grid_row.toggle_view(false, function() { grid_row.grid.add_new_row(grid_row.doc.idx, null, true); });
  218. return false;
  219. }
  220. })
  221. },
  222. run_custom_startup_code: function() {
  223. if(wn.control_panel.custom_startup_code)
  224. eval(wn.control_panel.custom_startup_code);
  225. }
  226. })