assets.cp.scripts.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.html(GetModalAlertTmpl(title, message, error));
  8. }
  9. }
  10. function ShowSystemMsgSuccess(title, message) {
  11. ShowSystemMsg(title, message, false);
  12. }
  13. function ShowSystemMsgError(title, message) {
  14. ShowSystemMsg(title, message, true);
  15. }
  16. function AjaxDone(data) {
  17. try {
  18. eval(data);
  19. } catch(e) {
  20. if(e instanceof SyntaxError) {
  21. console.log(data);
  22. ModalShowMsg('JavaScript Eval Error', e.message)
  23. }
  24. }
  25. }
  26. function AjaxFail() {
  27. console.log('Form send fail, page will be reloaded');
  28. window.location.reload(false);
  29. }
  30. function ActionUserSettings() {
  31. // Reset form to remove autocomplete
  32. $('form.form-user-settings')[0].reset();
  33. }
  34. function ActionSingOut() {
  35. $.ajax({
  36. type: "POST",
  37. url: '/cp/',
  38. data: {
  39. action: 'singout',
  40. }
  41. }).done(function(data) {
  42. AjaxDone(data)
  43. }).fail(function() {
  44. AjaxFail();
  45. });
  46. }
  47. $(document).ready(function() {
  48. // Ajax forms
  49. $('form').each(function() {
  50. $(this).submit(function(e) {
  51. var form = $(this);
  52. if(form.hasClass('loading')) {
  53. e.preventDefault();
  54. return;
  55. }
  56. // Block send button
  57. form.addClass('loading');
  58. var button = $(this).find('button[type=submit]');
  59. button.addClass('progress-bar-striped').addClass('progress-bar-animated');
  60. // Clear form messages
  61. form.find('.sys-messages').html('');
  62. $.ajax({
  63. type: "POST",
  64. url: form.attr('action'),
  65. data: form.serialize()
  66. }).done(function(data) {
  67. AjaxDone(data)
  68. }).fail(function() {
  69. AjaxFail();
  70. }).always(function() {
  71. form.removeClass('loading');
  72. button.removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  73. });
  74. e.preventDefault();
  75. });
  76. });
  77. });