all files / src/ wait.js

100% Statements 19/19
100% Branches 12/12
100% Functions 6/6
100% Lines 19/19
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                                                                 
/* eslint-disable no-param-reassign, no-unused-vars */
 
import { warn } from './utils';
 
function assert(vnode) {
  const vm = vnode.context;
 
  if (!vm.$i18n) {
    warn('No VueI18Next instance found in the Vue instance');
    return false;
  }
 
  return true;
}
 
function waitForIt(el, vnode) {
  if (vnode.context.$i18n.i18next.isInitialized) {
    el.hidden = false;
  } else {
    el.hidden = true;
    const initialized = () => {
      vnode.context.$forceUpdate();
      // due to emitter removing issue in i18next we need to delay remove
      setTimeout(() => {
        if (vnode.context && vnode.context.$i18n) {
          vnode.context.$i18n.i18next.off('initialized', initialized);
        }
      }, 1000);
    };
    vnode.context.$i18n.i18next.on('initialized', initialized);
  }
}
 
export function bind(el, binding, vnode) {
  if (!assert(vnode)) {
    return;
  }
 
  waitForIt(el, vnode);
}
 
export function update(el, binding, vnode, oldVNode) {
  if (vnode.context.$i18n.i18next.isInitialized) {
    el.hidden = false;
  }
}
 
export default {
  bind,
  update
};