(function() { var version = "3.5.0"; console.log("Version: " + version); try { var global = {}; // load jQuery if (window.jQuery === undefined) { appendScript('//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'); } global.isDev = /.*jira.atlassian.com\/secure\/RapidBoard.jspa\?.*projectKey=ANERDS.*/g.test(document.URL) // Jira || /.*pivotaltracker.com\/n\/projects\/510733.*/g.test(document.URL) // PivotTracker || ( /.*trello.com\/.*/g.test(document.URL) && jQuery("span.js-member-name").text() =='Bengt Brodersen'); // Trello global.isProd = !global.isDev; // wait untill all scripts loaded appendScript('https://qoomon.github.io/void', function() { try { main(); } catch (err) { handleError(err); } }); } catch (err) { handleError(err); } function handleError(err){ console.log("ERROR: " + err.stack); if (global.isProd) { ga('send', 'exception', { 'exDescription': err.message, 'exFatal': true }); } } function main() { init(); // determine application if (jQuery("meta[name='application-name'][ content='JIRA']").length > 0) { console.log("App: " + "Jira"); global.appFunctions = jiraFunctions; } else if (/.*pivotaltracker.com\/.*/g.test(document.URL)) { console.log("App: " + "PivotalTracker"); global.appFunctions = pivotalTrackerFunctions; } else if (/.*trello.com\/.*/g.test(document.URL)) { console.log("App: " + "Trello"); global.appFunctions = trelloFunctions; } else { alert("Unsupported app.Please create an issue at https://github.com/qoomon/Jira-Issue-Card-Printer"); return; } //preconditions if (jQuery("#card-print-overlay").length > 0) { alert("Print Card already opened!"); return; } // collect selcted issues var issueKeyList = global.appFunctions.getSelectedIssueKeyList(); if (issueKeyList.length <= 0) { alert("Please select at least one issue."); return; } // open print preview jQuery("body").append(printOverlayHTML); jQuery("#card-print-overlay").prepend(printOverlayStyle); jQuery("#rowCount").val(readCookie("card_printer_row_count",2)); jQuery("#columnCount").val(readCookie("card_printer_column_count",1)); jQuery("#card-scale-range").val(readCookie("card_printer_card_scale",1)); jQuery("#single-card-page-checkbox").attr('checked',readCookie("card_printer_single_card_page",false) != 'false'); jQuery("#hide-description-checkbox").attr('checked',readCookie("card_printer_hide_description",false) != 'false'); jQuery("#card-print-dialog-title").text("Card Print - Loading " + issueKeyList.length + " issues..."); renderCards(issueKeyList, function() { jQuery("#card-print-dialog-title").text("Card Print"); //print(); }); if (global.isProd) { ga('send', 'pageview'); } } function init() { addStringFunctions(); addDateFunctions(); global.hostOrigin = "https://qoomon.github.io/Jira-Issue-Card-Printer/"; if (global.isDev) { console.log("DEVELOPMENT"); global.hostOrigin = "https://rawgit.com/qoomon/Jira-Issue-Card-Printer/develop/"; } global.resourceOrigin = global.hostOrigin + "resources/"; if (global.isProd){ initGoogleAnalytics(); } } function print() { try { var rowCount = jQuery("#rowCount").val(); var columnCount = jQuery("#columnCount").val(); var scale = jQuery("#card-scale-range").val(); var singleCard = jQuery("#single-card-page-checkbox").is(':checked'); var hideDescription = jQuery("#hide-description-checkbox").is(':checked'); var printFrame = jQuery("#card-print-dialog-content-iframe"); var printWindow = printFrame[0].contentWindow; var printDocument = printWindow.document; if (global.isProd) { ga('send', 'event', 'button', 'click', 'print', jQuery(".card", printDocument).length); } var currentScale = jQuery("html", printDocument).css("font-size").replace("px", ""); printWindow.matchMedia("print").addListener(function() { var pageWidth = jQuery("body", printDocument).outerWidth(); var cardWidth = jQuery(".card", printDocument).outerWidth(); var newScale = currentScale * pageWidth / cardWidth; //jQuery("html", printDocument).css("font-size",newScale +"px"); }); ///////////////////////////////////////// printWindow.addEventListener("resize", refreshCard); printWindow.matchMedia("print").addListener(refreshCard); function refreshCard() { var cardElements = printDocument.querySelectorAll(".card"); forEach(cardElements, function (cardElement) { var cardContent = cardElement.querySelectorAll(".card-body")[0]; if (cardContent.scrollHeight > cardContent.offsetHeight) { cardContent.classList.add("zigzag"); } else { cardContent.classList.remove("zigzag"); } }); } function forEach(array, callback) { for (i = 0; i < array.length; i++) { callback(array[i]); } } ///////////////////////////////////////// printWindow.print(); jQuery("html", printDocument).css("font-size",currentScale +"px"); } catch (err) { handleError(err); } } function renderCards(issueKeyList, callback) { var printFrame = jQuery("#card-print-dialog-content-iframe"); var printWindow = printFrame[0].contentWindow; var printDocument = printWindow.document; printDocument.open(); printDocument.write("
/, ""); if (data.fields.assignee) { issueData.assignee = data.fields.assignee.displayName; var avatarUrl = data.fields.assignee.avatarUrls['48x48']; if (avatarUrl.indexOf("ownerId=") >= 0) { issueData.avatarUrl = avatarUrl; } } if (data.fields.duedate) { issueData.dueDate = new Date(data.fields.duedate).format('D d.m.'); } issueData.hasAttachment = data.fields.attachment.length > 0; issueData.storyPoints = data.fields.storyPoints; issueData.epicKey = data.fields.epicLink; if (issueData.epicKey) { jiraFunctions.getIssueData(issueData.epicKey, function(data) { issueData.epicName = data.fields.epicName; }, false); } issueData.url = window.location.origin + "/browse/" + issueData.key; //LRS Specific field mapping if (true) { //Desired-Date if (data.fields.desiredDate) { issueData.dueDate = new Date(data.fields.desiredDate).format('D d.m.'); } } callback(issueData); }); }; module.getIssueData = function(issueKey, callback, async) { async = typeof async !== 'undefined' ? async : true; //https://docs.atlassian.com/jira/REST/latest/ var url = '/rest/api/2/issue/' + issueKey + '?expand=renderedFields,names'; console.log("IssueUrl: " + url); //console.log("Issue: " + issueKey + " Loading..."); jQuery.ajax({ type: 'GET', url: url, data: {}, dataType: 'json', async: async, success: function(responseData) { //console.log("Issue: " + issueKey + " Loaded!"); // add custom fields with field names jQuery.each(responseData.names, function(key, value) { if (key.startsWith("customfield_")) { var newFieldId = value.toCamelCase(); //console.log("add new field: " + newFieldId + " with value from " + key); responseData.fields[value.toCamelCase()] = responseData.fields[key]; } }); callback(responseData); }, }); }; return module; }({})); var pivotalTrackerFunctions = (function (module) { module.getSelectedIssueKeyList = function() { //Single Story if (/.*\/stories\/.*/g.test(document.URL)) { return [document.URL.replace(/.*\/stories\/([^?]*).*/, '$1')]; // TODO } // Board if (/.*\/projects\/.*/g.test(document.URL)) { return jQuery('.story[data-id]:has(.selected)').map(function() { return jQuery(this).attr('data-id'); }); } return []; }; module.getCardData = function(issueKey, callback) { module.getIssueData(issueKey, function(data) { var issueData = {}; issueData.key = data.id; issueData.type = data.kind.toLowerCase(); issueData.summary = data.name; issueData.description = data.description; if (data.owned_by && data.owned_by.length > 0) { issueData.assignee = data.owner_ids[0].name; } if (data.deadline) { issueData.dueDate = new Date(data.deadline).format('D d.m.'); } // TODO issueData.hasAttachment = false; issueData.storyPoints = data.estimate; // TODO // issueData.epicKey = data.fields.epicLink; // if ( issueData.epicKey ) { // getIssueDataPivotalTracker(issueData.epicKey , function(data) { // issueData.epicName = data.fields.epicName; // }, false); // } issueData.url = data.url; callback(issueData); }); }; module.getIssueData = function(issueKey, callback, async) { async = typeof async !== 'undefined' ? async : true; //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"; console.log("IssueUrl: " + url); //console.log("Issue: " + issueKey + " Loading..."); jQuery.ajax({ type: 'GET', url: url, data: {}, dataType: 'json', async: async, success: function(responseData) { //console.log("Issue: " + issueKey + " Loaded!"); callback(responseData); }, }); }; return module; }({})); var trelloFunctions = (function (module) { module.getSelectedIssueKeyList = function() { //Card View if (/.*\/c\/.*/g.test(document.URL)) { return [document.URL.replace(/.*\/c\/([^/]*).*/g, '$1')]; } return []; }; module.getCardData = function(issueKey, callback) { module.getIssueData(issueKey, function(data) { var issueData = {}; issueData.key = data.idShort; // TODO get kind from label name // issueData.type = data.kind.toLowerCase(); issueData.summary = data.name; issueData.description = data.desc; if (data.members && data.members.length > 0) { issueData.assignee = data.members[0].fullName; issueData.avatarUrl = "https://trello-avatars.s3.amazonaws.com/"+data.members[0].avatarHash+"/170.png"; } if (data.due) { issueData.dueDate = new Date(data.due).format('D d.m.'); } issueData.hasAttachment = data.attachments > 0; issueData.url = data.shortUrl; callback(issueData); }); }; 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"; console.log("IssueUrl: " + url); //console.log("Issue: " + issueKey + " Loading..."); jQuery.ajax({ type: 'GET', url: url, data: {}, dataType: 'json', async: async, success: function(responseData) { //console.log("Issue: " + issueKey + " Loaded!"); callback(responseData); }, }); }; return module; }({})); })();