1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-20 07:40:57 +02:00
bitwarden-browser/src/popup/app/tools/toolsPasswordGeneratorController.js

118 lines
3.9 KiB
JavaScript
Raw Normal View History

angular
.module('bit.tools')
2016-09-22 19:15:42 +02:00
.controller('toolsPasswordGeneratorController', function ($scope, $state, $stateParams, passwordGenerationService,
2016-09-28 05:19:33 +02:00
toastr, $q, utilsService, $analytics) {
var addState = $stateParams.addState,
editState = $stateParams.editState;
2016-09-20 04:24:55 +02:00
$scope.showSelect = $stateParams.addState || $stateParams.editState;
utilsService.initListSectionItemListeners($(document), angular);
$scope.password = '-';
$scope.slider = {
value: 12,
options: {
floor: 5,
ceil: 64,
step: 1,
hideLimitLabels: true,
hidePointerLabels: true,
onChange: function () {
$scope.options.length = $scope.slider.value;
2016-09-28 05:19:33 +02:00
$scope.regenerate(false);
},
onEnd: function () {
2016-09-28 05:19:33 +02:00
$analytics.eventTrack('Generated Password');
$scope.saveOptions($scope.options);
}
}
};
$q.when(passwordGenerationService.getOptions()).then(function (options) {
$scope.options = options;
$scope.slider.value = options.length;
2016-09-28 05:19:33 +02:00
$scope.regenerate(false);
$analytics.eventTrack('Generated Password');
});
2016-09-28 05:19:33 +02:00
$scope.regenerate = function (trackRegenerateEvent) {
if (trackRegenerateEvent) {
$analytics.eventTrack('Regenerated Password');
}
$scope.password = passwordGenerationService.generatePassword($scope.options);
};
$scope.saveOptions = function (options) {
if (!options.uppercase && !options.lowercase && !options.number && !options.special) {
options.lowercase = $scope.options.lowercase = true;
}
if (!options.minNumber) {
options.minNumber = $scope.options.minNumber = 0;
}
if (!options.minSpecial) {
options.minSpecial = $scope.options.minSpecial = 0;
}
passwordGenerationService.saveOptions(options);
2016-09-28 05:19:33 +02:00
$scope.regenerate(false);
return true;
};
$scope.clipboardError = function (e, password) {
toastr.info('Your web browser does not support easy clipboard copying. Copy it manually instead.');
};
$scope.clipboardSuccess = function (e) {
2016-09-28 05:19:33 +02:00
$analytics.eventTrack('Copied Generated Password');
e.clearSelection();
toastr.info('Password copied!');
};
$scope.close = function () {
2016-09-20 04:24:55 +02:00
dismiss();
};
$scope.select = function () {
2016-09-28 05:19:33 +02:00
$analytics.eventTrack('Selected Generated Password');
2016-09-20 04:24:55 +02:00
if (addState) {
addState.site.password = $scope.password;
}
else if (editState) {
editState.site.password = $scope.password;
}
dismiss();
};
function dismiss() {
if (addState) {
$state.go('addSite', {
animation: 'out-slide-down',
2016-09-20 04:24:55 +02:00
fromCurrent: addState.fromCurrent,
site: addState.site,
returnScrollY: addState.returnScrollY,
returnSearchText: addState.returnSearchText
});
}
else if (editState) {
$state.go('editSite', {
animation: 'out-slide-down',
2016-09-20 04:24:55 +02:00
site: editState.site,
fromView: editState.fromView,
siteId: editState.siteId,
returnScrollY: editState.returnScrollY,
returnSearchText: editState.returnSearchText
});
}
else {
$state.go('tabs.tools', {
animation: 'out-slide-down'
});
}
2016-09-20 04:24:55 +02:00
}
});