派生自 wuyushui/SewerAndRainNetwork

陈泽平
2021-05-30 44e1d3824a7816864de8903ca8c579b6153ec891
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
export default class helper {
  /**
   * 点击mask关闭弹窗
   * @param  {[type]} event [description]
   * @param  {[type]} layer [description]
   * @return {[type]}       [description]
   */
  static clickMaskCloseAll (event, layer, id) {
    const mask = event.target.getAttribute('class')
    if (mask && (mask.indexOf('notify-mask') > -1 || mask.indexOf('icon-remove') > -1)) {
      layer.close(id)
    }
  }
 
  /**
   * 默认的yes按钮操作
   * @param  {[type]} event [description]
   * @return {[type]}       [description]
   */
  static btnyes (event, options, formValue) {
    if (typeof (options.yes) === 'function') {
      if (options.type === 6) {
        options.yes(formValue, options.id)
      } else {
        options.yes(options.id)
      }
    } else {
      options.layer.close(options.id)
    }
  }
 
  /**
   * 默认取消按钮操作
   * @param  {[type]} event [description]
   * @return {[type]}       [description]
   */
  static async btncancel (event, options) {
    if (typeof (options.cancel) === 'function') {
      await options.cancel(options.id)
    } else {
      options.layer.close(options.id)
    }
  }
 
  /**
   * 隐藏滚动条
   */
  static hiddenScrollBar (options) {
    if (!options.scrollbar) {
      const htmlDom = document.getElementsByTagName('html')[0]
      const htmlClass = [...htmlDom.classList]
      if (htmlClass.indexOf('vl-html-scrollbar-hidden') > -1) {
        return
      }
 
      const htmlWidth = htmlDom.offsetWidth
      // 隐藏滚动条
      // htmlDom.style.overflowY = "hidden";
      htmlDom.classList.add('vl-html-scrollbar-hidden')
      const htmlWidthH = htmlDom.offsetWidth
      htmlDom.style.marginRight = htmlWidthH - htmlWidth + 'px'
    }
  }
 
  /**
   * 鼠标拖动弹窗
   * @param  {[type]} event   [description]
   * @param  {[type]} options [description]
   * @return {[type]}         [description]
   */
  static moveStart (event, options) {
    options.offset = options.offset === 'auto' ? [] : options.offset
    if (options.offset.length === 0) {
      options.offset.push(document.getElementById(options.id + '').offsetLeft)
      options.offset.push(document.getElementById(options.id + '').offsetTop)
      options.offset.push(0)
    }
    if (options.offset.length === 2) {
      options.offset.push(0)
    }
    options.offset[0] = (document.getElementById(options.id + '').offsetLeft)
    options.offset[1] = (document.getElementById(options.id + '').offsetTop)
  }
 
  /**
   * 拖动弹窗
   * @param  {[type]} event  [description]
   * @param  {[type]} ismove [description]
   * @return {[type]}        [description]
   */
  static move (event, options, ismove) {
    if (ismove) {
      const o = document.getElementById(options.id + '_alert')
      o.style.left = options.offset[0] + (event.clientX - this.moveLeft) + 'px'
      o.style.top = options.offset[1] + (event.clientY - this.moveTop) + 'px'
    }
  }
 
  /**
   * [sleep description]
   * @param  {[type]} ms [description]
   * @return {[type]}    [description]
   */
  static sleep (ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }
 
  /**
   *  深度拷贝
   * @param {*} source
   */
  static deepClone (target) {
    const copyedObjs = [] // 此数组解决了循环引用和相同引用的问题,它存放已经递归到的目标对象
    function _deepCopy (target) {
      if ((typeof target !== 'object') || !target) {
        return target
      }
      for (let i = 0; i < copyedObjs.length; i++) {
        if (copyedObjs[i].target === target) {
          return copyedObjs[i].copyTarget
        }
      }
      let obj = {}
      if (Array.isArray(target)) {
        obj = [] // 处理target是数组的情况
      }
      copyedObjs.push({
        target: target,
        copyTarget: obj
      })
      Object.keys(target).forEach(key => {
        if (obj[key]) {
          return
        }
        obj[key] = _deepCopy(target[key])
      })
      return obj
    }
    return _deepCopy(target)
  }
 
  /**
   *  取偶数
   * @param {*} str
   */
  static evenNumber (str = '') {
    const result = str.match(/\d+/g)
    if (result) {
      const n = parseInt(result[0])
      if (n % 2 === 0) {
        return str
      } else {
        str = str.replace(n, n + 1)
        return str
      }
    } else {
      return str
    }
  }
}