Completed
Push — master ( 4bb4df...375c4e )
by Sander
10s
created

js/app/directives/credentialcounter.js   A

Complexity

Conditions 1
Paths 72

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
nc 72
nop 0
dl 0
loc 53
rs 9.5797
c 3
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.directive(ꞌcredentialCounterꞌ) 0 43 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
	angular.module('passmanApp')
32
		.directive('credentialCounter', [function () {
33
			return {
34
				template: '<div ng-show="counter" translate="number.filtered" translate-values="{number_filtered: counter, credential_number: total}"></div>',
35
				replace: false,
36
				restrict: 'A',
37
				scope: {
38
					filteredCredentials: '=credentialCounter',
39
					deleteTime: '=',
40
					vault: '=',
41
					filters: '='
42
				},
43
44
				link: function (scope) {
45
					function countCredentials() {
46
						var countedCredentials = 0;
47
						var total = 0;
48
						if(!scope.vault || !scope.vault.hasOwnProperty('credentials')){
49
							return;
50
						}
51
52
						angular.forEach(scope.vault.credentials, function (credential) {
53
							var pos = scope.filteredCredentials.map(function(c) { return c.guid; }).indexOf(credential.guid);
54
55
							if (scope.deleteTime === 0 && credential.hidden === 0 && credential.delete_time === 0) {
56
								total = total + 1;
57
								countedCredentials = (pos !== -1) ? countedCredentials + 1 : countedCredentials;
58
							}
59
60
							if (scope.deleteTime > 0 && credential.hidden === 0 && credential.delete_time > 0) {
61
								total = total + 1;
62
								countedCredentials = (pos !== -1) ? countedCredentials + 1 : countedCredentials;
63
							}
64
65
						});
66
						scope.counter = countedCredentials;
67
						scope.total = total;
68
					}
69
					scope.$watch('[filteredCredentials, deleteTime, filters]', function () {
70
						countCredentials();
71
					}, true);
72
				}
73
			};
74
		}]);
75
}());