|
| 1 | +/** |
| 2 | + * Copyright © 2016 Magento. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +/*browser:true*/ |
| 6 | +/*global define*/ |
| 7 | +define([ |
| 8 | + 'jquery', |
| 9 | + 'uiComponent', |
| 10 | + 'ko', |
| 11 | + 'Magento_Customer/js/model/customer', |
| 12 | + 'Magento_Customer/js/action/check-email-availability', |
| 13 | + 'Magento_Customer/js/action/login', |
| 14 | + 'Magento_Checkout/js/model/quote', |
| 15 | + 'Magento_Checkout/js/checkout-data', |
| 16 | + 'Magento_Checkout/js/model/full-screen-loader', |
| 17 | + 'mage/validation' |
| 18 | +], function ($, Component, ko, customer, checkEmailAvailability, loginAction, quote, checkoutData, fullScreenLoader) { |
| 19 | + 'use strict'; |
| 20 | + |
| 21 | + var validatedEmail = checkoutData.getValidatedEmailValue(); |
| 22 | + |
| 23 | + if (validatedEmail && !customer.isLoggedIn()) { |
| 24 | + quote.guestEmail = validatedEmail; |
| 25 | + } |
| 26 | + |
| 27 | + return Component.extend({ |
| 28 | + defaults: { |
| 29 | + template: 'Diglin_Username/form/element/email', |
| 30 | + email: checkoutData.getInputFieldEmailValue(), |
| 31 | + emailFocused: false, |
| 32 | + isLoading: false, |
| 33 | + isPasswordVisible: false, |
| 34 | + listens: { |
| 35 | + email: 'emailHasChanged', |
| 36 | + emailFocused: 'validateEmail' |
| 37 | + } |
| 38 | + }, |
| 39 | + checkDelay: 2000, |
| 40 | + checkRequest: null, |
| 41 | + isEmailCheckComplete: null, |
| 42 | + isCustomerLoggedIn: customer.isLoggedIn, |
| 43 | + forgotPasswordUrl: window.checkoutConfig.forgotPasswordUrl, |
| 44 | + emailCheckTimeout: 0, |
| 45 | + |
| 46 | + /** |
| 47 | + * Initializes observable properties of instance |
| 48 | + * |
| 49 | + * @returns {Object} Chainable. |
| 50 | + */ |
| 51 | + initObservable: function () { |
| 52 | + this._super() |
| 53 | + .observe(['email', 'emailFocused', 'isLoading', 'isPasswordVisible']); |
| 54 | + |
| 55 | + return this; |
| 56 | + }, |
| 57 | + |
| 58 | + /** |
| 59 | + * Callback on changing email property |
| 60 | + */ |
| 61 | + emailHasChanged: function () { |
| 62 | + var self = this; |
| 63 | + |
| 64 | + clearTimeout(this.emailCheckTimeout); |
| 65 | + |
| 66 | + if (self.validateEmail()) { |
| 67 | + quote.guestEmail = self.email(); |
| 68 | + checkoutData.setValidatedEmailValue(self.email()); |
| 69 | + } |
| 70 | + this.emailCheckTimeout = setTimeout(function () { |
| 71 | + if (self.validateEmail()) { |
| 72 | + self.checkEmailAvailability(); |
| 73 | + } else { |
| 74 | + self.isPasswordVisible(false); |
| 75 | + } |
| 76 | + }, self.checkDelay); |
| 77 | + |
| 78 | + checkoutData.setInputFieldEmailValue(self.email()); |
| 79 | + }, |
| 80 | + |
| 81 | + /** |
| 82 | + * Check email existing. |
| 83 | + */ |
| 84 | + checkEmailAvailability: function () { |
| 85 | + var self = this; |
| 86 | + this.validateRequest(); |
| 87 | + this.isEmailCheckComplete = $.Deferred(); |
| 88 | + this.isLoading(true); |
| 89 | + this.checkRequest = checkEmailAvailability(this.isEmailCheckComplete, this.email()); |
| 90 | + |
| 91 | + $.when(this.isEmailCheckComplete).done(function () { |
| 92 | + self.isPasswordVisible(false); |
| 93 | + }).fail(function () { |
| 94 | + self.isPasswordVisible(true); |
| 95 | + }).always(function () { |
| 96 | + self.isLoading(false); |
| 97 | + }); |
| 98 | + }, |
| 99 | + |
| 100 | + /** |
| 101 | + * If request has been sent -> abort it. |
| 102 | + * ReadyStates for request aborting: |
| 103 | + * 1 - The request has been set up |
| 104 | + * 2 - The request has been sent |
| 105 | + * 3 - The request is in process |
| 106 | + */ |
| 107 | + validateRequest: function () { |
| 108 | + if (this.checkRequest != null && $.inArray(this.checkRequest.readyState, [1, 2, 3])) { |
| 109 | + this.checkRequest.abort(); |
| 110 | + this.checkRequest = null; |
| 111 | + } |
| 112 | + }, |
| 113 | + |
| 114 | + /** |
| 115 | + * Local email validation. |
| 116 | + * |
| 117 | + * @param {Boolean} focused - input focus. |
| 118 | + * @returns {Boolean} - validation result. |
| 119 | + */ |
| 120 | + validateEmail: function (focused) { |
| 121 | + var loginFormSelector = 'form[data-role=email-with-possible-login]', |
| 122 | + usernameSelector = loginFormSelector + ' input[name=username]', |
| 123 | + loginForm = $(loginFormSelector), |
| 124 | + validator; |
| 125 | + |
| 126 | + loginForm.validation(); |
| 127 | + |
| 128 | + if (focused === false && !!this.email()) { |
| 129 | + return !!$(usernameSelector).valid(); |
| 130 | + } |
| 131 | + |
| 132 | + validator = loginForm.validate(); |
| 133 | + |
| 134 | + return validator.check(usernameSelector); |
| 135 | + }, |
| 136 | + |
| 137 | + /** |
| 138 | + * Log in form submitting callback. |
| 139 | + * |
| 140 | + * @param {HTMLElement} loginForm - form element. |
| 141 | + */ |
| 142 | + login: function (loginForm) { |
| 143 | + var loginData = {}, |
| 144 | + formDataArray = $(loginForm).serializeArray(); |
| 145 | + |
| 146 | + formDataArray.forEach(function (entry) { |
| 147 | + loginData[entry.name] = entry.value; |
| 148 | + }); |
| 149 | + |
| 150 | + if (this.isPasswordVisible() && $(loginForm).validation() && $(loginForm).validation('isValid')) { |
| 151 | + fullScreenLoader.startLoader(); |
| 152 | + loginAction(loginData).always(function() { |
| 153 | + fullScreenLoader.stopLoader(); |
| 154 | + }); |
| 155 | + } |
| 156 | + } |
| 157 | + }); |
| 158 | +}); |
0 commit comments