Blame view

src/utils/assist.js 9.16 KB
a68c11a5   梁灏   support Nuxt.js
1
2
  import Vue from 'vue';
  const isServer = Vue.prototype.$isServer;
7fa943eb   梁灏   init
3
  // 判断参数是否是其中之一
7c15ac9e   梁灏   add Message compo...
4
  export function oneOf (value, validList) {
7fa943eb   梁灏   init
5
6
7
8
9
10
      for (let i = 0; i < validList.length; i++) {
          if (value === validList[i]) {
              return true;
          }
      }
      return false;
7c15ac9e   梁灏   add Message compo...
11
12
13
14
  }
  
  export function camelcaseToHyphen (str) {
      return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
be966e9f   梁灏   add Modal component
15
16
17
18
19
  }
  
  // For Modal scrollBar hidden
  let cached;
  export function getScrollBarSize (fresh) {
228c87cd   梁灏   fixed some bug th...
20
      if (isServer) return 0;
be966e9f   梁灏   add Modal component
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
      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
55
56
57
  }
  
  // watch DOM change
a68c11a5   梁灏   support Nuxt.js
58
  export const MutationObserver = isServer ? false : window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver || false;
69576f47   梁灏   add Slider component
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  
  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
76
          const computed = document.defaultView.getComputedStyle(element, '');
69576f47   梁灏   add Slider component
77
78
79
80
          return element.style[styleName] || computed ? computed[styleName] : null;
      } catch(e) {
          return element.style[styleName];
      }
2cb8a6d9   梁灏   commit Table comp...
81
82
83
84
85
86
  }
  
  // firstUpperCase
  function firstUpperCase(str) {
      return str.toString()[0].toUpperCase() + str.toString().slice(1);
  }
d70cad45   梁灏   update TimePicker
87
  export {firstUpperCase};
2cb8a6d9   梁灏   commit Table comp...
88
89
90
91
92
  
  // Warn
  export function warnProp(component, prop, correctType, wrongType) {
      correctType = firstUpperCase(correctType);
      wrongType = firstUpperCase(wrongType);
2d948738   梁灏   update TimePicker...
93
      console.error(`[iView warn]: Invalid prop: type check failed for prop ${prop}. Expected ${correctType}, got ${wrongType}. (found in component: ${component})`);    // eslint-disable-line
0d136465   梁灏   update Table
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
  }
  
  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;
  }
  
2d948738   梁灏   update TimePicker...
138
139
140
  export {deepCopy};
  
  // scrollTop animation
43513f70   zhigang.li   add anchor component
141
  export function scrollTop(el, from = 0, to, duration = 500, endCallback) {
2d948738   梁灏   update TimePicker...
142
143
144
145
146
147
148
149
      if (!window.requestAnimationFrame) {
          window.requestAnimationFrame = (
              window.webkitRequestAnimationFrame ||
              window.mozRequestAnimationFrame ||
              window.msRequestAnimationFrame ||
              function (callback) {
                  return window.setTimeout(callback, 1000/60);
              }
af713093   梁灏   update TimePicker
150
          );
2d948738   梁灏   update TimePicker...
151
152
153
154
155
      }
      const difference = Math.abs(from - to);
      const step = Math.ceil(difference / duration * 50);
  
      function scroll(start, end, step) {
43513f70   zhigang.li   add anchor component
156
157
158
159
          if (start === end) {
              endCallback && endCallback();
              return;
          }
2d948738   梁灏   update TimePicker...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  
          let d = (start + step > end) ? end : start + step;
          if (start > end) {
              d = (start - step < end) ? end : start - step;
          }
  
          if (el === window) {
              window.scrollTo(d, d);
          } else {
              el.scrollTop = d;
          }
          window.requestAnimationFrame(() => scroll(d, end, step));
      }
      scroll(from, to, step);
21dad188   梁灏   prevent dispatch ...
174
175
176
  }
  
  // Find components upward
67c9b1c8   梁灏   fixed #591
177
  function findComponentUpward (context, componentName, componentNames) {
21dad188   梁灏   prevent dispatch ...
178
179
180
181
182
183
      if (typeof componentName === 'string') {
          componentNames = [componentName];
      } else {
          componentNames = componentName;
      }
  
67c9b1c8   梁灏   fixed #591
184
      let parent = context.$parent;
21dad188   梁灏   prevent dispatch ...
185
186
187
188
189
190
191
      let name = parent.$options.name;
      while (parent && (!name || componentNames.indexOf(name) < 0)) {
          parent = parent.$parent;
          if (parent) name = parent.$options.name;
      }
      return parent;
  }
a9131058   梁灏   optimize Select e...
192
193
  export {findComponentUpward};
  
3f281d6c   梁灏   update RadioGroup
194
  // Find component downward
