1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 23:33:31 +00:00
Files
browser/src/popup/app/vault/vaultAddCipherController.js
2017-10-25 15:45:39 -04:00

115 lines
3.6 KiB
JavaScript

angular
.module('bit.vault')
.controller('vaultAddCipherController', function ($scope, $state, $stateParams, cipherService, folderService,
cryptoService, toastr, utilsService, $analytics, i18nService, constantsService) {
$scope.i18n = i18nService;
$scope.constants = constantsService;
$scope.addFieldType = constantsService.fieldType.text.toString();
$scope.selectedType = constantsService.cipherType.login.toString();
var from = $stateParams.from,
folderId = $stateParams.folderId;
$scope.cipher = {
folderId: folderId,
name: $stateParams.name,
type: constantsService.cipherType.login,
login: {},
identity: {},
card: {},
secureNote: {
type: 0 // generic note
}
};
if ($stateParams.uri) {
$scope.cipher.login.uri = $stateParams.uri;
}
if ($stateParams.cipher) {
angular.extend($scope.cipher, $stateParams.cipher);
}
if (!$stateParams.cipher && $scope.cipher.name && $scope.cipher.login && $scope.cipher.login.uri) {
$('#username').focus();
}
else {
$('#name').focus();
}
utilsService.initListSectionItemListeners($(document), angular);
folderService.getAllDecrypted().then(function (folders) {
$scope.folders = folders;
});
$scope.typeChanged = function () {
$scope.cipher.type = parseInt($scope.selectedType);
};
$scope.savePromise = null;
$scope.save = function () {
if (!$scope.cipher.name || $scope.cipher.name === '') {
toastr.error(i18nService.nameRequired, i18nService.errorsOccurred);
return;
}
$scope.savePromise = cipherService.encrypt($scope.cipher).then(function (cipherModel) {
var cipher = new Cipher(cipherModel, true);
return cipherService.saveWithServer(cipher);
}).then(function (c) {
$analytics.eventTrack('Added Cipher');
toastr.success(i18nService.addedItem);
$scope.close();
});
};
$scope.close = function () {
if (from === 'current') {
$state.go('tabs.current', {
animation: 'out-slide-down'
});
}
else if (from === 'folder') {
$state.go('viewFolder', {
animation: 'out-slide-down',
folderId: folderId
});
}
else {
$state.go('tabs.vault', {
animation: 'out-slide-down'
});
}
};
$scope.addField = function (type) {
if (!$scope.cipher.fields) {
$scope.cipher.fields = [];
}
$scope.cipher.fields.push({
type: parseInt(type),
name: null,
value: null
});
};
$scope.removeField = function (field) {
var index = $scope.cipher.fields.indexOf(field);
if (index > -1) {
$scope.cipher.fields.splice(index, 1);
}
};
$scope.generatePassword = function () {
$analytics.eventTrack('Clicked Generate Password');
$state.go('passwordGenerator', {
animation: 'in-slide-up',
addState: {
from: from,
cipher: $scope.cipher
}
});
};
});