ViewUtil.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. Ext.define('saas.util.ViewUtil', {
  2. statics: {
  3. // 请求页面组件接口模板
  4. getCfgUrl: '/api/ui/customize/getConfig?name={viewName}',
  5. // 保存自定义配置
  6. saveCfgUrl: '/api/ui/customize/saveConfig',
  7. // 模板替换正则
  8. urlRe: /(.*){viewName}(.*)/g,
  9. // 从表允许自定义的配置名
  10. DETAIL_ALLOW_CUS_FIELDS: ['text', 'hidden', 'index', 'width'],
  11. getViewConfig: function (viewName) {
  12. let me = this,
  13. url = me.getCfgUrl.replace(me.urlRe, '$1' + viewName);
  14. return saas.util.BaseUtil.request({
  15. url,
  16. async: true
  17. })
  18. .then(function (res) {
  19. let cfg = me.decodeViewConfig(res.data);
  20. return cfg;
  21. })
  22. .catch(function (e) {
  23. saas.util.BaseUtil.showErrorToast('读取界面配置失败:' + e.message);
  24. return null;
  25. });
  26. },
  27. decodeViewConfig: function (cfgs) {
  28. cfgs = cfgs || [];
  29. let cfg = {};
  30. for (let i = 0; i < cfgs.length; i++) {
  31. let c = cfgs[i],
  32. pos = c.position;
  33. if (pos) {
  34. cfg[pos] = JSON.parse(c.content);
  35. }
  36. }
  37. return cfg;
  38. },
  39. applyColumns: function (viewName, columns) {
  40. let me = this;
  41. return me.getViewConfig(viewName)
  42. .then(function (cfg) {
  43. let newColumns = [];
  44. newColumns = me.initColumnItems(columns);
  45. if (cfg) {
  46. return me.applyColumnConfig(newColumns, cfg['columns']);
  47. } else {
  48. return newColumns;
  49. }
  50. });
  51. },
  52. /**
  53. * 为列字段添加可能会被自定义的初始化配置
  54. */
  55. initColumnItems: function (columns) {
  56. columns = Ext.isArray(columns) ? columns : [];
  57. let colCount = 1,
  58. newColumns = [];
  59. Ext.Array.each(columns, function (c, j) {
  60. let col = Object.assign({}, c);
  61. if (col.hidden || col.width == 0 || !col.dataIndex) {
  62. Ext.applyIf(col, {
  63. index: -1,
  64. initHidden: true
  65. });
  66. } else {
  67. Ext.applyIf(col, {
  68. text: '',
  69. hidden: false,
  70. index: colCount++,
  71. allowBlank: true,
  72. width: 100,
  73. initHidden: false
  74. });
  75. }
  76. newColumns.push(col);
  77. });
  78. Ext.Array.sort(newColumns, function (a, b) {
  79. return a.index - b.index;
  80. });
  81. return newColumns;
  82. },
  83. /**
  84. * 应用自定义配置到列字段
  85. */
  86. applyColumnConfig: function (columns, cusColumns) {
  87. let me = this;
  88. Ext.Array.each(cusColumns, function (cusCol) {
  89. let col = Ext.Array.findBy(columns, function (col) {
  90. return col.dataIndex == cusCol.dataIndex;
  91. });
  92. if (!!col) {
  93. let keys = Ext.Object.getAllKeys(cusCol);
  94. keys.map(function (k) {
  95. if (me.DETAIL_ALLOW_CUS_FIELDS.indexOf(k) != -1) {
  96. if (k != 'hidden' || col.allowBlank) {
  97. col['_init_' + k] = col.hasOwnProperty('_init_' + k) ? col['_init_' + k] : col[k];
  98. col[k] = cusCol[k];
  99. }
  100. }
  101. });
  102. }
  103. });
  104. Ext.Array.sort(columns, function (a, b) {
  105. return a.index - b.index;
  106. });
  107. return columns;
  108. },
  109. saveViewConfig: function (name, position, content) {
  110. let me = this;
  111. return saas.util.BaseUtil.request({
  112. url: me.saveCfgUrl,
  113. params: JSON.stringify({
  114. name: name,
  115. position: position,
  116. content: JSON.stringify(content)
  117. }),
  118. method: 'POST',
  119. });
  120. }
  121. }
  122. });