5ab32e9c   Sergio Crisostomo   remove unused cod...
195
  export function findComponentDownward (context, componentName) {
67c9b1c8   梁灏   fixed #591
196
      const childrens = context.$children;
a9131058   梁灏   optimize Select e...
197
198
199
      let children = null;
  
      if (childrens.length) {
5ab32e9c   Sergio Crisostomo   remove unused cod...
200
          for (const child of childrens) {
a9131058   梁灏   optimize Select e...
201
202
203
204
205
206
207
208
209
210
211
212
              const name = child.$options.name;
              if (name === componentName) {
                  children = child;
                  break;
              } else {
                  children = findComponentDownward(child, componentName);
                  if (children) break;
              }
          }
      }
      return children;
  }
3f281d6c   梁灏   update RadioGroup
213
214
  
  // Find components downward
5ab32e9c   Sergio Crisostomo   remove unused cod...
215
216
217
218
219
220
  export function findComponentsDownward (context, componentName) {
      return context.$children.reduce((components, child) => {
          if (child.$options.name === componentName) components.push(child);
          const foundChilds = findComponentsDownward(child, componentName);
          return components.concat(foundChilds);
      }, []);
3f281d6c   梁灏   update RadioGroup
221
  }
841cb1fe   Aresn   Menu support tran...
222
  
4bce7645   zhigang.li   make menu support...
223
224
225
  // Find components upward
  export function findComponentsUpward (context, componentName) {
      let parents = [];
444907c4   梁灏   update Vue
226
227
228
229
      const parent = context.$parent;
      if (parent) {
          if (parent.$options.name === componentName) parents.push(parent);
          return parents.concat(findComponentsUpward(parent, componentName));
4bce7645   zhigang.li   make menu support...
230
231
232
233
234
      } else {
          return [];
      }
  }
  
a163daa0   zhigang.li   Add logical treat...
235
  // Find brothers components
ce7b39c2   梁灏   fixed #3596
236
  export function findBrothersComponents (context, componentName, exceptMe = true) {
a163daa0   zhigang.li   Add logical treat...
237
238
239
      let res = context.$parent.$children.filter(item => {
          return item.$options.name === componentName;
      });
4c682092   zhigang.li   udpate
240
      let index = res.findIndex(item => item._uid === context._uid);
ce7b39c2   梁灏   fixed #3596
241
      if (exceptMe) res.splice(index, 1);
a163daa0   zhigang.li   Add logical treat...
242
243
244
      return res;
  }
  
841cb1fe   Aresn   Menu support tran...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  /* istanbul ignore next */
  const trim = function(string) {
      return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '');
  };
  
  /* istanbul ignore next */
  export function hasClass(el, cls) {
      if (!el || !cls) return false;
      if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.');
      if (el.classList) {
          return el.classList.contains(cls);
      } else {
          return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
      }
  }
  
  /* istanbul ignore next */
  export function addClass(el, cls) {
      if (!el) return;
      let curClass = el.className;
      const classes = (cls || '').split(' ');
  
      for (let i = 0, j = classes.length; i < j; i++) {
          const clsName = classes[i];
          if (!clsName) continue;
  
          if (el.classList) {
              el.classList.add(clsName);
          } else {
              if (!hasClass(el, clsName)) {
                  curClass += ' ' + clsName;
              }
          }
      }
      if (!el.classList) {
          el.className = curClass;
      }
  }
  
  /* istanbul ignore next */
  export function removeClass(el, cls) {
      if (!el || !cls) return;
      const classes = cls.split(' ');
      let curClass = ' ' + el.className + ' ';
  
      for (let i = 0, j = classes.length; i < j; i++) {
          const clsName = classes[i];
          if (!clsName) continue;
  
          if (el.classList) {
              el.classList.remove(clsName);
          } else {
              if (hasClass(el, clsName)) {
                  curClass = curClass.replace(' ' + clsName + ' ', ' ');
              }
          }
      }
      if (!el.classList) {
          el.className = trim(curClass);
      }
5ab32e9c   Sergio Crisostomo   remove unused cod...
305
  }
1a90ee6c   zhigang.li   move dimensionMap...
306
307
308
  
  export const dimensionMap = {
      xs: '480px',
6e97df10   梁灏   update default me...
309
310
311
312
313
      sm: '576px',
      md: '768px',
      lg: '992px',
      xl: '1200px',
      xxl: '1600px',
d85e5726   zhigang.li   create setMatchMe...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  };
  
  export function setMatchMedia () {
      if (typeof window !== 'undefined') {
          const matchMediaPolyfill = mediaQuery => {
              return {
                  media: mediaQuery,
                  matches: false,
                  on() {},
                  off() {},
              };
          };
          window.matchMedia = window.matchMedia || matchMediaPolyfill;
      }
04991c13   Graham Fairweather   Kebab join utilit...
328
  }
43513f70   zhigang.li   add anchor component
329
330
  
  export const sharpMatcherRegx = /#([^#]+)$/;