UNPKG

2.46 kBJavaScriptView Raw
1import _Object$keys from 'babel-runtime/core-js/object/keys';
2/*
3Copyright 2013-2015 ASIAL CORPORATION
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19import util from '../ons/util';
20
21/**
22 * Add vendor prefix.
23 *
24 * @param {String} name
25 * @return {String}
26 */
27var prefix = function () {
28 var styles = window.getComputedStyle(document.documentElement, '');
29 var prefix = (Array.prototype.slice.call(styles).join('').match(/-(moz|webkit|ms)-/) || styles.OLink === '' && ['', 'o'])[1];
30
31 return function (name) {
32 return '-' + prefix + '-' + util.hyphenate(name);
33 };
34}();
35
36/**
37 * Minimal utility library for manipulating element's style.
38 * Set element's style.
39 *
40 * @param {Element} element
41 * @param {Object} styles
42 * @return {Element}
43 */
44var styler = function styler(element, style) {
45 _Object$keys(style).forEach(function (key) {
46 if (key in element.style) {
47 element.style[key] = style[key];
48 } else if (prefix(key) in element.style) {
49 element.style[prefix(key)] = style[key];
50 } else {
51 util.warn('No such style property: ' + key);
52 }
53 });
54 return element;
55};
56
57/**
58 * @param {Element} element
59 * @param {String} styles Space-separated CSS properties to remove
60 */
61styler.clear = function (element) {
62 var styles = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
63
64 var clearlist = styles.split(/\s+/).reduce(function (r, s) {
65 return r.concat([util.hyphenate(s), prefix(s)]);
66 }, []),
67 keys = [];
68
69 var _loop = function _loop(i) {
70 var key = element.style[i];
71 if (clearlist.length === 0 || clearlist.some(function (s) {
72 return key.indexOf(s) === 0;
73 })) {
74 keys.push(key); // Store the key to fix Safari style indexes
75 }
76 };
77
78 for (var i = element.style.length - 1; i >= 0; i--) {
79 _loop(i);
80 }
81
82 keys.forEach(function (key) {
83 return element.style[key] = '';
84 });
85 element.getAttribute('style') === '' && element.removeAttribute('style');
86};
87
88export default styler;
\No newline at end of file