All files / components CalendarNav.vue

0% Statements 0/119
0% Branches 0/56
0% Functions 0/53
0% Lines 0/98
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
<template>
<!--Nav panel-->
<div class='c-nav'>
  <!--Nav months-->
  <div
    v-if='mode_ === "month"'>
    <!--Months header-->
    <div class='c-header'>
      <!--Previous year button-->
      <span
        class='c-arrow vc-angle-left'
        :class='{ "c-disabled": !canMovePrevYear }'
        @click='yearIndex--'>
      </span>
      <!--Mode switch button-->
      <span
        class='c-title'
        @click='selectMode("year")'>
        {{ yearIndex }}
      </span>
      <!--Next year button-->
      <span
        class='c-arrow vc-angle-right'
        :class='{ "c-disabled": !canMoveNextYear }'
        @click='yearIndex++'>
      </span>
    </div>
    <!--Months table-->
    <table class='c-table'>
      <tr v-for='(row, i) in monthRows' :key='i'>
        <td
          v-for='item in row'
          :key='item.month'>
          <div
            class='c-table-cell'
            :class='{ "c-active": item.isActive, "c-disabled": item.isDisabled }'
            @click='monthClick(item.month)'>
            <!--Month label-->
            {{ item.label.substring(0, 3) }}
            <!--Attribute indicators-->
            <transition name='indicators'>
              <div
                v-if='item.attributes'
                class='c-indicators'>
                <span
                  class='c-indicator'
                  v-for='attribute in item.attributes'
                  :key='attribute.key'
                  :style='attribute.style'>
                </span>
              </div>
            </transition>
          </div>
        </td>
      </tr>
    </table>
  </div>
  <!--Nav years-->
  <div
    v-if='mode_ === "year"'>
    <div class='c-header'>
      <!--Previous year group button-->
      <span
        class='c-arrow vc-angle-left'
        :class='{ "c-disabled": !canMovePrevYearGroup }'
        @click='yearGroupIndex--'>
      </span>
      <!--Mode switch button-->
      <span
        class='c-title'
        @click='selectMode("month")'>
        {{ firstYear }} - {{ lastYear }}
      </span>
      <!--Next year group button-->
      <span
        class='c-arrow vc-angle-right'
        :class='{ "c-disabled": !canMoveNextYearGroup }'
        @click='yearGroupIndex++'>
      </span>
    </div>
    <!--Years table-->
    <table class='c-table'>
      <tr v-for='(row, i) in yearRows' :key='i'>
        <td
          v-for='item in row'
          :key='item.year'>
          <div
            class='c-table-cell'
            :class='{ "c-active": item.year === year, "c-disabled": item.isDisabled }'
            @click='yearClick(item.year)'>
            <!--Year label-->
            {{ item.year }}
          </div>
        </td>
      </tr>
    </table>
  </div>
</div>
</template>
 
<script>
import DateInfo from '../utils/dateInfo';
import {
  getMonthComps,
  getFirstArrayItem,
  getLastArrayItem } from '../utils/helpers';
 
const _yearGroupCount = 12;
 
