refactor: promises

This commit is contained in:
Bengt Brodersen 2015-11-25 12:01:42 +01:00
parent 6ea0d253f4
commit b690b314fe

View File

@ -24,8 +24,8 @@
appendScript('https://qoomon.github.io/void', function() { appendScript('https://qoomon.github.io/void', function() {
init().then(function(){ init().then(function(){
main(); main();
}).catch(function(){ }).catch(function(cause){
alert("ERROR on init!"); alert("ERROR on init!\n\n" + cause);
}); });
}); });
@ -62,8 +62,6 @@
return; return;
} }
// open print preview // open print preview
jQuery("body").append(printOverlayHTML()); jQuery("body").append(printOverlayHTML());
jQuery("#card-print-overlay").prepend(printOverlayStyle()); jQuery("#card-print-overlay").prepend(printOverlayStyle());
@ -87,7 +85,7 @@
jQuery("#hide-status-checkbox").attr('checked', readCookie("card_printer_hide_status", 'true') == 'true'); jQuery("#hide-status-checkbox").attr('checked', readCookie("card_printer_hide_status", 'true') == 'true');
jQuery("#card-print-dialog-title").text("Card Printer " + global.version + " - Loading issues..."); jQuery("#card-print-dialog-title").text("Card Printer " + global.version + " - Loading issues...");
renderCards(issueKeyList, function() { renderCards(issueKeyList).then(function() {
jQuery("#card-print-dialog-title").text("Card Printer " + global.version); jQuery("#card-print-dialog-title").text("Card Printer " + global.version);
//print(); //print();
}); });
@ -98,6 +96,8 @@
} }
function init() { function init() {
var promises = [];
console.log("Init...") console.log("Init...")
addStringFunctions(); addStringFunctions();
addDateFunctions(); addDateFunctions();
@ -113,7 +113,7 @@
initGoogleAnalytics(); initGoogleAnalytics();
} }
var promises = [];
promises.push(httpGetCors(global.hostOrigin + "card.html", function(data){ promises.push(httpGetCors(global.hostOrigin + "card.html", function(data){
global.cardHtml = data; global.cardHtml = data;
console.log("foooooo: " + data); console.log("foooooo: " + data);
@ -133,7 +133,7 @@
printWindow.print(); printWindow.print();
} }
function renderCards(issueKeyList, callback) { function renderCards(issueKeyList) {
var printFrame = jQuery("#card-print-dialog-content-iframe"); var printFrame = jQuery("#card-print-dialog-content-iframe");
var printWindow = printFrame[0].contentWindow; var printWindow = printFrame[0].contentWindow;
@ -148,15 +148,15 @@
console.log("load " + issueKeyList.length + " issues..."); console.log("load " + issueKeyList.length + " issues...");
var deferredList = []; var promises = [];
jQuery.each(issueKeyList, function(index, issueKey) { jQuery.each(issueKeyList, function(index, issueKey) {
var card = cardElement(issueKey); var card = cardElement(issueKey);
card.attr("index", index); card.attr("index", index);
card.hide(); card.hide();
card.find('.issue-id').text(issueKey); card.find('.issue-id').text(issueKey);
jQuery("body", printDocument).append(card); jQuery("body", printDocument).append(card);
var deferred = addDeferred(deferredList);
global.appFunctions.getCardData(issueKey, function(cardData) { promises.push(global.appFunctions.getCardData(issueKey).then(function(cardData) {
//console.log("cardData: " + cardData); //console.log("cardData: " + cardData);
if (global.isProd) { if (global.isProd) {
ga('send', 'event', 'card', 'generate', cardData.type); ga('send', 'event', 'card', 'generate', cardData.type);
@ -164,19 +164,18 @@
fillCard(card, cardData); fillCard(card, cardData);
card.show(); card.show();
redrawCards(); redrawCards();
deferred.resolve(); }));
});
}); });
console.log("wait for issues loaded..."); console.log("wait for issues loaded...");
applyDeferred(deferredList, function() { return Promise.all(promises).then(function() {
console.log("...all issues loaded."); console.log("...all issues loaded.");
jQuery(printWindow).load(function() { jQuery(printWindow).load(function() {
console.log("...all resources loaded."); console.log("...all resources loaded.");
callback(); });
})
printDocument.close();
console.log("wait for resources loaded..."); console.log("wait for resources loaded...");
printDocument.close();
}); });
} }
@ -1054,16 +1053,6 @@ body {
//############################################################################################################################ //############################################################################################################################
//############################################################################################################################ //############################################################################################################################
function addDeferred(deferredList) {
var deferred = new jQuery.Deferred()
deferredList.push(deferred);
return deferred;
}
function applyDeferred(deferredList, callback) {
jQuery.when.apply(jQuery, deferredList).done(callback);
}
function readCookie(name, defaultValue) { function readCookie(name, defaultValue) {
var cookies = document.cookie.split('; '); var cookies = document.cookie.split('; ');
@ -1355,17 +1344,14 @@ body {
return []; return [];
}; };
module.getCardData = function(issueKey, callback) { module.getCardData = function(issueKey) {
module.getIssueData(issueKey, function(data) { var promises = [];
var issueData = {}; var issueData = {};
promises.push(module.getIssueData(issueKey).then(function(data) {
issueData.key = data.key; issueData.key = data.key;
issueData.type = data.fields.issuetype.name.toLowerCase(); issueData.type = data.fields.issuetype.name.toLowerCase();
issueData.summary = data.fields.summary; issueData.summary = data.fields.summary;
issueData.description = data.renderedFields.description; issueData.description = data.renderedFields.description;
if (data.fields.assignee) { if (data.fields.assignee) {
@ -1381,14 +1367,13 @@ body {
} }
issueData.hasAttachment = data.fields.attachment.length > 0; issueData.hasAttachment = data.fields.attachment.length > 0;
issueData.storyPoints = data.fields.storyPoints; issueData.storyPoints = data.fields.storyPoints;
issueData.epicKey = data.fields.epicLink; issueData.epicKey = data.fields.epicLink;
if (issueData.epicKey) { if (issueData.epicKey) {
jiraFunctions.getIssueData(issueData.epicKey, function(data) { promises.push(module.getIssueData(issueData.epicKey).then(function(data) {
issueData.epicName = data.fields.epicName; issueData.epicName = data.fields.epicName;
}, false); }));
} }
issueData.url = window.location.origin + "/browse/" + issueData.key; issueData.url = window.location.origin + "/browse/" + issueData.key;
@ -1400,24 +1385,21 @@ body {
issueData.dueDate = new Date(data.fields.desiredDate).format('D d.m.'); issueData.dueDate = new Date(data.fields.desiredDate).format('D d.m.');
} }
} }
}));
callback(issueData); return new Promise(function(resolve, reject) {
Promise.all(promises)
.then(function(){resolve(issueData);})
.catch(function(cause){reject(cause);});
}); });
}; };
module.getIssueData = function(issueKey, callback, async) { module.getIssueData = function(issueKey) {
async = typeof async !== 'undefined' ? async : true;
//https://docs.atlassian.com/jira/REST/latest/ //https://docs.atlassian.com/jira/REST/latest/
var url = '/rest/api/2/issue/' + issueKey + '?expand=renderedFields,names'; var url = '/rest/api/2/issue/' + issueKey + '?expand=renderedFields,names';
console.log("IssueUrl: " + url); console.log("IssueUrl: " + url);
//console.log("Issue: " + issueKey + " Loading..."); //console.log("Issue: " + issueKey + " Loading...");
jQuery.ajax({ return jQuery.getJSON(url).then(function(responseData) {
type: 'GET',
url: url,
data: {},
dataType: 'json',
async: async,
success: function(responseData) {
//console.log("Issue: " + issueKey + " Loaded!"); //console.log("Issue: " + issueKey + " Loaded!");
// add custom fields with field names // add custom fields with field names
jQuery.each(responseData.names, function(key, value) { jQuery.each(responseData.names, function(key, value) {
@ -1427,8 +1409,6 @@ body {
responseData.fields[fieldName] = responseData.fields[key]; responseData.fields[fieldName] = responseData.fields[key];
} }
}); });
callback(responseData);
},
}); });
}; };
@ -1453,17 +1433,14 @@ body {
return []; return [];
}; };
module.getCardData = function(issueKey, callback) { module.getCardData = function(issueKey) {
module.getIssueData(issueKey, function(data) { var promises = [];
var issueData = {}; var issueData = {};
promises.push(module.getIssueData(issueKey, function(data) {
issueData.key = data.id; issueData.key = data.id;
issueData.type = data.field.type[0]; issueData.type = data.field.type[0];
issueData.summary = data.field.summary; issueData.summary = data.field.summary;
issueData.description = data.field.description; issueData.description = data.field.description;
if (data.field.assignee) { if (data.field.assignee) {
@ -1492,34 +1469,27 @@ body {
// } // }
// //
issueData.url = window.location.origin + "/youtrack/issue/" + issueData.key; issueData.url = window.location.origin + "/youtrack/issue/" + issueData.key;
}));
callback(issueData); return new Promise(function(resolve, reject) {
Promise.all(promises)
.then(function(){resolve(issueData);})
.catch(function(cause){reject(cause);});
}); });
}; };
module.getIssueData = function(issueKey, callback, async) { module.getIssueData = function(issueKey) {
async = typeof async !== 'undefined' ? async : true;
//https://docs.atlassian.com/jira/REST/latest/
var url = '/youtrack/rest/issue/' + issueKey + '?'; var url = '/youtrack/rest/issue/' + issueKey + '?';
console.log("IssueUrl: " + url); console.log("IssueUrl: " + url);
//console.log("Issue: " + issueKey + " Loading..."); //console.log("Issue: " + issueKey + " Loading...");
jQuery.ajax({ return jQuery.getJSON(url).then(function(responseData) {
type: 'GET',
url: url,
data: {},
dataType: 'json',
async: async,
success: function(responseData) {
//console.log("Issue: " + issueKey + " Loaded!"); //console.log("Issue: " + issueKey + " Loaded!");
jQuery.each(responseData.field, function(key, value) { jQuery.each(responseData.field, function(key, value) {
// add fields with field names // add fields with field names
var fieldName = value.name.toCamelCase(); var fieldName = value.name.toCamelCase();
//console.log("add new field: " + newFieldId + " with value from " + fieldName); //console.log("add new field: " + newFieldId + " with value from " + fieldName);
responseData.field[fieldName] = value.value; responseData.field[fieldName] = value.value;
}); });
callback(responseData);
},
}); });
}; };
@ -1544,17 +1514,14 @@ body {
return []; return [];
}; };
module.getCardData = function(issueKey, callback) { module.getCardData = function(issueKey) {
module.getIssueData(issueKey, function(data) { var promises = [];
var issueData = {}; var issueData = {};
promises.push(module.getIssueData(issueKey, function(data) {
issueData.key = data.id; issueData.key = data.id;
issueData.type = data.kind.toLowerCase(); issueData.type = data.kind.toLowerCase();
issueData.summary = data.name; issueData.summary = data.name;
issueData.description = data.description; issueData.description = data.description;
if (data.owned_by && data.owned_by.length > 0) { if (data.owned_by && data.owned_by.length > 0) {
@ -1567,7 +1534,6 @@ body {
// TODO // TODO
issueData.hasAttachment = false; issueData.hasAttachment = false;
issueData.storyPoints = data.estimate; issueData.storyPoints = data.estimate;
// TODO // TODO
@ -1579,28 +1545,21 @@ body {
// } // }
issueData.url = data.url; issueData.url = data.url;
}));
callback(issueData); return new Promise(function(resolve, reject) {
Promise.all(promises)
.then(function(){resolve(issueData);})
.catch(function(cause){reject(cause);});
}); });
}; };
module.getIssueData = function(issueKey, callback, async) { module.getIssueData = function(issueKey, callback, async) {
async = typeof async !== 'undefined' ? async : true;
//http://www.pivotaltracker.com/help/api //http://www.pivotaltracker.com/help/api
var url = 'https://www.pivotaltracker.com/services/v5/stories/' + issueKey + "?fields=name,kind,description,story_type,owned_by(name),comments(file_attachments(kind)),estimate,deadline"; var url = 'https://www.pivotaltracker.com/services/v5/stories/' + issueKey + "?fields=name,kind,description,story_type,owned_by(name),comments(file_attachments(kind)),estimate,deadline";
console.log("IssueUrl: " + url); console.log("IssueUrl: " + url);
//console.log("Issue: " + issueKey + " Loading..."); //console.log("Issue: " + issueKey + " Loading...");
jQuery.ajax({ return jQuery.getJSON(url);
type: 'GET',
url: url,
data: {},
dataType: 'json',
async: async,
success: function(responseData) {
//console.log("Issue: " + issueKey + " Loaded!");
callback(responseData);
},
});
}; };
return module; return module;
@ -1618,17 +1577,16 @@ body {
}; };
module.getCardData = function(issueKey, callback) { module.getCardData = function(issueKey, callback) {
module.getIssueData(issueKey, function(data) { var promises = [];
var issueData = {}; var issueData = {};
promises.push(module.getIssueData(issueKey, function(data) {
issueData.key = data.idShort; issueData.key = data.idShort;
// TODO get kind from label name // TODO get kind from label name
// issueData.type = data.kind.toLowerCase(); // issueData.type = data.kind.toLowerCase();
issueData.summary = data.name; issueData.summary = data.name;
issueData.description = data.desc; issueData.description = data.desc;
if (data.members && data.members.length > 0) { if (data.members && data.members.length > 0) {
@ -1641,30 +1599,21 @@ body {
} }
issueData.hasAttachment = data.attachments > 0; issueData.hasAttachment = data.attachments > 0;
issueData.url = data.shortUrl; issueData.url = data.shortUrl;
}));
callback(issueData); return new Promise(function(resolve, reject) {
Promise.all(promises)
.then(function(){resolve(issueData);})
.catch(function(cause){reject(cause);});
}); });
}; };
module.getIssueData = function(issueKey, callback, async) { module.getIssueData = function(issueKey, callback, async) {
async = typeof async !== 'undefined' ? async : true;
//http://www.pivotaltracker.com/help/api
var url = "https://trello.com/1/cards/" + issueKey + "?members=true"; var url = "https://trello.com/1/cards/" + issueKey + "?members=true";
console.log("IssueUrl: " + url); console.log("IssueUrl: " + url);
//console.log("Issue: " + issueKey + " Loading..."); //console.log("Issue: " + issueKey + " Loading...");
jQuery.ajax({ return jQuery.getJSON(url);
type: 'GET',
url: url,
data: {},
dataType: 'json',
async: async,
success: function(responseData) {
//console.log("Issue: " + issueKey + " Loaded!");
callback(responseData);
},
});
}; };
return module; return module;