| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- window.resetChanged = function()
- {
- $('#pivotState').attr('data-changed', JSON.stringify([]));
- }
- window.getStateChange = function()
- {
- const ignores = ['queryCols', 'queryData', 'pivotCols', 'pivotData', 'pivotCellSpan', 'default[]'];
- const data = {};
- let changed = $('#pivotState').data('changed');
- changed = changed.filter(key => !ignores.includes(key));
- changed.forEach(key => (data[key] = pivotState()[key]));
- return data;
- }
- window.pivotState = function(key, value)
- {
- const state = $('#pivotState').data('state');
- let changed = $('#pivotState').data('changed');
- if(key?.length && state.hasOwnProperty(key))
- {
- state[key] = value;
- changed.push(key);
- changed = Array.from(new Set(changed));
- $('#pivotState').attr('data-state', JSON.stringify(state));
- $('#pivotState').attr('data-changed', JSON.stringify(changed));
- }
- return state;
- }
- window.postQueryResult = function(e, info)
- {
- const { item } = info;
- const { page } = item;
- const { pager } = pivotState();
- const { pageID, pageTotal } = pager;
- if(page == 'first' && pageID != 1) pager.pageID = 1;
- if(page == 'last' && pageTotal != pageID) pager.pageID = pageTotal;
- if(page == 'prev' && pageID - 1 >= 1) pager.pageID = pageID - 1;
- if(page == 'next' && pageID + 1 <= pageTotal) pager.pageID = pageID + 1;
- pivotState('pager', pager);
- updateDesignPage('query');
- }
- window.updateDesignPage = function(action, index)
- {
- const {link, formData, selectors} = buildPostParams(action, index);
- postAndLoadPage(link, formData, selectors, {modal: true});
- }
- window.postDesignPage = function(action, index)
- {
- const {link, formData} = buildPostParams(action, index);
- postAndLoadPage(link, formData, `pageJS/.zin-page-js,#configJS`, {modal: true});
- }
- window.buildPostParams = function(action, index)
- {
- if(!action) action = pivotState.action;
- pivotState.action = action;
- let actionSelectors = actionLoadTarget[action] ?? '#stepContent';
- if(Number.isInteger(index) || index) {
- let regex = new RegExp('%s', "g");
- actionSelectors = actionSelectors.replace(regex, index);
- }
- const data = getStateChange();
- if(action.startsWith('sqlBuilder-'))
- {
- const builder = getSqlBuilderPost(action, index);
- data.sqlbuilder = builder.data;
- actionSelectors = builder.selectors;
- if(action != 'sqlBuilder-step')
- {
- pivotState('sqlChanged', true);
- data.sqlChanged = true;
- disableNextStep();
- }
- }
- const formData = zui.createFormData({action, data: JSON.stringify(data)});
- let selectors = [actionSelectors, '#dictionarySideBar', '#pivotState', '#sqlModal', '#saveAsDraft', '#stepNav', 'pageJS/.zin-page-js', 'pageCSS/.zin-page-css>*', '#configJS'];
- const link = $('#pivotState').data('url');
- return {link, formData, selectors: selectors.filter(selector => selector?.length != 0).join(',')};
- }
- window.allowAutoGenDrills = function()
- {
- pivotState('autoGenDrills', true);
- }
- window.setDrillType = function(modalID, type)
- {
- setDrillField(modalID, 'type', type);
- }
- window.setDesignChangedWarning = function(targetField = 'all', showWarning = true)
- {
- const {drills} = pivotState();
- drills.forEach(function(value, index, drills)
- {
- if(!drills[index]) return;
- if(targetField == 'all' || targetField == value.field || value.field == '' || !value.field) drills[index]['warning'] = showWarning;
- });
- pivotState('drills', drills);
- }
- window.saveSettings = function()
- {
- pivotState('checkStepDesign', true);
- updateDesignPage('table');
- }
- window.savePivot = function(saveStage = 'publish')
- {
- const {stage, used} = pivotState();
- let isPublished = stage == 'published';
- if(saveStage == 'draft')
- {
- if(!canSwitchStepOrSave()) return zui.Modal.alert({message: cannotNextStep});
- return isSqlChanged() ? clearAllConfirm(() => setTimeout(() => confirmSaveDraft(isPublished),200)) : confirmSaveDraft(isPublished);
- }
- if(used)
- {
- zui.Modal.confirm({message: confirmPublish}).then((res) =>
- {
- if(res) save(saveStage);
- });
- }
- else
- {
- save(saveStage);
- }
- }
- window.save = function(stage)
- {
- pivotState('stage', stage);
- const data = getStateChange();
- const formData = zui.createFormData(data ? {action: 'publish', data: JSON.stringify(data)} : {action: 'publish'});
- const link = $('#pivotState').data('url');
- $.post(link, formData, function(response)
- {
- response = JSON.parse(response);
- if(response.result == 'success')
- {
- zui.Messager.show({content: response.message, type: 'success', time: 1000});
- setTimeout(() => loadPage(response.locate), 1000);
- }
- });
- }
- /**
- * 查询条件改变时重新加载自定义透视表。
- * Reload custom pivot table when query conditions changed.
- *
- * @access public
- * @return void
- */
- window.loadCustomPivot = function()
- {
- updateDesignPage('table');
- }
- /**
- * Judge whether filter is query type.
- *
- * @access public
- * @return bool
- */
- window.isQueryFilter = function()
- {
- const {filters} = pivotState();
- return filters.some(function(filter) {
- return filter.from === 'query';
- });
- }
- window.handleFilterChange = function(event, name, suffix)
- {
- const $target = $(event?.target);
- if(!$target) return;
- const value = $target.val();
- let valueKey = '';
- if(suffix) valueKey = suffix
- let {pivotFilters} = pivotState();
- pivotFilters = pivotFilters.map((filters) => {
- return filters.map((filter) => {
- if(filter.name == name)
- {
- filter.value = valueKey.length ? {...filter.value, [valueKey]: value} : value;
- }
- return filter;
- });
- });
- pivotState('pivotFilters', pivotFilters);
- }
- window.ajaxQuery = function()
- {
- updateDesignPage('query');
- }
- window.changeStep = function(e)
- {
- const $a = $(e.target).closest('a');
- const step = $a.attr('step');
- if(step == pivotState().step) return;
- if(!canSwitchStepOrSave()) return zui.Modal.alert({message: cannotNextStep});
- isSqlChanged() ? clearAllConfirm(() => switchStep(step)) : switchStep(step);
- }
- window.canSwitchStepOrSave = function()
- {
- return pivotState().step != 'query' || $('.query-next-disabled').hasClass('hidden');
- }
- window.isSqlChanged = function()
- {
- if(!pivotState().sqlChanged) return false;
- return checkSqlChange();
- }
- window.checkSqlChange = function()
- {
- const fields = pivotState().fieldSettings;
- const settings = pivotState().settings;
- for(let key in settings)
- {
- if(key.startsWith('group') && settings[key]?.length && !fields.hasOwnProperty(settings[key])) return true;
- }
- if(settings.columns)
- {
- for(let column of settings.columns)
- {
- if(!fields.hasOwnProperty(column.field) || (column.slice != 'noSlice' && !fields.hasOwnProperty(column.slice))) return true;
- }
- }
- if(!isQueryFilter())
- {
- const filters = pivotState().filters;
- for(let filter of filters)
- {
- if(!fields.hasOwnProperty(filter.field)) return true;
- }
- }
- const drills = pivotState().drills;
- for(let drill of drills)
- {
- if(drill === null) continue;
- if(!fields.hasOwnProperty(drill.field)) return true;
- for(let condition of drill.condition)
- {
- if(!fields.hasOwnProperty(condition.queryField)) return true;
- }
- }
- return false;
- }
- window.removeInvalidProps = function()
- {
- const fields = pivotState().fieldSettings;
- const settings = pivotState().settings;
- if(settings.columns) settings.columns = settings.columns.filter((column) => fields.hasOwnProperty(column.field) && (column.slice == 'noSlice' || fields.hasOwnProperty(column.slice)));
- const groups = [];
- Object.keys(settings).forEach(key => {
- if(key.startsWith('group'))
- {
- value = settings[key];
- if(fields.hasOwnProperty(value)) groups.push(value);
- delete settings[key];
- }
- });
- groups.forEach((group, index) => {
- settings['group' + (index + 1)] = group;
- });
- pivotState('settings', settings);
- if(!isQueryFilter())
- {
- const filters = pivotState().filters;
- let pivotFilters = pivotState().pivotFilters;
- const indexes = filters.reduce((indexes, filter, index) => {
- if(!fields.hasOwnProperty(filter.field)) indexes.push(index);
- return indexes;
- }, []);
- pivotState('filters', filters.filter((_, index) => !indexes.includes(index)));
- pivotFilters[1] = pivotFilters[1].filter((_, index) => !indexes.includes(index));
- pivotState('pivotFilters', pivotFilters);
- }
- let drills = pivotState().drills;
- drills = drills.filter((drill) => fields.hasOwnProperty(drill.field));
- drills.forEach((drill, index) => {
- drill.condition = drill.condition.filter((condition) => fields.hasOwnProperty(condition.queryField));
- drills[index] = drill;
- });
- pivotState('drills', drills);
- }
- window.clearSettings = function()
- {
- pivotState('settings', []);
- if(!isQueryFilter())
- {
- pivotState('filters', []);
- pivotState('pivotFilters', []);
- }
- pivotState('drills', []);
- pivotState('step2FinishSql', '');
- }
- window.clearAllConfirm = function(afterConfirm = null)
- {
- const clearBtn = {key: 'clear', type: 'primary', text: clearLang, 'class': 'btn btn-wide'};
- const keepBtn = {key: 'keep', type: 'default', text: keepLang, 'class': 'btn btn-wide'};
- const confirmOptions = {
- message: resetSettingsTip,
- actions: [clearBtn, keepBtn],
- onClickAction: (item) => {
- pivotState('sqlChanged', false);
- item.key == 'clear' ? clearSettings() : removeInvalidProps();
- afterConfirm?.();
- }
- };
- zui.Modal.confirm(confirmOptions);
- }
- window.confirmSaveDraft = function(isPublished)
- {
- if(pivotState().used)
- {
- zui.Modal.confirm({message: confirmDraft}).then((res) =>
- {
- if(res) save('draft');
- });
- }
- else
- {
- if(isPublished)
- {
- zui.Modal.confirm({message: draftSave}).then((res) =>
- {
- if(res) save('draft');
- });
- }
- else
- {
- save('draft');
- }
- }
- }
- window.setChanged = function(changed = true)
- {
- pivotState('filterChanged', changed);
- }
- window.getChanged = function()
- {
- return pivotState().changedWithoutSave;
- }
- window.nextStep = function()
- {
- const currentStep = pivotState().step;
- const index = stepOrder.findIndex(s => s == currentStep);
- if(index + 1 >= stepOrder.length) return;
- const nextStep = stepOrder[index + 1];
- if(!canSwitchStepOrSave()) return zui.Modal.alert({message: cannotNextStep});
- isSqlChanged() ? clearAllConfirm(() => switchStep(nextStep)) : switchStep(nextStep);
- }
- window.switchStep = function(step)
- {
- const designActions = $('#pivotState').data('actions');
- const action = designActions[step][0];
- pivotState('step', step);
- if(pivotState().mode == 'builder')
- {
- const {sqlbuilder} = pivotState();
- sqlbuilder.step = 'table';
- if(sqlbuilder.sql !== undefined) sqlbuilder.sql = window.latin1ToBase64(sqlbuilder.sql);
- pivotState('sqlbuilder', sqlbuilder);
- }
- updateDesignPage(action);
- }
- window.handleSqlChange = function()
- {
- const sql = window.latin1ToBase64($('#sqlForm').find('textarea[name="sql"]').val());
- pivotState('sql', sql);
- pivotState('sqlChanged', true);
- if(pivotState().mode == 'text')
- {
- const canChangeMode = !sql?.trim()?.length;
- pivotState('canChangeMode', canChangeMode);
- canChangeMode ? $('#changeMode').removeClass('hidden') : $('#changeMode').addClass('hidden')
- canChangeMode ? $('#changeModeDisabled').addClass('hidden') : $('#changeModeDisabled').removeClass('hidden')
- }
- disableNextStep();
- }
- window.disableNextStep = function()
- {
- $('.query-next').addClass('hidden');
- $('.query-next-disabled').removeClass('hidden');
- }
- window.saveFields = function()
- {
- let $form = $('#fieldSettingsForm');
- let formData = new FormData($form[0]);
- let data = {};
- for (var [key, value] of formData.entries()) {
- const pureKey = key.substring(0, key.indexOf("["));
- if(!data[pureKey]) data[pureKey] = [];
- data[pureKey].push(value);
- }
- let fieldSettings = {};
- data.key.forEach((key, index) => {
- fieldSettings[key] = {};
- Object.keys(data).forEach(itemKey => {
- if(itemKey == 'key') return;
- fieldSettings[key][itemKey] = data[itemKey][index];
- });
- });
- pivotState('fieldSettings', fieldSettings);
- updateDesignPage('saveFields');
- }
- window.exportData = function()
- {
- var $domObj = $('#stepContent').find(".table-condensed")[0];
- exportFile($domObj);
- }
- window.renderDrillResult = function(result, {col, row})
- {
- if(col.name == 'name' && row.data.type == 'program') result[0].props.href = $.createLink('program', 'kanban');
- return result;
- }
|