AclStruct.php.tableActions.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // @require var ID_STORAGE
  2. // @require var TABLE_NAME
  3. // @require var FETCH_URL
  4. // @require var ADD_ACTION_URL
  5. function Storage__tableStruct__addAction() {
  6. fetch(FETCH_URL, {
  7. method: 'POST',
  8. credentials: 'same-origin',// add cookies
  9. headers: { 'Content-Type': 'application/json' },
  10. body: JSON.stringify({
  11. idStorage: ID_STORAGE,
  12. table: TABLE_NAME
  13. })
  14. }).then(function (response) {
  15. if ('application/json' !== response.headers.get('Content-Type').substr(0, 16)) {
  16. throw 'Wrong response type: ' + response.headers.get('Content-Type')
  17. }
  18. return response.json()
  19. }).then(function (data) {
  20. if (!data) throw 'Empty response';
  21. console.log('loadDataAjax:fetch: request finished data:', data);
  22. if ('error' === data.type) {
  23. p5UI__notifyAjaxCallback(data);
  24. console.log('loadDataAjax:fetch: ERR data:', data);
  25. } else if ('success' === data.type) {
  26. if (!data.options) throw 'Missing options'
  27. if (!data.options.length) throw 'Brak akcji do wyboru'
  28. Storage__tableStruct__fetchedAddAction(data.options);
  29. } else {
  30. p5UI__notifyAjaxCallback(data);
  31. console.log('loadDataAjax:fetch: ERR data:', data);
  32. }
  33. }).catch(function (e) {
  34. p5UI__notifyAjaxCallback({
  35. type: 'error',
  36. msg: 'Request error: ' + e
  37. });
  38. console.log('fetchActionList: ERR:', e);
  39. });
  40. }
  41. function Storage__tableStruct__fetchedAddAction(options) {
  42. var optionsList = {};
  43. options.forEach(function (option) {
  44. optionsList[option.ID] = '[' + option.ID + '] ' + option.DESC
  45. })
  46. swal({
  47. title: 'Dodaj Akcję',
  48. input: 'radio',
  49. inputOptions: optionsList,
  50. showCancelButton: true,
  51. showCloseButton: true,
  52. inputClass: 'p5-swal-radio-as-list',
  53. inputValidator: function (result) {
  54. return new Promise(function (resolve, reject) {
  55. if (result) {
  56. resolve()
  57. } else {
  58. reject('Wybierz akcję')
  59. }
  60. })
  61. }
  62. }).then(function (result) {
  63. fetch(ADD_ACTION_URL, {
  64. method: 'POST',
  65. credentials: 'same-origin',// add cookies
  66. headers: { 'Content-Type': 'application/json' },
  67. body: JSON.stringify({
  68. idStorage: ID_STORAGE,
  69. table: TABLE_NAME,
  70. idAction: result
  71. })
  72. }).then(function (response) {
  73. if ('application/json' !== response.headers.get('Content-Type').substr(0, 16)) {
  74. throw 'Wrong response type: ' + response.headers.get('Content-Type')
  75. }
  76. return response.json()
  77. }).then(function (data) {
  78. if (!data) throw 'Empty response';
  79. console.log('ADD_ACTION_URL:fetch: request finished data:', data);
  80. if ('error' === data.type) {
  81. p5UI__notifyAjaxCallback(data);
  82. console.log('ADD_ACTION_URL:fetch: ERR data:', data);
  83. } else if ('success' === data.type) {
  84. p5UI__notifyAjaxCallback(data);
  85. } else {
  86. p5UI__notifyAjaxCallback(data);
  87. console.log('ADD_ACTION_URL:fetch: ERR data:', data);
  88. }
  89. }).catch(function (e) {
  90. p5UI__notifyAjaxCallback({
  91. type: 'error',
  92. msg: 'Request error: ' + e
  93. });
  94. console.log('fetchActionList: ERR:', e);
  95. });
  96. console.log('TODO: result', result);
  97. }).catch(function (e) {
  98. if ('close' === e || 'cancel' === e || 'overlay' === e) {
  99. } else throw e
  100. })
  101. }
  102. function Storage__tableStruct__editActionLabel(n, idAction) {
  103. console.log('F.Storage__tableStruct__editActionLabel idAction('+idAction+')', n)
  104. swal('TODO', 'Edytuj nazwę akcji', 'warning')
  105. }
  106. function Storage__tableStruct__addParamAction(n, idAction, flatDefArgs) {
  107. var defArgs = (flatDefArgs || '').split(';').map(function (flatArg) {
  108. var arg = flatArg.split('=')
  109. return { ID: arg[0], DESC: arg[1]}
  110. })
  111. console.log('F.Storage__tableStruct__addParamAction idAction('+idAction+')', defArgs, n)
  112. swal('TODO', '<div style=\"text-align:left\">Add PARAM_IN:<br> - ' + JSON.stringify(defArgs) + '<br> - or custom param' + '</div>', 'warning')
  113. }
  114. global.Storage__tableStruct__addAction = Storage__tableStruct__addAction
  115. global.Storage__tableStruct__fetchedAddAction = Storage__tableStruct__fetchedAddAction
  116. global.Storage__tableStruct__editActionLabel = Storage__tableStruct__editActionLabel
  117. global.Storage__tableStruct__addParamAction = Storage__tableStruct__addParamAction