All files / ee/app/assets/javascripts/oncall_schedules/mixins common_mixin.js

100% Statements 20/20
85.71% Branches 6/7
100% Functions 7/7
100% Lines 20/20

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                        18x 18x       126x             18x           7x       35x 35x   35x               19x 3x     16x     3x 3x 3x   3x         16x 16x 16x 16x   16x            
import { getDayDifference, isToday } from '~/lib/utils/datetime_utility';
import {
  PRESET_TYPES,
  oneHourOffsetDayView,
  oneDayOffsetWeekView,
  oneHourOffsetWeekView,
} from '../constants';
 
export default {
  currentDate: null,
  computed: {
    hasToday() {
      const timeframeItem = new Date(this.timeframeItem.getTime());
      const headerSubItems = new Array(7)
        .fill()
        .map(
          (val, i) =>
            new Date(
              timeframeItem.getFullYear(),
              timeframeItem.getMonth(),
              timeframeItem.getDate() + i,
            ),
        );
 
      return (
        this.$options.currentDate.getTime() >= headerSubItems[0].getTime() &&
        this.$options.currentDate.getTime() <= headerSubItems[headerSubItems.length - 1].getTime()
      );
    },
    isToday() {
      return isToday(this.timeframeItem);
    },
  },
  beforeCreate() {
    const currentDate = new Date();
    currentDate.setHours(0, 0, 0, 0);
 
    this.$options.currentDate = currentDate;
  },
  methods: {
    getIndicatorStyles(
      presetType = PRESET_TYPES.WEEKS,
      timeframeStartDate = new Date(),
      timelineWidth = 1,
    ) {
      if (presetType === PRESET_TYPES.DAYS) {
        return this.getDayViewIndicatorStyles();
      }
 
      return this.getWeekViewIndicatorStyles(timeframeStartDate, timelineWidth);
    },
    getDayViewIndicatorStyles() {
      const currentDate = new Date();
      const hours = oneHourOffsetDayView * currentDate.getHours();
      const minutes = oneHourOffsetDayView * (currentDate.getMinutes() / 60);
 
      return {
        left: `${hours + minutes}%`,
      };
    },
    getWeekViewIndicatorStyles(timeframeStartDate, timelineWidth) {
      const currentDate = new Date();
      const hourOffset = oneHourOffsetWeekView * currentDate.getHours();
      const daysSinceShiftStart = getDayDifference(timeframeStartDate, currentDate);
      const leftOffset = oneDayOffsetWeekView * daysSinceShiftStart + hourOffset;
 
      return {
        left: `${leftOffset / timelineWidth}%`,
      };
    },
  },
};