cp.scripts.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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(form) {
  34. form.submit(function(e) {
  35. if(form.hasClass('loading')) {
  36. e.preventDefault();
  37. return;
  38. }
  39. // Block send button
  40. form.addClass('loading').addClass('alert-here');
  41. var button = form.find('button[type=submit]');
  42. button.addClass('progress-bar-striped')
  43. .addClass('progress-bar-animated');
  44. // Another button
  45. if(button.attr('data-target') != '') {
  46. $('#' + button.attr('data-target')).addClass('progress-bar-striped')
  47. .addClass('progress-bar-animated');
  48. }
  49. // Clear form messages
  50. form.find('.sys-messages').html('');
  51. $.ajax({
  52. type: "POST",
  53. url: form.attr('action'),
  54. data: form.serialize()
  55. }).done(function(data) {
  56. FormDataWasChanged = false;
  57. AjaxDone(data)
  58. }).fail(function(xhr, status, error) {
  59. AjaxFail(xhr.responseText, status, error);
  60. }).always(function() {
  61. // Add delay for one second
  62. setTimeout(function() {
  63. form.removeClass('loading').removeClass('alert-here');
  64. button.removeClass('progress-bar-striped')
  65. .removeClass('progress-bar-animated');
  66. // Another button
  67. if(button.attr('data-target') != '') {
  68. $('#' + button.attr('data-target'))
  69. .removeClass('progress-bar-striped')
  70. .removeClass('progress-bar-animated');
  71. }
  72. }, 100);
  73. });
  74. // Prevent submit action
  75. e.preventDefault();
  76. });
  77. // Bind to another button
  78. var button = form.find('button[type=submit]');
  79. if(button.attr('data-target') != '') {
  80. $('#' + button.attr('data-target')).click(function() {
  81. button.click();
  82. });
  83. }
  84. // Mark body if any data in form was changed
  85. if(form.hasClass('prev-data-lost')) {
  86. form.find('input, textarea, select').on('input', function() {
  87. if(!FormDataWasChanged) {
  88. FormDataWasChanged = true;
  89. }
  90. });
  91. }
  92. };
  93. function AllFormsToAjax() {
  94. $('form').each(function() {
  95. FormToAjax($(this));
  96. });
  97. };
  98. function BindWindowBeforeUnload() {
  99. // Prevent page reload if data was changed
  100. $(window).bind('beforeunload', function(){
  101. if(FormDataWasChanged) {
  102. return 'Some data was changed and not saved. Are you sure want to leave page?';
  103. }
  104. });
  105. };
  106. function Initialize() {
  107. // Check if jQuery was loaded
  108. if(typeof $ == 'function') {
  109. AllFormsToAjax();
  110. BindWindowBeforeUnload();
  111. } else {
  112. console.log('Error: jQuery is not loaded!');
  113. }
  114. };
  115. // Initialize
  116. if(window.addEventListener) {
  117. // W3C standard
  118. window.addEventListener('load', Initialize, false);
  119. } else if(window.attachEvent) {
  120. // Microsoft
  121. window.attachEvent('onload', Initialize);
  122. };
  123. // Public
  124. return {
  125. ShowMsgSuccess: function(title, message) {
  126. ShowSystemMsg(title, message, false);
  127. },
  128. ShowMsgError: function(title, message) {
  129. ShowSystemMsg(title, message, true);
  130. },
  131. ModalUserProfile: function() {
  132. var html = '<div class="modal fade" id="sys-modal-user-settings" tabindex="-1" role="dialog" aria-labelledby="sysModalUserSettingsLabel" aria-hidden="true"> \
  133. <div class="modal-dialog modal-dialog-centered" role="document"> \
  134. <div class="modal-content"> \
  135. <form class="form-user-settings" action="/cp/" method="post" autocomplete="off"> \
  136. <input type="hidden" name="action" value="index-user-update-profile"> \
  137. <div class="modal-header"> \
  138. <h5 class="modal-title" id="sysModalUserSettingsLabel">My profile</h5> \
  139. <button type="button" class="close" data-dismiss="modal" aria-label="Close"> \
  140. <span aria-hidden="true">&times;</span> \
  141. </button> \
  142. </div> \
  143. <div class="modal-body text-left"> \
  144. <div class="form-group"> \
  145. <label for="first_name">First name</label> \
  146. <input type="text" class="form-control" id="first_name" name="first_name" value="' + window.CurrentUserProfileData.first_name + '" placeholder="User first name" autocomplete="off"> \
  147. </div> \
  148. <div class="form-group"> \
  149. <label for="last_name">Last name</label> \
  150. <input type="text" class="form-control" id="last_name" name="last_name" value="' + window.CurrentUserProfileData.last_name + '" placeholder="User last name" autocomplete="off"> \
  151. </div> \
  152. <div class="form-group"> \
  153. <label for="email">Email</label> \
  154. <input type="email" class="form-control" id="email" name="email" value="' + window.CurrentUserProfileData.email + '" placeholder="User email" autocomplete="off" required> \
  155. </div> \
  156. <div class="form-group"> \
  157. <label for="password">New password</label> \
  158. <input type="password" class="form-control" id="password" aria-describedby="passwordHelp" name="password" value="" placeholder="User new password" autocomplete="off"> \
  159. <small id="passwordHelp" class="form-text text-muted">Leave this field empty if you don\'t want change your password</small> \
  160. </div> \
  161. <div class="sys-messages"></div> \
  162. </div> \
  163. <div class="modal-footer"> \
  164. <button type="submit" class="btn btn-primary">Save</button> \
  165. <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> \
  166. </div> \
  167. </form> \
  168. </div> \
  169. </div> \
  170. </div>';
  171. $('#sys-modal-user-settings-placeholder').html(html);
  172. $("#sys-modal-user-settings").modal({
  173. backdrop: 'static',
  174. keyboard: false,
  175. show: false,
  176. });
  177. $('#sys-modal-user-settings').on('hidden.bs.modal', function(e) {
  178. $('#sys-modal-user-settings-placeholder').html('');
  179. });
  180. FormToAjax($('#sys-modal-user-settings form'));
  181. $("#sys-modal-user-settings").modal('show');
  182. },
  183. ActionLogout: function(message) {
  184. if(confirm(message)) {
  185. $.ajax({
  186. type: "POST",
  187. url: '/cp/',
  188. data: {
  189. action: 'index-user-logout',
  190. }
  191. }).done(function(data) {
  192. AjaxDone(data)
  193. }).fail(function(xhr, status, error) {
  194. AjaxFail(xhr.responseText, status, error);
  195. });
  196. }
  197. },
  198. ActionDataTableDelete: function(object, action, id, message) {
  199. if(confirm(message)) {
  200. $.ajax({
  201. type: "POST",
  202. url: '/cp/',
  203. data: {
  204. action: action,
  205. id: id,
  206. }
  207. }).done(function(data) {
  208. AjaxDone(data)
  209. }).fail(function(xhr, status, error) {
  210. AjaxFail(xhr.responseText, status, error);
  211. });
  212. }
  213. },
  214. };
  215. }(window, $);
  216. // Make it public
  217. window.fave = fave;
  218. }(window, jQuery));