TableAjax.php.edit.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // @require variables:
  2. if ('undefined' === typeof TABLE_AJAX_NODE_ID) throw "Missing TABLE_AJAX_NODE_ID"; // $this->_htmlID,
  3. if ('undefined' === typeof TABLE_AJAX_LABEL) throw "Missing TABLE_AJAX_LABEL"; // this->getLabelHtml()
  4. if ('undefined' === typeof FUNCTION_EDIT_ROUTE) throw "Missing FUNCTION_EDIT_ROUTE";
  5. if ('undefined' === typeof URL_EDIT_FORM_AJAX) throw "Missing URL_EDIT_FORM_AJAX";
  6. function TableAjax__EDIT_Route(args) {
  7. var recordID = args;
  8. if (typeof args == 'object') {
  9. recordID = args.shift();
  10. recordID = parseInt(recordID);
  11. }
  12. if (typeof recordID !== 'number' || recordID <= 0) {
  13. // TODO: msg
  14. return false;
  15. }
  16. var cont = jQuery('#' + TABLE_AJAX_NODE_ID).parent();
  17. cont.hide();
  18. // remove previous task content
  19. var taskCnt = jQuery('#'+TABLE_AJAX_NODE_ID+'_task');
  20. taskCnt.parent().remove();
  21. taskCnt.remove();
  22. var taskCont = jQuery('<div class="AjaxTableCont"></div>').insertBefore(cont);
  23. jQuery('<ul class="breadcrumb">' +
  24. '<li><a href="#" onclick="return tableAjaxBackToTable();">' + TABLE_AJAX_LABEL + '</a></li>' +
  25. '<li class="active">Edytuj rekord</li>' +
  26. '</ul>').appendTo(taskCont);
  27. taskCnt = jQuery('<div id="' + TABLE_AJAX_NODE_ID + '_task" class="AjaxTableTaskCnt AjaxTable-loading"></div>').appendTo(taskCont);
  28. jQuery('<span class="loading-info"> loading ...</span>').appendTo(taskCnt);
  29. window.fetch(URL_EDIT_FORM_AJAX + recordID, {
  30. method: 'GET',
  31. credentials: 'same-origin',
  32. }).then(function(response) {
  33. return response.json()
  34. }).then(function __route_edit_payload(payload) {
  35. taskCnt.removeClass('AjaxTable-loading');
  36. // console.log('editFormJson :: payload', payload)
  37. if ('success' == payload.type) {
  38. var node = document.createElement('div')
  39. taskCnt.get(0).appendChild(node)
  40. p5UI__buildDom(payload.body.reactNode, node)
  41. initDateTimePicker(jQuery(node));
  42. // console.log('editFormJson :: dom loaded - TODO: add action on save - P5UI__FeatureEditForm')
  43. } else {
  44. console.log('editFormJson :: ERROR payload', payload)
  45. }
  46. }).catch(function __route_edit_catch(e) {
  47. taskCnt.removeClass('AjaxTable-loading');
  48. console.log('editFormJson :: ERROR', e)
  49. p5UI__notifyAjaxCallback({
  50. type: 'error',
  51. msg: 'Request error ' + e
  52. });
  53. })
  54. }
  55. global[FUNCTION_EDIT_ROUTE] = TableAjax__EDIT_Route