viewConfig.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* globals $, urls, updateEnabled */
  2. (function () {
  3. var origData;
  4. var setupButtonClickOnRefreshedProperties = function () {
  5. $('#formRefreshCAS button').on('click', function (e) {
  6. e.preventDefault();
  7. // Show the refreshModal
  8. var myModal = $('#refreshModal').modal({
  9. keyboard: false,
  10. backdrop: 'static'
  11. });
  12. var primaryButton = myModal.find('.modal-footer button.btn-primary');
  13. // Disable the primary button
  14. primaryButton.prop('disabled', true).text('Refreshing...');
  15. $.post(e.target.parentNode.action, function (data) {
  16. if (data.length !== 0) {
  17. $('#refreshModal-label').text('Refreshed Properties');
  18. myModal.find('.modal-content .modal-body').html(
  19. '<pre>' + data + '</pre>' +
  20. '<p>Click &quot;OK&quot; to reload page.</p>'
  21. );
  22. } else {
  23. myModal.find('.modal-header .modal-title').text('Properties Refreshed');
  24. myModal.find('.modal-content .modal-body').html(
  25. '<p>Click &quot;OK&quot; to reload page.</p>'
  26. );
  27. }
  28. })
  29. .done(function () {
  30. primaryButton.prop('disabled', false).text('Reload page').on('click', function (e) {
  31. e.preventDefault();
  32. window.location.reload();
  33. });
  34. })
  35. .fail(function (jqXHR) {
  36. $('#refreshModal-label').text('Problem With Refreshing Properties');
  37. myModal.find('.modal-content .modal-body').html(
  38. '<div class="alert alert-warning"><strong>Status: ' + jqXHR.status + '</strong><p/>Unable to refresh the properties. Please try again.</div>'
  39. );
  40. primaryButton.prop('disabled', false).text('OK').on('click', function (e) {
  41. e.preventDefault();
  42. myModal.modal('hide');
  43. });
  44. });
  45. });
  46. };
  47. var createDataTable = function () {
  48. $('#viewConfigsTable').DataTable({
  49. 'autoWidth': false,
  50. 'initComplete': function (settings, json) {
  51. if (!json) {
  52. $('#loadingMessage').hide();
  53. $('#viewConfigError').show();
  54. $('#view-configuration').hide();
  55. } else {
  56. $('#loadingMessage').hide();
  57. $('#viewConfigError').hide();
  58. $('#view-configuration').show();
  59. }
  60. },
  61. 'drawCallback': function () {
  62. var api = this.api();
  63. if (api.page.info().pages > 1) {
  64. $('#' + $.fn.dataTable.tables()[0].id + '_paginate')[0].style.display = 'block';
  65. } else {
  66. $('#' + $.fn.dataTable.tables()[0].id + '_paginate')[0].style.display = 'none';
  67. }
  68. if (updateEnabled) {
  69. editTable();
  70. }
  71. },
  72. 'processing': true,
  73. 'ajax': {
  74. 'url': urls.getConfiguration,
  75. 'dataSrc': function (json) {
  76. var returnData = [];
  77. for (var item in json) {
  78. returnData.push({
  79. 'key': '<code>' + item + '</code>',
  80. 'value': '' + json[item] + ''
  81. });
  82. }
  83. return returnData;
  84. }
  85. },
  86. 'columns': [
  87. {'data': 'key', 'className': 'col-xs-6 key'},
  88. {'data': 'value', 'className': 'col-xs-6 value'}
  89. ],
  90. 'pageLength': 50
  91. });
  92. };
  93. var getRowData = function (row) {
  94. var tds = row.find('td');
  95. var tmp = {};
  96. $.each(tds, function (i) {
  97. if (i % 2 === 0) {
  98. tmp.key = $(this).text();
  99. } else {
  100. tmp.value = $(this).text();
  101. }
  102. });
  103. return tmp;
  104. };
  105. var editTable = function () {
  106. $('#viewConfigsTable').editableTableWidget({editor: $('<textarea>')});
  107. $('#viewConfigsTable td').on('focus', function () {
  108. origData = getRowData($(this).closest('tr'));
  109. });
  110. $('#viewConfigsTable tr').on('change', function () {
  111. var newChanges = getRowData($(this));
  112. var data = {old: origData, new: newChanges};
  113. $.ajax({url: urls.updateConfiguration, data: JSON.stringify(data), type: 'POST', contentType: 'application/json'})
  114. .fail(function () {
  115. var result = 'Failed to save settings.';
  116. $('#alertWrapper').addClass('alert-warning');
  117. $('#alertWrapper').removeClass('alert-success');
  118. $('#alertWrapper').text(result);
  119. $('#alertWrapper').show();
  120. })
  121. .success(function () {
  122. var result = 'Saved settings successfully.';
  123. $('#alertWrapper').removeClass('alert-warning');
  124. $('#alertWrapper').addClass('alert-success');
  125. $('#resultText').text(result);
  126. $('#alertWrapper').show();
  127. });
  128. });
  129. };
  130. // initialization *******
  131. (function init () {
  132. createDataTable();
  133. setupButtonClickOnRefreshedProperties();
  134. })();
  135. // Public Methods
  136. return {
  137. /**
  138. * Not used
  139. */
  140. };
  141. })();