createTableSelectedStateObject.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. var DBG = DBG || false;
  2. var DBG1 = true;
  3. // if (!URI_WPS) throw "Missing URI_WPS";
  4. if (!KEY_PREFIX) return; // KEY_PREFIX = KRS | KRS/p | MSIG | MSIG/p | CEIDG
  5. if (!URL_BAZA_OD) throw "Missing URL_BAZA_OD";
  6. if (!URL_BAZA_DO) throw "Missing URL_BAZA_DO";
  7. if (!URL_BAZA_NONE) throw "Missing URL_BAZA_NONE";
  8. var BAZA_OD_DO = BAZA_OD_DO || false;
  9. BAZA_OD_DO = (BAZA_OD_DO === 'OD' || BAZA_OD_DO === 'DO') ? BAZA_OD_DO : '';
  10. { // make fixed panel to select checkbox context OD, DO or default
  11. function _px(px) { return (!px) ? '0px' : '' + px + 'px'; }
  12. var panelNode = document.createElement('div')
  13. var panelWidth = 600;
  14. panelNode.style.position = 'fixed';
  15. panelNode.style.top = _px(0);
  16. panelNode.style.width = _px(panelWidth);
  17. panelNode.style.left = _px(window.innerWidth > panelWidth ? Math.floor(window.innerWidth / 2 - panelWidth / 2) : 0);
  18. panelNode.style.padding = '8px 15px'
  19. panelNode.style.textAlign = 'center'
  20. panelNode.className = 'alert alert-info'
  21. panelNode.style.boxShadow = '6px 6px 20px 0px #31708f'
  22. // panelNode.style.border = '1px solid red'
  23. // panelNode.style.padding = '6px'
  24. // panelNode.style.backgroundColor = 'lightRed'
  25. // panelNode.style.color = 'black'
  26. panelNode.innerHTML = 'Wybierz elementy do raportu: ' +
  27. '<div class="tblAjax__footer__toolbar__pagination" style="display:inline-block">' +
  28. '<a class="btn btn-xs btn-default' + (BAZA_OD_DO === 'OD' ? ' active' : '') + '" href="' + URL_BAZA_OD + '">' + 'OD' + '</a>' +
  29. ' ' +
  30. '<i class="glyphicon glyphicon-random" style="vertical-align:middle"></i>' +
  31. ' ' +
  32. '<a class="btn btn-xs btn-default' + (BAZA_OD_DO === 'DO' ? ' active' : '') + '" href="' + URL_BAZA_DO + '">' + 'DO' + '</a>' +
  33. ' ' +
  34. (!BAZA_OD_DO ? '' : '<a class="btn btn-xs btn-default" href="' + URL_BAZA_NONE + '">' + 'Anuluj' + '</a>') +
  35. '</div>' +
  36. '';
  37. document.body.appendChild(panelNode)
  38. }
  39. if (!BAZA_OD_DO) return;
  40. if (!global.p5VendorJs) throw "Missing p5 Vendor js libs";
  41. if (!global.p5VendorJs.Redux) throw "Missing p5 Vendor js lib: Redux";
  42. if (!localStorage) throw "Missing localStorage!";
  43. var Redux = global.p5VendorJs.Redux;
  44. var ReduxThunk = global.p5VendorJs.ReduxThunk;
  45. var createStoreWithThunkMiddleware = Redux.applyMiddleware(ReduxThunk)(Redux.createStore); // TODO: to vendor.js
  46. {
  47. // - `Bocian.biAuditForm.odFeatureKeys`: `["CEIDG.879134","KRS/p.4398202"]`
  48. // - `Bocian.biAuditForm.doFeatureKeys`: `["KRS.1533062","KRS/p.4616336"]`
  49. var bazaSelected = (function (from, prefix) { // from: OD | DO, prefix: KRS | KRS/p | MSIG | MSIG/p | CEIDG
  50. function _getSelectedListLocalStorageKey(from) {
  51. switch (from) {
  52. case 'OD': return 'Bocian.biAuditForm.odFeatureKeys';
  53. case 'DO': return 'Bocian.biAuditForm.doFeatureKeys';
  54. default: throw "Not implemented from selected list '" + from + "' - expected 'OD' or 'DO'";
  55. }
  56. }
  57. var _dbKey = _getSelectedListLocalStorageKey(from)
  58. function _fetchAll() {
  59. return JSON.parse( localStorage.getItem(_dbKey) || '[]' );
  60. }
  61. function _fetchList() {
  62. return _fetchAll().filter(function (key) {
  63. return (key.substr(0, prefix.length + 1) === prefix + '.');
  64. })
  65. }
  66. function _toKey(pk) { return prefix + '.' + pk; }
  67. function _isBaza(key) { return (key.substr(0, prefix.length + 1) === prefix + '.'); }
  68. function _isNotBaza(key) { return !_isBaza(key); }
  69. // function _toPk(key) { return key.substr(prefix.length + 1); }
  70. return {
  71. getTotal: function () { return _fetchList().length; },
  72. add: function (pk) {
  73. var all = new Set(_fetchAll())
  74. all.add(_toKey(pk))
  75. var newValue = Array.from(all)
  76. localStorage.setItem(_dbKey, JSON.stringify(newValue))
  77. },
  78. delete: function (pk) {
  79. var all = new Set(_fetchAll())
  80. all.delete(_toKey(pk))
  81. var newValue = Array.from(all)
  82. localStorage.setItem(_dbKey, JSON.stringify(newValue))
  83. },
  84. addList: function (pks) {
  85. var all = new Set(_fetchAll())
  86. pks.forEach(function (pk) {
  87. all.add(_toKey(pk))
  88. })
  89. var newValue = Array.from(all)
  90. localStorage.setItem(_dbKey, JSON.stringify(newValue))
  91. },
  92. deleteList: function (pks) {
  93. var all = new Set(_fetchAll())
  94. pks.forEach(function (pk) {
  95. all.delete(_toKey(pk))
  96. })
  97. var newValue = Array.from(all)
  98. localStorage.setItem(_dbKey, JSON.stringify(newValue))
  99. },
  100. deleteAll: function () {
  101. var newValue = _fetchAll().filter(_isNotBaza)
  102. localStorage.setItem(_dbKey, JSON.stringify(newValue))
  103. },
  104. getOnlySelectedPks: function (listPrimaryKeys) {
  105. var all = new Set(_fetchAll())
  106. return listPrimaryKeys.filter(function (pk) {
  107. return all.has(_toKey(pk))
  108. })
  109. },
  110. }
  111. })(BAZA_OD_DO, KEY_PREFIX);
  112. }
  113. function selectedStore(state, action) {
  114. var prevState = state || { isLoading: false, selected: [], loading: [], listPrimaryKeys: [], totalSelected: 0 };
  115. DBG && console.log('DBG::selectedStore', { prevState, action });
  116. switch (action.type) {
  117. case 'SET_CHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, {
  118. loading: p5Utils__arrayRemove(prevState.loading, action.primaryKey),
  119. selected: prevState.selected.concat(action.primaryKey),
  120. totalSelected: action.totalSelected,
  121. }));
  122. case 'SET_UNCHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, {
  123. loading: p5Utils__arrayRemove(prevState.loading, action.primaryKey),
  124. selected: p5Utils__arrayRemove(prevState.selected, action.primaryKey),
  125. totalSelected: action.totalSelected,
  126. }));
  127. case 'SET_LIST_CHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, {
  128. loading: [], // TODO: p5Utils__arrayExcept(prevState.loading, action.listPrimaryKeys),
  129. selected: [].concat(action.listPrimaryKeys), // TODO: p5Utils__arrayUniqueUnion(prevState.selected, action.listPrimaryKeys)
  130. totalSelected: action.totalSelected,
  131. }));
  132. case 'SET_LIST_UNCHECKED': return fixSelectAllInSelectedState(Object.assign({}, prevState, {
  133. loading: [], // TODO: p5Utils__arrayExcept(prevState.loading, action.listPrimaryKeys),
  134. selected: [], // TODO: p5Utils__arrayUniqueUnion(prevState.selected, action.listPrimaryKeys)
  135. totalSelected: action.totalSelected,
  136. }));
  137. case 'SET_LOADING': return Object.assign(prevState, {
  138. loading: prevState.loading.concat(action.primaryKey)
  139. });
  140. case 'SET_LIST_LOADING': return Object.assign(prevState, {
  141. loading: [].concat(action.listPrimaryKeys)
  142. });
  143. case 'SET_PRIMARY_KEYS': return fixSelectAllInSelectedState(Object.assign({}, prevState, {
  144. listPrimaryKeys: [].concat(action.listPrimaryKeys),
  145. loading: [].concat(action.listPrimaryKeys)
  146. }));
  147. // case 'SET_ERROR_MSG': return Object.assign(prevState, {
  148. // errorMsg: action.errorMsg
  149. // });
  150. default: return prevState;
  151. }
  152. }
  153. function fixSelectAllInSelectedState(state) {
  154. var len = state.listPrimaryKeys.length;
  155. var selLen = state.selected.length;
  156. var isAllSelected = ( len > 0 && selLen >= len ); // selected array may contain 'select-all'
  157. for (var i = 0, pk = ''; i < len; i++) {
  158. pk = state.listPrimaryKeys[i];
  159. if (-1 === state.selected.indexOf(pk)) {
  160. isAllSelected = false;
  161. break;
  162. }
  163. }
  164. var isSelectedSelectAll = ( -1 !== state.selected.indexOf('select-all') );
  165. if (isAllSelected && !isSelectedSelectAll) return Object.assign(state, {
  166. selected: ['select-all'].concat(state.selected)
  167. });
  168. if (!isAllSelected && isSelectedSelectAll) return Object.assign(state, {
  169. selected: p5Utils__arrayRemove(state.selected, 'select-all'),
  170. });
  171. return state;
  172. }
  173. function p5Utils__arrayRemove(array, toRemoveItem) {
  174. return array.filter(function (item) {
  175. return ( item !== toRemoveItem );
  176. });
  177. }
  178. function selectedActions(idContext) {
  179. var idContext = idContext || null;
  180. function setChecked(primaryKey, totalSelected) {
  181. return { type: 'SET_CHECKED', primaryKey: primaryKey, totalSelected: totalSelected }
  182. }
  183. function setUnchecked(primaryKey, totalSelected) {
  184. return { type: 'SET_UNCHECKED', primaryKey: primaryKey, totalSelected: totalSelected }
  185. }
  186. function setLoading(primaryKey) {
  187. return { type: 'SET_LOADING', primaryKey: primaryKey }
  188. }
  189. function setListChecked(listPrimaryKeys, totalSelected) {
  190. return { type: 'SET_LIST_CHECKED', listPrimaryKeys: listPrimaryKeys, totalSelected: totalSelected }
  191. }
  192. function setListUnchecked(listPrimaryKeys, totalSelected) {
  193. return { type: 'SET_LIST_UNCHECKED', listPrimaryKeys: listPrimaryKeys, totalSelected: totalSelected }
  194. }
  195. function setListLoading(listPrimaryKeys) {
  196. return { type: 'SET_LIST_LOADING', listPrimaryKeys: listPrimaryKeys }
  197. }
  198. function setErrorMsg(errorMsg) {
  199. return { type: 'SET_ERROR_MSG', errorMsg: errorMsg }
  200. }
  201. function toggle(namespace, primaryKey, checked) {
  202. DBG && console.log('DBG::toggle action: (pks:['+primaryKey+'])...');
  203. if ('select-all' === primaryKey) return toggleAll(namespace, checked);
  204. return function(dispatch, getState) {
  205. dispatch(setLoading(primaryKey));
  206. return new Promise(function (resolve, reject) {
  207. DBG && console.log('DBG::toggle action: (pks:['+primaryKey+'])...');
  208. if (checked) {
  209. bazaSelected.add(primaryKey)
  210. dispatch( setChecked(primaryKey, bazaSelected.getTotal()) );
  211. } else {
  212. bazaSelected.delete(primaryKey)
  213. dispatch( setUnchecked(primaryKey, bazaSelected.getTotal()) );
  214. }
  215. });
  216. }
  217. }
  218. function setPrimaryKeys(namespace, listPrimaryKeys) { // TODO: should set list primaryKeys, change checkboxes to loading and fetch its state, then update gui
  219. DBG && console.log('DBG::setPrimaryKeys action: (pks:['+listPrimaryKeys.join(',')+'])...');
  220. return function(dispatch, getState) {
  221. dispatch({ type: 'SET_PRIMARY_KEYS', listPrimaryKeys: listPrimaryKeys });
  222. // dispatch( setListLoading(listPrimaryKeys) );
  223. if (!listPrimaryKeys.length) return;
  224. return new Promise(function (resolve, reject) {
  225. DBG && console.log('DBG::setPrimaryKeys action: (pks:['+listPrimaryKeys.join(',')+'])...');
  226. var curSelectedPK = bazaSelected.getOnlySelectedPks(listPrimaryKeys)
  227. dispatch( setListChecked(curSelectedPK, bazaSelected.getTotal()) );
  228. });
  229. }
  230. }
  231. function toggleAll(namespace, checked) {
  232. var primaryKey = 'select-all';
  233. return function(dispatch, getState) {
  234. dispatch(setLoading(primaryKey));
  235. var state = getState();
  236. var listPrimaryKeys = state.listPrimaryKeys;
  237. return new Promise(function (resolve, reject) {
  238. DBG && console.log('DBG::toggleAll action: (pks:['+listPrimaryKeys.join(',')+'])...');
  239. if (checked) {
  240. bazaSelected.addList(listPrimaryKeys)
  241. dispatch( setListChecked(listPrimaryKeys, bazaSelected.getTotal()) );
  242. } else {
  243. bazaSelected.deleteList(listPrimaryKeys)
  244. dispatch( setListUnchecked(listPrimaryKeys, bazaSelected.getTotal()) );
  245. }
  246. });
  247. }
  248. }
  249. function unselectAll(namespace) {
  250. return function(dispatch, getState) {
  251. return new Promise(function (resolve, reject) {
  252. DBG && console.log('DBG::unselectAll action ...');
  253. bazaSelected.deleteAll()
  254. dispatch( setListChecked([], 0) );
  255. });
  256. };
  257. }
  258. function selectAllMatchingFilter(namespace, filterQuery) {
  259. DBG && console.log('DBG::selectAllMatchingFilter', { filterQuery });
  260. return function(dispatch, getState) {
  261. DBG && console.log('DBG::selectAllMatchingFilter state', state);
  262. var state = getState();
  263. var listPrimaryKeys = state.listPrimaryKeys;
  264. // var reqArgs = { typeName: namespace, primaryKey: listPrimaryKeys, filterQuery: filterQuery };
  265. // if (idContext) reqArgs.idContext = idContext;
  266. // return p5WPS__selectAllFeaturesMatchingFilter(reqArgs).then(function (listSelected) {
  267. // DBG && console.log('DBG::selectAllMatchingFilter dispatch success');
  268. // dispatch( setListChecked(listSelected.selected, listSelected.totalSelected) );
  269. // }).catch(function (e) {
  270. // p5UI__notifyAjaxCallback({type: 'error', msg: 'Wystąpił błąd #TA5: ' + e});
  271. // DBG && console.warn('DBG::selectAllMatchingFilter ERROR dispatch error');
  272. // // var fullListPks = ['select-all'].concat(listPrimaryKeys);
  273. // // dispatch( setListUnchecked(fullListPks) );
  274. // // dispatch( setErrorMsg(e) ); // TODO: show error with msg and refresh button
  275. // dispatch( setListLoading([]) );
  276. // });
  277. return new Promise(function (resolve, reject) {
  278. DBG && console.log('DBG::selectAllMatchingFilter action: (pks:['+listPrimaryKeys.join(',')+'])...');
  279. {
  280. reject('TODO: selectAllMatchingFilter');
  281. return;
  282. }
  283. // resolve();
  284. });
  285. }
  286. }
  287. return {
  288. setPrimaryKeys: setPrimaryKeys,
  289. setChecked: setChecked,
  290. setUnchecked: setUnchecked,
  291. toggle: toggle,
  292. unselectAll: unselectAll,
  293. selectAllMatchingFilter: selectAllMatchingFilter,
  294. }
  295. }
  296. function bocian_createTableSelectedStateObject(idContext) {
  297. var _idContext = idContext || 0;
  298. return {
  299. store: createStoreWithThunkMiddleware(selectedStore),
  300. actions: selectedActions(_idContext),
  301. }
  302. }
  303. // var original_global__createTableSelectedStateObject = global.createTableSelectedStateObject; // to revert back to original
  304. if (BAZA_OD_DO) {
  305. global.createTableSelectedStateObject = bocian_createTableSelectedStateObject;
  306. }