This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtoggle-password-directive-tips.js
76 lines (63 loc) · 2.33 KB
/
toggle-password-directive-tips.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
import angular from 'angular'
(function() {
'use strict'
angular.module('accounts.directives').directive('togglePasswordWithTips', togglePasswordWithTips)
togglePasswordWithTips.$inject = ['$timeout']
function togglePasswordWithTips($timeout) {
return {
restrict: 'E',
require: '^form',
template: require('../../views/directives/toggle-password-with-tips.directive')(),
link: function(scope, element, attrs, formController) {
var vm = scope.vm
vm.defaultPlaceholder = attrs.placeholder || ''
vm.placeholder = vm.defaultPlaceholder
vm.password = ''
vm.toggleShowLabel = 'Show'
var passwordInput = element.find('input')[0]
element.bind('click', function(event) {
passwordInput.focus()
})
element.bind('keyup', function(event) {
if (event.keyCode === 13) {
passwordInput.blur()
}
})
vm.onFocus = function(event) {
vm.passwordFocus = true
vm.placeholder = ''
element.addClass('focus')
}
vm.onBlur = function(event) {
var relatedTarget = angular.element(event.relatedTarget)
element.removeClass('focus')
// If you are blurring from the password input and clicking the checkbox
if (relatedTarget.attr('type') === 'checkbox' && relatedTarget.attr('id') === 'toggleInputTypeBtn') {
vm.passwordFocus = true
vm.placeholder = ''
passwordInput.focus()
} else {
// If you are blurring from the password input and clicking anywhere but the checkbox
$timeout(function () {
vm.passwordFocus = false
}, 100)
if (vm.password === '' || vm.password === undefined) {
vm.placeholder = vm.defaultPlaceholder
formController.password.$setPristine()
}
}
}
vm.toggleInputType = function() {
var $passwordInput = angular.element(passwordInput)
if ($passwordInput.attr('type') === 'text') {
$passwordInput.attr('type', 'password')
vm.toggleShowLabel = 'Show'
} else {
$passwordInput.attr('type', 'text')
vm.toggleShowLabel = 'Hide'
}
}
}
}
}
})()