zentaobiz.ui.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. window.resetChanged = function()
  2. {
  3. $('#pivotState').attr('data-changed', JSON.stringify([]));
  4. }
  5. window.getStateChange = function()
  6. {
  7. const ignores = ['queryCols', 'queryData', 'pivotCols', 'pivotData', 'pivotCellSpan', 'default[]'];
  8. const data = {};
  9. let changed = $('#pivotState').data('changed');
  10. changed = changed.filter(key => !ignores.includes(key));
  11. changed.forEach(key => (data[key] = pivotState()[key]));
  12. return data;
  13. }
  14. window.pivotState = function(key, value)
  15. {
  16. const state = $('#pivotState').data('state');
  17. let changed = $('#pivotState').data('changed');
  18. if(key?.length && state.hasOwnProperty(key))
  19. {
  20. state[key] = value;
  21. changed.push(key);
  22. changed = Array.from(new Set(changed));
  23. $('#pivotState').attr('data-state', JSON.stringify(state));
  24. $('#pivotState').attr('data-changed', JSON.stringify(changed));
  25. }
  26. return state;
  27. }
  28. window.postQueryResult = function(e, info)
  29. {
  30. const { item } = info;
  31. const { page } = item;
  32. const { pager } = pivotState();
  33. const { pageID, pageTotal } = pager;
  34. if(page == 'first' && pageID != 1) pager.pageID = 1;
  35. if(page == 'last' && pageTotal != pageID) pager.pageID = pageTotal;
  36. if(page == 'prev' && pageID - 1 >= 1) pager.pageID = pageID - 1;
  37. if(page == 'next' && pageID + 1 <= pageTotal) pager.pageID = pageID + 1;
  38. pivotState('pager', pager);
  39. updateDesignPage('query');
  40. }
  41. window.updateDesignPage = function(action, index)
  42. {
  43. const {link, formData, selectors} = buildPostParams(action, index);
  44. postAndLoadPage(link, formData, selectors, {modal: true});
  45. }
  46. window.postDesignPage = function(action, index)
  47. {
  48. const {link, formData} = buildPostParams(action, index);
  49. postAndLoadPage(link, formData, `pageJS/.zin-page-js,#configJS`, {modal: true});
  50. }
  51. window.buildPostParams = function(action, index)
  52. {
  53. if(!action) action = pivotState.action;
  54. pivotState.action = action;
  55. let actionSelectors = actionLoadTarget[action] ?? '#stepContent';
  56. if(Number.isInteger(index) || index) {
  57. let regex = new RegExp('%s', "g");
  58. actionSelectors = actionSelectors.replace(regex, index);
  59. }
  60. const data = getStateChange();
  61. if(action.startsWith('sqlBuilder-'))
  62. {
  63. const builder = getSqlBuilderPost(action, index);
  64. data.sqlbuilder = builder.data;
  65. actionSelectors = builder.selectors;
  66. if(action != 'sqlBuilder-step')
  67. {
  68. pivotState('sqlChanged', true);
  69. data.sqlChanged = true;
  70. disableNextStep();
  71. }
  72. }
  73. const formData = zui.createFormData({action, data: JSON.stringify(data)});
  74. let selectors = [actionSelectors, '#dictionarySideBar', '#pivotState', '#sqlModal', '#saveAsDraft', '#stepNav', 'pageJS/.zin-page-js', 'pageCSS/.zin-page-css>*', '#configJS'];
  75. const link = $('#pivotState').data('url');
  76. return {link, formData, selectors: selectors.filter(selector => selector?.length != 0).join(',')};
  77. }
  78. window.allowAutoGenDrills = function()
  79. {
  80. pivotState('autoGenDrills', true);
  81. }
  82. window.setDrillType = function(modalID, type)
  83. {
  84. setDrillField(modalID, 'type', type);
  85. }
  86. window.setDesignChangedWarning = function(targetField = 'all', showWarning = true)
  87. {
  88. const {drills} = pivotState();
  89. drills.forEach(function(value, index, drills)
  90. {
  91. if(!drills[index]) return;
  92. if(targetField == 'all' || targetField == value.field || value.field == '' || !value.field) drills[index]['warning'] = showWarning;
  93. });
  94. pivotState('drills', drills);
  95. }
  96. window.saveSettings = function()
  97. {
  98. pivotState('checkStepDesign', true);
  99. updateDesignPage('table');
  100. }
  101. window.savePivot = function(saveStage = 'publish')
  102. {
  103. const {stage, used} = pivotState();
  104. let isPublished = stage == 'published';
  105. if(saveStage == 'draft')
  106. {
  107. if(!canSwitchStepOrSave()) return zui.Modal.alert({message: cannotNextStep});
  108. return isSqlChanged() ? clearAllConfirm(() => setTimeout(() => confirmSaveDraft(isPublished),200)) : confirmSaveDraft(isPublished);
  109. }
  110. if(used)
  111. {
  112. zui.Modal.confirm({message: confirmPublish}).then((res) =>
  113. {
  114. if(res) save(saveStage);
  115. });
  116. }
  117. else
  118. {
  119. save(saveStage);
  120. }
  121. }
  122. window.save = function(stage)
  123. {
  124. pivotState('stage', stage);
  125. const data = getStateChange();
  126. const formData = zui.createFormData(data ? {action: 'publish', data: JSON.stringify(data)} : {action: 'publish'});
  127. const link = $('#pivotState').data('url');
  128. $.post(link, formData, function(response)
  129. {
  130. response = JSON.parse(response);
  131. if(response.result == 'success')
  132. {
  133. zui.Messager.show({content: response.message, type: 'success', time: 1000});
  134. setTimeout(() => loadPage(response.locate), 1000);
  135. }
  136. });
  137. }
  138. /**
  139. * 查询条件改变时重新加载自定义透视表。
  140. * Reload custom pivot table when query conditions changed.
  141. *
  142. * @access public
  143. * @return void
  144. */
  145. window.loadCustomPivot = function()
  146. {
  147. updateDesignPage('table');
  148. }
  149. /**
  150. * Judge whether filter is query type.
  151. *
  152. * @access public
  153. * @return bool
  154. */
  155. window.isQueryFilter = function()
  156. {
  157. const {filters} = pivotState();
  158. return filters.some(function(filter) {
  159. return filter.from === 'query';
  160. });
  161. }
  162. window.handleFilterChange = function(event, name, suffix)
  163. {
  164. const $target = $(event?.target);
  165. if(!$target) return;
  166. const value = $target.val();
  167. let valueKey = '';
  168. if(suffix) valueKey = suffix
  169. let {pivotFilters} = pivotState();
  170. pivotFilters = pivotFilters.map((filters) => {
  171. return filters.map((filter) => {
  172. if(filter.name == name)
  173. {
  174. filter.value = valueKey.length ? {...filter.value, [valueKey]: value} : value;
  175. }
  176. return filter;
  177. });
  178. });
  179. pivotState('pivotFilters', pivotFilters);
  180. }
  181. window.ajaxQuery = function()
  182. {
  183. updateDesignPage('query');
  184. }
  185. window.changeStep = function(e)
  186. {
  187. const $a = $(e.target).closest('a');
  188. const step = $a.attr('step');
  189. if(step == pivotState().step) return;
  190. if(!canSwitchStepOrSave()) return zui.Modal.alert({message: cannotNextStep});
  191. isSqlChanged() ? clearAllConfirm(() => switchStep(step)) : switchStep(step);
  192. }
  193. window.canSwitchStepOrSave = function()
  194. {
  195. return pivotState().step != 'query' || $('.query-next-disabled').hasClass('hidden');
  196. }
  197. window.isSqlChanged = function()
  198. {
  199. if(!pivotState().sqlChanged) return false;
  200. return checkSqlChange();
  201. }
  202. window.checkSqlChange = function()
  203. {
  204. const fields = pivotState().fieldSettings;
  205. const settings = pivotState().settings;
  206. for(let key in settings)
  207. {
  208. if(key.startsWith('group') && settings[key]?.length && !fields.hasOwnProperty(settings[key])) return true;
  209. }
  210. if(settings.columns)
  211. {
  212. for(let column of settings.columns)
  213. {
  214. if(!fields.hasOwnProperty(column.field) || (column.slice != 'noSlice' && !fields.hasOwnProperty(column.slice))) return true;
  215. }
  216. }
  217. if(!isQueryFilter())
  218. {
  219. const filters = pivotState().filters;
  220. for(let filter of filters)
  221. {
  222. if(!fields.hasOwnProperty(filter.field)) return true;
  223. }
  224. }
  225. const drills = pivotState().drills;
  226. for(let drill of drills)
  227. {
  228. if(drill === null) continue;
  229. if(!fields.hasOwnProperty(drill.field)) return true;
  230. for(let condition of drill.condition)
  231. {
  232. if(!fields.hasOwnProperty(condition.queryField)) return true;
  233. }
  234. }
  235. return false;
  236. }
  237. window.removeInvalidProps = function()
  238. {
  239. const fields = pivotState().fieldSettings;
  240. const settings = pivotState().settings;
  241. if(settings.columns) settings.columns = settings.columns.filter((column) => fields.hasOwnProperty(column.field) && (column.slice == 'noSlice' || fields.hasOwnProperty(column.slice)));
  242. const groups = [];
  243. Object.keys(settings).forEach(key => {
  244. if(key.startsWith('group'))
  245. {
  246. value = settings[key];
  247. if(fields.hasOwnProperty(value)) groups.push(value);
  248. delete settings[key];
  249. }
  250. });
  251. groups.forEach((group, index) => {
  252. settings['group' + (index + 1)] = group;
  253. });
  254. pivotState('settings', settings);
  255. if(!isQueryFilter())
  256. {
  257. const filters = pivotState().filters;
  258. let pivotFilters = pivotState().pivotFilters;
  259. const indexes = filters.reduce((indexes, filter, index) => {
  260. if(!fields.hasOwnProperty(filter.field)) indexes.push(index);
  261. return indexes;
  262. }, []);
  263. pivotState('filters', filters.filter((_, index) => !indexes.includes(index)));
  264. pivotFilters[1] = pivotFilters[1].filter((_, index) => !indexes.includes(index));
  265. pivotState('pivotFilters', pivotFilters);
  266. }
  267. let drills = pivotState().drills;
  268. drills = drills.filter((drill) => fields.hasOwnProperty(drill.field));
  269. drills.forEach((drill, index) => {
  270. drill.condition = drill.condition.filter((condition) => fields.hasOwnProperty(condition.queryField));
  271. drills[index] = drill;
  272. });
  273. pivotState('drills', drills);
  274. }
  275. window.clearSettings = function()
  276. {
  277. pivotState('settings', []);
  278. if(!isQueryFilter())
  279. {
  280. pivotState('filters', []);
  281. pivotState('pivotFilters', []);
  282. }
  283. pivotState('drills', []);
  284. pivotState('step2FinishSql', '');
  285. }
  286. window.clearAllConfirm = function(afterConfirm = null)
  287. {
  288. const clearBtn = {key: 'clear', type: 'primary', text: clearLang, 'class': 'btn btn-wide'};
  289. const keepBtn = {key: 'keep', type: 'default', text: keepLang, 'class': 'btn btn-wide'};
  290. const confirmOptions = {
  291. message: resetSettingsTip,
  292. actions: [clearBtn, keepBtn],
  293. onClickAction: (item) => {
  294. pivotState('sqlChanged', false);
  295. item.key == 'clear' ? clearSettings() : removeInvalidProps();
  296. afterConfirm?.();
  297. }
  298. };
  299. zui.Modal.confirm(confirmOptions);
  300. }
  301. window.confirmSaveDraft = function(isPublished)
  302. {
  303. if(pivotState().used)
  304. {
  305. zui.Modal.confirm({message: confirmDraft}).then((res) =>
  306. {
  307. if(res) save('draft');
  308. });
  309. }
  310. else
  311. {
  312. if(isPublished)
  313. {
  314. zui.Modal.confirm({message: draftSave}).then((res) =>
  315. {
  316. if(res) save('draft');
  317. });
  318. }
  319. else
  320. {
  321. save('draft');
  322. }
  323. }
  324. }
  325. window.setChanged = function(changed = true)
  326. {
  327. pivotState('filterChanged', changed);
  328. }
  329. window.getChanged = function()
  330. {
  331. return pivotState().changedWithoutSave;
  332. }
  333. window.nextStep = function()
  334. {
  335. const currentStep = pivotState().step;
  336. const index = stepOrder.findIndex(s => s == currentStep);
  337. if(index + 1 >= stepOrder.length) return;
  338. const nextStep = stepOrder[index + 1];
  339. if(!canSwitchStepOrSave()) return zui.Modal.alert({message: cannotNextStep});
  340. isSqlChanged() ? clearAllConfirm(() => switchStep(nextStep)) : switchStep(nextStep);
  341. }
  342. window.switchStep = function(step)
  343. {
  344. const designActions = $('#pivotState').data('actions');
  345. const action = designActions[step][0];
  346. pivotState('step', step);
  347. if(pivotState().mode == 'builder')
  348. {
  349. const {sqlbuilder} = pivotState();
  350. sqlbuilder.step = 'table';
  351. if(sqlbuilder.sql !== undefined) sqlbuilder.sql = window.latin1ToBase64(sqlbuilder.sql);
  352. pivotState('sqlbuilder', sqlbuilder);
  353. }
  354. updateDesignPage(action);
  355. }
  356. window.handleSqlChange = function()
  357. {
  358. const sql = window.latin1ToBase64($('#sqlForm').find('textarea[name="sql"]').val());
  359. pivotState('sql', sql);
  360. pivotState('sqlChanged', true);
  361. if(pivotState().mode == 'text')
  362. {
  363. const canChangeMode = !sql?.trim()?.length;
  364. pivotState('canChangeMode', canChangeMode);
  365. canChangeMode ? $('#changeMode').removeClass('hidden') : $('#changeMode').addClass('hidden')
  366. canChangeMode ? $('#changeModeDisabled').addClass('hidden') : $('#changeModeDisabled').removeClass('hidden')
  367. }
  368. disableNextStep();
  369. }
  370. window.disableNextStep = function()
  371. {
  372. $('.query-next').addClass('hidden');
  373. $('.query-next-disabled').removeClass('hidden');
  374. }
  375. window.saveFields = function()
  376. {
  377. let $form = $('#fieldSettingsForm');
  378. let formData = new FormData($form[0]);
  379. let data = {};
  380. for (var [key, value] of formData.entries()) {
  381. const pureKey = key.substring(0, key.indexOf("["));
  382. if(!data[pureKey]) data[pureKey] = [];
  383. data[pureKey].push(value);
  384. }
  385. let fieldSettings = {};
  386. data.key.forEach((key, index) => {
  387. fieldSettings[key] = {};
  388. Object.keys(data).forEach(itemKey => {
  389. if(itemKey == 'key') return;
  390. fieldSettings[key][itemKey] = data[itemKey][index];
  391. });
  392. });
  393. pivotState('fieldSettings', fieldSettings);
  394. updateDesignPage('saveFields');
  395. }
  396. window.exportData = function()
  397. {
  398. var $domObj = $('#stepContent').find(".table-condensed")[0];
  399. exportFile($domObj);
  400. }
  401. window.renderDrillResult = function(result, {col, row})
  402. {
  403. if(col.name == 'name' && row.data.type == 'program') result[0].props.href = $.createLink('program', 'kanban');
  404. return result;
  405. }