| Conditions | 1 |
| Paths | 2 |
| Total Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 | /* global API */ |
||
| 36 | .controller('MainCtrl', ['$scope', 'Settings', '$location', '$rootScope', function ($scope, Settings, $window, $rootScope) { |
||
| 37 | $scope.app = 'passman'; |
||
| 38 | var port = API.runtime.connect(null, { |
||
| 39 | name: "PassmanCommunication" |
||
| 40 | }); |
||
| 41 | |||
| 42 | var messageParser = function (message) { |
||
| 43 | var e = message.split(':'); |
||
| 44 | |||
| 45 | switch (e[0]) { |
||
|
|
|||
| 46 | case "credential_amount": |
||
| 47 | $scope.credential_amount = e[1]; |
||
| 48 | $scope.refreshing_credentials = false; |
||
| 49 | } |
||
| 50 | |||
| 51 | $scope.$apply(); |
||
| 52 | }; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Connect to the background service |
||
| 56 | */ |
||
| 57 | var initApp = function () { |
||
| 58 | port.onMessage.addListener(messageParser); |
||
| 59 | API.runtime.sendMessage(API.runtime.id, {method: "getMasterPasswordSet"}).then(function (isPasswordSet) { |
||
| 60 | function redirectToPrompt() { |
||
| 61 | window.location = '#!/locked'; |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | //First check attributes |
||
| 66 | if (!isPasswordSet) { |
||
| 67 | redirectToPrompt(); |
||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | getActiveTab(); |
||
| 72 | $scope.refreshing_credentials = true; |
||
| 73 | setTimeout(function () { |
||
| 74 | port.postMessage("credential_amount"); |
||
| 75 | }, 500); |
||
| 76 | }); |
||
| 77 | }; |
||
| 78 | |||
| 79 | $scope.refreshing_credentials = false; |
||
| 80 | $scope.refresh = function () { |
||
| 81 | $scope.refreshing_credentials = true; |
||
| 82 | API.runtime.sendMessage(API.runtime.id, {method: "getCredentials"}).then(function () { |
||
| 83 | setTimeout(function () { |
||
| 84 | port.postMessage("credential_amount"); |
||
| 85 | }, 2000); |
||
| 86 | }); |
||
| 87 | }; |
||
| 88 | |||
| 89 | var getActiveTab = function (cb) { |
||
| 90 | API.tabs.query({currentWindow: true, active: true}).then(function (tab) { |
||
| 91 | API.runtime.sendMessage(API.runtime.id, { |
||
| 92 | method: "getCredentialsByUrl", |
||
| 93 | args: [tab[0].url] |
||
| 94 | }).then(function (_logins) { |
||
| 95 | //var url = backgroundPage.processURL(tab.url, $rootScope.app_settings.ignoreProtocol, $rootScope.app_settings.ignoreSubdomain, $rootScope.app_settings.ignorePath); |
||
| 96 | $scope.found_credentials = _logins; |
||
| 97 | $scope.$apply(); |
||
| 98 | }); |
||
| 99 | }); |
||
| 100 | }; |
||
| 101 | |||
| 102 | $scope.lockExtension = function () { |
||
| 103 | API.runtime.sendMessage(API.runtime.id, {method: "setMasterPassword", args: {password: null}}).then(function () { |
||
| 104 | window.location = '#!/locked'; |
||
| 105 | }); |
||
| 106 | }; |
||
| 107 | |||
| 108 | API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) { |
||
| 109 | |||
| 110 | $rootScope.app_settings = settings; |
||
| 111 | if (!settings || Object.keys(settings).length === 0) { |
||
| 112 | window.location = '#!/setup'; |
||
| 113 | } else if (settings.hasOwnProperty('isInstalled')) { |
||
| 114 | window.location = '#!/locked'; |
||
| 115 | } else { |
||
| 116 | initApp(); |
||
| 117 | } |
||
| 118 | }); |
||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | $scope.goto_settings = function () { |
||
| 123 | window.location = '#!/settings'; |
||
| 124 | }; |
||
| 125 | |||
| 126 | $scope.goto_search = function () { |
||
| 127 | window.location = '#!/search'; |
||
| 128 | }; |
||
| 129 | |||
| 130 | $scope.editCredential = function (credential) { |
||
| 131 | window.location = '#!/edit/' + credential.guid; |
||
| 132 | }; |
||
| 133 | }]); |
||
| 134 | }()); |
||
| 136 |