Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/code/Magento/Customer/view/frontend/templates/form/login.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@
</form>
</div>
</div>

<script type="text/x-magento-init">
{
".field.email": {
"Magento_Customer/js/trim-username": {
"formSelector": "form.form-login"
}
}
}
</script>
65 changes: 65 additions & 0 deletions app/code/Magento/Customer/view/frontend/web/js/trim-username.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'jquery'
], function ($) {
'use strict';

$.widget('mage.trimUsername', {
options: {
cache: {},
formSelector: 'form',
emailSelector: 'input[type="email"]'
},

/**
* Widget initialization
* @private
*/
_create: function () {
// We need to look outside the module for backward compatibility, since someone can already use the module.
// @todo Narrow this selector in 2.3 so it doesn't accidentally finds the the email field from the
// newsletter email field or any other "email" field.
this.options.cache.email = $(this.options.formSelector).find(this.options.emailSelector);
this._bind();
},

/**
* Event binding, will monitor change, keyup and paste events.
* @private
*/
_bind: function () {
if (this.options.cache.email.length) {
this._on(this.options.cache.email, {
'change': this._trimUsername,
'keyup': this._trimUsername,
'paste': this._trimUsername
});
}
},

/**
* Trim username
* @private
*/
_trimUsername: function () {
var username = this._getUsername().trim();

this.options.cache.email.val(username);
},

/**
* Get username value
* @returns {*}
* @private
*/
_getUsername: function () {
return this.options.cache.email.val();
}
});

return $.mage.trimUsername;
});