GochiSiyan

login popup script

Mar 15th, 2022
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1.  
  2. /****************
  3. * jnews login & register popup
  4. ****************/
  5.  
  6. window.jnews.loginregister = {
  7. xhr: null,
  8. captcha: [],
  9. validateCaptcha: false,
  10. show_popup: function (popuplink) {
  11. var obj = this
  12. if (popuplink.length > 0) {
  13. popuplink.magnificPopup({
  14. type: 'inline',
  15. removalDelay: 500, //delay removal by X to allow out-animation
  16. midClick: true,
  17. // mainClass: 'mfp-zoom-out', // this class is for CSS animation below
  18. callbacks: {
  19. beforeOpen: function () {
  20. this.st.mainClass = 'mfp-zoom-out'
  21. jnews.body_inject.removeClass('jeg_show_menu')
  22. },
  23. change: function () {
  24. var element = this.content.find('.g-recaptcha')
  25. var type = this.content.find('form').data('type')
  26. var key = element.data('sitekey')
  27. this.content.find('.form-message p').remove()
  28. obj.validateCaptcha = false
  29.  
  30. if ( jnewsoption.recaptcha == 1 && element.length ) {
  31. if (!element.hasClass('loaded')) {
  32. obj.captcha[type] = grecaptcha.render(element.get(0), {
  33. sitekey: key,
  34. callback: obj.validateResponse.bind(obj),
  35. })
  36. $(element).addClass('loaded')
  37. } else {
  38. grecaptcha.reset(obj.captcha[type])
  39. }
  40. }
  41. },
  42. },
  43. })
  44. }
  45. },
  46. validateResponse: function (response) {
  47. if (response !== '') {
  48. this.validateCaptcha = true
  49. }
  50. },
  51. init: function () {
  52. var popuplink = jnews.body_inject ? jnews.body_inject.find('.jeg_popuplink') : $('.jeg_popuplink')
  53. this.show_popup(popuplink)
  54.  
  55. var popupparent = jnews.body_inject ? jnews.body_inject.find('.jeg_popuplink_parent a') : $('.jeg_popuplink_parent a')
  56. this.show_popup(popupparent)
  57. },
  58. validateForm: function (form) {
  59. var msg = $(form).find('.form-message')
  60. var type = $(form).data('type')
  61.  
  62. if ('register' === type) {
  63. var email = $(form).find('[name="email"]').val()
  64. if ('' === email) {
  65. msg.html("<p class='alert alert-error'>" + jnewsoption.lang.empty_email + '</p>')
  66. return false
  67. }
  68. }
  69.  
  70. if ('login' === type || 'register' === type) {
  71. var username = $(form).find('[name="username"]').val()
  72. if ('' === username) {
  73. msg.html("<p class='alert alert-error'>" + jnewsoption.lang.empty_username + '</p>')
  74. return false
  75. }
  76. }
  77.  
  78. if ('login' === type) {
  79. var password = $(form).find('[name="password"]').val()
  80. if ('' === password) {
  81. msg.html("<p class='alert alert-error'>" + jnewsoption.lang.empty_password + '</p>')
  82. return false
  83. }
  84. }
  85.  
  86. if ('forgot' === type) {
  87. var user_login = $(form).find('[name="user_login"]').val()
  88. if ('' === user_login) {
  89. msg.html("<p class='alert alert-error'>" + jnewsoption.lang.empty_username + '</p>')
  90. return false
  91. }
  92. }
  93.  
  94. // Validate Form
  95. if (jnewsoption.recaptcha == 1 && !this.validateCaptcha) {
  96. msg.html("<p class='alert alert-error'>" + jnewsoption.lang.invalid_recaptcha + '</p>')
  97. return false
  98. }
  99.  
  100. return true
  101. },
  102. hook_form: function () {
  103. var obj = this
  104.  
  105. $('.jeg_popupform.jeg_popup_account > form').each(function () {
  106. var form = this,
  107. $form = $(form),
  108. $form_button = $form.find('.button'),
  109. msg = $form.find('.form-message')
  110.  
  111. $form.on('submit', function (e) {
  112. e.preventDefault()
  113.  
  114. if (obj.validateForm(form)) {
  115. msg.html('')
  116. $form_button.val($form_button.data('process'))
  117.  
  118. if (obj.xhr !== null) obj.xhr.abort()
  119. obj.xhr = $.post(jnews_ajax_url, {action: 'jnews_refresh_nonce', refresh_action_nonce: 'jnews_nonce'}).always(function (data) {
  120. if ( data.jnews_nonce ) {
  121. $form.find('input[name="jnews_nonce"]').val(data.jnews_nonce)
  122. obj.xhr = $.ajax({
  123. url: jnews_ajax_url,
  124. type: 'post',
  125. dataType: 'json',
  126. data: $form.serialize(),
  127. success: function (data) {
  128. if (data.response === 1) {
  129. msg.html('<p class=\'alert alert-success\'>' + data.string + '</p>')
  130. if (data.refresh === 1) {
  131. window.location = jnewsoption.login_reload
  132. }
  133. }
  134.  
  135. if (data.response == 0) {
  136. var type = $form.data('type')
  137. $form_button.val($form_button.data('string'))
  138. msg.html('<p class=\'alert alert-error\'>' + data.string + '</p>')
  139. obj.show_popup(msg.find('.jeg_popuplink'));
  140. if( typeof grecaptcha !== 'undefined' ) {
  141. grecaptcha.reset(obj.captcha[type])
  142. }
  143. }
  144.  
  145. $form_button.val($form_button.data('string'))
  146. $form.trigger('reset')
  147. },
  148. })
  149. }
  150. })
  151. }
  152. })
  153. })
  154. },
  155. }
Advertisement
Add Comment
Please, Sign In to add comment