From 1a86c1b66457995accd06f8648e170e0144c37df Mon Sep 17 00:00:00 2001 From: brantje Date: Mon, 26 Sep 2016 17:42:41 +0200 Subject: [PATCH] Use promise --- js/importers/importer-randomdata.js | 106 +++++++++++++++------------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/js/importers/importer-randomdata.js b/js/importers/importer-randomdata.js index fb56945e..553fda75 100644 --- a/js/importers/importer-randomdata.js +++ b/js/importers/importer-randomdata.js @@ -12,59 +12,65 @@ PassmanImporter.randomData = { }; PassmanImporter.randomData.readFile = function (file_data,callback) { - var credential_list = []; - var tags = - ['Social media', - 'Hosting', - 'Forums', - 'Webshops', - 'FTP', - 'SSH', - 'Banking', - 'Applications', - 'Server stuff', - 'mysql', - 'Wifi', - 'Games', - 'Certificate', - 'Serials' + return new C_Promise(function(){ + var tags = + ['Social media', + 'Hosting', + 'Forums', + 'Webshops', + 'FTP', + 'SSH', + 'Banking', + 'Applications', + 'Server stuff', + 'mysql', + 'Wifi', + 'Games', + 'Certificate', + 'Serials' ]; - var label; - var generateCredential = function (max, i, cb) { - if(jQuery){ - var url = OC.generateUrl('apps/passman/api/internal/generate_person'); - $.ajax({ - url: url, - dataType: 'json', - success: function(data) { - var _credential = PassmanImporter.newCredential(); - label = (Math.random() >= 0.5) ? data.domain : data.email_d +' - ' + data.email_u; - _credential.label = label; - _credential.username = data.username; - _credential.password = data.password; - _credential.url = data.url; + var label; + var credential_list = []; + var _this = this; + var generateCredential = function (max, i, callback) { + if(jQuery){ + var url = OC.generateUrl('apps/passman/api/internal/generate_person'); + $.ajax({ + url: url, + dataType: 'json', + success: function(data) { + var _credential = PassmanImporter.newCredential(); + label = (Math.random() >= 0.5) ? data.domain : data.email_d +' - ' + data.email_u; + _credential.label = label; + _credential.username = data.username; + _credential.password = data.password; + _credential.url = data.url; - var tag_amount = Math.floor(Math.random()*5); - for(var ta = 0; ta < tag_amount; ta++){ - var item = tags[Math.floor(Math.random()*tags.length)]; - var tag = { - text: item - }; - if(_credential.tags.indexOf(tag) === -1){ - _credential.tags.push(tag); + var tag_amount = Math.floor(Math.random()*5); + for(var ta = 0; ta < tag_amount; ta++){ + var item = tags[Math.floor(Math.random()*tags.length)]; + var tag = { + text: item + }; + if(_credential.tags.indexOf(tag) === -1){ + _credential.tags.push(tag); + } + } + credential_list.push(_credential); + if(i <= max){ + _this.call_progress(i/max*100); + generateCredential(max, i + 1, callback) + } else { + callback(credential_list) } } - credential_list.push(_credential); - if(i <= max){ - generateCredential(max, i + 1, callback) - } else { - cb(credential_list) - } - } - }); - } - }; - generateCredential(9, 0,function(credential_list){ - callback(credential_list); + }); + } + }; + + + generateCredential(9, 0,function(credential_list){ + _this.call_then(credential_list); + }); }); };