1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.getContainer = getContainer;
|
9 | exports.trim = trim;
|
10 | exports.splitWords = splitWords;
|
11 | exports.create = create;
|
12 | exports.remove = remove;
|
13 | exports.addClass = addClass;
|
14 | exports.removeClass = removeClass;
|
15 | exports.hasClass = hasClass;
|
16 | exports.setClass = setClass;
|
17 | exports.getClass = getClass;
|
18 | exports.empty = empty;
|
19 | exports.setTransform = setTransform;
|
20 | exports.triggerResize = triggerResize;
|
21 | exports.printCanvas = printCanvas;
|
22 | exports.getViewPortScale = getViewPortScale;
|
23 | exports.DPR = void 0;
|
24 |
|
25 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
26 |
|
27 | var docStyle = window.document.documentElement.style;
|
28 |
|
29 | function getContainer(domId) {
|
30 | var $dom = domId;
|
31 |
|
32 | if (typeof domId === 'string') {
|
33 | $dom = document.getElementById(domId);
|
34 | }
|
35 |
|
36 | return $dom;
|
37 | }
|
38 |
|
39 | function trim(str) {
|
40 | return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
|
41 | }
|
42 |
|
43 | function splitWords(str) {
|
44 | return trim(str).split(/\s+/);
|
45 | }
|
46 |
|
47 | function testProp(props) {
|
48 | if (!docStyle) {
|
49 | return props[0];
|
50 | }
|
51 |
|
52 | for (var i in props) {
|
53 | if (props[i] && props[i] in docStyle) {
|
54 | return props[i];
|
55 | }
|
56 | }
|
57 |
|
58 | return props[0];
|
59 | }
|
60 |
|
61 | function create(tagName, className, container) {
|
62 | var el = document.createElement(tagName);
|
63 | el.className = className || '';
|
64 |
|
65 | if (container) {
|
66 | container.appendChild(el);
|
67 | }
|
68 |
|
69 | return el;
|
70 | }
|
71 |
|
72 | function remove(el) {
|
73 | var parent = el.parentNode;
|
74 |
|
75 | if (parent) {
|
76 | parent.removeChild(el);
|
77 | }
|
78 | }
|
79 |
|
80 | function addClass(el, name) {
|
81 | if (el.classList !== undefined) {
|
82 | var classes = splitWords(name);
|
83 |
|
84 | for (var i = 0, len = classes.length; i < len; i++) {
|
85 | el.classList.add(classes[i]);
|
86 | }
|
87 | } else if (!hasClass(el, name)) {
|
88 | var className = getClass(el);
|
89 | setClass(el, (className ? className + ' ' : '') + name);
|
90 | }
|
91 | }
|
92 |
|
93 | function removeClass(el, name) {
|
94 | if (el.classList !== undefined) {
|
95 | el.classList.remove(name);
|
96 | } else {
|
97 | setClass(el, trim((' ' + getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
|
98 | }
|
99 | }
|
100 |
|
101 | function hasClass(el, name) {
|
102 | if (el.classList !== undefined) {
|
103 | return el.classList.contains(name);
|
104 | }
|
105 |
|
106 | var className = getClass(el);
|
107 | return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className);
|
108 | }
|
109 |
|
110 | function setClass(el, name) {
|
111 | if (el instanceof HTMLElement) {
|
112 | el.className = name;
|
113 | } else {
|
114 | el.className.baseVal = name;
|
115 | }
|
116 | }
|
117 |
|
118 | function getClass(el) {
|
119 | if (el instanceof SVGElement) {
|
120 | el = el.correspondingElement;
|
121 | }
|
122 |
|
123 | return el.className.baseVal === undefined ? el.className : el.className.baseVal;
|
124 | }
|
125 |
|
126 | function empty(el) {
|
127 | while (el && el.firstChild) {
|
128 | el.removeChild(el.firstChild);
|
129 | }
|
130 | }
|
131 |
|
132 | var transformProp = testProp(['transform', 'WebkitTransform']);
|
133 |
|
134 | function setTransform(el, value) {
|
135 | el.style[transformProp] = value;
|
136 | }
|
137 |
|
138 | function triggerResize() {
|
139 | if (typeof Event === 'function') {
|
140 | window.dispatchEvent(new Event('resize'));
|
141 | } else {
|
142 | var evt = window.document.createEvent('UIEvents');
|
143 | evt.initUIEvent('resize', true, false, window, 0);
|
144 | window.dispatchEvent(evt);
|
145 | }
|
146 | }
|
147 |
|
148 | function printCanvas(canvas) {
|
149 | var css = ['padding: ' + (canvas.height / 2 - 8) + 'px ' + canvas.width / 2 + 'px;', 'line-height: ' + canvas.height + 'px;', 'background-image: url(' + canvas.toDataURL() + ');'];
|
150 | console.log('%c\n', css.join(''));
|
151 | }
|
152 |
|
153 | function getViewPortScale() {
|
154 | var _content;
|
155 |
|
156 | var meta = document.querySelector('meta[name="viewport"]');
|
157 |
|
158 | if (!meta) {
|
159 | return 1;
|
160 | }
|
161 |
|
162 | var contentItems = (_content = meta.content) === null || _content === void 0 ? void 0 : _content.split(',');
|
163 | var scale = contentItems.find(function (item) {
|
164 | var _item$split = item.split('='),
|
165 | _item$split2 = (0, _slicedToArray2.default)(_item$split, 2),
|
166 | key = _item$split2[0],
|
167 | value = _item$split2[1];
|
168 |
|
169 | return key === 'initial-scale';
|
170 | });
|
171 | return scale ? scale.split('=')[1] * 1 : 1;
|
172 | }
|
173 |
|
174 | var DPR = getViewPortScale() < 1 ? 1 : window.devicePixelRatio;
|
175 | exports.DPR = DPR;
|
176 |
|
\ | No newline at end of file |