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