main.js 2.33 KB
/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: limin
 * @Date: 2021-08-13 14:12:11
 * @LastEditors: limin
 * @LastEditTime: 2021-09-24 10:41:31
 * @FilePath: \scrm\scrm-sub-blog\src\main.js
 */
import Vue from 'vue'
import apis from './api'
import './public-path'
import '@/assets/css/public.scss'
// import 'glsx-components/dist/glsx-components.css'
// 针对template模板中使用可选链操作符
import chaining from '@/plugins/chaining'

import { start } from '@/setup'
Vue.use(chaining)
Vue.config.productionTip = false
Vue.prototype.$apis = apis
Vue.directive('preventReClick', {
  bind: (el, binding) => {
    let time = binding.value // 防抖时间
    if (!time) {
      // 用户若不设置防抖时间,则默认1s
      time = 1000
    }
    let fun
    el.addEventListener(
      'click',
      event => {
        if (!fun) {
          // 第一次执行
          fun = setTimeout(() => {
            fun = null
          }, time)
        } else {
          // 直接取消当前事件
          event && event.stopImmediatePropagation()
        }
      },
      true
    )
  }
})
function adjustColumnWidth(table) {
  const colgroup = table.querySelector('colgroup')
  const colDefs = [...colgroup.querySelectorAll('col')]
  colDefs.forEach(col => {
    const clsName = col.getAttribute('name')
    const cells = [...table.querySelectorAll(`td.${clsName}`), ...table.querySelectorAll(`th.${clsName}`)]
    // 忽略加了"leave-alone"类的列
    if (cells[0]?.classList?.contains?.('leave-alone')) {
      return
    }
    const widthList = cells.map(el => {
      return el.querySelector('.cell')?.scrollWidth || 0
    })
    const max = Math.max(...widthList)
    const padding = 32
    table.querySelectorAll(`col[name=${clsName}]`).forEach(el => {
      el.setAttribute('width', max + padding)
    })
  })
}
Vue.directive('fit-columns', {
  update() {},
  bind() {},
  inserted(el) {
    setTimeout(() => {
      adjustColumnWidth(el)
    }, 300)
  },
  componentUpdated(el) {
    el.classList.add('r-table')
    setTimeout(() => {
      adjustColumnWidth(el)
    }, 300)
  },
  unbind() {}
})
start()
export { bootstrap, mount, unmount } from '@/setup'
export const http = params => Vue.prototype.$http(params)
export const http_brand = params => Vue.prototype.$http_brand(params)
export const http_common = params => Vue.prototype.$http_common(params)