Workbench.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. Ext.ux.Workbench = function(security) {
  2. this.map = {};
  3. this.security = security;
  4. };
  5. Ext.ux.Workbench.prototype = {
  6. init: function() {
  7. this.initView();
  8. this.initLoginWindow();
  9. },
  10. initView: function() {
  11. this.north = this.createNorthPanel();
  12. this.south = this.createSouthPanel();
  13. this.west = this.createWestPanel();
  14. this.center = this.createCenterPanel();
  15. var viewport = new Ext.Viewport({
  16. layout: 'border',
  17. items: [
  18. this.north,
  19. this.south,
  20. this.west,
  21. this.center
  22. ]
  23. });
  24. setTimeout(function(){
  25. Ext.get('loading').remove();
  26. Ext.get('loading-mask').fadeOut({remove:true});
  27. }, 500);
  28. },
  29. initLoginWindow: function() {
  30. if (this.security.loginUrl) {
  31. this.loginWindow = new Ext.ux.LoginWindow({
  32. url: this.security.loginUrl,
  33. callback: function(result) {
  34. this.rebuildMenu(result.info.menus);
  35. this.loginWindow.hide();
  36. }.createDelegate(this)
  37. });
  38. Ext.Ajax.request({
  39. url: this.security.checkUrl,
  40. success: function(response) {
  41. var result = Ext.decode(response.responseText);
  42. if (result.success) {
  43. this.rebuildMenu(result.info.menus);
  44. } else {
  45. this.loginWindow.show();
  46. }
  47. },
  48. failure: function(response) {
  49. Ext.Msg.alert('错误', '无法访问服务器。');
  50. },
  51. scope: this
  52. });
  53. } else {
  54. this.loginWindow = new Ext.ux.LoginWindow({
  55. url: (typeof WEB_ROOT == 'undefined' ? '.' : WEB_ROOT) + '/scripts/ux/window/login.jsp',
  56. callback: function(result) {
  57. this.rebuildMenu(this.security);
  58. this.loginWindow.hide();
  59. }.createDelegate(this)
  60. });
  61. Ext.Ajax.request({
  62. url: (typeof WEB_ROOT == 'undefined' ? '.' : WEB_ROOT) + '/scripts/ux/window/login.jsp',
  63. success: function(response) {
  64. var result = Ext.decode(response.responseText);
  65. if (result.success) {
  66. this.rebuildMenu(this.security);
  67. } else {
  68. this.loginWindow.show();
  69. }
  70. },
  71. failure: function(response) {
  72. Ext.Msg.alert('错误', '无法访问服务器。');
  73. },
  74. scope: this
  75. });
  76. }
  77. },
  78. logout: function() {
  79. if (this.security.logoutUrl) {
  80. Ext.Ajax.request({
  81. url: this.security.logoutUrl,
  82. success: function(response) {
  83. this.clean();
  84. this.loginWindow.formPanel.getForm().reset();
  85. this.loginWindow.show();
  86. },
  87. failure: function(response) {
  88. Ext.Msg.alert('错误', '无法访问服务器。');
  89. },
  90. scope: this
  91. });
  92. } else {
  93. Ext.Ajax.request({
  94. url: (typeof WEB_ROOT == 'undefined' ? '.' : WEB_ROOT) + '/scripts/ux/window/logout.jsp',
  95. success: function(response) {
  96. this.clean();
  97. this.loginWindow.formPanel.getForm().reset();
  98. this.loginWindow.show();
  99. },
  100. failure: function(response) {
  101. Ext.Msg.alert('错误', '无法访问服务器。');
  102. },
  103. scope: this
  104. });
  105. }
  106. },
  107. createNorthPanel: function() {
  108. return {
  109. region: 'north',
  110. height: 80,
  111. margins: '5 5 5 5',
  112. html: '<img src="' + (typeof WEB_ROOT == 'undefined' ? '.' : WEB_ROOT) + '/scripts/ux/family168.png">',
  113. bbar: new Ext.Toolbar(['->', {
  114. text: 'Logout',
  115. iconCls: 'logout-btn',
  116. handler: function() {
  117. this.logout();
  118. },
  119. scope: this
  120. }])
  121. };
  122. },
  123. createSouthPanel: function() {
  124. return {
  125. region: 'south',
  126. height: 18,
  127. margins: '5 0 0 0',
  128. border: false,
  129. frame: false,
  130. bodyStyle: 'background-color:#99BBEE;color:white;font-size:12px;font-weight:bold;',
  131. html: new Date().toString()
  132. };
  133. },
  134. createWestPanel: function() {
  135. var westPanel = new Ext.Panel({
  136. id: 'mainAccordion',
  137. region: 'west',
  138. title: '功能菜单',
  139. layout: 'accordion',
  140. cls: 'west-menu',
  141. width: 150,
  142. minSize: 120,
  143. maxSize: 200,
  144. split: true,
  145. collapsible: true,
  146. margins: '0 0 0 5',
  147. cmargins: '0 5 0 5',
  148. frame: true,
  149. border: true,
  150. defaults: {
  151. border: false,
  152. lines: false,
  153. autoScroll: true,
  154. bodyStyle: 'background: #fff;',
  155. collapseFirst: true
  156. }
  157. });
  158. return westPanel;
  159. },
  160. createCenterPanel: function() {
  161. var tabPanel = new Ext.TabPanel({
  162. region: 'center',
  163. layoutOnTabChange: true,
  164. enableTabScroll: true,
  165. monitorResize: true,
  166. activeTab: 0,
  167. margins: '0 5 0 0',
  168. plain: true,
  169. frame: true,
  170. hideMode: 'offsets',
  171. deferredRender: false,
  172. deferredLayout: false,
  173. defaults: {
  174. closable: true,
  175. viewConfig: {
  176. forceFit: true
  177. },
  178. margins: '5 5 5 5',
  179. bodyBorder: true
  180. },
  181. items: [{
  182. id: 'Home',
  183. title: '欢迎您',
  184. closable: false,
  185. autoScroll: true,
  186. iconCls: 'welcome',
  187. html: '<table width="100%" height="100%"><tr><td align="center">'
  188. + '<a href="http://www.china-pub.com/195152" target="_blank">'
  189. + '<img src="' + (typeof WEB_ROOT == 'undefined' ? '.' : WEB_ROOT) + '/scripts/ux/cover.jpg" width="300" height="380" border="0" />'
  190. + '</a></td></tr></table>'
  191. }]
  192. });
  193. return tabPanel;
  194. },
  195. register: function(name, fn, iconCls, notConstructor) {
  196. if (fn !== null && typeof fn !== 'function') {
  197. console.error(fn + ' must be a function for ' + name + '.');
  198. }
  199. App.view.map[name] = {
  200. fn: fn,
  201. iconCls: iconCls,
  202. notConstructor: notConstructor
  203. };
  204. },
  205. openTab: function(name) {
  206. var comp = this.map[name];
  207. if (typeof comp === 'undefined') {
  208. return;
  209. }
  210. if (comp.notConstructor === true) {
  211. comp.fn.call(this);
  212. } else {
  213. var tabs = this.center;
  214. var tabItem = tabs.getItem(name);
  215. if (tabItem == null) {
  216. var c = comp.fn;
  217. var p = new c;
  218. tabItem = tabs.add(p);
  219. }
  220. tabs.activate(tabItem);
  221. }
  222. },
  223. rebuildMenu: function(info) {
  224. var mainAccordion = this.west;
  225. for (var i = 0; i < info.length; i++) {
  226. var title = info[i].text;
  227. var comp = this.map[info[i].content];
  228. if (typeof comp === 'undefined') {
  229. continue;
  230. }
  231. var iconCls = comp.iconCls;
  232. for (var j = 0; j < info[i].children.length; j++) {
  233. var item = info[i].children[j];
  234. var child = this.map[item.content];
  235. if (typeof child !== 'undefined') {
  236. item.iconCls = child.iconCls;
  237. }
  238. }
  239. var p = new Ext.tree.TreePanel({
  240. title: title,
  241. iconCls: iconCls,
  242. rootVisible: false,
  243. loader: new Ext.tree.TreeLoader(),
  244. root: new Ext.tree.AsyncTreeNode({
  245. text:'功能菜单',
  246. children: info[i].children
  247. })
  248. });
  249. p.on('click', function(node) {
  250. this.openTab(node.attributes.content);
  251. }, this);
  252. mainAccordion.add(p);
  253. }
  254. mainAccordion.doLayout();
  255. },
  256. clean: function() {
  257. var tabs = this.center;
  258. for (var i = tabs.items.length; i > 0; i--) {
  259. var tab = tabs.getComponent(i);
  260. tabs.remove(tab, true);
  261. }
  262. this.west.removeAll();
  263. },
  264. notExists: function(id) {
  265. var tabItem = this.center.getItem(id);
  266. if (tabItem) {
  267. this.center.activate(tabItem);
  268. return false;
  269. } else {
  270. return true;
  271. }
  272. },
  273. openPanel: function(p) {
  274. var tabItem = this.center.add(p);
  275. this.center.activate(tabItem);
  276. },
  277. getPanel: function(id) {
  278. return this.center.getItem(id);
  279. },
  280. addPanel: function(conf) {
  281. var tabItem = this.center.getItem(conf.id);
  282. if (tabItem) {
  283. tabItem = new conf.fn;
  284. this.center.add(tabItem);
  285. }
  286. conf.callback.call(window, tabItem);
  287. this.center.activate(tabItem);
  288. }
  289. };