cp.scripts.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 ActionSingOut() {
  34. $.ajax({
  35. type: "POST",
  36. url: '/cp/',
  37. data: {
  38. action: 'singout',
  39. }
  40. }).done(function(data) {
  41. AjaxDone(data)
  42. }).fail(function() {
  43. AjaxFail();
  44. });
  45. }
  46. $(document).ready(function() {
  47. // Ajax forms
  48. $('form').each(function() {
  49. $(this).submit(function(e) {
  50. var form = $(this);
  51. if(form.hasClass('loading')) {
  52. e.preventDefault();
  53. return;
  54. }
  55. // Block send button
  56. form.addClass('loading').addClass('alert-here');
  57. var button = $(this).find('button[type=submit]');
  58. button.addClass('progress-bar-striped').addClass('progress-bar-animated');
  59. // Another button
  60. if(button.attr('data-target') != '') {
  61. $('#' + button.attr('data-target')).addClass('progress-bar-striped').addClass('progress-bar-animated');
  62. }
  63. // Clear form messages
  64. form.find('.sys-messages').html('');
  65. $.ajax({
  66. type: "POST",
  67. url: form.attr('action'),
  68. data: form.serialize()
  69. }).done(function(data) {
  70. AjaxDone(data)
  71. }).fail(function() {
  72. AjaxFail();
  73. }).always(function() {
  74. // Add delay for one second
  75. setTimeout(function() {
  76. form.removeClass('loading').removeClass('alert-here');
  77. button.removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  78. // Another button
  79. if(button.attr('data-target') != '') {
  80. $('#' + button.attr('data-target')).removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  81. }
  82. }, 100);
  83. });
  84. e.preventDefault();
  85. });
  86. // Bind to another button
  87. var button = $(this).find('button[type=submit]');
  88. if(button.attr('data-target') != '') {
  89. $('#' + button.attr('data-target')).click(function() {
  90. button.click();
  91. });
  92. }
  93. });
  94. // Remove alert from modal on close
  95. $('.modal.fade').on('hidden.bs.modal', function() {
  96. modal_alert_place = $(this).find('.sys-messages');
  97. if(modal_alert_place.length) {
  98. modal_alert_place.html('');
  99. }
  100. // Reset form at modal close
  101. form = $(this).find('form');
  102. if(form.length) {
  103. form[0].reset();
  104. }
  105. }).on('show.bs.modal', function() {
  106. // Reset form at modal open
  107. form = $(this).find('form');
  108. if(form.length) {
  109. form[0].reset();
  110. }
  111. });
  112. });