cp.scripts.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. function GetModalAlertTmpl(title, message, error) {
  2. return '<div class="alert alert-' + (!error?'success':'danger') + ' alert-dismissible fade show" role="alert"><strong>' + title + '</strong> ' + message + '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
  3. }
  4. function ShowSystemMsg(title, message, error) {
  5. var modal_alert_place = $('.modal.show .sys-messages');
  6. if(!modal_alert_place.length) {
  7. modal_alert_place = $('form.alert-here .sys-messages');
  8. }
  9. if(modal_alert_place.length) {
  10. modal_alert_place.html(GetModalAlertTmpl(title, message, error));
  11. }
  12. }
  13. function ShowSystemMsgSuccess(title, message) {
  14. ShowSystemMsg(title, message, false);
  15. }
  16. function ShowSystemMsgError(title, message) {
  17. ShowSystemMsg(title, message, true);
  18. }
  19. function AjaxDone(data) {
  20. try {
  21. eval(data);
  22. } catch(e) {
  23. if(e instanceof SyntaxError) {
  24. console.log(data);
  25. console.log('JavaScript Eval Error', e.message)
  26. }
  27. }
  28. }
  29. function AjaxFail() {
  30. console.log('Form send fail, page will be reloaded');
  31. window.location.reload(false);
  32. }
  33. function ActionIndexUserLogout() {
  34. $.ajax({
  35. type: "POST",
  36. url: '/cp/',
  37. data: {
  38. action: 'index-user-logout',
  39. }
  40. }).done(function(data) {
  41. AjaxDone(data)
  42. }).fail(function() {
  43. AjaxFail();
  44. });
  45. }
  46. function ActionDataTableDelete(object, action, id, msg) {
  47. if(confirm(msg)) {
  48. $.ajax({
  49. type: "POST",
  50. url: '/cp/',
  51. data: {
  52. action: action,
  53. id: id,
  54. }
  55. }).done(function(data) {
  56. AjaxDone(data)
  57. }).fail(function() {
  58. AjaxFail();
  59. });
  60. }
  61. }
  62. $(document).ready(function() {
  63. // Ajax forms
  64. $('form').each(function() {
  65. $(this).submit(function(e) {
  66. var form = $(this);
  67. if(form.hasClass('loading')) {
  68. e.preventDefault();
  69. return;
  70. }
  71. // Block send button
  72. form.addClass('loading').addClass('alert-here');
  73. var button = $(this).find('button[type=submit]');
  74. button.addClass('progress-bar-striped').addClass('progress-bar-animated');
  75. // Another button
  76. if(button.attr('data-target') != '') {
  77. $('#' + button.attr('data-target')).addClass('progress-bar-striped').addClass('progress-bar-animated');
  78. }
  79. // Clear form messages
  80. form.find('.sys-messages').html('');
  81. $.ajax({
  82. type: "POST",
  83. url: form.attr('action'),
  84. data: form.serialize()
  85. }).done(function(data) {
  86. AjaxDone(data)
  87. }).fail(function() {
  88. AjaxFail();
  89. }).always(function() {
  90. // Add delay for one second
  91. setTimeout(function() {
  92. form.removeClass('loading').removeClass('alert-here');
  93. button.removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  94. // Another button
  95. if(button.attr('data-target') != '') {
  96. $('#' + button.attr('data-target')).removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  97. }
  98. }, 100);
  99. });
  100. e.preventDefault();
  101. });
  102. // Bind to another button
  103. var button = $(this).find('button[type=submit]');
  104. if(button.attr('data-target') != '') {
  105. $('#' + button.attr('data-target')).click(function() {
  106. button.click();
  107. });
  108. }
  109. });
  110. // Remove alert from modal on close
  111. $('.modal.fade').on('hidden.bs.modal', function() {
  112. modal_alert_place = $(this).find('.sys-messages');
  113. if(modal_alert_place.length) {
  114. modal_alert_place.html('');
  115. }
  116. // Reset form at modal close
  117. form = $(this).find('form');
  118. if(form.length) {
  119. form[0].reset();
  120. }
  121. }).on('show.bs.modal', function() {
  122. // Reset form at modal open
  123. form = $(this).find('form');
  124. if(form.length) {
  125. form[0].reset();
  126. }
  127. });
  128. });