import Vue from 'vue'


//树形数组
Vue.prototype.forumTreeData = function (list,value,title,parentKey) {
    let temp = {}
    let ans = []
    if (!parentKey) {
      parentKey = 'fatherId'
    }
    if (!list || list.length === 0 || !value || !title) return ans
    for (let i in list) {
      // eslint-disable-next-line no-eval
      list[i].value = eval(`list[i].${value}`)
      // eslint-disable-next-line no-eval
      list[i].title = eval(`list[i].${title}`)
      // eslint-disable-next-line no-eval
      list[i].label = eval(`list[i].${title}`)
      temp[list[i].value] = list[i]
    }

    for (let i in temp) {
      // if (temp[i].functionFlag === 0) this.$set(temp[i],'expand',true)
      if (temp[i][parentKey] !== 0 && temp[i][parentKey] !== null) {
        if (temp[temp[i][parentKey]] && !temp[temp[i][parentKey]].children) {
          temp[temp[i][parentKey]].children = []
          temp[temp[i][parentKey]].children.push(temp[i])
        } else if (temp[temp[i][parentKey]]){
          temp[temp[i][parentKey]].children.push(temp[i])
        }
      } else {
        ans.push(temp[i])
      }
    }
    return ans
}



/**
 * @Date 2020-08-12 16:36
 * @Author H_x_d
 * @Description 监听缓存
 */
Vue.prototype.$addStorageEvent = function (type, key, data) {
  if (type === 1) {
    // 创建一个StorageEvent事件
    let newStorageEvent = document.createEvent('StorageEvent');
    const storage = {
      setItem: function (k, val) {
        localStorage.setItem(k, val);
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
        // 派发对象
        window.dispatchEvent(newStorageEvent);
      }
    }
    return storage.setItem(key, data);
  } else {
    // 创建一个StorageEvent事件
    let newStorageEvent = document.createEvent('StorageEvent');
    const storage = {
      setItem: function (k, val) {
        sessionStorage.setItem(k, val);
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
        // 派发对象
        window.dispatchEvent(newStorageEvent);
      }
    }
    return storage.setItem(key, data);
  }
}

