1 | var docStyle = window.document.documentElement.style;
|
2 | export function getContainer(domId) {
|
3 | var $dom = domId;
|
4 |
|
5 | if (typeof domId === 'string') {
|
6 | $dom = document.getElementById(domId);
|
7 | }
|
8 |
|
9 | return $dom;
|
10 | }
|
11 | export function trim(str) {
|
12 | return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
|
13 | }
|
14 | export function splitWords(str) {
|
15 | return trim(str).split(/\s+/);
|
16 | }
|
17 |
|
18 | function testProp(props) {
|
19 | if (!docStyle) {
|
20 | return props[0];
|
21 | }
|
22 |
|
23 | for (var i in props) {
|
24 | if (props[i] && props[i] in docStyle) {
|
25 | return props[i];
|
26 | }
|
27 | }
|
28 |
|
29 | return props[0];
|
30 | }
|
31 |
|
32 | export function create(tagName, className, container) {
|
33 | var el = document.createElement(tagName);
|
34 | el.className = className || '';
|
35 |
|
36 | if (container) {
|
37 | container.appendChild(el);
|
38 | }
|
39 |
|
40 | return el;
|
41 | }
|
42 | export function remove(el) {
|
43 | var parent = el.parentNode;
|
44 |
|
45 | if (parent) {
|
46 | parent.removeChild(el);
|
47 | }
|
48 | }
|
49 | export function addClass(el, name) {
|
50 | if (el.classList !== undefined) {
|
51 | var classes = splitWords(name);
|
52 |
|
53 | for (var i = 0, len = classes.length; i < len; i++) {
|
54 | el.classList.add(classes[i]);
|
55 | }
|
56 | } else if (!hasClass(el, name)) {
|
57 | var className = getClass(el);
|
58 | setClass(el, (className ? className + ' ' : '') + name);
|
59 | }
|
60 | }
|
61 | export function removeClass(el, name) {
|
62 | if (el.classList !== undefined) {
|
63 | el.classList.remove(name);
|
64 | } else {
|
65 | setClass(el, trim((' ' + getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
|
66 | }
|
67 | }
|
68 | export function hasClass(el, name) {
|
69 | if (el.classList !== undefined) {
|
70 | return el.classList.contains(name);
|
71 | }
|
72 |
|
73 | var className = getClass(el);
|
74 | return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className);
|
75 | }
|
76 | export function setClass(el, name) {
|
77 | if (el instanceof HTMLElement) {
|
78 | el.className = name;
|
79 | } else {
|
80 | el.className.baseVal = name;
|
81 | }
|
82 | }
|
83 | export function getClass(el) {
|
84 | if (el instanceof SVGElement) {
|
85 | el = el.correspondingElement;
|
86 | }
|
87 |
|
88 | return el.className.baseVal === undefined ? el.className : el.className.baseVal;
|
89 | }
|
90 | export function empty(el) {
|
91 | while (el && el.firstChild) {
|
92 | el.removeChild(el.firstChild);
|
93 | }
|
94 | }
|
95 | var transformProp = testProp(['transform', 'WebkitTransform']);
|
96 | export function setTransform(el, value) {
|
97 | el.style[transformProp] = value;
|
98 | }
|
99 | export function triggerResize() {
|
100 | if (typeof Event === 'function') {
|
101 | window.dispatchEvent(new Event('resize'));
|
102 | } else {
|
103 | var evt = window.document.createEvent('UIEvents');
|
104 | evt.initUIEvent('resize', true, false, window, 0);
|
105 | window.dispatchEvent(evt);
|
106 | }
|
107 | }
|
108 | export function printCanvas(canvas) {
|
109 | var css = ['padding: ' + (canvas.height / 2 - 8) + 'px ' + canvas.width / 2 + 'px;', 'line-height: ' + canvas.height + 'px;', 'background-image: url(' + canvas.toDataURL() + ');'];
|
110 | console.log('%c\n', css.join(''));
|
111 | }
|
112 |
|
\ | No newline at end of file |