From 8db3f313673b58e87f5ef811d42f85c3b3274bff Mon Sep 17 00:00:00 2001 From: Marcos Zuriaga Date: Mon, 26 Sep 2016 17:05:47 +0200 Subject: [PATCH] Changes on promises keepass importer ready and using promises --- js/app/controllers/import.js | 9 ++++-- js/app/services/shareservice.js | 18 +++++------ js/importers/importer-keepasscsv.js | 48 ++++++++++++++++------------- js/lib/promise.js | 22 +++++++------ 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/js/app/controllers/import.js b/js/app/controllers/import.js index 5099745b..fbfb35ff 100644 --- a/js/app/controllers/import.js +++ b/js/app/controllers/import.js @@ -82,7 +82,10 @@ angular.module('passmanApp') $scope.startImport = function(){ if(file_data){ - $window.PassmanImporter[$scope.selectedImporter.id].readFile(file_data, function(parseddata){ + $window.PassmanImporter[$scope.selectedImporter.id] + .readFile(file_data) + .then(function(parseddata){ + console.log("woof!"); parsed_data = parseddata; _log('Parsed '+ parsed_data.length + ' credentials, starting to import'); $scope.current_import_length = parsed_data.length; @@ -91,9 +94,9 @@ angular.module('passmanApp') } else { // @TODO Show message no data found } + }).progress(function(percentage){ + console.log(percentage + '%'); }); - - } } diff --git a/js/app/services/shareservice.js b/js/app/services/shareservice.js index a9a26817..9e2a5e91 100644 --- a/js/app/services/shareservice.js +++ b/js/app/services/shareservice.js @@ -26,7 +26,7 @@ angular.module('passmanApp') }); }, generateRSAKeys: function(key_length, progress, callback){ - var p = new C_Promise(function(promise){ + var p = new C_Promise(function(){ var state = forge.pki.rsa.createKeyPairGenerationState(key_length, 0x10001); var step = function() { // run for 100 ms @@ -34,32 +34,32 @@ angular.module('passmanApp') // console.log(state); if (state.p !== null) { // progress(50); - promise.call_progress(50); + this.call_progress(50); } else { // progress(0); - promise.call_progress(50); + this.call_progress(0); } - setTimeout(step, 1); + setTimeout(step.bind(this), 1); } else { // callback(state.keys); - promise.call_then(state.keys); + this.call_then(state.keys); } }; - setTimeout(step, 100); + setTimeout(step.bind(this), 100); }); return p; }, generateSharedKey: function(size){ size = size || 20; - return new C_Promise(function(promise){ /** prmise C_Promise **/ + return new C_Promise(function(){ CRYPTO.PASSWORD.generate(size, function(pass) { - promise.call_then(pass); + this.call_then(pass); }, function(progress) { - promise.call_progress(progress); + this.call_progress(progress); } ); }) diff --git a/js/importers/importer-keepasscsv.js b/js/importers/importer-keepasscsv.js index 0a63daee..bd1767c0 100644 --- a/js/importers/importer-keepasscsv.js +++ b/js/importers/importer-keepasscsv.js @@ -12,28 +12,32 @@ PassmanImporter.keepassCsv = { }; PassmanImporter.keepassCsv.readFile = function (file_data, callback) { - var parsed_csv = PassmanImporter.readCsv(file_data); - var credential_list = []; - for (var i = 0; i < parsed_csv.length; i++) { - var row = parsed_csv[i]; - var _credential = PassmanImporter.newCredential(); - _credential.label = row.account; - _credential.username = row.login_name; - _credential.password = row.password; - _credential.url = row.web_site; - if (row.hasOwnProperty('expires')) { - row.expires = row.expires.replace('"',''); - _credential.expire_time = new Date(row.expires).getTime() / 1000; - } - var tags = [{text: row.group}]; - if (row.hasOwnProperty('group_tree')) { - var exploded_tree = row.group_tree.split('\\\\'); - for (var t = 0; t < exploded_tree.length; t++) { - tags.push({text: exploded_tree[t]}); + var p = new C_Promise(function(){ + var parsed_csv = PassmanImporter.readCsv(file_data); + var credential_list = []; + for (var i = 0; i < parsed_csv.length; i++) { + var row = parsed_csv[i]; + var _credential = PassmanImporter.newCredential(); + _credential.label = row.account; + _credential.username = row.login_name; + _credential.password = row.password; + _credential.url = row.web_site; + if (row.hasOwnProperty('expires')) { + row.expires = row.expires.replace('"',''); + _credential.expire_time = new Date(row.expires).getTime() / 1000; } + var tags = [{text: row.group}]; + if (row.hasOwnProperty('group_tree')) { + var exploded_tree = row.group_tree.split('\\\\'); + for (var t = 0; t < exploded_tree.length; t++) { + tags.push({text: exploded_tree[t]}); + } + } + _credential.tags = tags; + credential_list.push(_credential); + this.call_progress(i/parsed_csv.length*100); } - _credential.tags = tags; - credential_list.push(_credential); - } - callback(credential_list); + this.call_then(credential_list); + }); + return p; }; \ No newline at end of file diff --git a/js/lib/promise.js b/js/lib/promise.js index af3910d0..831edc4f 100644 --- a/js/lib/promise.js +++ b/js/lib/promise.js @@ -9,26 +9,28 @@ */ function C_Promise(workload){ + this.update = null; this.finally = null; this.error_function = null; this.then = function(callback){ this.finally = callback; return this; - } + }; this.progress = function(callback){ this.update = callback; return this; - } + }; this.error = function (callback){ this.error_function = callback; return this; - } + }; this.call_then = function(data){ - if (this.finally !== undefined) this.finally(data); - } + if (this.finally !== null) this.finally(data); + }; this.call_progress = function(data){ - if (this.progress !== undefined) this.progress(data); - } + if (this.update !== null) this.update(data); + }; this.call_error = function(data){ - if(this.error_function !== undefined) this.error_function(data); - } - setTimeout(workload(this), 100); + if(this.error_function !== null) this.error_function(data); + }; + + setTimeout(workload.bind(this), 100); } \ No newline at end of file