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

modifier-util.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import _Object$keys from 'babel-runtime/core-js/object/keys';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _createClass from 'babel-runtime/helpers/createClass';
  4. /*
  5. Copyright 2013-2015 ASIAL CORPORATION
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. import util from '../util.js';
  17. import autoStyle from '../autostyle.js';
  18. var ModifierUtil = function () {
  19. function ModifierUtil() {
  20. _classCallCheck(this, ModifierUtil);
  21. }
  22. _createClass(ModifierUtil, null, [{
  23. key: 'diff',
  24. /**
  25. * @param {String} last
  26. * @param {String} current
  27. */
  28. value: function diff(last, current) {
  29. last = makeDict(('' + last).trim());
  30. current = makeDict(('' + current).trim());
  31. var removed = _Object$keys(last).reduce(function (result, token) {
  32. if (!current[token]) {
  33. result.push(token);
  34. }
  35. return result;
  36. }, []);
  37. var added = _Object$keys(current).reduce(function (result, token) {
  38. if (!last[token]) {
  39. result.push(token);
  40. }
  41. return result;
  42. }, []);
  43. return { added: added, removed: removed };
  44. function makeDict(modifier) {
  45. var dict = {};
  46. ModifierUtil.split(modifier).forEach(function (token) {
  47. return dict[token] = token;
  48. });
  49. return dict;
  50. }
  51. }
  52. /**
  53. * @param {Object} diff
  54. * @param {Array} diff.removed
  55. * @param {Array} diff.added
  56. * @param {Object} classList
  57. * @param {String} template
  58. */
  59. }, {
  60. key: 'applyDiffToClassList',
  61. value: function applyDiffToClassList(diff, classList, template) {
  62. diff.added.map(function (modifier) {
  63. return template.replace(/\*/g, modifier);
  64. }).forEach(function (klass) {
  65. return klass.split(/\s+/).forEach(function (k) {
  66. return classList.add(k);
  67. });
  68. });
  69. diff.removed.map(function (modifier) {
  70. return template.replace(/\*/g, modifier);
  71. }).forEach(function (klass) {
  72. return klass.split(/\s+/).forEach(function (k) {
  73. return classList.remove(k);
  74. });
  75. });
  76. }
  77. /**
  78. * @param {Object} diff
  79. * @param {Array} diff.removed
  80. * @param {Array} diff.added
  81. * @param {HTMLElement} element
  82. * @param {Object} scheme
  83. */
  84. }, {
  85. key: 'applyDiffToElement',
  86. value: function applyDiffToElement(diff, element, scheme) {
  87. _Object$keys(scheme).forEach(function (selector) {
  88. var targetElements = !selector || util.match(element, selector) ? [element] : Array.prototype.filter.call(element.querySelectorAll(selector), function (targetElement) {
  89. return !util.findParent(targetElement, element.tagName, function (parent) {
  90. return parent === element;
  91. });
  92. });
  93. for (var i = 0; i < targetElements.length; i++) {
  94. ModifierUtil.applyDiffToClassList(diff, targetElements[i].classList, scheme[selector]);
  95. }
  96. });
  97. }
  98. /**
  99. * @param {String} last
  100. * @param {String} current
  101. * @param {HTMLElement} element
  102. * @param {Object} scheme
  103. */
  104. }, {
  105. key: 'onModifierChanged',
  106. value: function onModifierChanged(last, current, element, scheme) {
  107. ModifierUtil.applyDiffToElement(ModifierUtil.diff(last, current), element, scheme);
  108. autoStyle.restoreModifier(element);
  109. }
  110. }, {
  111. key: 'refresh',
  112. value: function refresh(element, scheme) {
  113. ModifierUtil.applyDiffToElement(ModifierUtil.diff('', element.getAttribute('modifier') || ''), element, scheme);
  114. }
  115. /**
  116. * @param {HTMLElement} element
  117. * @param {Object} scheme
  118. */
  119. }, {
  120. key: 'initModifier',
  121. value: function initModifier(element, scheme) {
  122. var modifier = element.getAttribute('modifier');
  123. if (typeof modifier !== 'string') {
  124. return;
  125. }
  126. ModifierUtil.applyDiffToElement({
  127. removed: [],
  128. added: ModifierUtil.split(modifier)
  129. }, element, scheme);
  130. }
  131. }, {
  132. key: 'split',
  133. value: function split(modifier) {
  134. if (typeof modifier !== 'string') {
  135. return [];
  136. }
  137. return modifier.trim().split(/ +/).filter(function (token) {
  138. return token !== '';
  139. });
  140. }
  141. /**
  142. * Add modifier token to an element.
  143. */
  144. }, {
  145. key: 'addModifier',
  146. value: function addModifier(element, modifierToken) {
  147. if (!element.hasAttribute('modifier')) {
  148. element.setAttribute('modifier', modifierToken);
  149. } else {
  150. var tokens = ModifierUtil.split(element.getAttribute('modifier'));
  151. if (tokens.indexOf(modifierToken) == -1) {
  152. tokens.push(modifierToken);
  153. element.setAttribute('modifier', tokens.join(' '));
  154. }
  155. }
  156. }
  157. /**
  158. * Remove modifier token from an element.
  159. */
  160. }, {
  161. key: 'removeModifier',
  162. value: function removeModifier(element, modifierToken) {
  163. if (element.hasAttribute('modifier')) {
  164. var tokens = ModifierUtil.split(element.getAttribute('modifier'));
  165. var index = tokens.indexOf(modifierToken);
  166. if (index !== -1) {
  167. tokens.splice(index, 1);
  168. element.setAttribute('modifier', tokens.join(' '));
  169. }
  170. }
  171. }
  172. }]);
  173. return ModifierUtil;
  174. }();
  175. export default ModifierUtil;