All files / src/components CalendarDay.vue

0% Statements 0/195
0% Branches 0/107
0% Functions 0/40
0% Lines 0/176
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
<template>
<popover
  align='center'
  transition='fade'
  class='c-day-popover'
  :content-offset='popoverContentOffset'
  :visibility='popoverVisibility'
  :content-style='popoverContentStyle'
  :is-interactive='popoverIsInteractive'
  toggle-visible-on-click>
  <div
    class='c-day'
    :style='dayCellStyle'>
    <!-- Background layers -->
    <transition-group
      name='background'
      tag='div'
      class='c-day-backgrounds'>
      <div
        v-for='background in backgrounds'
        :key='background.key'
        :class='background.wrapperClass'>
        <div
          class='c-day-background'
          :style='background.style'>
        </div>
      </div>
    </transition-group>
    <!-- Content layer -->
    <div
      class='c-day-layer c-day-box-center-center'>
      <div
        ref='dayContent'
        class='c-day-content'
        :style='contentStyle_'
        @click='click'
        @mouseenter='mouseenter'
        @mouseover='mouseover'
        @mouseleave='mouseleave'>
        {{ label }}
      </div>
    </div>
    <!-- Dots layer -->
    <div
      class='c-day-layer c-day-box-center-bottom'
      v-if='hasDots'>
      <div
        class='c-day-dots'
        :style='dotsStyle'>
        <span
          v-for='dot in dots'
          :key='dot.key'
          class='c-day-dot'
          :style='dot.style'>
        </span>
      </div>
    </div>
    <!-- Bars layer -->
    <div
      class='c-day-layer c-day-box-center-bottom'
      v-if='hasBars'>
      <div
        class='c-day-bars'
        :style='barsStyle'>
        <span
          v-for='bar in bars'
          :key='bar.key'
          class='c-day-bar'
          :style='bar.style'>
        </span>
      </div>
    </div>
  </div>
  <!-- Popover content -->
  <div
    class='c-day-popover-content'
    slot='popover-content'>
    <!-- Header slot -->
    <slot
      name='popover-header'
      :day-info='dayInfo'
      :attributes='attributesList'>
    </slot>
    <!-- Content row slots -->
    <calendar-day-popover-row
      v-for='popover in popovers'
      :key='popover.key'
      :attribute='popover.attribute'
      :hide-indicator='popover.hideIndicator'>
      <slot
        :name='popover.slot'
        :attribute='popover.attribute'
        :custom-data='popover.attribute.customData'
        :day-info='dayInfo'>
        <span
          class='popover-label'
          :style='popover.labelStyle'
          :key='popover.key'>
          {{ popover.label }}
        </span>
      </slot>
    </calendar-day-popover-row>
  </div>
</popover>
</template>
 
<script>
import Popover from './Popover';
import CalendarDayPopoverRow from './CalendarDayPopoverRow';
import { arrayHasItems, objectFromArray } from '../utils/helpers';
import { isFunction } from '../utils/typeCheckers';
 
