All files / src/components MsgSwitch.vue

100% Statements 15/15
90% Branches 9/10
100% Functions 5/5
100% Lines 15/15
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                            5x       5x             15x             23x 23x       38x 38x   38x 19x     19x 19x 14x   19x 19x 19x                                
<template>
  <span class="msg-switch">
    <!-- list of overridings and dynamic messages -->
    <slot :value="value" />
  </span>
</template>
 
<script>
export default {
  props: {
    value: { type: String, default: '' },
  },
 
  data() {
    return { cases: {} };
  },
 
  provide() {
    return {
      registerMsgCase: this.registerMsgCase,
    };
  },
 
  watch: {
    value() {
      this.updateDisplayedCase();
    },
  },
 
  methods: {
    registerMsgCase(matchingValue, child) {
      // First matching case is displayed.
      this.cases[matchingValue] = this.cases[matchingValue] || child;
      this.updateDisplayedCase();
    },
 
    updateDisplayedCase() {
      const oldCase = this.displayedCase;
      const newCase = this.cases[this.value] || this.cases['*'];
 
      if (oldCase === newCase) {
        return;
      }
 
      this.displayedCase = newCase;
      if (oldCase) {
        oldCase.$el.classList.remove('match');
      }
E      if (newCase) {
        newCase.$el.classList.add('match');
        newCase.updateCode(this.value);
      }
    },
  },
};
</script>
 
<style>
.msg-switch .msg-case {
  display: none;
}
 
.msg-switch .msg-case.match {
  display: inherit;
}
</style>