cp.scripts.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(message) {
  34. if(confirm(message)) {
  35. $.ajax({
  36. type: "POST",
  37. url: '/cp/',
  38. data: {
  39. action: 'index-user-logout',
  40. }
  41. }).done(function(data) {
  42. AjaxDone(data)
  43. }).fail(function() {
  44. AjaxFail();
  45. });
  46. }
  47. }
  48. function ActionDataTableDelete(object, action, id, message) {
  49. if(confirm(message)) {
  50. $.ajax({
  51. type: "POST",
  52. url: '/cp/',
  53. data: {
  54. action: action,
  55. id: id,
  56. }
  57. }).done(function(data) {
  58. AjaxDone(data)
  59. }).fail(function() {
  60. AjaxFail();
  61. });
  62. }
  63. }
  64. $(document).ready(function() {
  65. $('form').each(function() {
  66. // Ajax forms
  67. $(this).submit(function(e) {
  68. var form = $(this);
  69. if(form.hasClass('loading')) {
  70. e.preventDefault();
  71. return;
  72. }
  73. // Block send button
  74. form.addClass('loading').addClass('alert-here');
  75. var button = $(this).find('button[type=submit]');
  76. button.addClass('progress-bar-striped').addClass('progress-bar-animated');
  77. // Another button
  78. if(button.attr('data-target') != '') {
  79. $('#' + button.attr('data-target')).addClass('progress-bar-striped').addClass('progress-bar-animated');
  80. }
  81. // Clear form messages
  82. form.find('.sys-messages').html('');
  83. $.ajax({
  84. type: "POST",
  85. url: form.attr('action'),
  86. data: form.serialize()
  87. }).done(function(data) {
  88. $('body').removeClass('data-changed');
  89. AjaxDone(data)
  90. }).fail(function() {
  91. AjaxFail();
  92. }).always(function() {
  93. // Add delay for one second
  94. setTimeout(function() {
  95. form.removeClass('loading').removeClass('alert-here');
  96. button.removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  97. // Another button
  98. if(button.attr('data-target') != '') {
  99. $('#' + button.attr('data-target')).removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  100. }
  101. }, 100);
  102. });
  103. e.preventDefault();
  104. });
  105. // Bind to another button
  106. var button = $(this).find('button[type=submit]');
  107. if(button.attr('data-target') != '') {
  108. $('#' + button.attr('data-target')).click(function() {
  109. button.click();
  110. });
  111. }
  112. // Mark body if any data in form was changed
  113. $(this).find('input, textarea, select').on('input', function() {
  114. $('body').addClass('data-changed');
  115. });
  116. });
  117. // Remove alert from modal on close
  118. $('.modal.fade').on('hidden.bs.modal', function() {
  119. modal_alert_place = $(this).find('.sys-messages');
  120. if(modal_alert_place.length) {
  121. modal_alert_place.html('');
  122. }
  123. // Reset form at modal close
  124. form = $(this).find('form');
  125. if(form.length) {
  126. form[0].reset();
  127. }
  128. }).on('show.bs.modal', function() {
  129. // Reset form at modal open
  130. form = $(this).find('form');
  131. if(form.length) {
  132. form[0].reset();
  133. }
  134. });
  135. // Prevent page reload if data was changed
  136. $(window).bind('beforeunload', function(){
  137. if($('body').hasClass('data-changed')) {
  138. return 'Some data was changed without saving, are you sure want to leave page?';
  139. }
  140. });
  141. });