export default {
  components: {
    Popover,
    CalendarDayPopoverRow,
  },
  props: {
    dayInfo: { type: Object, required: true },
    attributes: Object,
    popoverContentOffset: { type: String, default: '7px' },
    styles: Object,
  },
  data() {
    return {
      backgrounds: [],
      dots: [],
      bars: [],
      popovers: [],
      contentStyle: null,
      contentHoverStyle: null,
      popoverVisibility: 'hidden',
      popoverIsInteractive: false,
      isHovered: false,
    };
  },
  computed: {
    label() {
      return this.dayInfo.label;
    },
    dateTime() {
      return this.dayInfo.dateTime;
    },
    inMonth() {
      return this.dayInfo.inMonth;
    },
    dayCellStyle() {
      // Merge 'not in month' style if needed
      return this.inMonth ? this.styles.dayCell : {
        ...this.styles.dayCell,
        ...this.styles.dayCellNotInMonth,
      };
    },
    contentStyle_() {
      let style = this.contentStyle;
      if (this.isHovered) style = { ...style, ...this.contentHoverStyle };
      return style;
    },
    attributesList() {
      return this.attributes.find(this.dayInfo);
    },
    attributesMap() {
      return objectFromArray(this.attributesList);
    },
    hasBackgrounds() {
      return arrayHasItems(this.backgrounds);
    },
    hasDots() {
      return arrayHasItems(this.dots);
    },
    dotsStyle() {
      return this.styles.dots;
    },
    hasBars() {
      return arrayHasItems(this.bars);
    },
    barsStyle() {
      return this.styles.bars;
    },
    hasPopovers() {
      return !!arrayHasItems(this.popovers);
    },
    popoverContentStyle() {
      return this.styles.dayPopoverContent;
    },
    eventDayInfo() {
      return {
        ...this.dayInfo,
        el: this.$refs.dayContent,
      };
    },
  },
  watch: {
    attributes() {
      this.processAttributes();
    },
    styles() {
      this.processAttributes();
    },
    popovers(val) {
      let visibility = '';
      let isInteractive = false;
      let content;
      val.forEach((popover) => {
        if (!visibility && popover.visibility) visibility = popover.visibility;
        isInteractive = isInteractive || popover.isInteractive;
        content = content || popover.label || popover.slot;
      });
      this.popoverVisibility = visibility || (content && 'hover') || 'hidden';
      this.popoverIsInteractive = isInteractive;
    },
  },
  created() {
    this.processAttributes();
  },
  methods: {
    click() {
      this.$emit('dayselect', this.eventDayInfo, this.attributesMap);
    },
    mouseenter() {
      this.$emit('daymouseenter', this.eventDayInfo, this.attributesMap);
    },
    mouseover() {
      this.isHovered = true;
      this.$emit('daymouseover', this.eventDayInfo, this.attributesMap);
    },
    mouseleave() {
      this.isHovered = false;
      this.$emit('daymouseleave', this.eventDayInfo, this.attributesMap);
    },
    processAttributes() {
      const backgrounds = [];
      const dots = [];
      const bars = [];
      const popovers = [];
      const contentStyles = [];
      const contentHoverStyles = [];
      // Get the day attributes
      this
        .attributesList
        .forEach((attribute) => {
          const { highlight, highlightCaps, dot, bar, popover, contentStyle, contentStyleCaps, contentHoverStyle } = attribute;
          const { startTime, endTime } = attribute.targetDate;
          const onStart = startTime === this.dateTime;
          const onEnd = endTime === this.dateTime;
          // Add backgrounds for highlight if needed
          if (highlight && !(onStart && onEnd && highlightCaps)) backgrounds.push(this.getBackground(attribute));
          if (highlightCaps && (onStart || onEnd)) backgrounds.push(this.getBackgroundCap(attribute));
          // Add dot if needed
          if (dot) dots.push(this.getDot(attribute));
          // Add bar if needed
          if (bar) bars.push(this.getBar(attribute));
          // Add popover if needed
          if (popover) popovers.unshift(this.getPopover(attribute));
          // Add content style if needed
          if (contentStyle && !(onStart && onEnd && contentStyleCaps)) contentStyles.push(contentStyle);
          if (contentStyleCaps && (onStart || onEnd)) contentStyles.push(contentStyleCaps);
          // Add content hover style if needed
          if (contentHoverStyle) contentHoverStyles.push(contentHoverStyle);
        });
      // Assign day attributes
      this.backgrounds = backgrounds;
      this.dots = dots;
      this.bars = bars;
      this.popovers = popovers;
      this.contentStyle = Object.assign({}, this.styles.dayContent, ...contentStyles);
      this.contentHoverStyle = Object.assign({}, this.styles.dayContentHover, ...contentHoverStyles);
    },
    getBackground(attribute) {
      // Initialize the background object
      const { key, highlight, targetDate } = attribute;
      const { animated, width, height, backgroundColor, borderColor, borderWidth, borderStyle, borderRadius, opacity } = highlight;
      const background = {
        key,
        style: {
          width: width || height,
          height,
          backgroundColor,
          borderColor,
          borderWidth,
          borderStyle,
          borderRadius,
          opacity,
        },
      };
      if (targetDate.isDate) {
        background.wrapperClass = `c-day-layer c-day-box-center-center ${animated ? 'c-day-scale-enter c-day-scale-leave' : ''}`;
      } else {
        const onStart = targetDate.startTime === this.dateTime;
        const onEnd = targetDate.endTime === this.dateTime;
        const endLongWidth = '95%';
        const endShortWidth = '50%';
        // Is the day date on the highlight start and end date
        if (onStart && onEnd) {
          const animation = animated ? 'c-day-scale-enter c-day-scale-leave' : '';
          background.wrapperClass = `c-day-layer c-day-box-center-center ${animation}`;
          background.style.width = endLongWidth;
          background.style.borderWidth = borderWidth;
          background.style.borderRadius = `${borderRadius} ${borderRadius} ${borderRadius} ${borderRadius}`;
        // Is the day date on the highlight start date
        } else if (onStart) {
          const animation = animated ? 'c-day-slide-left-scale-enter' : '';
          background.wrapperClass = `c-day-layer c-day-box-right-center shift-right ${animation}`;
          if (attribute.highlightCaps) {
            background.style.width = endShortWidth;
            background.style.borderWidth = `${borderWidth} 0 ${borderWidth} 0`;
            background.style.borderRadius = 0;
          } else {
            background.style.width = endLongWidth;
            background.style.borderWidth = `${borderWidth} 0 ${borderWidth} ${borderWidth}`;
            background.style.borderRadius = `${borderRadius} 0 0 ${borderRadius}`;
          }
        // Is the day date on the highlight end date
        } else if (onEnd) {
          const animation = animated ? 'c-day-slide-right-scale-enter' : '';
          background.wrapperClass = `c-day-layer c-day-box-left-center shift-left ${animation}`;
          if (attribute.highlightCaps) {
            background.style.width = endShortWidth;
            background.style.borderWidth = `${borderWidth} 0 ${borderWidth} 0`;
            background.style.borderRadius = 0;
          } else {
            background.style.width = endLongWidth;
            background.style.borderWidth = `${borderWidth} ${borderWidth} ${borderWidth} 0`;
            background.style.borderRadius = `0 ${borderRadius} ${borderRadius} 0`;
          }
        // Is the day date between the highlight start/end dates
        } else {
          background.wrapperClass = 'c-day-layer c-day-box-center-center shift-left-right';
          background.style.width = '100%';
          background.style.borderWidth = `${borderWidth} 0`;
          background.style.borderRadius = '0';
        }
      }
      return background;
    },
    getBackgroundCap(attribute) {
      const backgroundExists = !!this.backgrounds.find(b => b.key === attribute.key);
      const { startTime, endTime } = attribute.targetDate;
      const { animated, width, height, backgroundColor, borderColor, borderWidth, borderStyle, borderRadius, opacity } = attribute.highlightCaps;
      let animation = '';
      if (animated) {
        if (startTime === endTime) {
          animation = 'c-day-scale-enter c-day-scale-leave';
        } else if (startTime === this.dateTime) {
          animation = backgroundExists ? 'c-day-slide-right-translate-enter' : 'c-day-slide-left-translate-enter';
        } else {
          animation = backgroundExists ? 'c-day-slide-left-translate-enter' : 'c-day-slide-right-translate-enter';
        }
      }
      return {
        key: `${attribute.key}-cap`,
        wrapperClass: `c-day-layer c-day-box-center-center ${animated ? animation : ''}`,
        style: {
          width: width || height,
          height,
          backgroundColor,
          borderColor,
          borderWidth,
          borderStyle,
          borderRadius,
          opacity,
        },
      };
    },
    getDot({ key, dot }) {
      return {
        key,
        style: {
          width: dot.diameter,
          height: dot.diameter,
          backgroundColor: dot.backgroundColor,
          borderColor: dot.borderColor,
          borderWidth: dot.borderWidth,
          borderStyle: dot.borderStyle,
          borderRadius: dot.borderRadius,
          opacity: dot.opacity,
        },
      };
    },
    getBar({ key, bar }) {
      return {
        key,
        style: {
          height: bar.height,
          backgroundColor: bar.backgroundColor,
          borderColor: bar.borderColor,
          borderWidth: bar.borderWidth,
          borderStyle: bar.borderStyle,
          opacity: bar.opacity,
        },
      };
    },
    getPopover(attribute) {
      const {
        label,
        labelStyle,
        hideIndicator,
        slot,
        visibility,
        isInteractive,
      } = attribute.popover;
      return {
        key: attribute.key,
        customData: attribute.customData,
        attribute,
        label: isFunction(label) ? label(attribute, this.dayInfo) : label,
        labelStyle: isFunction(labelStyle) ? labelStyle(attribute, this.dayInfo) : labelStyle,
        hideIndicator,
        slot,
        visibility,
        isInteractive: isInteractive !== undefined ? isInteractive : !!slot,
      };
    },
  },
};
</script>
 
