Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.04 KB | None | 0 0
  1. (function($, Models, Collections, Views) {
  2. Views.Auth = Backbone.View.extend({
  3. events: {
  4. // user register
  5. 'submit form.signup_form_submit': 'doRegister',
  6. 'submit form#signup_form': 'doRegisterNew',
  7. // user login
  8. 'submit form.signin_form_submit': 'doLogin',
  9. 'submit form#signin_form': 'doLoginNew',
  10. // user forgot password
  11. 'submit form.forgot_form': 'doSendPassword',
  12. },
  13. /**
  14. * init view setup Block Ui and Model User
  15. */
  16. initialize: function() {
  17. this.user = AE.App.user;
  18. this.blockUi = new Views.BlockUi();
  19. this.initValidator();
  20. //check button
  21. var clickCheckbox = document.querySelector('form.signup_form_submit .sign-up-switch'),
  22. roleInput = $("form.signup_form_submit input#role"),
  23. hire_text = $('.hire-text').val();
  24. work_text = $('.work-text').val();
  25. view = this;
  26. if ($('.sign-up-switch').length > 0 && clickCheckbox) {
  27. if($('#signup_form_submit, .signup_form_submit').find('span.user-role').hasClass('hire'))
  28. {
  29. $('form.signup_form_submit .sign-up-switch').parents('.user-type').find('small').css({
  30. "left" : -5 + "px"
  31. })
  32. }
  33. clickCheckbox.onchange = function(event) {
  34. var _this = $(event.currentTarget);
  35. var _switch = _this.parents('.user-type');
  36. if (clickCheckbox.checked) {
  37. roleInput.val("freelancer");
  38. $('form.signup_form_submit .user-type span.text').text(work_text).removeClass('hire').addClass('work');
  39. _switch.find('small').css({
  40. "left" : (_switch.find('.switchery').width() - _switch.find('small').width() + 5) + "px"
  41. });
  42. } else {
  43. roleInput.val("employer");
  44. $('form.signup_form_submit .user-type span.text').text(hire_text).removeClass('work').addClass('hire');
  45. _switch.find('small').css({
  46. "left" : -5 + "px"
  47. })
  48. }
  49. };
  50. }
  51. // Event checkbox
  52. $(".login-remember").click(function(event) {
  53. if ($(this).find('#remember').is(':checked')) {
  54. $("input#remember").val(1);
  55. } else {
  56. $("input#remember").val(0);
  57. }
  58. });
  59. },
  60. /**
  61. * init form validator rules
  62. * can override this function by using prototype
  63. */
  64. initValidator: function() {
  65. // login rule
  66. this.login_validator = $("form.signin_form_submit").validate({
  67. rules: {
  68. user_login: "required",
  69. user_pass: "required"
  70. }
  71. });
  72. this.login_new_validator = $("form#signin_form").validate({
  73. rules: {
  74. user_login: "required",
  75. user_pass: "required"
  76. },
  77. highlight: function(element, errorClass, validClass) {
  78. var required_id = $(element).attr('id');
  79. var $container = $(element).closest('div');
  80. if (!$container.hasClass('error')) {
  81. $container.addClass('error').removeClass(validClass);
  82. }
  83. },
  84. });
  85. this.register_new_validator = $("form#signup_form").validate({
  86. rules: {
  87. first_name: "required",
  88. last_name: "required",
  89. user_login: "required",
  90. user_pass: "required",
  91. user_phone: "required",
  92. user_email: {
  93. required: true,
  94. email: true
  95. },
  96. repeat_pass: {
  97. required: true,
  98. equalTo: "#user_pass"
  99. }
  100. },
  101. highlight: function(element, errorClass, validClass) {
  102. var required_id = $(element).attr('id');
  103. var $container = $(element).closest('div');
  104. if (!$container.hasClass('error')) {
  105. $container.addClass('error').removeClass(validClass);
  106. }
  107. },
  108. });
  109. /**
  110. * register rule
  111. */
  112. if ($('#agreement').length > 0) {
  113. this.register_validator = $("form.signup_form_submit").validate({
  114. rules: {
  115. first_name: "required",
  116. last_name: "required",
  117. user_login: "required",
  118. user_pass: "required",
  119. agreement : "required",
  120. user_email: {
  121. required: true,
  122. email: true
  123. },
  124. repeat_pass: {
  125. required: true,
  126. equalTo: "#user_pass"
  127. }
  128. }
  129. });
  130. } else {
  131. this.register_validator = $("form.signup_form_submit").validate({
  132. rules: {
  133. first_name: "required",
  134. last_name: "required",
  135. user_login: "required",
  136. user_pass: "required",
  137. user_email: {
  138. required: true,
  139. email: true
  140. },
  141. repeat_pass: {
  142. required: true,
  143. equalTo: "#user_pass"
  144. }
  145. }
  146. });
  147. }
  148. /**
  149. * forgot pass email rule
  150. */
  151. this.forgot_validator = $("form.forgot_form").validate({
  152. rules: {
  153. user_email: {
  154. required: true,
  155. email: true
  156. },
  157. },
  158. highlight: function(element, errorClass, validClass) {
  159. var required_id = $(element).attr('id');
  160. var $container = $(element).closest('div');
  161. if (!$container.hasClass('error')) {
  162. $container.addClass('error').removeClass(validClass);
  163. }
  164. }
  165. });
  166. },
  167. doRegisterNew: function(event) {
  168. event.preventDefault();
  169. event.stopPropagation();
  170. this.removeError();
  171. /**
  172. * call validator init
  173. */
  174. this.initValidator();
  175. var form = $(event.currentTarget),
  176. button = form.find('button.btn-submit'),
  177. view = this;
  178. /**
  179. * scan all fields in form and set the value to model user
  180. */
  181. form.find('input, textarea, select').each(function() {
  182. view.user.set($(this).attr('name'), $(this).val());
  183. })
  184. // check form validate and process sign-up
  185. if (this.register_new_validator.form() && !form.hasClass("processing")) {
  186. this.user.set('do', 'register');
  187. this.user.request('create', {
  188. beforeSend: function() {
  189. view.blockUi.block(button);
  190. form.addClass('processing');
  191. },
  192. success: function(user, status, jqXHR) {
  193. var data = status.data;
  194. form.removeClass('processing');
  195. // trigger event process authentication
  196. // AE.pubsub.trigger('ae:user:auth', user, status, jqXHR);
  197. if (status.success) {
  198. AE.pubsub.trigger('ae:notification', {
  199. msg: status.msg,
  200. notice_type: 'success'
  201. });
  202. if( data.redirect_url == '') {
  203. window.location.href = ae_globals.homeURL;
  204. } else {
  205. window.location.href = data.redirect_url;
  206. }
  207. } else {
  208. view.showError(form, status.msg);
  209. view.blockUi.unblock();
  210. if(typeof grecaptcha != 'undefined'){
  211. grecaptcha.reset();
  212. }
  213. }
  214. }
  215. });
  216. }
  217. },
  218. /**
  219. * user sign-up catch event when user submit form signup
  220. */
  221. doRegister: function(event) {
  222. event.preventDefault();
  223. event.stopPropagation();
  224. /**
  225. * call validator init
  226. */
  227. this.initValidator();
  228. var form = $(event.currentTarget),
  229. button = form.find('button.btn-submit'),
  230. view = this;
  231. /**
  232. * scan all fields in form and set the value to model user
  233. */
  234. form.find('input, textarea, select').each(function() {
  235. view.user.set($(this).attr('name'), $(this).val());
  236. })
  237. // check form validate and process sign-up
  238. if (this.register_validator.form() && !form.hasClass("processing")) {
  239. this.user.set('do', 'register');
  240. this.user.request('create', {
  241. beforeSend: function() {
  242. view.blockUi.block(button);
  243. form.addClass('processing');
  244. },
  245. success: function(user, status, jqXHR) {
  246. view.blockUi.unblock();
  247. form.removeClass('processing');
  248. // trigger event process authentication
  249. AE.pubsub.trigger('ae:user:auth', user, status, jqXHR);
  250. if (status.success) {
  251. AE.pubsub.trigger('ae:notification', {
  252. msg: status.msg,
  253. notice_type: 'success'
  254. });
  255. } else {
  256. AE.pubsub.trigger('ae:notification', {
  257. msg: status.msg,
  258. notice_type: 'error'
  259. });
  260. }
  261. }
  262. });
  263. }
  264. },
  265. doLoginNew: function(event){
  266. event.preventDefault();
  267. event.stopPropagation();
  268. this.removeError();
  269. /**
  270. * call validator init
  271. */
  272. this.initValidator();
  273. var form = $(event.currentTarget),
  274. button = form.find('button.btn-submit'),
  275. view = this;
  276. /**
  277. * scan all fields in form and set the value to model user
  278. */
  279. form.find('input, textarea, select').each(function() {
  280. view.user.set($(this).attr('name'), $(this).val());
  281. })
  282. // check form validate and process sign-in
  283. if (this.login_new_validator.form() && !form.hasClass("processing")) {
  284. this.user.set('do', 'login');
  285. this.user.request('read', {
  286. beforeSend: function() {
  287. view.blockUi.block(button);
  288. form.addClass('processing');
  289. },
  290. success: function(user, status, jqXHR) {
  291. var data = status.data;
  292. form.removeClass('processing');
  293. // trigger event process authentication
  294. // AE.pubsub.trigger('ae:user:auth', user, status, jqXHR);
  295. if (status.success) {
  296. AE.pubsub.trigger('ae:notification', {
  297. msg: status.msg,
  298. notice_type: 'success'
  299. });
  300. if( data.redirect_url == '') {
  301. window.location.href = ae_globals.homeURL;
  302. } else {
  303. window.location.href = data.redirect_url;
  304. }
  305. } else {
  306. view.showError(form, status.msg);
  307. view.blockUi.unblock();
  308. }
  309. }
  310. });
  311. }
  312. },
  313. /**
  314. * user login,catch event when user submit login form
  315. */
  316. doLogin: function(event) {
  317. event.preventDefault();
  318. event.stopPropagation();
  319. /**
  320. * call validator init
  321. */
  322. this.initValidator();
  323. var form = $(event.currentTarget),
  324. button = form.find('button.btn-submit'),
  325. view = this;
  326. /**
  327. * scan all fields in form and set the value to model user
  328. */
  329. form.find('input, textarea, select').each(function() {
  330. view.user.set($(this).attr('name'), $(this).val());
  331. })
  332. // check form validate and process sign-in
  333. if (this.login_validator.form() && !form.hasClass("processing")) {
  334. this.user.set('do', 'login');
  335. this.user.request('read', {
  336. beforeSend: function() {
  337. view.blockUi.block(button);
  338. form.addClass('processing');
  339. },
  340. success: function(user, status, jqXHR) {
  341. view.blockUi.unblock();
  342. form.removeClass('processing');
  343. // trigger event process authentication
  344. AE.pubsub.trigger('ae:user:auth', user, status, jqXHR);
  345. if (status.success) {
  346. AE.pubsub.trigger('ae:notification', {
  347. msg: status.msg,
  348. notice_type: 'success'
  349. });
  350. } else {
  351. AE.pubsub.trigger('ae:notification', {
  352. msg: status.msg,
  353. notice_type: 'error'
  354. });
  355. }
  356. }
  357. });
  358. }
  359. },
  360. /**
  361. * user forgot password
  362. */
  363. doSendPassword: function(event) {
  364. event.preventDefault();
  365. event.stopPropagation();
  366. this.removeError();
  367. /**
  368. * call validator init
  369. */
  370. this.initValidator();
  371. var form = $(event.currentTarget),
  372. email = form.find('input#user_email').val(),
  373. button = form.find('button.btn-submit'),
  374. view = this;
  375. if (this.forgot_validator.form() && !form.hasClass("processing")) {
  376. this.user.set('user_login', email);
  377. this.user.set('do', 'forgot');
  378. this.user.request('read', {
  379. beforeSend: function() {
  380. view.blockUi.block(button);
  381. form.addClass('processing');
  382. },
  383. success: function(user, status, jqXHR) {
  384. var data = status.data;
  385. form.removeClass('processing');
  386. if (status.success) {
  387. AE.pubsub.trigger('ae:notification', {
  388. msg: status.msg,
  389. notice_type: 'success'
  390. });
  391. window.location.href = ae_globals.homeURL;
  392. } else {
  393. view.showError(form, status.msg);
  394. view.blockUi.unblock();
  395. }
  396. // view.blockUi.unblock();
  397. // if (status.success) {
  398. // AE.pubsub.trigger('ae:notification', {
  399. // msg: status.msg,
  400. // notice_type: 'success'
  401. // });
  402. // } else {
  403. // AE.pubsub.trigger('ae:notification', {
  404. // msg: status.msg,
  405. // notice_type: 'error'
  406. // });
  407. // }
  408. }
  409. });
  410. }
  411. },
  412. /**
  413. * Remove HTML error
  414. */
  415. removeError: function(){
  416. $('.fre-validate-error').remove();
  417. },
  418. /**
  419. * Append HTML error
  420. */
  421. showError: function(form, msg){
  422. var template= '<ul class="fre-validate-error">'+
  423. '<li>'+msg+'</li>'+
  424. '</ul>';
  425. form.prepend(template);
  426. }
  427. });
  428. })(jQuery, window.AE.Models, window.AE.Collections, window.AE.Views);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement