All files / ee/app/assets/javascripts/epic/utils epic_utils.js

82.86% Statements 29/35
52% Branches 13/25
85.71% Functions 6/7
81.82% Lines 27/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                        9x             9x       9x 17x     9x 5x   5x 1x       15x   9x   9x     4x 4x         9x                 1x       1x   1x 1x 1x 1x   1x 1x 1x 1x         1x             1x                         9x                        
import $ from 'jquery';
import Cookies from 'js-cookie';
 
import { sanitize } from '~/lib/dompurify';
import createGqClient, { fetchPolicies } from '~/lib/graphql';
 
import { parseBoolean } from '~/lib/utils/common_utils';
import { dateInWords, parsePikadayDate } from '~/lib/utils/datetime_utility';
import { __, s__, sprintf } from '~/locale';
 
import { dateTypes } from '../constants';
 
const gqClient = createGqClient(
  {},
  {
    fetchPolicy: fetchPolicies.NO_CACHE,
  },
);
 
const triggerDocumentEvent = (eventName, eventParam) => {
  $(document).trigger(eventName, eventParam);
};
 
const bindDocumentEvent = (eventName, callback) => {
  $(document).on(eventName, callback);
};
 
const toggleContainerClass = (className) => {
  const containerEl = document.querySelector('.page-with-contextual-sidebar');
 
  if (containerEl) {
    containerEl.classList.toggle(className);
  }
};
 
const getCollapsedGutter = () => parseBoolean(Cookies.get('collapsed_gutter'));
 
const setCollapsedGutter = (value) => Cookies.set('collapsed_gutter', value);
 
const getDateValidity = (startDateTime, dueDateTime) => {
  // If both dates are defined
  // only then compare, return true otherwise
  Eif (startDateTime && dueDateTime) {
    return startDateTime.getTime() < dueDateTime.getTime();
  }
  return true;
};
 
const getDateFromMilestonesTooltip = ({
  dateType = dateTypes.start,
  startDateSourcingMilestoneTitle,
  startDateSourcingMilestoneDates,
  startDateTimeFromMilestones,
  dueDateSourcingMilestoneTitle,
  dueDateSourcingMilestoneDates,
  dueDateTimeFromMilestones,
}) => {
  const dateSourcingMilestoneTitle = sanitize(
    dateType === dateTypes.start ? startDateSourcingMilestoneTitle : dueDateSourcingMilestoneTitle,
  );
  const sourcingMilestoneDates =
    dateType === dateTypes.start ? startDateSourcingMilestoneDates : dueDateSourcingMilestoneDates;
 
  Eif (startDateTimeFromMilestones && dueDateTimeFromMilestones) {
    const { startDate, dueDate } = sourcingMilestoneDates;
    let startDateInWords = __('No start date');
    let dueDateInWords = __('No due date');
 
    Eif (startDate && dueDate) {
      const startDateObj = parsePikadayDate(startDate);
      const dueDateObj = parsePikadayDate(dueDate);
      startDateInWords = dateInWords(
        startDateObj,
        true,
        startDateObj.getFullYear() === dueDateObj.getFullYear(),
      );
      dueDateInWords = dateInWords(dueDateObj, true);
    } else if (startDate && !dueDate) {
      startDateInWords = dateInWords(parsePikadayDate(startDate), true);
    } else {
      dueDateInWords = dateInWords(parsePikadayDate(dueDate), true);
    }
 
    return `${dateSourcingMilestoneTitle}<br/><span class="text-tertiary">${startDateInWords} – ${dueDateInWords}</span>`;
  }
 
  return sprintf(
    s__(
      "Epics|To schedule your epic's %{epicDateType} date based on milestones, assign a milestone with a %{epicDateType} date to any issue in the epic.",
    ),
    {
      epicDateType: dateTypes.start === dateType ? s__('Epics|start') : s__('Epics|due'),
    },
  );
};
 
const epicUtils = {
  gqClient,
  triggerDocumentEvent,
  bindDocumentEvent,
  toggleContainerClass,
  getCollapsedGutter,
  setCollapsedGutter,
  getDateValidity,
  getDateFromMilestonesTooltip,
};
 
export default epicUtils;