Blame view

src/utils/assist.js 3.71 KB
7fa943eb   梁灏   init
1
  // 判断参数是否是其中之一
7c15ac9e   梁灏   add Message compo...
2
  export function oneOf (value, validList) {
7fa943eb   梁灏   init
3
4
5
6
7
8
      for (let i = 0; i < validList.length; i++) {
          if (value === validList[i]) {
              return true;
          }
      }
      return false;
7c15ac9e   梁灏   add Message compo...
9
10
11
12
  }
  
  export function camelcaseToHyphen (str) {
      return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
be966e9f   梁灏   add Modal component
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
  }
  
  // For Modal scrollBar hidden
  let cached;
  export function getScrollBarSize (fresh) {
      if (fresh || cached === undefined) {
          const inner = document.createElement('div');
          inner.style.width = '100%';
          inner.style.height = '200px';
  
          const outer = document.createElement('div');
          const outerStyle = outer.style;
  
          outerStyle.position = 'absolute';
          outerStyle.top = 0;
          outerStyle.left = 0;
          outerStyle.pointerEvents = 'none';
          outerStyle.visibility = 'hidden';
          outerStyle.width = '200px';
          outerStyle.height = '150px';
          outerStyle.overflow = 'hidden';
  
          outer.appendChild(inner);
  
          document.body.appendChild(outer);
  
          const widthContained = inner.offsetWidth;
          outer.style.overflow = 'scroll';
          let widthScroll = inner.offsetWidth;
  
          if (widthContained === widthScroll) {
              widthScroll = outer.clientWidth;
          }
  
          document.body.removeChild(outer);
  
          cached = widthContained - widthScroll;
      }
      return cached;
3e855e34   梁灏   fixed #46
52
53
54
  }
  
  // watch DOM change
69576f47   梁灏   add Slider component
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  export const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver || false;
  
  const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
  const MOZ_HACK_REGEXP = /^moz([A-Z])/;
  
  function camelCase(name) {
      return name.replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
          return offset ? letter.toUpperCase() : letter;
      }).replace(MOZ_HACK_REGEXP, 'Moz$1');
  }
  // getStyle
  export function getStyle (element, styleName) {
      if (!element || !styleName) return null;
      styleName = camelCase(styleName);
      if (styleName === 'float') {
          styleName = 'cssFloat';
      }
      try {
0d136465   梁灏   update Table
73
          const computed = document.defaultView.getComputedStyle(element, '');
69576f47   梁灏   add Slider component
74
75
76
77
          return element.style[styleName] || computed ? computed[styleName] : null;
      } catch(e) {
          return element.style[styleName];
      }
2cb8a6d9   梁灏   commit Table comp...
78
79
80
81
82
83
84
85
86
87
88
89
  }
  
  // firstUpperCase
  function firstUpperCase(str) {
      return str.toString()[0].toUpperCase() + str.toString().slice(1);
  }
  
  // Warn
  export function warnProp(component, prop, correctType, wrongType) {
      correctType = firstUpperCase(correctType);
      wrongType = firstUpperCase(wrongType);
      console.error(`[iView warn]: Invalid prop: type check failed for prop ${prop}. Expected ${correctType}, got ${wrongType}. (found in component: ${component})`);
0d136465   梁灏   update Table
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
  }
  
  function typeOf(obj) {
      const toString = Object.prototype.toString;
      const map = {
          '[object Boolean]'  : 'boolean',
          '[object Number]'   : 'number',
          '[object String]'   : 'string',
          '[object Function]' : 'function',
          '[object Array]'    : 'array',
          '[object Date]'     : 'date',
          '[object RegExp]'   : 'regExp',
          '[object Undefined]': 'undefined',
          '[object Null]'     : 'null',
          '[object Object]'   : 'object'
      };
      return map[toString.call(obj)];
  }
  
  // deepCopy
  function deepCopy(data) {
      const t = typeOf(data);
      let o;
  
      if (t === 'array') {
          o = [];
      } else if ( t === 'object') {
          o = {};
      } else {
          return data;
      }
  
      if (t === 'array') {
          for (let i = 0; i < data.length; i++) {
              o.push(deepCopy(data[i]));
          }
      } else if ( t === 'object') {
          for (let i in data) {
              o[i] = deepCopy(data[i]);
          }
      }
      return o;
  }
  
  export {deepCopy}