{"version":3,"sources":["login/src/login.js"],"names":["$","LoginForm","Garnish","Base","extend","$form","$loginNameInput","$passwordInput","$rememberMeCheckbox","$forgotPasswordLink","$rememberPasswordLink","$submitBtn","$spinner","forgotPassword","validateOnInput","init","$errors","Craft","PasswordInput","removeListener","$newPasswordInput","addListener","this","isMobileBrowser","val","focus","validate","loginNameVal","length","window","useEmailAsUsername","t","match","passwordLength","minPasswordLength","attribute","min","maxPasswordLength","max","onInput","clearErrors","event","onSubmit","error","preventDefault","showError","addClass","submitForgotPassword","submitLogin","response","success","MessageSentModal","postActionRequest","data","textStatus","onSubmitResponse","returnUrl","password","prop","location","href","shake","removeClass","onSwitchForm","trigger","appendTo","velocity","base","$container","toggleClass","text","Modal","$bod","hide","jQuery"],"mappings":"CAAA,SAACA,GAEG,IAAAC,EAAAC,QAAAC,KAAAC,OAAA,CACIH,MAAS,KACTI,gBADgC,KAEhCC,eAAiB,KACjBC,oBAHgC,KAIhCC,oBAJgC,KAKhCC,sBALgC,KAMhCC,WAAAA,KACAC,SAAY,KACZC,QAAU,KAGVC,gBAAgB,EAChBC,iBAAiB,EAGbC,KAAKV,WACAC,KAAAA,MAAAA,EAAL,eACKC,KAAAA,gBAAmBP,EAAA,cACnBQ,KAAAA,eAAuBR,EAAC,aACxBS,KAAAA,oBAAwBT,EAAA,eACxBU,KAAAA,oBAA0BV,EAAA,oBAC1BW,KAAAA,sBAALX,EAAA,sBACKY,KAAAA,WAAaZ,EAAA,WACbgB,KAAAA,SAAYhB,EAAA,YAEbiB,KAAMC,QAAAA,EAAV,iBAEQ,IAAKC,MAAAA,cAAoBZ,KAAAA,eAAgB,CACpCA,cAALa,IACKC,KAAAA,eAAiBd,KAAAA,eAAyB,SAClDe,KAAAf,eAAAa,EALLE,KAAAD,YAAAC,KAAAf,eAAA,QAAA,cAWKc,KAAAA,YAAiBX,KAAAA,gBAAuB,QAAS,WACjDW,KAAAA,YAAiBhB,KAAtBE,eAAuC,QAEvC,WAJAe,KAAKD,YAAY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","sourcesContent":["(function($) {\n    /** global: Craft */\n    /** global: Garnish */\n    var LoginForm = Garnish.Base.extend({\n        $form: null,\n        $loginNameInput: null,\n        $passwordInput: null,\n        $rememberMeCheckbox: null,\n        $forgotPasswordLink: null,\n        $rememberPasswordLink: null,\n        $submitBtn: null,\n        $spinner: null,\n        $errors: null,\n\n        forgotPassword: false,\n        validateOnInput: false,\n\n        init: function() {\n            this.$form = $('#login-form');\n            this.$loginNameInput = $('#loginName');\n            this.$passwordInput = $('#password');\n            this.$rememberMeCheckbox = $('#rememberMe');\n            this.$forgotPasswordLink = $('#forgot-password');\n            this.$rememberPasswordLink = $('#remember-password');\n            this.$submitBtn = $('#submit');\n            this.$spinner = $('#spinner');\n            this.$errors = $('#login-errors');\n\n            new Craft.PasswordInput(this.$passwordInput, {\n                onToggleInput: $newPasswordInput => {\n                    this.removeListener(this.$passwordInput, 'input');\n                    this.$passwordInput = $newPasswordInput;\n                    this.addListener(this.$passwordInput, 'input', 'onInput');\n                },\n            });\n\n            this.addListener(this.$loginNameInput, 'input', 'onInput')\n            this.addListener(this.$passwordInput, 'input', 'onInput');\n            this.addListener(this.$forgotPasswordLink, 'click', 'onSwitchForm');\n            this.addListener(this.$rememberPasswordLink, 'click', 'onSwitchForm');\n            this.addListener(this.$form, 'submit', 'onSubmit');\n\n            // Focus first empty field in form\n            if (!Garnish.isMobileBrowser()) {\n                if (this.$loginNameInput.val()) {\n                    this.$passwordInput.focus();\n                } else {\n                    this.$loginNameInput.focus();\n                }\n            }\n        },\n\n        validate: function() {\n            const loginNameVal = this.$loginNameInput.val();\n            if (loginNameVal.length === 0) {\n                if (window.useEmailAsUsername) {\n                    return Craft.t('app', 'Invalid email.');\n                }\n                return Craft.t('app', 'Invalid username or email.');\n            }\n\n            if (window.useEmailAsUsername && !loginNameVal.match('.+@.+\\..+')) {\n                return Craft.t('app', 'Invalid email.');\n            }\n\n            if (!this.forgotPassword) {\n                const passwordLength = this.$passwordInput.val().length;\n                if (passwordLength < window.minPasswordLength) {\n                    return Craft.t('yii', '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.', {\n                        attribute: Craft.t('app', 'Password'),\n                        min: window.minPasswordLength,\n                    });\n                }\n                if (passwordLength > window.maxPasswordLength) {\n                    return Craft.t('yii', '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.', {\n                        attribute: Craft.t('app', 'Password'),\n                        max: window.maxPasswordLength,\n                    });\n                }\n            }\n\n            return true;\n        },\n\n        onInput: function(event) {\n            if (this.validateOnInput && this.validate() === true) {\n                this.clearErrors();\n            }\n        },\n\n        onSubmit: function(event) {\n            // Prevent full HTTP submits\n            event.preventDefault();\n\n            const error = this.validate();\n            if (error !== true) {\n                this.showError(error);\n                this.validateOnInput = true;\n                return;\n            }\n\n            this.$submitBtn.addClass('active');\n            this.$spinner.removeClass('hidden');\n\n            this.clearErrors();\n\n            if (this.forgotPassword) {\n                this.submitForgotPassword();\n            } else {\n                this.submitLogin();\n            }\n        },\n\n        submitForgotPassword: function() {\n            var data = {\n                loginName: this.$loginNameInput.val()\n            };\n\n            Craft.postActionRequest('users/send-password-reset-email', data, (response, textStatus) => {\n                if (textStatus === 'success') {\n                    if (response.success) {\n                        new MessageSentModal();\n                    } else {\n                        this.showError(response.error);\n                    }\n                }\n\n                this.onSubmitResponse();\n            });\n        },\n\n        submitLogin: function() {\n            var data = {\n                loginName: this.$loginNameInput.val(),\n                password: this.$passwordInput.val(),\n                rememberMe: (this.$rememberMeCheckbox.prop('checked') ? 'y' : '')\n            };\n\n            Craft.postActionRequest('users/login', data, (response, textStatus) => {\n                if (textStatus === 'success') {\n                    if (response.success) {\n                        window.location.href = response.returnUrl;\n                    } else {\n                        Garnish.shake(this.$form);\n                        this.onSubmitResponse();\n\n                        // Add the error message\n                        this.showError(response.error);\n                    }\n                } else {\n                    this.onSubmitResponse();\n                }\n            });\n\n            return false;\n        },\n\n        onSubmitResponse: function() {\n            this.$submitBtn.removeClass('active');\n            this.$spinner.addClass('hidden');\n        },\n\n        showError: function(error) {\n            this.clearErrors();\n\n            $('<p style=\"display: none;\">' + error + '</p>')\n                .appendTo(this.$errors)\n                .velocity('fadeIn');\n        },\n\n        clearErrors: function() {\n            this.$errors.empty();\n        },\n\n        onSwitchForm: function(event) {\n            if (!Garnish.isMobileBrowser()) {\n                this.$loginNameInput.trigger('focus');\n            }\n\n            this.clearErrors();\n\n            this.forgotPassword = !this.forgotPassword;\n\n            this.$form.toggleClass('reset-password', this.forgotPassword);\n            this.$submitBtn.text(Craft.t('app', this.forgotPassword ? 'Reset Password' : 'Login'));\n        },\n    });\n\n    var MessageSentModal = Garnish.Modal.extend({\n        init: function() {\n            var $container = $('<div class=\"modal fitted email-sent\"><div class=\"body\">' + Craft.t('app', 'Check your email for instructions to reset your password.') + '</div></div>')\n                .appendTo(Garnish.$bod);\n\n            this.base($container);\n        },\n\n        hide: function() {\n        }\n    });\n\n    new LoginForm();\n})(jQuery);\n"]}