cp.scripts.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. (function(window, $) {
  2. var fave = function(window, $) {
  3. // Private
  4. var FormDataWasChanged = false;
  5. function GetModalAlertTmpl(title, message, error) {
  6. 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>';
  7. };
  8. function ShowSystemMsg(title, message, error) {
  9. var modal_alert_place = $('.modal.show .sys-messages');
  10. if(!modal_alert_place.length) {
  11. modal_alert_place = $('form.alert-here .sys-messages');
  12. }
  13. if(modal_alert_place.length) {
  14. modal_alert_place.html(GetModalAlertTmpl(title, message, error));
  15. }
  16. };
  17. function AjaxDone(data) {
  18. try {
  19. eval(data);
  20. } catch(e) {
  21. if(e instanceof SyntaxError) {
  22. console.log(data);
  23. console.log('Error: JavaScript code eval error', e.message)
  24. }
  25. }
  26. };
  27. function AjaxFail(data, status, error) {
  28. console.log('Error: data sending error, page will be reloaded', data, status, error);
  29. setTimeout(function() {
  30. window.location.reload(false);
  31. }, 1000);
  32. };
  33. function FormToAjax() {
  34. $('form').each(function() {
  35. $(this).submit(function(e) {
  36. var form = $(this);
  37. if(form.hasClass('loading')) {
  38. e.preventDefault();
  39. return;
  40. }
  41. // Block send button
  42. form.addClass('loading').addClass('alert-here');
  43. var button = $(this).find('button[type=submit]');
  44. button.addClass('progress-bar-striped')
  45. .addClass('progress-bar-animated');
  46. // Another button
  47. if(button.attr('data-target') != '') {
  48. $('#' + button.attr('data-target')).addClass('progress-bar-striped')
  49. .addClass('progress-bar-animated');
  50. }
  51. // Clear form messages
  52. form.find('.sys-messages').html('');
  53. $.ajax({
  54. type: "POST",
  55. url: form.attr('action'),
  56. data: form.serialize()
  57. }).done(function(data) {
  58. FormDataWasChanged = false;
  59. AjaxDone(data)
  60. }).fail(function(xhr, status, error) {
  61. AjaxFail(xhr.responseText, status, error);
  62. }).always(function() {
  63. // Add delay for one second
  64. setTimeout(function() {
  65. form.removeClass('loading').removeClass('alert-here');
  66. button.removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  67. // Another button
  68. if(button.attr('data-target') != '') {
  69. $('#' + button.attr('data-target')).removeClass('progress-bar-striped').removeClass('progress-bar-animated');
  70. }
  71. }, 100);
  72. });
  73. // Prevent submit action
  74. e.preventDefault();
  75. });
  76. // Bind to another button
  77. var button = $(this).find('button[type=submit]');
  78. if(button.attr('data-target') != '') {
  79. $('#' + button.attr('data-target')).click(function() {
  80. button.click();
  81. });
  82. }
  83. // Mark body if any data in form was changed
  84. if($(this).hasClass('prev-data-lost')) {
  85. $(this).find('input, textarea, select').on('input', function() {
  86. if(!FormDataWasChanged) {
  87. FormDataWasChanged = true;
  88. }
  89. });
  90. }
  91. });
  92. };
  93. function FixFormInModal() {
  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. };
  113. function BindWindowBeforeUnload() {
  114. // Prevent page reload if data was changed
  115. $(window).bind('beforeunload', function(){
  116. if(FormDataWasChanged) {
  117. return 'Some data was changed and not saved. Are you sure want to leave page?';
  118. }
  119. });
  120. };
  121. function Initialize() {
  122. // Check if jQuery was loaded
  123. if(typeof $ == 'function') {
  124. FormToAjax();
  125. FixFormInModal();
  126. BindWindowBeforeUnload();
  127. } else {
  128. console.log('Error: jQuery is not loaded!');
  129. }
  130. };
  131. // Initialize
  132. if(window.addEventListener) {
  133. // W3C standard
  134. window.addEventListener('load', Initialize, false);
  135. } else if(window.attachEvent) {
  136. // Microsoft
  137. window.attachEvent('onload', Initialize);
  138. };
  139. // Public
  140. return {
  141. ShowMsgSuccess: function(title, message) {
  142. ShowSystemMsg(title, message, false);
  143. },
  144. ShowMsgError: function(title, message) {
  145. ShowSystemMsg(title, message, true);
  146. },
  147. ActionLogout: function(message) {
  148. if(confirm(message)) {
  149. $.ajax({
  150. type: "POST",
  151. url: '/cp/',
  152. data: {
  153. action: 'index-user-logout',
  154. }
  155. }).done(function(data) {
  156. AjaxDone(data)
  157. }).fail(function(xhr, status, error) {
  158. AjaxFail(xhr.responseText, status, error);
  159. });
  160. }
  161. },
  162. ActionDataTableDelete: function(object, action, id, message) {
  163. if(confirm(message)) {
  164. $.ajax({
  165. type: "POST",
  166. url: '/cp/',
  167. data: {
  168. action: action,
  169. id: id,
  170. }
  171. }).done(function(data) {
  172. AjaxDone(data)
  173. }).fail(function(xhr, status, error) {
  174. AjaxFail(xhr.responseText, status, error);
  175. });
  176. }
  177. },
  178. };
  179. }(window, $);
  180. // Make it public
  181. window.fave = fave;
  182. }(window, jQuery));