consentReview.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. var infoEl = $('tr.info').clone();
  2. var strings = strings;
  3. /* Formatting function for row details - modify as you need */
  4. function format(d) {
  5. var detail = infoEl.clone();
  6. // show
  7. $(detail).toggleClass('hidden');
  8. // add data
  9. var dec = d.decision;
  10. // date
  11. detail.find('.created-date')[0].append(date(dec.createdDate).toLocaleString());
  12. // options & reminder
  13. if (dec.options === 'ALWAYS') {
  14. detail.find('.consent-reminder').parent().remove();
  15. } else {
  16. var unit = dec.reminderTimeUnit.toLowerCase();
  17. detail.find('.consent-reminder span:not(.' + unit + ')').remove();
  18. if (dec.reminder === 1) {
  19. var _unit = detail.find('.consent-reminder span.' + unit);
  20. _unit.html(_unit.text().slice(0, -1));
  21. }
  22. detail.find('.consent-reminder').prepend(dec.reminder);
  23. }
  24. detail.find('.consent-options span:not(.' + dec.options.toLowerCase().replace('_','-') + ')').remove();
  25. // render attribute table
  26. attributeTable(detail.find('.consent-attributes'),d.attributes);
  27. // enable tooltip
  28. detail.find('.consent-options [data-toggle="tooltip"]').tooltip();
  29. // setup delete button
  30. var del = detail.find('.btn-danger');
  31. var data = { 'id': dec.id, 'service': dec.service };
  32. del.on('click', data, function (e) {
  33. e.preventDefault();
  34. confirm(e.data.id, e.data.service);
  35. });
  36. return detail;
  37. }
  38. function alertUser(message, alertType) {
  39. $('#alertWrapper').append('<div id="alertdiv" class="alert alert-' + alertType + ' alert-dismissible">' +
  40. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' +
  41. '<span class="alertMessage">' + message + '</span></div>'
  42. );
  43. setTimeout(function () { // this will automatically close the alert and remove this if the users doesnt close it in 5 secs
  44. $('#alertdiv').remove();
  45. }, 5000);
  46. }
  47. function attributeTable(t, attributes) {
  48. var table;
  49. if ( $.fn.dataTable.isDataTable( t ) ) {
  50. table = $(t).DataTable();
  51. }
  52. else {
  53. table = $(t).DataTable( {
  54. paging : false,
  55. searching : false,
  56. info: false
  57. } );
  58. }
  59. table.clear();
  60. for (var property in attributes) {
  61. if (attributes.hasOwnProperty(property)) {
  62. table.row.add([
  63. '<code>' + property + '</code>', '<code>' + attributes[property] + '</code>'
  64. ]).draw(false);
  65. }
  66. }
  67. }
  68. function date(d) {
  69. var date = new Date(d[0],d[1]-1,d[2],d[3],d[4],d[5]);
  70. return date;
  71. }
  72. function confirm(decisionId, service) {
  73. $('#confirmdiv').remove();
  74. var svcStr = service.length > 70 ? service.substr(0,68) + '...' : service;
  75. var message = strings.confirm.replace('{}', svcStr);
  76. $('#alertWrapper').append('<div id="confirmdiv" class="alert alert-warning">' +
  77. '<span class="alertMessage">' + message + '</span><br/>' +
  78. '<button type="button" id="delete" class="btn btn-xs btn-danger" aria-label="Yes"><strong>' +
  79. strings.yes + ' </strong></button>' +
  80. '<button type="button" class="btn btn-xs btn-default" aria-label="No" value="' + decisionId + '"><strong>' +
  81. strings.no + '</strong></button></div>'
  82. );
  83. $('#confirmdiv .btn').click(function() {
  84. $('#confirmdiv').alert('close');
  85. });
  86. $('#delete').click(function() {
  87. removeDecision(decisionId);
  88. });
  89. }
  90. function removeDecision(decisionId) {
  91. var factory = {};
  92. factory.httpHeaders = {};
  93. factory.httpHeaders[$('meta[name=\'_csrf_header\']').attr('content')] = $('meta[name=\'_csrf\']').attr('content');
  94. $.ajax({
  95. type: 'post',
  96. url: urls.delete,
  97. data: {decisionId: decisionId},
  98. headers: factory.httpHeaders,
  99. dataType: 'json',
  100. success: function (data) {
  101. // Reinitialize the table data
  102. $('#consentDecisions').DataTable().ajax.reload();
  103. if (!data) {
  104. alertUser(strings.error, 'danger');
  105. } else {
  106. alertUser(strings.success, 'success');
  107. // Reload the page
  108. location.reload();
  109. }
  110. },
  111. error: function () {
  112. alertUser('There appears to be an error. Please try your request again.', 'danger');
  113. }
  114. });
  115. }
  116. var consentDecisions = (function () {
  117. var createDataTable = function () {
  118. $('#consentDecisions').DataTable({
  119. 'order': [[0, 'desc']],
  120. 'initComplete': function (settings, json) {
  121. if (!json || json.length == 0) {
  122. $('#consent-decisons').hide();
  123. $('#loadingMessage').hide();
  124. $('#no-consent-decisions').show();
  125. } else {
  126. $('#loadingMessage').hide();
  127. $('#no-consent-decisions').hide();
  128. $('#consent-decisons').show();
  129. }
  130. },
  131. 'language': strings.data,
  132. 'paging': false,
  133. 'ajax': {
  134. 'url': urls.getConsentDecisions,
  135. 'dataSrc': ''
  136. },
  137. 'data': consentDecisions,
  138. 'columnDefs': [
  139. {
  140. 'targets': 0,
  141. 'className': 'created-date',
  142. 'data': function (row) {
  143. return date(row.decision.createdDate);
  144. },
  145. 'render': function (data) {
  146. var opts = { year: 'numeric', month: 'numeric' };
  147. return '<div class="label label-primary"><span class="hidden">' + data.toISOString() +
  148. '</span>' + data.toLocaleDateString('en', opts ) +
  149. '</div>';
  150. }
  151. },
  152. {
  153. 'targets': 1,
  154. 'data': 'decision.service',
  155. 'className': 'col service-id',
  156. 'render': function (data) {
  157. if ($(window).width() <= 855) {
  158. return data.length > 70 ?
  159. '<span title="' + data + '">' + data.substr(0, 68) + '...</span>' : data;
  160. } else {
  161. return data.length > 180 ?
  162. '<span title="' + data + '">' + data.substr(0, 178) + '...</span>' : data;
  163. }
  164. }
  165. }
  166. ]
  167. });
  168. };
  169. var addEventHandlers = function () {
  170. /* Performs logout for consent application, no SLO */
  171. $('#logout').click(function() {
  172. var logout = window.location + '/logout';
  173. window.location.assign(logout);
  174. });
  175. // Add event listener for opening and closing details
  176. $(document).on('click', '#consentDecisions > tbody > tr:not(.info)', function () {
  177. var table = $('#consentDecisions').DataTable();
  178. var tr = $(this);
  179. var row = table.row(tr);
  180. if (row.child.isShown()) {
  181. // This row is already open - close it
  182. row.child.hide();
  183. tr.removeClass('shown');
  184. }
  185. else {
  186. // Open this row
  187. row.child(format(row.data()), 'info').show();
  188. tr.addClass('shown');
  189. }
  190. });
  191. };
  192. // initialization *******
  193. (function init() {
  194. createDataTable();
  195. addEventHandlers();
  196. })();
  197. // Public Methods
  198. return {
  199. /**
  200. * Not used
  201. */
  202. };
  203. })();