Quellcode durchsuchen

Merge branch 'test-live-select-podmiot'

Piotr Labudda vor 4 Jahren
Ursprung
Commit
884f8afb76
2 geänderte Dateien mit 365 neuen und 0 gelöschten Zeilen
  1. 335 0
      theme/assets/js/createTableSelectedStateObject.js
  2. 30 0
      theme/bocian.php

+ 335 - 0
theme/assets/js/createTableSelectedStateObject.js

@@ -0,0 +1,335 @@
+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: ' +
+		'<div class="tblAjax__footer__toolbar__pagination" style="display:inline-block">' +
+			'<a class="btn btn-xs btn-default' + (BAZA_OD_DO === 'OD' ? ' active' : '') + '" href="' + URL_BAZA_OD + '">' + 'OD' + '</a>' +
+			' ' +
+			'<i class="glyphicon glyphicon-random" style="vertical-align:middle"></i>' +
+			' ' +
+			'<a class="btn btn-xs btn-default' + (BAZA_OD_DO === 'DO' ? ' active' : '') + '" href="' + URL_BAZA_DO + '">' + 'DO' + '</a>' +
+			' ' +
+			(!BAZA_OD_DO ? '' : '<a class="btn btn-xs btn-default" href="' + URL_BAZA_NONE + '">' + 'Anuluj' + '</a>') +
+		'</div>' +
+	'';
+	
+	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;
+}

+ 30 - 0
theme/bocian.php

@@ -64,6 +64,36 @@ class Theme_bocian extends ThemeDefault {
 			'URL_BI_AUDIT_RESULT' => Router::getRoute('UrlAction_BiAuditGenerate')->getLink(''),
 			'ID_ARG_BI_AUDIT_RESULT' => 'ID_BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA'
 		]);
+
+
+		// override checkbox selecting for live select to localStorage - PROBLEM: how to choose OD/DO?
+		if ('ViewTableAjax' === V::get('_route', '', $_GET)
+			// TODO: what tables? maybe GET arg?
+			// && 'default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA' === V::get('namespace', '', $_GET)
+		) {
+			$keyPrefix = $this->_getKeyPrefixFromNamespace(V::get('namespace', '', $_GET));
+			if ($keyPrefix) {
+				UI::inlineJS(dirname(__FILE__) . '/assets/js/createTableSelectedStateObject.js', [
+					// 'URI_WPS' => Request::getPathUri() . 'wps.php',
+					'KEY_PREFIX' => $keyPrefix,
+					'BAZA_OD_DO' => V::get('BAZA_OD_DO', '', $_GET), // OD | DO | ''
+					'URL_BAZA_OD' => 'index.php?' . http_build_query(array_merge($_GET, [ 'BAZA_OD_DO' => 'OD' ])),
+					'URL_BAZA_DO' => 'index.php?' . http_build_query(array_merge($_GET, [ 'BAZA_OD_DO' => 'DO' ])),
+					'URL_BAZA_NONE' => 'index.php?' . http_build_query(array_merge($_GET, [ 'BAZA_OD_DO' => '' ])),
+					'DBG' => ('1' === V::get('DBG_JS', '', $_GET)),
+				]);
+			}
+		}
+	}
+	function _getKeyPrefixFromNamespace($ns) {
+		switch ($ns) {
+			case 'default_db/BI_audit_KRS/BI_audit_KRS': return 'KRS';
+			case 'default_db/BI_audit_KRS_person/BI_audit_KRS_person': return 'KRS/p';
+			case 'default_db/BI_audit_MSIG/BI_audit_MSIG': return 'MSIG';
+			case 'default_db/BI_audit_MSIG_person/BI_audit_MSIG_person': return 'MSIG/p';
+			case 'default_db/BI_audit_CEIDG/BI_audit_CEIDG': return 'CEIDG';
+			default: return '';
+		}
 	}
 
 	function login($data) {