MainTab.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import _ from 'lodash'
  2. import $ from 'jquery'
  3. import qs from 'qs'
  4. import {invokeMethod} from "../utils";
  5. export default function () {
  6. Ext.define('Yvan.MainTab', {
  7. extend: 'Ext.tab.Panel',
  8. xtype: 'maintab',
  9. plugins: Ext.create('Ext.ux.TabCloseMenu', {
  10. closeTabText: '关闭',
  11. closeOthersTabsText: '关闭其他',
  12. closeAllTabsText: '关闭所有'
  13. }),
  14. constructor(config) {
  15. const me = this
  16. const newConfig = _.defaultsDeep({
  17. // 强制属性
  18. }, config)
  19. this.superclass.constructor.call(me, newConfig)
  20. },
  21. initComponent() {
  22. const me = this
  23. this.on({
  24. tabchange(tabPanel, newCard, oldCard, eOpts) {
  25. _.defer(() => {
  26. me.changeHash(newCard.path)
  27. window['cp'] = newCard.yvanScope
  28. })
  29. $(window).trigger('tabChange', {
  30. tabPanel, newCard, oldCard, eOpts
  31. });
  32. },
  33. afterrender() {
  34. _.defer(() => {
  35. me.tryRestoryPage()
  36. })
  37. }
  38. })
  39. this.superclass.initComponent.call(me)
  40. },
  41. tryRestoryPage() {
  42. const me = this
  43. let hash = '' + window.location.hash
  44. if (hash && hash.indexOf('page=') > 0) {
  45. if (hash.startsWith('#')) {
  46. hash = hash.substr(1)
  47. }
  48. if (hash) {
  49. const {page} = qs.parse(hash)
  50. if (page) {
  51. // this.openScope(page)
  52. me.fireEvent('restorypage', page)
  53. }
  54. }
  55. }
  56. },
  57. changeHash(pageUrl) {
  58. window.location.hash = qs.stringify({page: pageUrl})
  59. },
  60. /**
  61. * 添加一个业务模块实例到选项卡
  62. * @param scopeInstance 业务对象实例
  63. * @param config ExtJS配置对象
  64. * @param panelInitCallback panel初始化之后会调用函数进行构造前的加工
  65. */
  66. addScope(scopeInstance, config, panelInitCallback) {
  67. scopeInstance.topScope = scopeInstance
  68. const me = this
  69. if (config.path) {
  70. for (let i = 0; i < this.items.items.length; i++) {
  71. // 找到当前 tabs 里有没有已经打开过
  72. const tab = this.items.items[i]
  73. if (_.isEqual(tab.path, config.path)) {
  74. // 激活
  75. this.setActiveTab(tab);
  76. return tab
  77. }
  78. }
  79. }
  80. const newPanel = new Ext.panel.Panel({
  81. closable: true,
  82. ...config
  83. })
  84. if (typeof panelInitCallback === 'function') {
  85. panelInitCallback(newPanel)
  86. }
  87. // 添加业务模块
  88. const newTab = this.add(newPanel)
  89. this.setActiveTab(newTab);
  90. newTab.on({
  91. destroy(sender) {
  92. me.changeHash('')
  93. delete window['cp']
  94. }
  95. })
  96. return newTab
  97. }
  98. });
  99. }