Snippets

Mythics JIRA - Log Quick Work

Created by Jonathan Hult last modified
// Only on Browse Issue page or Issues View page
if (/^(\/browse\/|\/projects\/(.*)\/issues\/)/i.test(location.pathname)) {
    
    var LogQuickWork = LogQuickWork || {};
    
    // Main function
    LogQuickWork.init = function() {
      
        // Wait until page is loaded
        AJS.toInit(function() {
        
            // Only do if issue status is not Closed
            if (AJS.$(":first-child", "#status-val").text() == "Closed") {
                // Remove standard JIRA Log Work links/buttons 
                AJS.$(".issueaction-log-work").remove();
            } else {
                // Find atl_token in page
                LogQuickWork.token = LogQuickWork.token || AJS.$('#atlassian-token').attr('content');

                // Get Issue ID
                LogQuickWork.issueID = LogQuickWork.issueID || JIRA.Issue.getIssueId();

                // Add buttons
                LogQuickWork.addButtons();

                // Add click handler
                AJS.$('.log-quick-work').click(LogQuickWork.clickHandler);
            }
        });
    };
    
    // Fire on initial page load
    LogQuickWork.init();
    
    // On refresh
    JIRA.bind(JIRA.Events.ISSUE_REFRESHED, LogQuickWork.init);

    // Fire on page refresh
    LogQuickWork.addButtons = function() {
        var opsBar = AJS.$('#opsbar-opsbar-operations');

        var timesToLog = ['15m', '30m', '45m', '1h'];

        var timesToLogLength = timesToLog.length;
        
        for (var i = 0; i < timesToLogLength; i++) {
            var time = timesToLog[i];

            var a = document.createElement('a');
            AJS.$(a).attr('class', 'aui-button toolbar-trigger log-quick-work');
            AJS.$(a).attr('id', 'log-work-' + time);
            AJS.$(a).attr('title', 'Log Quick Work -  ' + time);
            AJS.$(a).attr('time-to-log', time);

            var span = document.createElement('span');
            AJS.$(span).attr('class', 'trigger-label');

            var label = document.createTextNode('+' + time);
            AJS.$(opsBar).append(AJS.$(a).append(AJS.$(span).append(label)));
        }
    };
    
    // Click handler
    LogQuickWork.clickHandler = function() {
        AJS.$.ajax({
            url: '/secure/CreateWorklog.jspa',
            type: 'POST',
            dataType: 'html',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            data: {
                inline: true,
                decorator: 'dialog',
                atl_token: LogQuickWork.token,
                id: LogQuickWork.issueID,
                timeLogged: $(this).attr('time-to-log'),
                // 11/Jul/16 1:16 PM
                startDate: moment().format('Do/MMM/YY h:mm a'),
                adjustEstimate: 'auto'
            },
            success: function() {
                JIRA.trigger(JIRA.Events.REFRESH_ISSUE_PAGE, [LogQuickWork.issueID]);
            },
            error: function() {
                alert('Failed to log work');
            }
        });
    };
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.