All files / src/vtips index.js

0% Statements 0/43
0% Branches 0/35
0% Functions 0/9
0% Lines 0/39
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                                                                                                                                               
import component from './component.js'
 
export default function (Vue, config) {
  const PromptConstructor = Vue.extend(component)
  const tipsList = []
  
  function getAnInstance () {
    if (tipsList.length > 1) {
      const instance = tipsList[0]
      tipsList.splice(0, 1)
      return instance
    }
    return new PromptConstructor({
      el: document.createElement('div')
    })
  }
 
  const returnAnInstance = instance => {
    if (instance) {
      tipsList.push(instance)
    }
  }
 
  function getElPosition (el) {
    if (window.getComputedStyle && (el.parentNode && !el.parentNode.style.position)) {
      const _position = window.getComputedStyle(el.parentNode).position
      if (!_position || _position === 'static') {
        el.parentNode.style.position = 'relative'
      }
    }
    return {
      top: el.offsetTop - 38,
      left: el.offsetLeft
    }
  }
 
  const removeDom = target => {
    const container = getContainer()
    if (target.parentNode) {
      container.removeChild(target)
    }
  }
 
  PromptConstructor.prototype.close = function (el) {
    this.exist = false
    // removeDom(el)
    // this.exist = true
    // returnAnInstance(this)
  }
  
  const vTips = (options = {}) => {
    const instance = options.target || getAnInstance()
    const container = options.el.parentNode
    if (options.remove) {
      instance.close(options.target)
      return
    }
 
    instance.message = typeof options === 'string' ? options : options.message
    instance.position = options.el ? getElPosition(options.el, instance) : options.position
    instance.exist = false
    instance.errorClass = options.errorClass || config.errorClass
    instance.errorIcon = options.errorIcon || config.errorIcon
    container.appendChild(instance.$el)
    Vue.nextTick(function () {
      instance.exist = true
    })
    return instance
  }
 
  return vTips
}