ViewUtil.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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: false
  17. })
  18. .then(function (res) {
  19. if (res.success) {
  20. let cfg = me.decodeViewConfig(res.data);
  21. return cfg;
  22. } else {
  23. return null
  24. }
  25. })
  26. .catch(function (response) {
  27. saas.util.BaseUtil.showErrorToast('读取自定义配置失败:' + response.message);
  28. return null;
  29. });
  30. },
  31. decodeViewConfig: function (cfgs) {
  32. cfgs = cfgs || [];
  33. let cfg = {};
  34. for (let i = 0; i < cfgs.length; i++) {
  35. let c = cfgs[i],
  36. pos = c.position;
  37. if (pos) {
  38. cfg[pos] = JSON.parse(c.content);
  39. }
  40. }
  41. return cfg;
  42. },
  43. applyColumns: function (viewName, columns) {
  44. let me = this;
  45. return me.getViewConfig(viewName)
  46. .then(function (cfg) {
  47. let newColumns = [];
  48. newColumns = me.initColumnItems(columns);
  49. if (cfg) {
  50. return me.applyColumnConfig(newColumns, cfg['columns']);
  51. } else {
  52. return newColumns;
  53. }
  54. });
  55. },
  56. /**
  57. * 为列字段添加可能会被自定义的初始化配置
  58. */
  59. initColumnItems: function (columns) {
  60. columns = Ext.isArray(columns) ? columns : [];
  61. let colCount = 1,
  62. newColumns = [];
  63. Ext.Array.each(columns, function (c, j) {
  64. let col = Object.assign({}, c);
  65. if (col.hidden || col.width == 0 || !col.dataIndex) {
  66. Ext.applyIf(col, {
  67. index: -1,
  68. initHidden: true
  69. });
  70. } else {
  71. Ext.applyIf(col, {
  72. text: '',
  73. hidden: false,
  74. index: colCount++,
  75. allowBlank: true,
  76. width: 100,
  77. initHidden: false
  78. });
  79. }
  80. newColumns.push(col);
  81. });
  82. Ext.Array.sort(newColumns, function (a, b) {
  83. return a.index - b.index;
  84. });
  85. return newColumns;
  86. },
  87. /**
  88. * 应用自定义配置到列字段
  89. */
  90. applyColumnConfig: function (columns, cusColumns) {
  91. let me = this;
  92. Ext.Array.each(cusColumns, function (cusCol) {
  93. let col = Ext.Array.findBy(columns, function (col) {
  94. return col.dataIndex == cusCol.dataIndex;
  95. });
  96. if (!!col) {
  97. let keys = Ext.Object.getAllKeys(cusCol);
  98. keys.map(function (k) {
  99. if (me.DETAIL_ALLOW_CUS_FIELDS.indexOf(k) != -1) {
  100. if (k != 'hidden' || col.allowBlank) {
  101. col['_init_' + k] = col.hasOwnProperty('_init_' + k) ? col['_init_' + k] : col[k];
  102. col[k] = cusCol[k];
  103. }
  104. }
  105. });
  106. }
  107. });
  108. Ext.Array.sort(columns, function (a, b) {
  109. return a.index - b.index;
  110. });
  111. return columns;
  112. },
  113. saveViewConfig: function (name, position, content) {
  114. let me = this;
  115. return saas.util.BaseUtil.request({
  116. url: me.saveCfgUrl,
  117. params: JSON.stringify({
  118. name: name,
  119. position: position,
  120. content: JSON.stringify(content)
  121. }),
  122. method: 'POST',
  123. });
  124. }
  125. }
  126. });