UNPKG

5.14 kBJavaScriptView Raw
1/**
2 * 常用方法
3 */
4
5const SPECIAL_CHARS_REGEXP = /([:\-_]+(.))/g
6const MOZ_HACK_REGEXP = /^moz([A-Z])/
7
8const trim = function (string) {
9 return (string || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '')
10}
11
12/**
13 * [camelCaseToHyphen 将驼峰命名转换为连字符]
14 * @param {[string]} str [驼峰命名的字符串]
15 * @return {[string]} [连字符的字符串]
16 */
17export function camelCaseToHyphen (str) {
18 return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
19}
20
21export function camelCase (name) {
22 return name.replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) {
23 return offset ? letter.toUpperCase() : letter
24 }).replace(MOZ_HACK_REGEXP, 'Moz$1')
25}
26
27export function getStyle (element, styleName) {
28 if (!element || !styleName) return null
29
30 styleName = camelCase(styleName)
31 if (styleName === 'float') {
32 styleName = 'cssFloat'
33 }
34
35 try {
36 const computed = document.defaultView.getComputedStyle(element, '')
37 return element.style[styleName] || computed ? computed[styleName] : null
38 } catch (e) {
39 return element.style[styleName]
40 }
41}
42
43function typeOf (obj) {
44 const map = {
45 '[object Boolean]': 'boolean',
46 '[object Number]': 'number',
47 '[object String]': 'string',
48 '[object Function]': 'function',
49 '[object Array]': 'array',
50 '[object Date]': 'date',
51 '[object RegExp]': 'regExp',
52 '[object Undefined]': 'undefined',
53 '[object Null]': 'null',
54 '[object Object]': 'object',
55 }
56 return map[Object.prototype.toString.call(obj)]
57}
58
59export function deepCopy (data) {
60 const type = typeOf(data)
61 let obj
62
63 if (type === 'array') {
64 obj = []
65 } else if (type === 'object') {
66 obj = {}
67 } else {
68 return data
69 }
70
71 if (type === 'array') {
72 for (let i = 0; i < data.length; i++) {
73 obj.push(deepCopy(data[i]))
74 }
75 } else if (type === 'object') {
76 for (const i in data) {
77 obj[i] = deepCopy(data[i])
78 }
79 }
80
81 return obj
82}
83
84export function hasClass (el, cls) {
85 if (!el || !cls) return false
86 if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.')
87 if (el.classList) {
88 return el.classList.contains(cls)
89 }
90 return (` ${el.className} `).indexOf(` ${cls} `) > -1
91}
92
93export function addClass (el, cls) {
94 if (!el) return
95
96 const classes = (cls || '').split(' ')
97 let curClass = el.className
98
99 for (let i = 0, j = classes.length; i < j; i++) {
100 const clsName = classes[i]
101 if (!clsName) continue
102
103 if (el.classList) {
104 el.classList.add(clsName)
105 } else if (!hasClass(el, clsName)) {
106 curClass += ` ${clsName}`
107 }
108 }
109 if (!el.classList) {
110 el.className = curClass
111 }
112}
113
114export function removeClass (el, cls) {
115 if (!el || !cls) return
116 const classes = cls.split(' ')
117 let curClass = ` ${el.className} `
118
119 for (let i = 0, j = classes.length; i < j; i++) {
120 const clsName = classes[i]
121 if (!clsName) continue
122
123 if (el.classList) {
124 el.classList.remove(clsName)
125 } else if (hasClass(el, clsName)) {
126 curClass = curClass.replace(` ${clsName} `, ' ')
127 }
128 }
129 if (!el.classList) {
130 el.className = trim(curClass)
131 }
132}
133
134export function findComponentUpward (context, componentName, componentNames) {
135 if (typeof componentName === 'string') {
136 componentNames = [componentName]
137 } else {
138 componentNames = componentName
139 }
140
141 let parent = context.$parent
142 let name = parent.$options.name
143 while (parent && (!name || componentNames.indexOf(name) < 0)) {
144 parent = parent.$parent
145 if (parent) name = parent.$options.name
146 }
147
148 return parent
149}
150
151export function findComponentsUpward (context, componentName, components = []) {
152 let parent = context.$parent
153 let name = parent.$options.name
154
155 while (parent && name) {
156 if (componentName === name) {
157 components.push(parent)
158 }
159
160 parent = parent.$parent
161 if (parent) {
162 name = parent.$options.name
163 }
164 }
165
166 return components
167}
168
169export function findComponentDownward (context, componentName) {
170 const childrens = context.$children
171 let children
172
173 if (childrens.length) {
174 childrens.forEach(child => {
175 if (child.$options.name === componentName) {
176 children = child
177 }
178 })
179
180 for (let i = 0, len = childrens.length; i < len; i++) {
181 const child = childrens[i]
182 const name = child.$options.name
183
184 if (name === componentName) {
185 children = child
186 break
187 } else {
188 children = findComponentDownward(child, componentName)
189 if (children) break
190 }
191 }
192 }
193
194 return children
195}
196
197export function findComponentsDownward (context, componentName, components = []) {
198 const childrens = context.$children
199
200 if (childrens.length) {
201 childrens.forEach(child => {
202 const subChildren = child.$children
203 const name = child.$options.name
204
205 if (name === componentName) {
206 components.push(child)
207 }
208 if (subChildren.length) {
209 const findChildren = findComponentsDownward(child, componentName, components)
210 if (findChildren) {
211 components.concat(findChildren)
212 }
213 }
214 })
215 }
216
217 return components
218}