AjaxContent.php.script.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var DBG = 0;
  2. var DBG1 = 1;
  3. if (!ID_HTML_NODE) throw "Missing 'ID_HTML_NODE'!";
  4. // if (!URL_FETCH_CONTENT) throw "Missing 'URL_FETCH_CONTENT'!";
  5. var URL_FETCH_CONTENT = URL_FETCH_CONTENT || '';
  6. if (!global.fetch) throw "Missing 'fetch'!";
  7. if (!global.p5VendorJs.ReactDOM) throw "Missing ReactDOM"
  8. if (!global.p5UI__buildDom) throw "Missing 'p5UI__buildDom'!"; // static/vendor.js, static/p5UI/buildDom.js
  9. if (!global.P5UI__AjaxContent) throw "Missing 'P5UI__AjaxContent'!"; // static/vendor.js, static/p5UI/AjaxContent.js
  10. var NOTIFY_USER_MODE = NOTIFY_USER_MODE || 'inline'; // inline | notify
  11. var LOADING = LOADING || 'loading ...';
  12. var ReactDOM = global.p5VendorJs.ReactDOM;
  13. var h = global.p5VendorJs.React.createElement;
  14. var P5UI__AjaxContent = global.P5UI__AjaxContent;
  15. var rootNode = document.getElementById(ID_HTML_NODE);
  16. if (!rootNode) throw "Node not exists";
  17. ReactDOM.render(
  18. h(P5UI__AjaxContent, {
  19. url: URL_FETCH_CONTENT,
  20. loading: LOADING,
  21. }),
  22. rootNode
  23. )
  24. return;
  25. global.fetch(URL_FETCH_CONTENT, {
  26. credentials: "same-origin",
  27. }).then(function (response) {
  28. DBG1 && console.log("DBG:response", { status: response.status, response: response });
  29. response.text()
  30. .then(response.ok ? _handleFetchResponseSuccess : _handleFetchResponseFail)
  31. .catch(function (err) {
  32. DBG1 && console.log("Response Error #1:")
  33. DBG1 && console.error(err)
  34. // throw err;
  35. doNotify('error', "" + err);
  36. })
  37. }).then(function (data) {
  38. DBG1 && console.warn("DBG:response data", { data });
  39. }).catch(function (err) {
  40. DBG1 && console.log("Response Error #2:")
  41. DBG1 && console.error(err)
  42. })
  43. function _handleFetchResponseFail(responseText) {
  44. DBG1 && console.warn("DBG:response fail responseText", { responseText });
  45. if (!responseText) throw "Error";
  46. _handleFetchResponseText(false, responseText)
  47. }
  48. function _handleFetchResponseSuccess(responseText) {
  49. DBG1 && console.warn("DBG:response success responseText", { responseText });
  50. _handleFetchResponseText(true, responseText)
  51. }
  52. function _handleFetchResponseText(isOk, responseText) {
  53. if ('{' !== responseText.substr(0, 1)) throw "Response Parse Error: Expected json.";
  54. try {
  55. var jsonResponse = JSON.parse(responseText);
  56. if (!jsonResponse.msg) throw "Response Parse Error: Expected json with msg.";
  57. if (!isOk) throw jsonResponse.msg; // TODO: throw or just show alert for user
  58. _handleFetchResponseJson(jsonResponse)
  59. } catch (err) {
  60. throw "Response Parse Error: " + err;
  61. }
  62. }
  63. function _handleFetchResponseJson(jsonResponse) {
  64. switch (jsonResponse.type) {
  65. case 'error': return doNotify('error', (jsonResponse.msg || "Wystąpił błąd"));
  66. case 'success': return doShowContent(jsonResponse.body);
  67. default: return doNotify('warning', jsonResponse.msg);
  68. }
  69. }
  70. function doNotify(type, msg) {
  71. // TODO: if (NOTIFY_USER_MODE) // 'inline' | 'notify'
  72. type = ('error' == type) ? 'danger' : type;
  73. rootNode.innerHTML = '<div class="alert alert-' + type + '" style="margin-bottom:0">' + msg + '</div>';
  74. }
  75. function doShowContent(body) {
  76. if (body.reactNode) return p5UI__buildDom(body.reactNode, rootNode);
  77. if (body.html) return doShowHtmlContent(body.html, rootNode);
  78. if (typeof body === 'string' || body instanceof String) return doShowHtmlContent(body, rootNode);
  79. else {
  80. // TODO: if DBG
  81. rootNode.innerHTML = '<pre>' + JSON.stringify(body, null, 4) + '</pre>';
  82. }
  83. }
  84. function doShowHtmlContent(htmlContent, node) {
  85. node.innerHTML = htmlContent;
  86. }
  87. // }).then(function (response) {
  88. // return response.text()
  89. // }).then(function (responseText) {
  90. // try {
  91. // return JSON.parse(responseText)
  92. // } catch (e) {
  93. // throw responseText
  94. // }
  95. // }).then(function (result) {
  96. // if ('success' == result.type) {
  97. // p5UI__notifyAjaxCallback(result)
  98. // resolve(result.data)
  99. // } else {
  100. // p5UI__notifyAjaxCallback(result)
  101. // reject(result.msg || "Wystąpił błąd!")
  102. // }
  103. // }).catch(function (e) {
  104. // reject("ajax response error: " + e)
  105. // });
  106. // .fetch(P5MENU_URL
  107. // , (!postData)
  108. // ? { method: 'GET',
  109. // headers: { 'Content-Type': 'application/json' },
  110. // credentials: 'same-origin',
  111. // }
  112. // : { method: 'POST',
  113. // headers: { 'Content-Type': 'application/json' },
  114. // credentials: 'same-origin',
  115. // body: JSON.stringify(postData)
  116. // }
  117. // ).then(function (response) {
  118. // return response.json()
  119. // }).then(function (response) {
  120. // if ('success' === response.type) {
  121. // _update(response.body)
  122. // } else {
  123. // // err...
  124. // }
  125. // })