Completed
Push — master ( fc3c01...f1dad4 )
by Sander
11s
created

js/app/directives/credentialfield.js   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 71
rs 9.1369
cc 1
nc 1
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A angular.directive(ꞌcredentialFieldꞌ) 0 59 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
	/**
26
	 * @ngdoc directive
27
	 * @name passmanApp.directive:passwordGen
28
	 * @description
29
	 * # passwordGen
30
	 */
31
32
	angular.module('passmanApp')
33
		.directive('credentialField', ['$timeout', '$translate', function ($timeout, $translate) {
34
			return {
35
				scope: {
36
					value: '=value',
37
					secret: '=secret'
38
				},
39
				restrict: 'A',
40
				replace: 'true',
41
				template: "" +
42
				'<span class="credential_field">' +
43
				'<div class="value" ng-class="{\'ellipsis\': isLink}">' +
44
				'<span ng-repeat="n in [] | range:value.length" ng-if="!valueVisible">*</span>' +
45
				'<span ng-if="valueVisible">{{value}}</span>' +
46
				'</div>' +
47
				'<div class="tools">' +
48
				'<div class="cell" ng-if="toggle" tooltip="tggltxt" ng-click="toggleVisibility()"><i class="fa" ng-class="{\'fa-eye\': !valueVisible, \'fa-eye-slash\': valueVisible }"></i></div>' +
49
				'<div class="cell" ng-if="isLink"><a ng-href="{{value}}" target="_blank"><i tooltip="\'Open in new window\'" class="link fa fa-external-link"></i></a></div>' +
50
				'<div class="cell" ngclipboard-success="onSuccess(e);" ngclipboard-error="onError(e);" ngclipboard data-clipboard-text="{{value}}"><i  tooltip="copy_msg" class="fa fa-clipboard"></i></div>' +
51
				'</div></span>',
52
				link: function (scope) {
53
					var expression = /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/gi;
54
					var regex = new RegExp(expression);
55
					$translate(['toggle.visibility', 'copy', 'copied']).then(function (translations) {
56
						scope.tggltxt = translations['toggle.visibility'];
57
						scope.copy_msg = translations['copy.field'];
58
					});
59
60
					scope.$watch("value", function () {
61
						if (scope.value) {
62
							if (scope.secret) {
63
								scope.valueVisible = false;
64
							}
65
							if (scope.value.match(regex)) {
66
								scope.isLink = true;
67
68
							}
69
						}
70
					});
71
					if (!scope.toggle) {
72
						if (scope.secret) {
73
							scope.toggle = true;
74
						}
75
					}
76
77
					var timer;
78
					scope.onSuccess = function () {
79
						scope.copy_msg = $translate.instant('copied') ;
80
						$timeout.cancel(timer);
81
						timer = $timeout(function () {
82
							scope.copy_msg = $translate.instant('copy');
83
						}, 5000);
84
					};
85
					scope.valueVisible = true;
86
					scope.toggleVisibility = function () {
87
						scope.valueVisible = !scope.valueVisible;
88
					};
89
				}
90
			};
91
		}]);
92
93
}());