dvanbell / Create subtask

// ==UserScript==
// @name         Create subtask
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Automate some tedious tasks in JIRA when creating a subtask
// @license MIT
// @author       Denzel Van Belle, 2019 (https://openuserjs.org/users/dvanbell
// @match        https://jira.rel.apps.telenet.be/*
// @grant        GM_addStyle
// @require      http://code.jquery.com/jquery-latest.js
// ==/UserScript==

(function () {
  'use strict';

  /**
    Fill in some default fields with only 1 click.
  **/

  GM_addStyle(`
  .Tampered_span:hover { opacity: 0.7; } 
  .Tampered_span_blue { background-color: #2A67A5 } 
  .Tampered_span_green { background-color: #4CAF50 }
`);

  var checkExistInterval;

  setListenerInterval()

  function setListenerInterval() {
    checkExistInterval = setInterval(() => {
      const fieldToTamper = document.getElementsByClassName('qf-field-issuetype')[0];
      if (fieldToTamper) {
        createButton(fieldToTamper, 'development');
        createButton(fieldToTamper, 'Biz+Dev');
        createButton(fieldToTamper, 'BUSINESS');
        fill_summary();
        clearInterval(checkExistInterval);
        addCloseListeners();
      }
    }, 1000);
  }

  function addCloseListeners() {
    const cancelBtn = document.querySelectorAll('#create-issue-submit ~ .cancel')[0];
    const submitBtn = document.getElementById('create-issue-submit');

    cancelBtn.onclick = setListenerInterval;
    submitBtn.onclick = setListenerInterval;
  }

  function fill_summary() {
    document.getElementById('summary').addEventListener('input', function (evt) {
      const summary_field = document.getElementById('summary');
      switch (summary_field.value) {
        case 'dev  ':
          summary_field.value = '[DEV] ';
          break;
        case 'test  ':
          summary_field.value = '[TEST] ';
          break;
        case 'val  ':
          summary_field.value = '[VAL] ';
          break;
        case 'doc  ':
          summary_field.value = '[DOC] ';
          break;
        case 'ana  ':
          summary_field.value = '[ANA] ';
          break;
      }
    });
  }

  function createButton(element, name) {
    const span = document.createElement('span');
    if (name === 'development') {
      span.innerText = "</development>";
      span.className = "Tampered_span Tampered_span_blue";
      span.name = 'development';
    }
    else {
      span.innerText = name;
      span.className = "Tampered_span Tampered_span_green";
      span.name = name;
    }
    span.style = `
      margin-left: 63px; margin-top: 10px; line-height: 3em; color: white; padding: 10px; border-radius: 10px; cursor: pointer;`;
    span.onclick = fill_fields;
    element.append(span);
  }

  function fill_fields(e) {
    fill_labels(e.target.name);
    fill_assignedTeam();
    check_createAnother();
  }

  function fill_labels(text) {
    // Add the label in front-end for UX (this label does not affect the subtask)
    const representation_lbl = document.querySelectorAll('#labels-multi-select .representation .items')[0];
    representation_lbl.innerHTML = `<li class=\"item-row\" role=\"option\" aria-describedby=\"label-0\" id=\"item-row-2\"><button type=\"button\" tabindex=\"-1\" class=\"value-item\"><span><span class=\"value-text\">${text}</span></span></button><em class=\"item-delete\" aria-label=\" \" original-title=\"\"></em></li>`;

    // for the actual value to be picked up, add option to hidden select
    const hidden_labels_select = document.getElementById('labels');
    hidden_labels_select.innerHTML = `<option value="development" title="development" selected="selected">${text}</option>`;

    trigger_enter()
  }

  function fill_assignedTeam() {
    const assignedTeam_parent = document.getElementById('customfield_14800');
    assignedTeam_parent.value = '14200';

    trigger_onchange(assignedTeam_parent);

    const assignedTeam_child = document.getElementById('customfield_14800:1');
    assignedTeam_child.value = '14205';

  }

  function check_createAnother() {
    const createAnother = document.getElementById('qf-create-another');
    createAnother.checked = "checked";
  }

  function trigger_onchange(el) {
    const evt = document.createEvent("HTMLEvents");
    evt.initEvent("change", false, true);
    el.dispatchEvent(evt);
  }

  function trigger_enter() {
    const e = $.Event("keydown");
    e.which = 13;
    e.keyCode = 13;
    $(document).trigger(e);
  }
})();