keymanager, syncmanager

This commit is contained in:
Mo Bitar
2017-01-25 15:52:03 -06:00
parent 13e6ac59a9
commit a76f725f7f
23 changed files with 1088 additions and 721 deletions

View File

@@ -1,5 +1,5 @@
angular.module('app.frontend')
.directive("header", function(apiController, extensionManager){
.directive("header", function(apiController){
return {
restrict: 'E',
scope: {},
@@ -16,15 +16,9 @@ angular.module('app.frontend')
}
}
})
.controller('HeaderCtrl', function ($state, apiController, modelManager, $timeout, extensionManager, dbManager) {
.controller('HeaderCtrl', function (apiController, modelManager, $timeout, dbManager) {
this.user = apiController.user;
this.extensionManager = extensionManager;
this.loginData = {mergeLocal: true};
this.changePasswordPressed = function() {
this.showNewPasswordForm = !this.showNewPasswordForm;
}
this.accountMenuPressed = function() {
this.serverData = {url: apiController.getServer()};
@@ -32,126 +26,19 @@ angular.module('app.frontend')
this.showFaq = false;
this.showNewPasswordForm = false;
this.showExtensionsMenu = false;
this.showIOMenu = false;
}
this.toggleExtensions = function() {
this.showAccountMenu = false;
this.showIOMenu = false;
this.showExtensionsMenu = !this.showExtensionsMenu;
}
this.toggleExtensionForm = function() {
this.newExtensionData = {};
this.showNewExtensionForm = !this.showNewExtensionForm;
}
this.submitNewExtensionForm = function() {
if(this.newExtensionData.url) {
extensionManager.addExtension(this.newExtensionData.url, function(response){
if(!response) {
alert("Unable to register this extension. Make sure the link is valid and try again.");
} else {
this.newExtensionData.url = "";
this.showNewExtensionForm = false;
}
}.bind(this))
}
}
this.selectedAction = function(action, extension) {
action.running = true;
extensionManager.executeAction(action, extension, null, function(response){
action.running = false;
if(response && response.error) {
action.error = true;
alert("There was an error performing this action. Please try again.");
} else {
action.error = false;
apiController.sync(null);
}
})
}
this.syncProviderActionIsEnabled = function(action) {
var provider = apiController.syncProviderForURL(action.url);
if(!provider) {
return null;
}
return provider.status;
}
this.enableSyncProvider = function(action, extension, primary) {
if(extension.encrypted && !extension.ek) {
alert("You must set an encryption key for this extension before enabling this action.");
return;
}
var provider = apiController.findOrCreateSyncProviderForUrl(action.url);
provider.primary = primary;
provider.enabled = true;
provider.ek = extension.ek;
apiController.addSyncProvider(provider);
}
this.disableSyncProvider = function(action, extension) {
apiController.removeSyncProvider(apiController.syncProviderForURL(action.url));
}
this.deleteExtension = function(extension) {
if(confirm("Are you sure you want to delete this extension?")) {
extensionManager.deleteExtension(extension);
var syncProviderAction = extension.syncProviderAction;
if(syncProviderAction) {
apiController.removeSyncProvider(apiController.syncProviderForURL(syncProviderAction.url));
}
}
}
this.reloadExtensionsPressed = function() {
if(confirm("For your security, reloading extensions will disable any currently enabled sync providers and repeat actions.")) {
extensionManager.refreshExtensionsFromServer();
var syncProviderAction = extension.syncProviderAction;
if(syncProviderAction) {
apiController.removeSyncProvider(apiController.syncProviderForURL(syncProviderAction.url));
}
}
}
this.changeServer = function() {
apiController.setServer(this.serverData.url, true);
}
this.signOutPressed = function() {
this.toggleIO = function() {
this.showIOMenu = !this.showIOMenu;
this.showExtensionsMenu = false;
this.showAccountMenu = false;
apiController.signout(function(){
window.location.reload();
})
}
this.submitPasswordChange = function() {
this.passwordChangeData.status = "Generating New Keys...";
$timeout(function(){
if(data.password != data.password_confirmation) {
alert("Your new password does not match its confirmation.");
return;
}
apiController.changePassword(this.passwordChangeData.current_password, this.passwordChangeData.new_password, function(response){
})
}.bind(this))
}
this.localNotesCount = function() {
return modelManager.filteredNotes.length;
}
this.mergeLocalChanged = function() {
if(!this.loginData.mergeLocal) {
if(!confirm("Unchecking this option means any of the notes you have written while you were signed out will be deleted. Are you sure you want to discard these notes?")) {
this.loginData.mergeLocal = true;
}
}
}
this.refreshData = function() {
@@ -171,110 +58,4 @@ angular.module('app.frontend')
this.syncUpdated = function() {
this.lastSyncDate = new Date();
}
this.loginSubmitPressed = function() {
this.loginData.status = "Generating Login Keys...";
$timeout(function(){
apiController.login(this.loginData.email, this.loginData.user_password, function(response){
if(!response || response.error) {
var error = response ? response.error : {message: "An unknown error occured."}
this.loginData.status = null;
if(!response || (response && !response.didDisplayAlert)) {
alert(error.message);
}
} else {
this.onAuthSuccess(response.user);
}
}.bind(this));
}.bind(this))
}
this.submitRegistrationForm = function() {
this.loginData.status = "Generating Account Keys...";
$timeout(function(){
apiController.register(this.loginData.email, this.loginData.user_password, function(response){
if(!response || response.error) {
var error = response ? response.error : {message: "An unknown error occured."}
this.loginData.status = null;
alert(error.message);
} else {
this.onAuthSuccess(response.user);
}
}.bind(this));
}.bind(this))
}
this.encryptionStatusForNotes = function() {
var allNotes = modelManager.filteredNotes;
return allNotes.length + "/" + allNotes.length + " notes encrypted";
}
this.archiveEncryptionFormat = {encrypted: true};
this.downloadDataArchive = function() {
var link = document.createElement('a');
link.setAttribute('download', 'notes.json');
link.href = apiController.itemsDataFile(this.archiveEncryptionFormat.encrypted);
link.click();
}
this.performImport = function(data, password) {
this.importData.loading = true;
// allow loading indicator to come up with timeout
$timeout(function(){
apiController.importJSONData(data, password, function(success, response){
console.log("Import response:", success, response);
this.importData.loading = false;
if(success) {
this.importData = null;
} else {
alert("There was an error importing your data. Please try again.");
}
}.bind(this))
}.bind(this))
}
this.submitImportPassword = function() {
this.performImport(this.importData.data, this.importData.password);
}
this.importFileSelected = function(files) {
this.importData = {};
var file = files[0];
var reader = new FileReader();
reader.onload = function(e) {
var data = JSON.parse(e.target.result);
$timeout(function(){
if(data.auth_params) {
// request password
this.importData.requestPassword = true;
this.importData.data = data;
} else {
this.performImport(data, null);
}
}.bind(this))
}.bind(this)
reader.readAsText(file);
}
this.onAuthSuccess = function(user) {
var block = function(){
window.location.reload();
this.showLogin = false;
this.showRegistration = false;
}.bind(this);
if(!this.loginData.mergeLocal) {
dbManager.clearAllItems(function(){
block();
});
} else {
block();
}
}
});
});