UserProNetMediaZaliczka.php.fetchData.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // @require var SYNC_URL
  2. function zaliczkaFetchData(query, args, resultCallback) {
  3. console.warn('TODO: zaliczkaFetchData(query, objectNamespace, resultCallback) query('+query+') args:', args);
  4. // objectNamespace: Object {"@namespace": "default_db:IN7_DZIENNIK_KORESP", id: "xsd:integer", title: "xsd:string", kategoriaKosztu: "xsd:string"}
  5. if ('object' !== typeof args) {
  6. console.log("BUG: Unsupported type '" + args + "'");
  7. return;
  8. }
  9. if (!args['@namespace'] && !args['objectNamespace']) {
  10. // TODO: log error
  11. console.error("BUG: Missing namespace in args");
  12. return;
  13. }
  14. if (args['@namespace']) {
  15. switch (args['@namespace']) {
  16. case 'default_db/IN7_MK_BAZA_DYSTRYBUCJI/ZaliczkaProjekt':
  17. case 'default_db/IN7_DZIENNIK_KORESP/ZaliczkaKoresp':
  18. {
  19. superagent
  20. .post(SYNC_URL)
  21. .type('json') // header ĺapplication/x-www-form-urlencoded' requires type('form');
  22. .send({
  23. schema: args,
  24. query: query
  25. })
  26. .set('Accept', 'application/json')
  27. .end(function(err, res) {
  28. var payload = (err || !res.ok || 'application/json' !== res.type)
  29. ? {type: 'warning', msg: (res.body && res.body.msg) ? res.body.msg : 'Request error', body: res.body}
  30. : {type: 'success', msg: res.body.msg || '', body: res.body}
  31. jQuery(document).trigger('DBG:notify', payload)
  32. console.log('F.fetchDataAjax res.body', res.body)
  33. console.log('F.fetchDataAjax payload', payload)
  34. jQuery.notify(payload.msg, payload.type)
  35. resultCallback(payload.body)
  36. });
  37. }
  38. break;
  39. default: {
  40. console.log("BUG: Unsupported type namespace '" + args['@namespace'] + "'");
  41. }
  42. }
  43. return;
  44. }
  45. if ('default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja' == args['objectNamespace']) {
  46. switch (args['childName']) {
  47. case 'kategoria_kosztu': {
  48. fetch(SYNC_URL, {
  49. method: 'POST',
  50. credentials: 'same-origin',// add cookies
  51. headers: { 'Content-Type': 'application/json' },
  52. body: JSON.stringify({
  53. objectNamespace: args['objectNamespace'],
  54. childName: args['childName'],
  55. query: query
  56. })
  57. }).then(function (response) {
  58. return response.json()
  59. }).then(function (data) {
  60. if (!data) throw "Empty response";
  61. console.log('loadDataAjax:fetch: request finished data:', data);
  62. if ('error' === data.type) {
  63. p5UI__notifyAjaxCallback(data);
  64. console.log('loadDataAjax:fetch: ERR:', e);
  65. } else if ('success' === data.type) {
  66. resultCallback({options: data.options || []});
  67. } else {
  68. p5UI__notifyAjaxCallback(data);
  69. console.log('loadDataAjax:fetch: ERR:', e);
  70. }
  71. }).catch(function (e) {
  72. p5UI__notifyAjaxCallback({
  73. type: 'error',
  74. msg: "Request error " + e
  75. });
  76. console.log('loadDataAjax:fetch: ERR:', e);
  77. });
  78. } break;
  79. default: {
  80. console.warn("BUG: Unsupported namespace '" + args['objectNamespace'] + "'");
  81. }
  82. }
  83. return;
  84. }
  85. }
  86. global.zaliczkaFetchData = zaliczkaFetchData