<style lang='sass' scoped>
 
@import '../styles/vars.sass'
@import '../styles/mixins.sass'

.c-day-popover
  flex: 1
 
.c-day
  height: $day-height
  position: relative
 
.c-day-layer
  position: absolute
  left: 0
  right: 0
  top: 0
  bottom: 0
  pointer-events: none
 
.c-day-box-center-center
  +box()
  height: 100%
  transform-origin: 50% 50%

.c-day-box-left-center
  +box(flex-start)
  height: 100%
  transform-origin: 0% 50%
 
.c-day-box-right-center
  +box(flex-end)
  height: 100%
  transform-origin: 100% 50%
 
.c-day-box-center-bottom
  +box(center, flex-end)
 
.c-day-backgrounds
  position: relative
  width: 100%
  height: 100%
  overflow: hidden
  backface-visibility: hidden // Prevents glitches in Chrome by forcing hardware acceleration

.c-day-background
  transition: height $background-transition-time, background-color $background-transition-time

.shift-left
  margin-left: -1px
 
.shift-right
  margin-right: -1px
 
.shift-left-right
  margin: 0 -1px
 
.c-day-dots
  +box()
 
.c-day-dot
  width: $dot-diameter
  height: $dot-diameter
  border-radius: $dot-border-radius
  background-color: $dot-background-color
  transition: all $day-content-transition-time
  &:not(:last-child)
    margin-right: $dot-spacing
 
