All files / src BaseDialog.vue.js

100% Statements 27/27
87.5% Branches 7/8
100% Functions 9/9
100% Lines 25/25

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         1x                       4x     4x 3x 3x       4x             2x 2x 1x             6x 6x 6x 6x 6x             12x 1x 1x 1x 1x               7x 7x 7x 7x 7x          
// @ts-check
 
import Modal from "./BaseModal.vue";
import Vue from "vue";
import { makeDeferred } from "@cern/prom";
import KeyboardEventMixin from "./mixins/BaseKeyboardEventMixin";
 
/**
 * @typedef {{ modal: V.Instance<typeof Modal> }} Refs
 */
 
const options = {
  /** @type  {prom.Deferred<any> | null} */
  _request: null
};
 
const component = /** @type {V.Constructor<typeof options, Refs>} */ (Vue).extend({
  name: "BaseDialog",
  ...options,
  components: { Modal },
  mixins: [ KeyboardEventMixin({ local: true }) ],
  props: {
    title: { type: String, default: "" }
  },
  /**
   * @return {{ text: string, visible: boolean }}
   */
  data() {
    return { text: "", visible: false };
  },
  mounted() {
    if (!this.$scopedSlots["footer"]) {
      this.onKey("esc", () => this.resolve(false));
      this.onKey("enter", () => this.resolve(true));
    }
  },
  beforeDestroy() {
    this.reject(new Error("destroyed"));
  },
  methods: {
    /**
     * @param  {boolean} [hidden]
     */
    onHiddenEvent(hidden) {
      this.$emit("hidden", hidden);
      if (hidden) {
        this.reject(new Error("hidden"));
      }
    },
    /**
     * @param  {any} [value] promise output value
     */
    resolve(value) {
      Eif (this.$options._request) {
        this.visible = false;
        this.$refs.modal.show(false);
        this.$options._request.resolve(value);
        this.$options._request = null;
      }
    },
    /**
     * @param  {any} err promise rejection error
     */
    reject(err) {
      if (this.$options._request) {
        this.visible = false;
        this.$refs.modal.show(false);
        this.$options._request.reject(err);
        this.$options._request = null;
      }
    },
    /**
     * @template T=any
     * @return {Promise<T>}
     */
    async request() {
      this.reject(new Error("aborted"));
      this.visible = true;
      this.$refs.modal.show(true);
      this.$options._request = makeDeferred();
      return this.$options._request.promise;
    }
  }
});
export default component;