frontend.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. (function ($) {
  2. FLBuilderLoginForm = function (settings) {
  3. this.settings = settings;
  4. this.nodeClass = '.fl-node-' + settings.id;
  5. this.loginform = $(this.nodeClass + ' .fl-login-form.login');
  6. this.loginbutton = this.loginform.find('a.fl-button');
  7. this.logoutform = $(this.nodeClass + ' .fl-login-form.logout');
  8. this.logoutbutton = this.logoutform.find('a.fl-button');
  9. this._init();
  10. };
  11. FLBuilderLoginForm.prototype = {
  12. settings: {},
  13. nodeClass: '',
  14. form: null,
  15. button: null,
  16. _init: function () {
  17. this.loginbutton.on('click', $.proxy(this._loginForm, this));
  18. this.logoutbutton.on('click', $.proxy(this._logoutForm, this));
  19. this.loginform.find('input[type="password"]').on('keypress', $.proxy(this._onEnterKey, this));
  20. },
  21. _loginForm: function (e) {
  22. var submitButton = $(e.currentTarget),
  23. currentForm = submitButton.closest('.fl-login-form'),
  24. postId = currentForm.closest('.fl-builder-content').data('post-id'),
  25. templateId = currentForm.data('template-id'),
  26. templateNodeId = currentForm.data('template-node-id'),
  27. nodeId = currentForm.closest('.fl-module').data('node'),
  28. buttonText = submitButton.find('.fl-button-text').text(),
  29. waitText = submitButton.closest('.fl-form-button').data('wait-text'),
  30. name = currentForm.find('input[name=fl-login-form-name]'),
  31. password = currentForm.find('input[name=fl-login-form-password]'),
  32. remember = currentForm.find('input[name=fl-login-form-remember]'),
  33. nonce = this.loginform.find('input#fl-login-form-nonce').val(),
  34. valid = true,
  35. ajaxData = null;
  36. e.preventDefault();
  37. if (submitButton.hasClass('fl-form-button-disabled')) {
  38. return; // Already submitting
  39. }
  40. if (name.length > 0 && name.val() == '') {
  41. name.addClass('fl-form-error');
  42. name.siblings('.fl-form-error-message').show();
  43. valid = false;
  44. }
  45. if ('' == password.val()) {
  46. password.addClass('fl-form-error');
  47. password.siblings('.fl-form-error-message').show();
  48. valid = false;
  49. }
  50. if (valid) {
  51. currentForm.find('> .fl-form-error-message').hide();
  52. submitButton.find('.fl-button-text').text(waitText);
  53. submitButton.data('original-text', buttonText);
  54. submitButton.addClass('fl-form-button-disabled');
  55. ajaxData = {
  56. action: 'fl_builder_login_form_submit',
  57. name: name.val(),
  58. password: password.val(),
  59. post_id: postId,
  60. remember: remember.is(':checked'),
  61. template_id: templateId,
  62. template_node_id: templateNodeId,
  63. node_id: nodeId,
  64. nonce: nonce
  65. };
  66. $.post(FLBuilderLayoutConfig.paths.wpAjaxUrl, ajaxData, $.proxy(function (response) {
  67. this._loginFormComplete(response, submitButton);
  68. }, this));
  69. }
  70. },
  71. _logoutForm: function (e) {
  72. var submitButton = $(e.currentTarget),
  73. nonce = this.logoutform.find('input#fl-login-form-nonce').val();
  74. e.preventDefault();
  75. ajaxData = {
  76. action: 'fl_builder_logout_form_submit',
  77. nonce: nonce
  78. };
  79. $.post(FLBuilderLayoutConfig.paths.wpAjaxUrl, ajaxData, $.proxy(function (response) {
  80. this._logoutFormComplete(response, submitButton);
  81. }, this));
  82. },
  83. _logoutFormComplete: function (response, button) {
  84. if ( this.settings.lo_url.length > 0 ) {
  85. window.location.href = this.settings.lo_url;
  86. } else {
  87. location.reload()
  88. }
  89. },
  90. _loginFormComplete: function (response, button) {
  91. var buttonText = button.data('original-text'),
  92. form = button.closest('.fl-login-form');
  93. if (false === response.success) {
  94. form.find('> .fl-form-error-message').html(response.data);
  95. form.find('> .fl-form-error-message').show();
  96. button.removeClass('fl-form-button-disabled');
  97. button.find('.fl-button-text').text(buttonText);
  98. } else {
  99. if ('current' == response.data.url) {
  100. window.location.reload();
  101. } else if ('referrer' == response.data.url) {
  102. document.referrer ? window.location = document.referrer : history.back()
  103. } else {
  104. window.location.href = response.data.url;
  105. }
  106. }
  107. },
  108. _onEnterKey: function (e) {
  109. if (e.which == 13) {
  110. var currentForm = $(e.currentTarget).closest('.fl-login-form');
  111. currentForm.find('a.fl-button').trigger('click');
  112. }
  113. }
  114. }
  115. })(jQuery);