Ant.php.async.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. if (!RUN_ANT_JOB_URL) throw "Missing RUN_ANT_JOB_URL";
  2. if (!RUN_TEST_ANT_JOB_URL) throw "Missing RUN_TEST_ANT_JOB_URL";
  3. var DBG = (DBG) ? true : false;
  4. var DBG1 = true;
  5. function p5UI_runAntAsyncJob(event, targetNode, props) {
  6. event.stopPropagation();
  7. event.preventDefault();
  8. var props = props || {};
  9. var href = targetNode.getAttribute('href');
  10. var namespace = targetNode.getAttribute('data-namespace');
  11. var primaryKey = targetNode.getAttribute('data-primaryKey');
  12. var ant_path = targetNode.getAttribute('data-ant_path');
  13. var ant_template = targetNode.getAttribute('data-ant_template');
  14. DBG1 && console.log("DBG:p5UI_runAntAsyncJob ", { props_TEST: props.TEST ? true : false, targetNode, href, namespace, primaryKey, ant_path, ant_template })
  15. if (targetNode._isLoading) return;
  16. {
  17. targetNode._isLoading = true;
  18. var handleRemoveRefreshIcon = _addRefreshIcon(targetNode);
  19. var handleRemoveRefreshIcon = (function (targetNode) {
  20. return function () {
  21. if (targetNode._isLoading) {
  22. targetNode._isLoading = false;
  23. _removeRefreshIcon(targetNode);
  24. }
  25. }
  26. })(targetNode);
  27. // setTimeout(handleRemoveRefreshIcon, 1000);
  28. global.fetch(props.TEST ? RUN_TEST_ANT_JOB_URL : RUN_ANT_JOB_URL, {
  29. method: 'POST',
  30. header: {
  31. 'contentType': 'applications/json'
  32. },
  33. credentials: 'same-origin',
  34. body: JSON.stringify({
  35. namespace,
  36. primaryKey,
  37. ant_path,
  38. ant_template,
  39. })
  40. }).then(function (response) {
  41. handleRemoveRefreshIcon()
  42. return response.json()
  43. })
  44. .then(function (result) {
  45. DBG1 && console.log("DBG:response", { result })
  46. if (result.type && 'error' === result.type) return _handleResponseError(result);
  47. if (result.type && 'success' === result.type) return _handleResponseSuccess(result);
  48. _handleResponseError(result);
  49. });
  50. }
  51. return false;
  52. }
  53. function _handleResponseError(result) {
  54. p5UI__notifyAjaxCallback(result);
  55. }
  56. function _handleResponseSuccess(result) {
  57. p5UI__notifyAjaxCallback(result);
  58. }
  59. function _addRefreshIcon(targetNode) {
  60. var loadingNode = document.createElement('i');
  61. loadingNode.setAttribute('class', 'glyphicon glyphicon-refresh');
  62. loadingNode.style.marginLeft = "8px";
  63. loadingNode._p5Node = 'loading-icon';
  64. targetNode.appendChild(loadingNode);
  65. }
  66. function _removeRefreshIcon(targetNode) {
  67. var lastNode = targetNode.lastChild;
  68. if (!lastNode) return;
  69. DBG1 && console.log("DBG:_removeRefreshIcon", { targetNode, lastNode, lastNode_p5Node: lastNode._p5Node });
  70. if (!lastNode._p5Node) return;
  71. if ('loading-icon' != lastNode._p5Node) return;
  72. targetNode.removeChild(lastNode);
  73. }
  74. global.p5UI_runAntAsyncJob = p5UI_runAntAsyncJob;