.c-day-bars
  +box(flex-start)
  width: $bars-width
 
.c-day-bar
  flex-grow: 1
  height: $bar-height
  background-color: $bar-background-color
  transition: all $day-content-transition-time
 
.c-day-content
  +box()
  width: $day-content-width
  height: $day-content-height
  font-size: $day-content-font-size
  font-weight: $day-content-font-weight
  line-height: 1
  border-radius: $day-content-border-radius
  transition: all $day-content-transition-time
  user-select: none
  cursor: default
  pointer-events: all
 
.c-day-popover-content
  font-size: $day-popover-font-size
  font-weight: $day-popover-font-weight
 
// TRANSITION ANIMATIONS
 
.background-enter-active
 
  &.c-day-fade-enter
    transition: $fade-transition
 
  &.c-day-slide-right-scale-enter
    animation: $slide-right-scale-enter-animation
 
  &.c-day-slide-right-translate-enter
    animation: $slide-right-translate-enter-animation
 
  &.c-day-slide-left-scale-enter
    animation: $slide-left-scale-enter-animation
 
  &.c-day-slide-left-translate-enter
    animation: $slide-left-translate-enter-animation
 
  &.c-day-scale-enter
    animation: $scale-enter-animation
 
.background-leave-active
  &.c-day-fade-leave
    transition: $fade-transition
 
  &.c-day-scale-leave
    animation: $scale-leave-animation
 
.background-enter
  &.c-day-fade-enter
    opacity: 0
 
.background-leave-to
  &.c-day-fade-leave
    opacity: 0
 
</style>