assets.cp.scripts.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ModalShowMsg('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 ActionUserSettings() {
  34. // Reset form to remove autocomplete
  35. $('form.form-user-settings')[0].reset();
  36. }
  37. function ActionSingOut() {
  38. $.ajax({
  39. type: "POST",
  40. url: '/cp/',
  41. data: {
  42. action: 'singout',
  43. }
  44. }).done(function(data) {
  45. AjaxDone(data)
  46. }).fail(function() {
  47. AjaxFail();
  48. });
  49. }
  50. $(document).ready(function() {
  51. // Ajax forms
  52. $('form').each(function() {
  53. $(this).submit(function(e) {
  54. var form = $(this);
  55. if(form.hasClass('loading')) {
  56. e.preventDefault();
  57. return;
  58. }
  59. // Block send button
  60. form.addClass('loading').addClass('alert-here');
  61. var button = $(this).find('button[type=submit]');
  62. button.addClass('progress-bar-striped').addClass('progress-bar-animated');
  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. }, 500);
  79. });
  80. e.preventDefault();
  81. });
  82. });
  83. });