TableAjax.php.hist.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 URL_HIST_BASE) throw "Missing URL_HIST_BASE"; // 'index-ajax.php?_zasobID={$this->_zasobID}&_cls={__CLASS__}&_hash={$this->_htmlID}&_task=HIST'; // &ID=...
  5. if ('undefined' === typeof FUNCTION_HIST_ROUTE) throw "Missing FUNCTION_HIST_ROUTE"; // ''
  6. function TableAjax__HIST_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">Historia rekordu</li>' +
  26. '</ul>').appendTo(taskCont);
  27. var 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_HIST_BASE + '&ID=' + recordID, {
  30. method: 'GET',
  31. credentials: 'same-origin',
  32. }).then(function(response) {
  33. return response.json()
  34. }).then(function __route_hist_payload(payload) {
  35. taskCnt.removeClass('AjaxTable-loading');
  36. var data = payload;
  37. histAjaxOut = '<fieldset>' +
  38. '<legend>' + data['label'] +
  39. '<span class="pull-right valign-btns-bottom">';
  40. for (i in data['row_functions']) {
  41. histAjaxOut += data['row_functions'][i];
  42. // TODO: fetch more row functions
  43. }
  44. histAjaxOut += '</span>' +
  45. '</legend>'
  46. '</fieldset>';
  47. if (!data['rows']) {
  48. histAjaxOut += '<div class="alert alert-info">' +
  49. '<h4>Brak danych</h4>' +
  50. '</div>';
  51. } else {
  52. histAjaxOut += '<table class="table table-striped table-hover table-bordered table-condensed AjaxTableHist">' +
  53. '<thead>' +
  54. '<tr>' +
  55. '<th>Data</th>' +
  56. '<th>Autor</th>' +
  57. '<th>Zmiany</th>' +
  58. '</tr>' +
  59. '</thead>' +
  60. '<tbody>';
  61. for (i in data['rows']) {
  62. var row = data['rows'][i];
  63. histAjaxOut += '<tr data-id_hist="' + row['ID'] + '">' +
  64. '<td style="white-space:nowrap">' + row['_created'] + '</td>' +
  65. '<td>' + row['_author'] + '</td>' +
  66. '<td>';
  67. for (j in row['changes']) {
  68. var change = row['changes'][j];
  69. var fieldName = change['fieldName'];
  70. if (['ID', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR', 'A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR'].indexOf(fieldName) >= 0) continue;
  71. if ('N/S;' == change['value']) continue;
  72. histAjaxOut += '<p>' +
  73. '<em>' + (data['field_label'][fieldName] || fieldName) + '</em>: ';
  74. histAjaxOut += (change['acl_read'])
  75. ? change['value']
  76. : '<span title="Brak uprawnień do odczytu tego pola">*****</span>'
  77. if (change['revert_function_url']) {
  78. histAjaxOut += ' <button' +
  79. ' class="btn btn-xs btn-default"' +
  80. ' onClick="return p5UI__ajaxLinkClick(this, \''+change['revert_function_url']+'\', \''+change['revert_function_data']+'\')"' +
  81. ' title="Cofnij dane do tej wartości">' +
  82. '<i class="glyphicon glyphicon-floppy-disk"></i> cofnij' +
  83. '</button>';
  84. }
  85. histAjaxOut += '</p>';
  86. }
  87. histAjaxOut += '</td>' +
  88. '</tr>';
  89. }
  90. histAjaxOut += '</tbody>' +
  91. '</table>';
  92. }
  93. {// old view - flat table
  94. histAjaxOut += '<div style="overflow-x:scroll; overflow-y:visible; padding-bottom:1px; margin:10px 0;">' +
  95. '<table class="table table-striped table-hover table-bordered table-condensed AjaxTableHist">' +
  96. '<thead>' +
  97. '<tr>' + "\n";
  98. histAjaxOut += '<th>Data</th>';
  99. histAjaxOut += '<th>Autor</th>';
  100. for (j in data['fields']) {
  101. var fieldName = data['fields'][j];
  102. histAjaxOut += '<th>' + (data['field_label'][fieldName] || fieldName).replace(/_/g, ' ') + '</th>';
  103. }
  104. histAjaxOut += '</tr>' +
  105. '</thead>' +
  106. '<tbody>';
  107. for (i in data['rows']) {
  108. var row = data['rows'][i];
  109. histAjaxOut += '<tr>';
  110. histAjaxOut += '<td><nobr>' + row['_created'] + '</nobr></td>';
  111. histAjaxOut += '<td>' + row['_author'] + '</td>';
  112. for (j in data['fields']) {
  113. var fieldName = data['fields'][j];
  114. histAjaxOut += '<td>';
  115. ( (!(fieldName in row['changes']) || row['changes'][fieldName]['value'] == 'N/S;')
  116. ? histAjaxOut += '<em>N/S;</em>'
  117. : ( (row['changes'][fieldName]['acl_read'])
  118. ? histAjaxOut += row['changes'][fieldName]['value']
  119. : histAjaxOut += '<span title="Brak uprawnień do odczytu tego pola">*****</span>'
  120. )
  121. );
  122. histAjaxOut += '</td>';
  123. }
  124. histAjaxOut += '</tr>';
  125. }
  126. histAjaxOut += '</tbody>' +
  127. '</table>' +
  128. '</div>';
  129. }
  130. jQuery(histAjaxOut).appendTo(taskCnt);
  131. }).catch(function __route_hist_catch(e) {
  132. taskCnt.removeClass('AjaxTable-loading');
  133. p5UI__notifyAjaxCallback({
  134. type: 'error',
  135. msg: 'Request error ' + e
  136. });
  137. })
  138. }
  139. global[FUNCTION_HIST_ROUTE] = TableAjax__HIST_Route