All files / src/BaseInput BaseInputDate.vue.js

100% Statements 17/17
80% Branches 8/10
100% Functions 8/8
100% Lines 17/17

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                    1x                           7x             5x                 2x 2x             3x 2x 2x   3x           9x       7x 5x   7x             2x           1x 1x 1x          
// @ts-check
import { has, invoke, isEmpty, isNil } from "lodash";
 
import InputError from "./InputError.vue";
import InputErrorMixin from "./InputErrorMixin";
import TDPicker from "../plugins/TempusDominusDatePicker.vue";
import BaseKeepFocusMixin from "../mixins/BaseKeepFocusMixin";
import BaseFocusInOutMixin from "../mixins/BaseFocusInOutMixin";
import Vue from "vue";
 
const component = /** @type {V.Constructor<any, any>} */(Vue).extend({
  name: "BaseInputDate",
  components: { InputError, TDPicker },
  mixins: [ BaseKeepFocusMixin, BaseFocusInOutMixin, InputErrorMixin ],
  model: { prop: "timestamp", event: "edit" },
  props: {
    timestamp: { type: Number, default: null },
    inEdit: { type: Boolean, default: false },
    noEditIcon: { type: Boolean, default: false }
  },
  /**
   * @return {{ editTimestamp: number | null, hasEditRequest: boolean }}
   */
  data() {
    return { editTimestamp: null, hasEditRequest: false };
  },
  computed: {
    /**
     * @return {boolean}
     */
    showEditIcon() {
      return this.hasFocus && this.hasEditRequest;
    }
  },
  watch: {
    /**
     * @param {number?} v
     * @param {number?} old
     */
    timestamp(v, old) {
      Eif (isEmpty(this.editTimestamp) || (this.editTimestamp === old)) {
        this.editTimestamp = v;
      }
    },
    /**
     * @param {boolean} v
     */
    inEdit(v) {
      if (v) {
        this.editTimestamp = this.timestamp; /* clear old editTimestamp when entering edit */
        this.$emit("edit", this.editTimestamp);
      }
      this.keepFocusOnNextTick("input");
    },
    /**
     * @param {number} value
     */ // @ts-ignore
    editTimestamp(value) {
      this.$emit("edit", value);
    }
  },
  mounted() {
    if (!isNil(this.timestamp)) {
      this.editTimestamp = this.timestamp;
    }
    this.hasEditRequest = has(this.$listeners, "edit-request");
  },
  methods: {
    /**
     * @param {number} timestamp
     */
    onChange(timestamp) {
      this.editTimestamp = timestamp;
    },
    /**
     * @param  {boolean} value
     */
    emitEditRequest(value) {
      invoke(this.$refs, [ "input", "focus" ]);
      this.keepFocusOnNextTick("input");
      this.$emit("edit-request", value);
    }
  }
});
export default component;