您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

197 行
4.2 KiB

  1. // Copyright 2013 Web Notes Technologies Pvt Ltd
  2. // MIT Licensed. 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._toc = [
  16. "wn.model",
  17. "wn.ui",
  18. "wn.utils",
  19. "wn.views"
  20. ]
  21. wn.Application = Class.extend({
  22. init: function() {
  23. this.load_startup();
  24. },
  25. load_startup: function() {
  26. var me = this;
  27. if(window.app) {
  28. wn.call({
  29. method: 'startup',
  30. callback: function(r, rt) {
  31. wn.provide('wn.boot');
  32. wn.boot = r;
  33. if(wn.boot.profile.name=='Guest') {
  34. window.location = 'index.html';
  35. return;
  36. }
  37. me.startup();
  38. }
  39. })
  40. } else {
  41. this.startup();
  42. }
  43. },
  44. startup: function() {
  45. // load boot info
  46. this.load_bootinfo();
  47. // page container
  48. this.make_page_container();
  49. // navbar
  50. this.make_nav_bar();
  51. // favicon
  52. this.set_favicon();
  53. if(user!="Guest") this.set_user_display_settings();
  54. // trigger app startup
  55. $(document).trigger('startup');
  56. if(wn.boot) {
  57. // route to home page
  58. wn.route();
  59. }
  60. this.start_notification_updates();
  61. $(document).trigger('app_ready');
  62. },
  63. set_user_display_settings: function() {
  64. if(wn.boot.profile.background_image) {
  65. wn.ui.set_user_background(wn.boot.profile.background_image);
  66. }
  67. },
  68. load_bootinfo: function() {
  69. if(wn.boot) {
  70. wn.control_panel = wn.boot.control_panel;
  71. wn.modules = wn.boot.modules;
  72. this.set_globals();
  73. this.sync_pages();
  74. } else {
  75. this.set_as_guest();
  76. }
  77. },
  78. start_notification_updates: function() {
  79. var me = this;
  80. setInterval(function() {
  81. me.refresh_notifications();
  82. }, 30000);
  83. // first time loaded in boot
  84. $(document).trigger("notification-update");
  85. // refresh notifications if user is back after sometime
  86. $(document).on("session_alive", function() {
  87. me.refresh_notifications();
  88. })
  89. },
  90. refresh_notifications: function() {
  91. if(wn.session_alive) {
  92. wn.call({
  93. method: "webnotes.widgets.notification.get",
  94. callback: function(r) {
  95. if(r.message) {
  96. $.extend(wn.boot.notification_info, r.message);
  97. $(document).trigger("notification-update");
  98. }
  99. },
  100. no_spinner: true
  101. });
  102. }
  103. },
  104. set_globals: function() {
  105. // for backward compatibility
  106. profile = wn.boot.profile;
  107. user = wn.boot.profile.name;
  108. user_fullname = wn.user_info(user).fullname;
  109. user_defaults = profile.defaults;
  110. user_roles = profile.roles;
  111. user_email = profile.email;
  112. sys_defaults = wn.boot.sysdefaults;
  113. },
  114. sync_pages: function() {
  115. // clear cached pages if timestamp is not found
  116. if(localStorage["page_info"]) {
  117. wn.boot.allowed_pages = [];
  118. page_info = JSON.parse(localStorage["page_info"]);
  119. $.each(wn.boot.page_info, function(name, modified) {
  120. if(page_info[name]!=modified) {
  121. delete localStorage["_page:" + name];
  122. }
  123. wn.boot.allowed_pages.push(name);
  124. });
  125. } else {
  126. wn.boot.allowed_pages = keys(wn.boot.page_info);
  127. }
  128. localStorage["page_info"] = JSON.stringify(wn.boot.page_info);
  129. },
  130. set_as_guest: function() {
  131. // for backward compatibility
  132. profile = {name:'Guest'};
  133. user = 'Guest';
  134. user_fullname = 'Guest';
  135. user_defaults = {};
  136. user_roles = ['Guest'];
  137. user_email = '';
  138. sys_defaults = {};
  139. },
  140. make_page_container: function() {
  141. if($("#body_div").length) {
  142. $(".splash").remove();
  143. wn.temp_container = $("<div id='temp-container' style='display: none;'>")
  144. .appendTo("body");
  145. wn.container = new wn.views.Container();
  146. }
  147. },
  148. make_nav_bar: function() {
  149. // toolbar
  150. if(wn.boot) {
  151. wn.container.wntoolbar = new wn.ui.toolbar.Toolbar();
  152. }
  153. },
  154. logout: function() {
  155. var me = this;
  156. me.logged_out = true;
  157. wn.call({
  158. method:'logout',
  159. callback: function(r) {
  160. if(r.exc) {
  161. console.log(r.exc);
  162. return;
  163. }
  164. me.redirect_to_login();
  165. }
  166. })
  167. },
  168. redirect_to_login: function() {
  169. window.location.href = 'index.html';
  170. },
  171. set_favicon: function() {
  172. var link = $('link[type="image/x-icon"]').remove().attr("href");
  173. $('<link rel="shortcut icon" href="' + link + '" type="image/x-icon">').appendTo("head")
  174. $('<link rel="icon" href="' + link + '" type="image/x-icon">').appendTo("head")
  175. }
  176. })