All files / src/mixins BaseKeepFocusMixin.js

52.63% Statements 10/19
39.28% Branches 11/28
57.14% Functions 4/7
58.82% Lines 10/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                        30x     1x       1x                                 28x   2x                 2x 2x       2x 2x 2x                                              
// @ts-check
 
import { mixinMaker } from "../utils";
 
/**
 * @param {Vue|Element} el
 * @returns {HTMLElement}
 */
function getDomElement(el) {
  // This will handle also the case when it's a Vue component
  // (for example in BaseParamDate)
  // @ts-ignore this check is correct.
  return (el && el.$el) ? el.$el : el;
}
 
const options = {
  _hasFocus: false
};
 
const component = mixinMaker({
  ...options,
  methods: {
    /**
     * @param  {string|null} [ref]
     */
    focusBubble(ref) {
      for (let elt = /** @type {HTMLElement|null} */ (getDomElement(ref ? /** @type {Element} */(this.$refs[ref]) : this)); elt; elt = elt.parentElement) {
        elt.focus();
        if (elt === document.activeElement) { break; }
      }
    },
    /**
     * @param {string|null} [ref]
     * @param {boolean} [bubble]
     */
    keepFocusOnNextTick(ref, bubble = false) {
      if (getDomElement(ref ? /** @type {Element} */(this.$refs[ref]) : this) ===
          document.activeElement) {
        this.setFocusOnNextTick(ref, bubble);
      }
    },
 
    /**
     * @param {string|null} [ref]
     * @param {boolean} [bubble]
     */
    setFocusOnNextTick(ref, bubble = true) {
      this.$nextTick(() => {
        Iif (bubble) {
          this.focusBubble(ref ?? null);
        }
        else {
          const el = getDomElement(ref ? /** @type {Element} */(this.$refs[ref]) : this);
          Eif (el) {
            /** @type HTMLElement */(el).focus();
          }
        }
      });
    },
    /**
     * @param {string|null} [ref]
     */
    saveFocusState(ref) {
      /** @type {typeof options} */(this.$options)._hasFocus = ((ref ? this.$refs[ref] : this) ===
        document.activeElement);
    },
    /**
     * @param {string|null} [ref]
     */
    restoreFocusState(ref) {
      if (/** @type {typeof options} */(this.$options)._hasFocus) {
        /** @type HTMLElement */(ref ? this.$refs[ref] : this.$el).focus();
      }
    }
  }
});
export default component;