var DBG = DBG || false; var DBG1 = true; // if (!URI_WPS) throw "Missing URI_WPS"; if (!KEY_PREFIX) return; // KEY_PREFIX = KRS | KRS/p | MSIG | MSIG/p | CEIDG if (!URL_BAZA_OD) throw "Missing URL_BAZA_OD"; if (!URL_BAZA_DO) throw "Missing URL_BAZA_DO"; if (!URL_BAZA_NONE) throw "Missing URL_BAZA_NONE"; var BAZA_OD_DO = BAZA_OD_DO || false; BAZA_OD_DO = (BAZA_OD_DO === 'OD' || BAZA_OD_DO === 'DO') ? BAZA_OD_DO : ''; { // make fixed panel to select checkbox context OD, DO or default function _px(px) { return (!px) ? '0px' : '' + px + 'px'; } var panelNode = document.createElement('div') var panelWidth = 600; panelNode.style.position = 'fixed'; panelNode.style.top = _px(0); panelNode.style.width = _px(panelWidth); panelNode.style.left = _px(window.innerWidth > panelWidth ? Math.floor(window.innerWidth / 2 - panelWidth / 2) : 0); panelNode.style.padding = '8px 15px' panelNode.style.textAlign = 'center' panelNode.className = 'alert alert-info' panelNode.style.boxShadow = '6px 6px 20px 0px #31708f' // panelNode.style.border = '1px solid red' // panelNode.style.padding = '6px' // panelNode.style.backgroundColor = 'lightRed' // panelNode.style.color = 'black' panelNode.innerHTML = 'Wybierz elementy do raportu: ' + '' + ''; document.body.appendChild(panelNode) } if (!BAZA_OD_DO) return; if (!global.p5VendorJs) throw "Missing p5 Vendor js libs"; if (!global.p5VendorJs.Redux) throw "Missing p5 Vendor js lib: Redux"; if (!localStorage) throw "Missing localStorage!"; var Redux = global.p5VendorJs.Redux; var ReduxThunk = global.p5VendorJs.ReduxThunk; var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js { // - `Bocian.biAuditForm.odFeatureKeys`: `["CEIDG.879134","KRS/p.4398202"]` // - `Bocian.biAuditForm.doFeatureKeys`: `["KRS.1533062","KRS/p.4616336"]` var bazaSelected = (function (from, prefix) { // from: OD | DO, prefix: KRS | KRS/p | MSIG | MSIG/p | CEIDG function _getSelectedListLocalStorageKey(from) { switch (from) { case 'OD': return 'Bocian.biAuditForm.odFeatureKeys'; case 'DO': return 'Bocian.biAuditForm.doFeatureKeys'; default: throw "Not implemented from selected list '" + from + "' - expected 'OD' or 'DO'"; } } var _dbKey = _getSelectedListLocalStorageKey(from) function _fetchAll() { return JSON.parse( localStorage.getItem(_dbKey) || '[]' ); } function _fetchList() { return _fetchAll().filter(function (key) { return (key.substr(0, prefix.length + 1) === prefix + '.'); }) } function _toKey(pk) { return prefix + '.' + pk; } function _isBaza(key) { return (key.substr(0, prefix.length + 1) === prefix + '.'); } function _isNotBaza(key) { return !_isBaza(key); } // function _toPk(key) { return key.substr(prefix.length + 1); } return { getTotal: function () { return _fetchList().length; }, add: function (pk) { var all = new Set(_fetchAll()) all.add(_toKey(pk)) var newValue = Array.from(all) localStorage.setItem(_dbKey, JSON.stringify(newValue)) }, delete: function (pk) { var all = new Set(_fetchAll()) all.delete(_toKey(pk)) var newValue = Array.from(all) localStorage.setItem(_dbKey, JSON.stringify(newValue)) }, addList: function (pks) { var all = new Set(_fetchAll()) pks.forEach(function (pk) { all.add(_toKey(pk)) }) var newValue = Array.from(all) localStorage.setItem(_dbKey, JSON.stringify(newValue)) }, deleteList: function (pks) { var all = new Set(_fetchAll()) pks.forEach(function (pk) { all.delete(_toKey(pk)) }) var newValue = Array.from(all) localStorage.setItem(_dbKey, JSON.stringify(newValue)) }, deleteAll: function () { var newValue = _fetchAll().filter(_isNotBaza) localStorage.setItem(_dbKey, JSON.stringify(newValue)) }, getOnlySelectedPks: function (listPrimaryKeys) { var all = new Set(_fetchAll()) return listPrimaryKeys.filter(function (pk) { return all.has(_toKey(pk)) }) }, } })(BAZA_OD_DO, KEY_PREFIX); } function selectedStore(state, action) { var prevState = state || { isLoading: false, selected: [], loading: [], listPrimaryKeys: [], totalSelected: 0 }; DBG && console.log('DBG::selectedStore', { prevState, action }); switch (action.type) { case 'SET_CHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, { loading: p5Utils__arrayRemove(prevState.loading, action.primaryKey), selected: prevState.selected.concat(action.primaryKey), totalSelected: action.totalSelected, })); case 'SET_UNCHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, { loading: p5Utils__arrayRemove(prevState.loading, action.primaryKey), selected: p5Utils__arrayRemove(prevState.selected, action.primaryKey), totalSelected: action.totalSelected, })); case 'SET_LIST_CHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, { loading: [], // TODO: p5Utils__arrayExcept(prevState.loading, action.listPrimaryKeys), selected: [].concat(action.listPrimaryKeys), // TODO: p5Utils__arrayUniqueUnion(prevState.selected, action.listPrimaryKeys) totalSelected: action.totalSelected, })); case 'SET_LIST_UNCHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, { loading: [], // TODO: p5Utils__arrayExcept(prevState.loading, action.listPrimaryKeys), selected: [], // TODO: p5Utils__arrayUniqueUnion(prevState.selected, action.listPrimaryKeys) totalSelected: action.totalSelected, })); case 'SET_LOADING': return Object.assign(prevState, { loading: prevState.loading.concat(action.primaryKey) }); case 'SET_LIST_LOADING': return Object.assign(prevState, { loading: [].concat(action.listPrimaryKeys) }); case 'SET_PRIMARY_KEYS': return fixSelectAllInSelectedState(Object.assign({}, prevState, { listPrimaryKeys: [].concat(action.listPrimaryKeys), loading: [].concat(action.listPrimaryKeys) })); // case 'SET_ERROR_MSG': return Object.assign(prevState, { // errorMsg: action.errorMsg // }); default: return prevState; } } function fixSelectAllInSelectedState(state) { var len = state.listPrimaryKeys.length; var selLen = state.selected.length; var isAllSelected = ( len > 0 && selLen >= len ); // selected array may contain 'select-all' for (var i = 0, pk = ''; i < len; i++) { pk = state.listPrimaryKeys[i]; if (-1 === state.selected.indexOf(pk)) { isAllSelected = false; break; } } var isSelectedSelectAll = ( -1 !== state.selected.indexOf('select-all') ); if (isAllSelected && !isSelectedSelectAll) return Object.assign(state, { selected: ['select-all'].concat(state.selected) }); if (!isAllSelected && isSelectedSelectAll) return Object.assign(state, { selected: p5Utils__arrayRemove(state.selected, 'select-all'), }); return state; } function p5Utils__arrayRemove(array, toRemoveItem) { return array.filter(function (item) { return ( item !== toRemoveItem ); }); } function selectedActions(idContext) { var idContext = idContext || null; function setChecked(primaryKey, totalSelected) { return { type: 'SET_CHECKED', primaryKey: primaryKey, totalSelected: totalSelected } } function setUnchecked(primaryKey, totalSelected) { return { type: 'SET_UNCHECKED', primaryKey: primaryKey, totalSelected: totalSelected } } function setLoading(primaryKey) { return { type: 'SET_LOADING', primaryKey: primaryKey } } function setListChecked(listPrimaryKeys, totalSelected) { return { type: 'SET_LIST_CHECKED', listPrimaryKeys: listPrimaryKeys, totalSelected: totalSelected } } function setListUnchecked(listPrimaryKeys, totalSelected) { return { type: 'SET_LIST_UNCHECKED', listPrimaryKeys: listPrimaryKeys, totalSelected: totalSelected } } function setListLoading(listPrimaryKeys) { return { type: 'SET_LIST_LOADING', listPrimaryKeys: listPrimaryKeys } } function setErrorMsg(errorMsg) { return { type: 'SET_ERROR_MSG', errorMsg: errorMsg } } function toggle(namespace, primaryKey, checked) { DBG && console.log('DBG::toggle action: (pks:['+primaryKey+'])...'); if ('select-all' === primaryKey) return toggleAll(namespace, checked); return function(dispatch, getState) { dispatch(setLoading(primaryKey)); return new Promise(function (resolve, reject) { DBG && console.log('DBG::toggle action: (pks:['+primaryKey+'])...'); if (checked) { bazaSelected.add(primaryKey) dispatch( setChecked(primaryKey, bazaSelected.getTotal()) ); } else { bazaSelected.delete(primaryKey) dispatch( setUnchecked(primaryKey, bazaSelected.getTotal()) ); } }); } } function setPrimaryKeys(namespace, listPrimaryKeys) { // TODO: should set list primaryKeys, change checkboxes to loading and fetch its state, then update gui DBG && console.log('DBG::setPrimaryKeys action: (pks:['+listPrimaryKeys.join(',')+'])...'); return function(dispatch, getState) { dispatch({ type: 'SET_PRIMARY_KEYS', listPrimaryKeys: listPrimaryKeys }); // dispatch( setListLoading(listPrimaryKeys) ); if (!listPrimaryKeys.length) return; return new Promise(function (resolve, reject) { DBG && console.log('DBG::setPrimaryKeys action: (pks:['+listPrimaryKeys.join(',')+'])...'); var curSelectedPK = bazaSelected.getOnlySelectedPks(listPrimaryKeys) dispatch( setListChecked(curSelectedPK, bazaSelected.getTotal()) ); }); } } function toggleAll(namespace, checked) { var primaryKey = 'select-all'; return function(dispatch, getState) { dispatch(setLoading(primaryKey)); var state = getState(); var listPrimaryKeys = state.listPrimaryKeys; return new Promise(function (resolve, reject) { DBG && console.log('DBG::toggleAll action: (pks:['+listPrimaryKeys.join(',')+'])...'); if (checked) { bazaSelected.addList(listPrimaryKeys) dispatch( setListChecked(listPrimaryKeys, bazaSelected.getTotal()) ); } else { bazaSelected.deleteList(listPrimaryKeys) dispatch( setListUnchecked(listPrimaryKeys, bazaSelected.getTotal()) ); } }); } } function unselectAll(namespace) { return function(dispatch, getState) { return new Promise(function (resolve, reject) { DBG && console.log('DBG::unselectAll action ...'); bazaSelected.deleteAll() dispatch( setListChecked([], 0) ); }); }; } function selectAllMatchingFilter(namespace, filterQuery) { DBG && console.log('DBG::selectAllMatchingFilter', { filterQuery }); return function(dispatch, getState) { DBG && console.log('DBG::selectAllMatchingFilter state', state); var state = getState(); var listPrimaryKeys = state.listPrimaryKeys; // var reqArgs = { typeName: namespace, primaryKey: listPrimaryKeys, filterQuery: filterQuery }; // if (idContext) reqArgs.idContext = idContext; // return p5WPS__selectAllFeaturesMatchingFilter(reqArgs).then(function (listSelected) { // DBG && console.log('DBG::selectAllMatchingFilter dispatch success'); // dispatch( setListChecked(listSelected.selected, listSelected.totalSelected) ); // }).catch(function (e) { // p5UI__notifyAjaxCallback({type: 'error', msg: 'Wystąpił błąd #TA5: ' + e}); // DBG && console.warn('DBG::selectAllMatchingFilter ERROR dispatch error'); // // var fullListPks = ['select-all'].concat(listPrimaryKeys); // // dispatch( setListUnchecked(fullListPks) ); // // dispatch( setErrorMsg(e) ); // TODO: show error with msg and refresh button // dispatch( setListLoading([]) ); // }); return new Promise(function (resolve, reject) { DBG && console.log('DBG::selectAllMatchingFilter action: (pks:['+listPrimaryKeys.join(',')+'])...'); { reject('TODO: selectAllMatchingFilter'); return; } // resolve(); }); } } return { setPrimaryKeys: setPrimaryKeys, setChecked: setChecked, setUnchecked: setUnchecked, toggle: toggle, unselectAll: unselectAll, selectAllMatchingFilter: selectAllMatchingFilter, } } function bocian_createTableSelectedStateObject(idContext) { var _idContext = idContext || 0; return { store: createStoreWithThunkMiddleware(selectedStore), actions: selectedActions(_idContext), } } // var original_global__createTableSelectedStateObject = global.createTableSelectedStateObject; // to revert back to original if (BAZA_OD_DO) { global.createTableSelectedStateObject = bocian_createTableSelectedStateObject; }