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

index.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. import _Array$from from 'babel-runtime/core-js/array/from';
  2. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  3. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  4. import _createClass from 'babel-runtime/helpers/createClass';
  5. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  6. import _inherits from 'babel-runtime/helpers/inherits';
  7. /*
  8. Copyright 2013-2015 ASIAL CORPORATION
  9. Licensed under the Apache License, Version 2.0 (the "License");
  10. you may not use this file except in compliance with the License.
  11. You may obtain a copy of the License at
  12. http://www.apache.org/licenses/LICENSE-2.0
  13. Unless required by applicable law or agreed to in writing, software
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. */
  19. import onsElements from '../../ons/elements';
  20. import animit from '../../ons/animit';
  21. import util from '../../ons/util';
  22. import styler from '../../ons/styler';
  23. import autoStyle from '../../ons/autostyle';
  24. import ModifierUtil from '../../ons/internal/modifier-util';
  25. import AnimatorFactory from '../../ons/internal/animator-factory';
  26. import { ListItemAnimator, SlideListItemAnimator } from './animator';
  27. import BaseElement from '../base/base-element';
  28. import contentReady from '../../ons/content-ready';
  29. var defaultClassName = 'list-item';
  30. var scheme = {
  31. '.list-item': 'list-item--*',
  32. '.list-item__left': 'list-item--*__left',
  33. '.list-item__center': 'list-item--*__center',
  34. '.list-item__right': 'list-item--*__right',
  35. '.list-item__label': 'list-item--*__label',
  36. '.list-item__title': 'list-item--*__title',
  37. '.list-item__subtitle': 'list-item--*__subtitle',
  38. '.list-item__thumbnail': 'list-item--*__thumbnail',
  39. '.list-item__icon': 'list-item--*__icon'
  40. };
  41. var _animatorDict = {
  42. 'default': SlideListItemAnimator,
  43. 'none': ListItemAnimator
  44. };
  45. /**
  46. * @element ons-list-item
  47. * @category list
  48. * @modifier tappable
  49. * [en]Make the list item change appearance when it's tapped. On iOS it is better to use the "tappable" and "tap-background-color" attribute for better behavior when scrolling.[/en]
  50. * [ja]タップやクリックした時に効果が表示されるようになります。[/ja]
  51. * @modifier chevron
  52. * [en]Display a chevron at the right end of the list item and make it change appearance when tapped.[/en]
  53. * [ja][/ja]
  54. * @modifier longdivider
  55. * [en]Displays a long horizontal divider between items.[/en]
  56. * [ja][/ja]
  57. * @modifier nodivider
  58. * [en]Removes the divider between list items.[/en]
  59. * [ja][/ja]
  60. * @modifier material
  61. * [en]Display a Material Design list item.[/en]
  62. * [ja][/ja]
  63. * @description
  64. * [en]
  65. * Component that represents each item in a list. The list item is composed of four parts that are represented with the `left`, `center`, `right` and `expandable-content` classes. These classes can be used to ensure that the content of the list items is properly aligned.
  66. *
  67. * ```
  68. * <ons-list-item>
  69. * <div class="left">Left</div>
  70. * <div class="center">Center</div>
  71. * <div class="right">Right</div>
  72. * <div class="expandable-content">Expandable content</div>
  73. * </ons-list-item>
  74. * ```
  75. *
  76. * There are also a number of classes (prefixed with `list-item__*`) that help when putting things like icons and thumbnails into the list items.
  77. * [/en]
  78. * [ja][/ja]
  79. * @seealso ons-list
  80. * [en]ons-list component[/en]
  81. * [ja]ons-listコンポーネント[/ja]
  82. * @seealso ons-list-header
  83. * [en]ons-list-header component[/en]
  84. * [ja]ons-list-headerコンポーネント[/ja]
  85. * @codepen yxcCt
  86. * @tutorial vanilla/Reference/list
  87. * @example
  88. * <ons-list-item>
  89. * <div class="left">
  90. * <ons-icon icon="md-face" class="list-item__icon"></ons-icon>
  91. * </div>
  92. * <div class="center">
  93. * <div class="list-item__title">Title</div>
  94. * <div class="list-item__subtitle">Subtitle</div>
  95. * </div>
  96. * <div class="right">
  97. * <ons-switch></ons-switch>
  98. * </div>
  99. * </ons-list-item>
  100. */
  101. var ListItemElement = function (_BaseElement) {
  102. _inherits(ListItemElement, _BaseElement);
  103. /**
  104. * @attribute modifier
  105. * @type {String}
  106. * @description
  107. * [en]The appearance of the list item.[/en]
  108. * [ja]各要素の表現を指定します。[/ja]
  109. */
  110. /**
  111. * @attribute lock-on-drag
  112. * @type {String}
  113. * @description
  114. * [en]Prevent vertical scrolling when the user drags horizontally.[/en]
  115. * [ja]この属性があると、ユーザーがこの要素を横方向にドラッグしている時に、縦方向のスクロールが起きないようになります。[/ja]
  116. */
  117. /**
  118. * @attribute tappable
  119. * @type {Boolean}
  120. * @description
  121. * [en]Makes the element react to taps. `prevent-tap` attribute can be added to child elements like buttons or inputs to prevent this effect. `ons-*` elements are ignored by default.[/en]
  122. * [ja][/ja]
  123. */
  124. /**
  125. * @attribute tap-background-color
  126. * @type {Color}
  127. * @description
  128. * [en] Changes the background color when tapped. For this to work, the attribute "tappable" needs to be set. The default color is "#d9d9d9". It will display as a ripple effect on Android.[/en]
  129. * [ja][/ja]
  130. */
  131. /**
  132. * @attribute expandable
  133. * @type {Boolean}
  134. * @description
  135. * [en]Makes the element able to be expanded to reveal extra content. For this to work, the expandable content must be defined in `div.expandable-content`.[/en]
  136. * [ja][/ja]
  137. */
  138. /**
  139. * @attribute animation
  140. * @type {String}
  141. * @default default
  142. * @description
  143. * [en]The animation used when showing and hiding the expandable content. Can be either `"default"` or `"none"`.[/en]
  144. * [ja][/ja]
  145. */
  146. function ListItemElement() {
  147. _classCallCheck(this, ListItemElement);
  148. var _this = _possibleConstructorReturn(this, (ListItemElement.__proto__ || _Object$getPrototypeOf(ListItemElement)).call(this));
  149. _this._animatorFactory = _this._updateAnimatorFactory();
  150. _this.toggleExpansion = _this.toggleExpansion.bind(_this);
  151. // Elements ignored when tapping
  152. var re = /^ons-(?!col$|row$|if$)/i;
  153. _this._shouldIgnoreTap = function (e) {
  154. return e.hasAttribute('prevent-tap') || re.test(e.tagName);
  155. };
  156. // show and hide functions for Vue hidable mixin
  157. _this.show = _this.showExpansion;
  158. _this.hide = _this.hideExpansion;
  159. contentReady(_this, function () {
  160. _this._compile();
  161. });
  162. return _this;
  163. }
  164. /**
  165. * Compiles the list item.
  166. *
  167. * Various elements are allowed in the body of a list item:
  168. *
  169. * - div.left, div.right, and div.center are allowed as direct children
  170. * - if div.center is not defined, anything that isn't div.left, div.right or div.expandable-content will be put in a div.center
  171. * - if div.center is defined, anything that isn't div.left, div.right or div.expandable-content will be ignored
  172. * - if list item has expandable attribute:
  173. * - div.expandable-content is allowed as a direct child
  174. * - div.top is allowed as direct child
  175. * - if div.top is defined, anything that isn't div.expandable-content should be inside div.top - anything else will be ignored
  176. * - if div.right is not defined, a div.right will be created with a drop-down chevron
  177. *
  178. * See the tests for examples.
  179. */
  180. _createClass(ListItemElement, [{
  181. key: '_compile',
  182. value: function _compile() {
  183. autoStyle.prepare(this);
  184. this.classList.add(defaultClassName);
  185. var top = void 0,
  186. expandableContent = void 0;
  187. var topContent = [];
  188. _Array$from(this.childNodes).forEach(function (node) {
  189. if (node.nodeType !== Node.ELEMENT_NODE) {
  190. topContent.push(node);
  191. } else if (node.classList.contains('top')) {
  192. top = node;
  193. } else if (node.classList.contains('expandable-content')) {
  194. expandableContent = node;
  195. } else {
  196. topContent.push(node);
  197. }
  198. if (node.nodeName !== 'ONS-RIPPLE') {
  199. node.remove();
  200. }
  201. });
  202. topContent = top ? _Array$from(top.childNodes) : topContent;
  203. var left = void 0,
  204. right = void 0,
  205. center = void 0;
  206. var centerContent = [];
  207. topContent.forEach(function (node) {
  208. if (node.nodeType !== Node.ELEMENT_NODE) {
  209. centerContent.push(node);
  210. } else if (node.classList.contains('left')) {
  211. left = node;
  212. } else if (node.classList.contains('right')) {
  213. right = node;
  214. } else if (node.classList.contains('center')) {
  215. center = node;
  216. } else {
  217. centerContent.push(node);
  218. }
  219. });
  220. if (this.hasAttribute('expandable')) {
  221. this.classList.add('list-item--expandable');
  222. if (!top) {
  223. top = document.createElement('div');
  224. top.classList.add('top');
  225. }
  226. top.classList.add('list-item__top');
  227. this.appendChild(top);
  228. this._top = top;
  229. if (expandableContent) {
  230. expandableContent.classList.add('list-item__expandable-content');
  231. this.appendChild(expandableContent);
  232. }
  233. if (!right) {
  234. right = document.createElement('div');
  235. right.classList.add('list-item__right', 'right');
  236. // We cannot use a pseudo-element for this chevron, as we cannot animate it using
  237. // JS. So, we make a chevron span instead.
  238. var chevron = document.createElement('span');
  239. chevron.classList.add('list-item__expand-chevron');
  240. right.appendChild(chevron);
  241. }
  242. } else {
  243. top = this;
  244. }
  245. if (!center) {
  246. center = document.createElement('div');
  247. center.classList.add('center');
  248. centerContent.forEach(function (node) {
  249. return center.appendChild(node);
  250. });
  251. }
  252. center.classList.add('list-item__center');
  253. top.appendChild(center);
  254. if (left) {
  255. left.classList.add('list-item__left');
  256. top.appendChild(left);
  257. }
  258. if (right) {
  259. right.classList.add('list-item__right');
  260. top.appendChild(right);
  261. }
  262. util.updateRipple(this);
  263. ModifierUtil.initModifier(this, scheme);
  264. }
  265. /**
  266. * @method showExpansion
  267. * @signature showExpansion()
  268. * @description
  269. * [en]Show the expandable content if the element is expandable.[/en]
  270. * [ja][/ja]
  271. */
  272. }, {
  273. key: 'showExpansion',
  274. value: function showExpansion() {
  275. var _this2 = this;
  276. if (this.hasAttribute('expandable') && !this._expanding) {
  277. this.expanded = true;
  278. this._expanding = true;
  279. var animator = this._animatorFactory.newAnimator();
  280. animator.showExpansion(this, function () {
  281. _this2.classList.add('expanded');
  282. _this2._expanding = false;
  283. });
  284. }
  285. }
  286. /**
  287. * @method hideExpansion
  288. * @signature hideExpansion()
  289. * @description
  290. * [en]Hide the expandable content if the element expandable.[/en]
  291. * [ja][/ja]
  292. */
  293. }, {
  294. key: 'hideExpansion',
  295. value: function hideExpansion() {
  296. var _this3 = this;
  297. if (this.hasAttribute('expandable') && !this._expanding) {
  298. this.expanded = false;
  299. this._expanding = true;
  300. var animator = this._animatorFactory.newAnimator();
  301. animator.hideExpansion(this, function () {
  302. _this3.classList.remove('expanded');
  303. _this3._expanding = false;
  304. });
  305. }
  306. }
  307. }, {
  308. key: 'toggleExpansion',
  309. value: function toggleExpansion() {
  310. this.classList.contains('expanded') ? this.hideExpansion() : this.showExpansion();
  311. this.dispatchEvent(new Event('expansion'));
  312. }
  313. }, {
  314. key: '_updateAnimatorFactory',
  315. value: function _updateAnimatorFactory() {
  316. return new AnimatorFactory({
  317. animators: _animatorDict,
  318. baseClass: ListItemAnimator,
  319. baseClassName: 'ListItemAnimator',
  320. defaultAnimation: this.getAttribute('animation') || 'default'
  321. });
  322. }
  323. }, {
  324. key: 'attributeChangedCallback',
  325. value: function attributeChangedCallback(name, last, current) {
  326. switch (name) {
  327. case 'class':
  328. util.restoreClass(this, defaultClassName, scheme);
  329. break;
  330. case 'modifier':
  331. ModifierUtil.onModifierChanged(last, current, this, scheme);
  332. break;
  333. case 'ripple':
  334. util.updateRipple(this);
  335. break;
  336. case 'animation':
  337. this._animatorFactory = this._updateAnimatorFactory();
  338. break;
  339. }
  340. }
  341. }, {
  342. key: 'connectedCallback',
  343. value: function connectedCallback() {
  344. var _this4 = this;
  345. contentReady(this, function () {
  346. _this4._setupListeners(true);
  347. _this4._originalBackgroundColor = _this4.style.backgroundColor;
  348. _this4.tapped = false;
  349. });
  350. }
  351. }, {
  352. key: 'disconnectedCallback',
  353. value: function disconnectedCallback() {
  354. this._setupListeners(false);
  355. }
  356. }, {
  357. key: '_setupListeners',
  358. value: function _setupListeners(add) {
  359. var action = (add ? 'add' : 'remove') + 'EventListener';
  360. util[action](this, 'touchstart', this._onTouch, { passive: true });
  361. util[action](this, 'touchmove', this._onRelease, { passive: true });
  362. this[action]('touchcancel', this._onRelease);
  363. this[action]('touchend', this._onRelease);
  364. this[action]('touchleave', this._onRelease);
  365. this[action]('drag', this._onDrag);
  366. this[action]('mousedown', this._onTouch);
  367. this[action]('mouseup', this._onRelease);
  368. this[action]('mouseout', this._onRelease);
  369. if (this._top) {
  370. this._top[action]('click', this.toggleExpansion);
  371. }
  372. }
  373. }, {
  374. key: '_onDrag',
  375. value: function _onDrag(event) {
  376. var gesture = event.gesture;
  377. // Prevent vertical scrolling if the users pans left or right.
  378. if (this.hasAttribute('lock-on-drag') && ['left', 'right'].indexOf(gesture.direction) > -1) {
  379. gesture.preventDefault();
  380. }
  381. }
  382. }, {
  383. key: '_onTouch',
  384. value: function _onTouch(e) {
  385. var _this5 = this;
  386. if (this.tapped || this !== e.target && (this._shouldIgnoreTap(e.target) || util.findParent(e.target, this._shouldIgnoreTap, function (p) {
  387. return p === _this5;
  388. }))) {
  389. return; // Ignore tap
  390. }
  391. this.tapped = true;
  392. var touchStyle = { transition: 'background-color 0.0s linear 0.02s, box-shadow 0.0s linear 0.02s' };
  393. if (this.hasAttribute('tappable')) {
  394. if (this.style.backgroundColor) {
  395. this._originalBackgroundColor = this.style.backgroundColor;
  396. }
  397. touchStyle.backgroundColor = this.getAttribute('tap-background-color') || '#d9d9d9';
  398. touchStyle.boxShadow = '0px -1px 0px 0px ' + touchStyle.backgroundColor;
  399. }
  400. styler(this, touchStyle);
  401. }
  402. }, {
  403. key: '_onRelease',
  404. value: function _onRelease() {
  405. this.tapped = false;
  406. this.style.backgroundColor = this._originalBackgroundColor || '';
  407. styler.clear(this, 'transition boxShadow');
  408. }
  409. }, {
  410. key: 'expandableContent',
  411. get: function get() {
  412. return this.querySelector('.list-item__expandable-content');
  413. }
  414. }, {
  415. key: 'expandChevron',
  416. get: function get() {
  417. return this.querySelector('.list-item__expand-chevron');
  418. }
  419. }], [{
  420. key: 'observedAttributes',
  421. get: function get() {
  422. return ['modifier', 'class', 'ripple', 'animation'];
  423. }
  424. }]);
  425. return ListItemElement;
  426. }(BaseElement);
  427. export default ListItemElement;
  428. onsElements.ListItem = ListItemElement;
  429. customElements.define('ons-list-item', ListItemElement);