export default {
  props: {
    monthLabels: { type: Array, required: true },
    mode: { type: String, default: 'month' },
    value: { type: Object, default: () => ({ month: 0, year: 0 }) },
    validator: { type: Function, default: () => () => true },
    attributes: Array,
  },
  data() {
    return {
      mode_: '',
      yearIndex: 0,
      yearGroupIndex: 0,
      attributesMap: {},
    };
  },
  computed: {
    month() {
      return this.value ? this.value.month || 0 : 0;
    },
    year() {
      return this.value ? this.value.year || 0 : 0;
    },
    canMovePrevYear() {
      return this.validator({ month: 12, year: this.yearIndex - 1 });
    },
    canMoveNextYear() {
      return this.validator({ month: 1, year: this.yearIndex + 1 });
    },
    canMovePrevYearGroup() {
      return this.validator({ month: 12, year: this.firstYear - 1 });
    },
    canMoveNextYearGroup() {
      return this.validator({ month: 1, year: this.lastYear + 1 });
    },
    monthItems() {
      return this.monthLabels.map((ml, i) => {
        const month = i + 1;
        return {
          month,
          label: ml,
          attributes: this.getMonthAttributes(month),
          isActive: month === this.month && this.yearIndex === this.year,
          isDisabled: !this.validator({ month, year: this.yearIndex }),
        };
      });
    },
    yearItems() {
      const startYear = this.yearGroupIndex * _yearGroupCount;
      const endYear = startYear + _yearGroupCount;
      const items = [];
      for (let i = startYear; i < endYear; i += 1) {
        items.push({
          year: i,
          isDisabled: !this.validator({ month: this.month, year: i }),
        });
      }
      return items;
    },
    monthRows() {
      return this.createRows(this.monthItems, 3);
    },
    yearRows() {
      return this.createRows(this.yearItems, 3);
    },
    firstYear() {
      return getFirstArrayItem(this.yearItems.map(i => i.year), 0);
    },
    lastYear() {
      return getLastArrayItem(this.yearItems.map(i => i.year), 0);
    },
  },
  watch: {
    mode(val) {
      this.mode_ = val;
    },
    year() {
      this.yearIndex = this.year;
    },
    yearIndex(val) {
      this.yearGroupIndex = this.getYearGroupIndex(val);
    },
    attributes() {
      this.mapAttributes(true);
    },
    yearGroupIndex() {
      this.mapAttributes();
    },
  },
  created() {
    this.mode_ = this.mode;
    this.yearIndex = this.year;
  },
  methods: {
    getMonthAttributes(month) {
      if (!this.attributesMap[this.yearIndex] || !this.attributesMap[this.yearIndex][month]) return undefined;
      return Object.values(this.attributesMap[this.yearIndex][month]);
    },
    mapAttributes(clearCache) {
      // Clear map if there are no attributes
      if (!this.attributes || !this.attributes.length) {
        this.attributesMap = {};
        return;
      }
      // Clear cache if needed
      const map = clearCache ? {} : this.attributesMap;
      // Cycle each year in the current year group
      for (let y = this.firstYear; y <= this.lastYear; y++) {
        // If there isn't current year data...
        if (!map[y]) {
          const yearData = {};
          // Cycle each month
          for (let m = 1; m <= 12; m++) {
            const monthData = {};
            // Get range for the current month
            const comps = getMonthComps(m, y);
            const monthRange = new DateInfo({
              start: new Date(comps.year, comps.month - 1, 1),
              end: new Date(comps.year, comps.month - 1, comps.days),
            });
            // Assign attribute data if they lie in month range
            this.attributes.forEach((a) => {
              if (a.dates.find(d => d.intersects(monthRange))) {
                monthData[a.key] = this.getAttributeInfo(a);
              }
            });
            // Assign month data
            if (Object.keys(monthData).length) yearData[m] = monthData;
          }
          // Assign year data
          if (Object.keys(yearData).length) map[y] = yearData;
        }
      }
      // Use object spread to trigger Vue reactivity
      this.attributesMap = { ...map };
    },
    getAttributeInfo(attr) {
      let color;
      if (attr.highlight) {
        color = attr.highlight.backgroundColor;
      } else if (attr.dot) {
        color = attr.dot.backgroundColor;
      } else if (attr.bar) {
        color = attr.bar.backgroundColor;
      } else if (attr.contentStyle) {
        color = attr.contentStyle.backgroundColor || attr.contentStyle.color;
      }
      return {
        key: attr.key,
        style: {
          backgroundColor: color,
        },
      };
    },
    getYearGroupIndex(year) {
      return Math.floor(year / _yearGroupCount);
    },
    monthClick(month) {
      this.$emit('input', { month, year: this.yearIndex });
    },
    yearClick(year) {
      this.yearIndex = year;
      this.selectMode('month');
    },
    selectMode(mode) {
      this.mode_ = mode;
      this.$emit('update:mode', mode);
    },
    createRows(items, columnCount) {
      const rows = [];
      let row = [];
      items.forEach((item) => {
        row.push(item);
        if (row.length >= columnCount) {
          rows.push(row);
          row = [];
        }
      });
      return rows;
    },
  },
};
</script>

<style lang='sass' scoped>

@import '../styles/vars.sass'
@import '../styles/mixins.sass'

$cell-transition: all 0.1s ease-in-out
 
.c-nav
  transition: height 5s ease-in-out
  color: #333333
 
.c-header
  display: flex
  justify-content: space-between
  align-items: center
  border-bottom: 1px solid #dadada
  padding: 3px 0

.c-arrow
  +box()
  font-size: $arrow-font-size
  width: $arrow-width
  height: $arrow-height
  transition: $arrow-transition
  cursor: pointer
  user-select: none
  &:hover
    opacity: 0.5

.c-title
  font-size: $nav-title-font-size
  font-weight: $nav-title-font-weight
  transition: $title-transition
  cursor: pointer
  user-select: none
  &:hover
    opacity: 0.5
 
.c-table-cell
  display: flex
  flex-direction: column
  justify-content: center
  align-items: center
  height: 100%
  position: relative
  user-select: none
  cursor: pointer
  font-size: $nav-table-font-size
  font-weight: $nav-table-font-weight
  background-color: white
  transition: $cell-transition
  &:hover
    background-color: #f0f0f0
 
.c-disabled
  opacity: 0.2
  cursor: not-allowed
  pointer-events: none
  &:hover
    background-color: transparent
 
.c-active
  background-color: #f0f0f0
  font-weight: $weight-semibold
 
.c-indicators
  position: absolute
  display: flex
  justify-content: center
  align-items: center
  bottom: 5px
  width: 100%
  transition: $cell-transition
  .c-indicator
    width: 5px
    height: 5px
    border-radius: 50%
    &:not(:first-child)
      margin-left: 3px
 
.c-table
  table-layout: fixed
  width: 100%
  border-collapse: collapse
  tr
    td
      border: 1px solid #dadada
      width: 60px
      height: 34px
      &:first-child
        border-left: 0
      &:last-child
        border-right: 0
    &:first-child
      td
        border-top: 0
    &:last-child
      td
        border-bottom: 0
 
.indicators-enter-active, .indicators-leave-active
  transition: $cell-transition
 
.indicators-enter, .indicators-leave-to
  opacity: 0
 
</style>