Skip to content

Commit

Permalink
feat(jira-default-wl-type): be more careful with setting default wlType
Browse files Browse the repository at this point in the history
Also introduce more meaningful code ordering, plus some more cleanup.
  • Loading branch information
pbodnar committed May 11, 2023
1 parent 88dd8a4 commit 8c42c2b
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions JIRA_def-worklog-type.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// @namespace pbo
// @description Changes default Worklog Type in JIRA with the Worklog express add-on.
// @match https://your.jira.example.com/browse/*
// @version 1.1.0
// @version 1.1.1
// @grant GM_registerMenuCommand
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
// ==/UserScript==

(function($) {
(function ($) {

/* Great users of this script can safely change values of the following constants: */

Expand All @@ -19,8 +19,10 @@
* 2. Find the <select> element with id "wl-type" in your browser's HTML inspector.
* 3. Inside that element, find the <option> element which you prefer as the default choice.
* 4. Copy the "value" attribute of that element here.
*
* Defaults to null.
*/
var DEFAULT_WORKLOG_TYPE = 1;
var DEFAULT_WORKLOG_TYPE = null;

/**
* Whether to remember which type was lastly selected for a given issue
Expand Down Expand Up @@ -63,26 +65,45 @@ try {
var PROCESSED_FLAG = 'def-wltype-processed';

function setDefaultWorklogType() {
var targets = $('#wl-type').filter(':not([' + PROCESSED_FLAG + '])');

var targets = $('#wl-type').filter(`:not([${PROCESSED_FLAG}])`);

// note: there should be always 1 item (select field)
for (var i = 0; i < targets.length; i++) {
// note: there should be always 1 item (select field)
var wlType = REUSE_ISSUE_LAST_WORKLOG_TYPE ? getIssueWorklogType() : DEFAULT_WORKLOG_TYPE;
trace('Setting default worklog type ' + wlType + ' to item #' + (i + 1));
var target = targets[i];
$(target).attr(PROCESSED_FLAG, true);

if (REUSE_ISSUE_LAST_WORKLOG_TYPE) {
unsafeWindow.$(target).on("change", storeIssueWorklogType);
unsafeWindow.$(target).on('change', storeIssueWorklogType);
}

var wlType = REUSE_ISSUE_LAST_WORKLOG_TYPE ? getIssueWorklogType() : DEFAULT_WORKLOG_TYPE;

// sanity checks
if ($(target).val() == wlType) {
trace('Default worklog type does not differ from actual value - do nothing');
continue;
}
if (!wlType && !DEFAULT_WORKLOG_TYPE) {
trace('No predefined nor remembered default worklog type - do nothing');
continue;
}
if ($(target).children().toArray().every(option => $(option).val() != wlType)) { // eslint-disable-line no-loop-func
console.warn(`DEFAULT_WORKLOG_TYPE and/or remembered value of [${wlType}] is not present among possible options - do nothing`);
continue;
}

trace(`Setting default worklog type ${wlType} to item #${i + 1}`);
$(target).val(wlType);
// note: if we call trigger() on this script's jQuery, page's jQuery won't catch the event
unsafeWindow.$(target).trigger('change');
}
}

setInterval(setDefaultWorklogType, 500);
if (!DEFAULT_WORKLOG_TYPE && !REUSE_ISSUE_LAST_WORKLOG_TYPE) {
trace('None of the script options are enabled - do nothing');
} else {
setInterval(setDefaultWorklogType, 500);
}

trace('started');

Expand All @@ -93,7 +114,7 @@ function getIssueWorklogType() {

var wlType = window.localStorage.getItem(WL_TYPE_KEY_PREFIX + issueKey);
if (wlType) {
trace('Using worklog type for issue from local storage: ' + wlType);
trace(`Using worklog type for issue from local storage: ${wlType}`);
} else {
wlType = DEFAULT_WORKLOG_TYPE;
}
Expand All @@ -105,7 +126,7 @@ function storeIssueWorklogType(event) {
var issueKey = $('#key-val').text();

var wlType = $(event.target).val();
trace('Storing worklog type for issue in local storage: ' + wlType);
trace(`Storing worklog type for issue in local storage: ${wlType}`);
window.localStorage.setItem(WL_TYPE_KEY_PREFIX + issueKey, wlType);
}

Expand Down

0 comments on commit 8c42c2b

Please sign in to comment.