angular.module("LoginDirective", ["AjaxService", "UserService"]).directive('login', function (Ajax, User, $mdToast, $timeout) { return { templateUrl: "?component=" + "Directives/Login" + "&html", restrict: "E", scope: { onLogin: "&" }, link: function ($scope) { $scope.load = function () { $scope.user = {}; if (localStorage) { var tmp = localStorage.getItem("email"); if (tmp && tmp !== "") { $scope.user.cliente_email = tmp; $scope.user.ricorda_email = true; } } $timeout(function () { var elem = angular.element('.login_password:-webkit-autofill'); if (elem.length > 0) { elem.parent().addClass('md-input-has-value'); } }, 150); }; $scope.onSignin = false; $scope.setResetForm = function(v) { $scope.isResetForm = v; }; $scope.signin = function () { if ($scope.user.cliente_email && $scope.user.cliente_email !== "" && $scope.user.cliente_password && $scope.user.cliente_password !== "") { $scope.onSignin = true; Ajax.post("User", "signin", {user: $scope.user}, function (result) { if (result) { if (result.success) { User.reload(); if (localStorage) { if ($scope.user.ricorda_email) { localStorage.setItem("email", $scope.user.cliente_email); } else { localStorage.removeItem("email"); } } if ($scope.onLogin) { $scope.onLogin(); } } else { $mdToast.show($mdToast.simple() .textContent(result.message) .hideDelay(3000)); } } $scope.onSignin = false; }); } else { $mdToast.show($mdToast.simple() .textContent('Inserire e-mail e password') .hideDelay(3000)); } }; $scope.onRecoverPassword = false; $scope.recover = { account: null }; $scope.recoverPassword = function () { if ($scope.recover.account && $scope.recover.account !== "") { $scope.onRecoverPassword = true; Ajax.post("User", "recoverPassword", {account: $scope.recover.account}, function (result) { if (result) { if (result.success) { $scope.isResetForm = false; $mdToast.show($mdToast.simple() .textContent(result.message) .hideDelay(3000)); } else { $mdToast.show($mdToast.simple() .textContent(result.message) .hideDelay(3000)); } } $scope.onRecoverPassword = false; $scope.recover.account = null; }); } }; $scope.load(); } }; });