1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.matches = exports.hasDOM = undefined;
|
5 |
|
6 | var _typeof2 = require('babel-runtime/helpers/typeof');
|
7 |
|
8 | var _typeof3 = _interopRequireDefault(_typeof2);
|
9 |
|
10 | exports.hasClass = hasClass;
|
11 | exports.addClass = addClass;
|
12 | exports.removeClass = removeClass;
|
13 | exports.toggleClass = toggleClass;
|
14 | exports.getNodeHozWhitespace = getNodeHozWhitespace;
|
15 | exports.getStyle = getStyle;
|
16 | exports.setStyle = setStyle;
|
17 | exports.scrollbar = scrollbar;
|
18 | exports.hasScroll = hasScroll;
|
19 | exports.getOffset = getOffset;
|
20 | exports.getPixels = getPixels;
|
21 | exports.getClosest = getClosest;
|
22 | exports.getMatches = getMatches;
|
23 | exports.saveRef = saveRef;
|
24 |
|
25 | var _string = require('./string');
|
26 |
|
27 | var _object = require('./object');
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var hasDOM = exports.hasDOM = typeof window !== 'undefined' && !!window.document && !!document.createElement;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function hasClass(node, className) {
|
47 |
|
48 | if (!hasDOM || !node) {
|
49 | return false;
|
50 | }
|
51 |
|
52 | if (node.classList) {
|
53 | return node.classList.contains(className);
|
54 | } else {
|
55 | return node.className.indexOf(className) > -1;
|
56 | }
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function addClass(node, className, _force) {
|
68 |
|
69 | if (!hasDOM || !node) {
|
70 | return;
|
71 | }
|
72 |
|
73 | if (node.classList) {
|
74 | node.classList.add(className);
|
75 | } else if (_force === true || !hasClass(node, className)) {
|
76 | node.className += ' ' + className;
|
77 | }
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function removeClass(node, className, _force) {
|
89 |
|
90 | if (!hasDOM || !node) {
|
91 | return;
|
92 | }
|
93 |
|
94 | if (node.classList) {
|
95 | node.classList.remove(className);
|
96 | } else if (_force === true || hasClass(node, className)) {
|
97 | node.className = node.className.replace(className, '').replace(/\s+/g, ' ').trim();
|
98 | }
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function toggleClass(node, className) {
|
111 |
|
112 | if (!hasDOM || !node) {
|
113 | return false;
|
114 | }
|
115 |
|
116 | if (node.classList) {
|
117 | return node.classList.toggle(className);
|
118 | } else {
|
119 | var flag = hasClass(node, className);
|
120 | flag ? removeClass(node, className, true) : addClass(node, className, true);
|
121 |
|
122 | return !flag;
|
123 | }
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | var matches = exports.matches = function () {
|
136 | var matchesFn = null;
|
137 |
|
138 | if (hasDOM) {
|
139 | var _body = document.body || document.head;
|
140 | matchesFn = _body.matches ? 'matches' : _body.webkitMatchesSelector ? 'webkitMatchesSelector' : _body.msMatchesSelector ? 'msMatchesSelector' : _body.mozMatchesSelector ? 'mozMatchesSelector' : null;
|
141 | }
|
142 |
|
143 | return function (node, selector) {
|
144 | if (!hasDOM || !node) {
|
145 | return false;
|
146 | }
|
147 |
|
148 | return matchesFn ? node[matchesFn](selector) : false;
|
149 | };
|
150 | }();
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | function _getComputedStyle(node) {
|
159 | return node && node.nodeType === 1 ? window.getComputedStyle(node, null) : {};
|
160 | }
|
161 |
|
162 | var PIXEL_PATTERN = /margin|padding|width|height|max|min|offset|size|top/i;
|
163 | var removePixel = { left: 1, top: 1, right: 1, bottom: 1 };
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | function _getStyleValue(node, type, value) {
|
173 | type = type.toLowerCase();
|
174 |
|
175 | if (value === 'auto') {
|
176 | if (type === 'height') {
|
177 | return node.offsetHeight || 0;
|
178 | }
|
179 | if (type === 'width') {
|
180 | return node.offsetWidth || 0;
|
181 | }
|
182 | }
|
183 |
|
184 | if (!(type in removePixel)) {
|
185 |
|
186 | removePixel[type] = PIXEL_PATTERN.test(type);
|
187 | }
|
188 |
|
189 | return removePixel[type] ? parseFloat(value) || 0 : value;
|
190 | }
|
191 |
|
192 | var floatMap = { cssFloat: 1, styleFloat: 1, float: 1 };
|
193 |
|
194 | function getNodeHozWhitespace(node) {
|
195 | var paddingLeft = getStyle(node, 'paddingLeft');
|
196 | var paddingRight = getStyle(node, 'paddingRight');
|
197 | var marginLeft = getStyle(node, 'marginLeft');
|
198 | var marginRight = getStyle(node, 'marginRight');
|
199 | return paddingLeft + paddingRight + marginLeft + marginRight;
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | function getStyle(node, name) {
|
209 |
|
210 | if (!hasDOM || !node) {
|
211 | return null;
|
212 | }
|
213 |
|
214 | var style = _getComputedStyle(node);
|
215 |
|
216 |
|
217 | if (arguments.length === 1) {
|
218 | return style;
|
219 | }
|
220 |
|
221 |
|
222 | if ((0, _object.isPlainObject)(style)) {
|
223 | return null;
|
224 | }
|
225 |
|
226 | name = floatMap[name] ? 'cssFloat' in node.style ? 'cssFloat' : 'styleFloat' : name;
|
227 |
|
228 | return _getStyleValue(node, name, style.getPropertyValue((0, _string.hyphenate)(name)) || node.style[(0, _string.camelcase)(name)]);
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | function setStyle(node, name, value) {
|
247 |
|
248 | if (!hasDOM || !node) {
|
249 | return false;
|
250 | }
|
251 |
|
252 |
|
253 | if ((typeof name === 'undefined' ? 'undefined' : (0, _typeof3.default)(name)) === 'object' && arguments.length === 2) {
|
254 | (0, _object.each)(name, function (val, key) {
|
255 | return setStyle(node, key, val);
|
256 | });
|
257 | } else {
|
258 | name = floatMap[name] ? 'cssFloat' in node.style ? 'cssFloat' : 'styleFloat' : name;
|
259 | if (typeof value === 'number' && PIXEL_PATTERN.test(name)) {
|
260 | value = value + 'px';
|
261 | }
|
262 | node.style[(0, _string.camelcase)(name)] = value;
|
263 | }
|
264 | }
|
265 |
|
266 | var isScrollDisplay = function isScrollDisplay(element) {
|
267 | try {
|
268 | var scrollbarStyle = window.getComputedStyle(element, '::-webkit-scrollbar');
|
269 | return !scrollbarStyle || scrollbarStyle.getPropertyValue('display') !== 'none';
|
270 | } catch (e) {
|
271 |
|
272 | }
|
273 |
|
274 | return true;
|
275 | };
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | function scrollbar() {
|
282 | var scrollDiv = document.createElement('div');
|
283 | scrollDiv.className += 'just-to-get-scrollbar-size';
|
284 |
|
285 | setStyle(scrollDiv, {
|
286 | position: 'absolute',
|
287 | width: '100px',
|
288 | height: '100px',
|
289 | overflow: 'scroll',
|
290 | top: '-9999px'
|
291 | });
|
292 | document.body && document.body.appendChild(scrollDiv);
|
293 | var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
|
294 | var scrollbarHeight = scrollDiv.offsetHeight - scrollDiv.clientHeight;
|
295 | document.body.removeChild(scrollDiv);
|
296 |
|
297 | return {
|
298 | width: scrollbarWidth,
|
299 | height: scrollbarHeight
|
300 | };
|
301 | }
|
302 |
|
303 | function hasScroll(containerNode) {
|
304 |
|
305 | var overflow = getStyle(containerNode, 'overflow');
|
306 | if (overflow === 'hidden') {
|
307 | return false;
|
308 | }
|
309 |
|
310 | var parentNode = containerNode.parentNode;
|
311 |
|
312 | return parentNode && parentNode.scrollHeight > parentNode.clientHeight && scrollbar().width > 0 && isScrollDisplay(parentNode) && isScrollDisplay(containerNode);
|
313 | }
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | function getOffset(node) {
|
320 | var rect = node.getBoundingClientRect();
|
321 | var win = node.ownerDocument.defaultView;
|
322 | return {
|
323 | top: rect.top + win.pageYOffset,
|
324 | left: rect.left + win.pageXOffset
|
325 | };
|
326 | }
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | function getPixels(len) {
|
334 | var win = document.defaultView;
|
335 | if (typeof +len === 'number' && !isNaN(+len)) {
|
336 | return +len;
|
337 | }
|
338 |
|
339 | if (typeof len === 'string') {
|
340 | var PX_REG = /(\d+)px/;
|
341 | var VH_REG = /(\d+)vh/;
|
342 | if (Array.isArray(len.match(PX_REG))) {
|
343 | return +len.match(PX_REG)[1] || 0;
|
344 | }
|
345 |
|
346 | if (Array.isArray(len.match(VH_REG))) {
|
347 | var _1vh = win.innerHeight / 100;
|
348 | return +(len.match(VH_REG)[1] * _1vh) || 0;
|
349 | }
|
350 | }
|
351 |
|
352 | return 0;
|
353 | }
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | function getClosest(dom, selector) {
|
362 |
|
363 | if (!hasDOM || !dom) {
|
364 | return null;
|
365 | }
|
366 |
|
367 |
|
368 |
|
369 | if (!Element.prototype.closest) {
|
370 | if (!document.documentElement.contains(dom)) return null;
|
371 | do {
|
372 | if (getMatches(dom, selector)) return dom;
|
373 | dom = dom.parentElement;
|
374 | } while (dom !== null);
|
375 | } else {
|
376 | return dom.closest(selector);
|
377 | }
|
378 | return null;
|
379 | }
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 | function getMatches(dom, selector) {
|
388 |
|
389 | if (!hasDOM || !dom) {
|
390 | return null;
|
391 | }
|
392 |
|
393 |
|
394 | if (Element.prototype.matches) {
|
395 | return dom.matches(selector);
|
396 | } else if (Element.prototype.msMatchesSelector) {
|
397 | return dom.msMatchesSelector(selector);
|
398 | } else if (Element.prototype.webkitMatchesSelector) {
|
399 | return dom.webkitMatchesSelector(selector);
|
400 | }
|
401 |
|
402 | return null;
|
403 | }
|
404 |
|
405 | function saveRef(ref) {
|
406 | if (!ref) {
|
407 | return null;
|
408 | }
|
409 | return function (element) {
|
410 | if (typeof ref === 'string') {
|
411 | throw new Error('can not set ref string for ' + ref);
|
412 | } else if (typeof ref === 'function') {
|
413 | ref(element);
|
414 | } else if (Object.prototype.hasOwnProperty.call(ref, 'current')) {
|
415 | ref.current = element;
|
416 | }
|
417 | };
|
418 | } |
\ | No newline at end of file |