| Conditions | 1 |
| Paths | 2 |
| Total Lines | 112 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 23 | (function () { |
||
| 24 | 'use strict'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @ngdoc directive |
||
| 28 | * @name passmanApp.directive:passwordGen |
||
| 29 | * @description |
||
| 30 | * # passwordGen |
||
| 31 | */ |
||
| 32 | angular.module('passmanExtension') |
||
| 33 | .directive('otpGenerator', ['$compile', '$timeout', |
||
| 34 | function ($compile, $timeout) { |
||
| 35 | function dec2hex(s) { |
||
|
|
|||
| 36 | return (s < 15.5 ? '0' : '') + Math.round(s).toString(16); |
||
| 37 | } |
||
| 38 | |||
| 39 | function hex2dec(s) { |
||
| 40 | return parseInt(s, 16); |
||
| 41 | } |
||
| 42 | |||
| 43 | function base32tohex(base32) { |
||
| 44 | if (!base32) { |
||
| 45 | return; |
||
| 46 | } |
||
| 47 | var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
||
| 48 | var bits = ""; |
||
| 49 | var hex = ""; |
||
| 50 | var i; |
||
| 51 | for (i = 0; i < base32.length; i++) { |
||
| 52 | var val = base32chars.indexOf(base32.charAt(i).toUpperCase()); |
||
| 53 | bits += leftpad(val.toString(2), 5, '0'); |
||
| 54 | } |
||
| 55 | |||
| 56 | for (i = 0; i + 4 <= bits.length; i += 4) { |
||
| 57 | var chunk = bits.substr(i, 4); |
||
| 58 | hex = hex + parseInt(chunk, 2).toString(16); |
||
| 59 | } |
||
| 60 | return hex; |
||
| 61 | |||
| 62 | } |
||
| 63 | |||
| 64 | function leftpad(str, len, pad) { |
||
| 65 | if (len + 1 >= str.length) { |
||
| 66 | str = Array(len + 1 - str.length).join(pad) + str; |
||
| 67 | } |
||
| 68 | return str; |
||
| 69 | } |
||
| 70 | |||
| 71 | return { |
||
| 72 | restrict: 'A', |
||
| 73 | // template: '<span class="otp_generator">{{otp}} <span ng-bind="timeleft"></span></span>', |
||
| 74 | template: '<span class="otp_generator"><span class="code">{{otp}}</span> <copy-text text="otp"></copy-text> <div class="radial-progress"><div class="circle"><div class="mask full"><div class="fill"></div></div><div class="mask half"><div class="fill"></div><div class="fill fix"></div></div></div></div></span>', |
||
| 75 | transclude: false, |
||
| 76 | scope: { |
||
| 77 | secret: '=' |
||
| 78 | }, |
||
| 79 | replace: true, |
||
| 80 | link: function (scope, el) { |
||
| 81 | scope.otp = null; |
||
| 82 | scope.timeleft = null; |
||
| 83 | scope.timer = null; |
||
| 84 | var updateOtp = function () { |
||
| 85 | if (!scope.secret) { |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | window.OTP.secret = scope.secret; |
||
| 89 | scope.otp = window.OTP.getOTP(); |
||
| 90 | }; |
||
| 91 | |||
| 92 | var transform_styles = ['-webkit-transform', '-ms-transform', 'transform']; |
||
| 93 | var timer = function () { |
||
| 94 | var epoch = Math.round(new Date().getTime() / 1000.0); |
||
| 95 | var countDown = 30 - (epoch % 30); |
||
| 96 | if (epoch % 30 === 0) { |
||
| 97 | updateOtp(); |
||
| 98 | } |
||
| 99 | |||
| 100 | var percent = (countDown / 30) * 100 / 100; |
||
| 101 | for (var i = 0; i < transform_styles.length; i++) { |
||
| 102 | var rotation = percent * 180; |
||
| 103 | var fill_rotation = rotation; |
||
| 104 | var fix_rotation = rotation * 2; |
||
| 105 | |||
| 106 | $(el).find('.circle .fill, .circle .mask.full').css(transform_styles[i], 'rotate(' + fill_rotation + 'deg)'); |
||
| 107 | $(el).find('.circle .fill.fix').css(transform_styles[i], 'rotate(' + fix_rotation + 'deg)'); |
||
| 108 | |||
| 109 | } |
||
| 110 | |||
| 111 | scope.timeleft = countDown; |
||
| 112 | scope.timer = $timeout(timer, 100); |
||
| 113 | |||
| 114 | }; |
||
| 115 | scope.$watch("secret", function (n) { |
||
| 116 | if (n) { |
||
| 117 | $timeout.cancel(scope.timer); |
||
| 118 | updateOtp(); |
||
| 119 | timer(); |
||
| 120 | } else { |
||
| 121 | $timeout.cancel(scope.timer); |
||
| 122 | } |
||
| 123 | }, true); |
||
| 124 | scope.$on( |
||
| 125 | "$destroy", |
||
| 126 | function () { |
||
| 127 | $timeout.cancel(scope.timer); |
||
| 128 | } |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | }; |
||
| 132 | } |
||
| 133 | ]); |
||
| 134 | }()); |