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