Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

styler.js 2.4KB

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