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

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _createClass from 'babel-runtime/helpers/createClass';
  4. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  5. import _inherits from 'babel-runtime/helpers/inherits';
  6. /*
  7. Copyright 2013-2015 ASIAL CORPORATION
  8. Licensed under the Apache License, Version 2.0 (the "License");
  9. you may not use this file except in compliance with the License.
  10. You may obtain a copy of the License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. */
  18. import onsElements from '../../ons/elements';
  19. import util from '../../ons/util';
  20. import internal from '../../ons/internal';
  21. import BaseElement from '../base/base-element';
  22. import Animator from './animator-css';
  23. import contentReady from '../../ons/content-ready';
  24. import ModifierUtil from '../../ons/internal/modifier-util';
  25. var defaultClassName = 'ripple';
  26. var scheme = {
  27. '': 'ripple--*',
  28. '.ripple__wave': 'ripple--*__wave',
  29. '.ripple__background': 'ripple--*__background'
  30. };
  31. /**
  32. * @element ons-ripple
  33. * @category visual
  34. * @description
  35. * [en]
  36. * Adds a Material Design "ripple" effect to an element. The ripple effect will spread from the position where the user taps.
  37. *
  38. * Some elements such as `<ons-button>` and `<ons-fab>` support a `ripple` attribute.
  39. * [/en]
  40. * [ja]マテリアルデザインのリップル効果をDOM要素に追加します。[/ja]
  41. * @codepen wKQWdZ
  42. * @tutorial vanilla/Reference/ripple
  43. * @modifier light-gray
  44. * [en]Change the color of effects to light gray.[/en]
  45. * [ja]エフェクトの色が明るい灰色になります。[/ja]
  46. * @guide theming.html#cross-platform-styling-autostyling
  47. * [en]Cross platform styling[/en]
  48. * [ja]Cross platform styling[/ja]
  49. * @example
  50. * <div class="my-div">
  51. * <ons-ripple></ons-ripple>
  52. * </div>
  53. *
  54. * @example
  55. * <ons-button ripple>Click me!</ons-button>
  56. */
  57. var RippleElement = function (_BaseElement) {
  58. _inherits(RippleElement, _BaseElement);
  59. /**
  60. * @attribute color
  61. * @type {String}
  62. * @description
  63. * [en]Color of the ripple effect.[/en]
  64. * [ja]リップルエフェクトの色を指定します。[/ja]
  65. */
  66. /**
  67. * @attribute modifier
  68. * @type {String}
  69. * @description
  70. * [en]The appearance of the ripple effect.[/en]
  71. * [ja]エフェクトの表現を指定します。[/ja]
  72. */
  73. /**
  74. * @attribute background
  75. * @type {String}
  76. * @description
  77. * [en]Color of the background.[/en]
  78. * [ja]背景の色を設定します。[/ja]
  79. */
  80. /**
  81. * @attribute size
  82. * @type {String}
  83. * @description
  84. * [en]Sizing of the wave on ripple effect. Set "cover" or "contain". Default is "cover".[/en]
  85. * [ja]エフェクトのサイズを指定します。"cover"もしくは"contain"を指定します。デフォルトは"cover"です。[/ja]
  86. */
  87. /**
  88. * @attribute center
  89. * @type {Boolean}
  90. * @description
  91. * [en]If this attribute presents, change the position of wave effect to center of the target element.[/en]
  92. * [ja]この要素を設定すると、エフェクトの位置が要素の真ん中から始まります。[/ja]
  93. */
  94. /**
  95. * @attribute disabled
  96. * @description
  97. * [en]If this attribute is set, the ripple effect will be disabled.[/en]
  98. * [ja]この属性が設定された場合、リップルエフェクトは無効になります。[/ja]
  99. */
  100. function RippleElement() {
  101. _classCallCheck(this, RippleElement);
  102. var _this = _possibleConstructorReturn(this, (RippleElement.__proto__ || _Object$getPrototypeOf(RippleElement)).call(this));
  103. _this._onTap = _this._onTap.bind(_this);
  104. _this._onHold = _this._onHold.bind(_this);
  105. _this._onDragStart = _this._onDragStart.bind(_this);
  106. _this._onRelease = _this._onRelease.bind(_this);
  107. contentReady(_this, function () {
  108. return _this._compile();
  109. });
  110. _this._animator = new Animator();
  111. ['color', 'center', 'start-radius', 'background', 'modifier'].forEach(function (e) {
  112. _this.attributeChangedCallback(e, null, _this.getAttribute(e));
  113. });
  114. return _this;
  115. }
  116. _createClass(RippleElement, [{
  117. key: '_compile',
  118. value: function _compile() {
  119. this.classList.add(defaultClassName);
  120. this._wave = this.getElementsByClassName('ripple__wave')[0];
  121. this._background = this.getElementsByClassName('ripple__background')[0];
  122. if (!(this._background && this._wave)) {
  123. this._wave = util.create('.ripple__wave');
  124. this._background = util.create('.ripple__background');
  125. this.appendChild(this._wave);
  126. this.appendChild(this._background);
  127. }
  128. ModifierUtil.initModifier(this, scheme);
  129. }
  130. }, {
  131. key: '_getEffectSize',
  132. value: function _getEffectSize() {
  133. var sizes = ['cover', 'contain'];
  134. if (this.hasAttribute('size')) {
  135. var size = this.getAttribute('size');
  136. if (sizes.indexOf(size) !== -1) {
  137. return size;
  138. }
  139. }
  140. return 'cover';
  141. }
  142. }, {
  143. key: '_calculateCoords',
  144. value: function _calculateCoords(e) {
  145. var x = void 0,
  146. y = void 0,
  147. h = void 0,
  148. w = void 0,
  149. r = void 0;
  150. var b = this.getBoundingClientRect();
  151. var size = this._getEffectSize();
  152. var error = function error() {
  153. return util.throw('Ripple invalid state');
  154. };
  155. if (this._center) {
  156. x = b.width / 2;
  157. y = b.height / 2;
  158. if (size === 'cover') {
  159. r = Math.sqrt(x * x + y * y);
  160. } else if (size === 'contain') {
  161. r = Math.min(x, y);
  162. } else {
  163. error();
  164. }
  165. } else {
  166. x = (typeof e.clientX === 'number' ? e.clientX : e.changedTouches[0].clientX) - b.left;
  167. y = (typeof e.clientY === 'number' ? e.clientY : e.changedTouches[0].clientY) - b.top;
  168. h = Math.max(y, b.height - y);
  169. w = Math.max(x, b.width - x);
  170. if (size === 'cover') {
  171. r = Math.sqrt(h * h + w * w);
  172. } else if (size === 'contain') {
  173. r = Math.min(Math.round(h / 2), Math.round(w / 2));
  174. } else {
  175. error();
  176. }
  177. }
  178. return { x: x, y: y, r: r };
  179. }
  180. }, {
  181. key: '_rippleAnimation',
  182. value: function _rippleAnimation(e) {
  183. var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 300;
  184. var _animator = this._animator,
  185. _wave = this._wave,
  186. _background = this._background,
  187. _minR = this._minR;
  188. var _calculateCoords2 = this._calculateCoords(e),
  189. x = _calculateCoords2.x,
  190. y = _calculateCoords2.y,
  191. r = _calculateCoords2.r;
  192. _animator.stopAll({ stopNext: 1 });
  193. _animator.animate(_background, { opacity: 1 }, duration);
  194. util.extend(_wave.style, {
  195. opacity: 1,
  196. top: y - _minR + 'px',
  197. left: x - _minR + 'px',
  198. width: 2 * _minR + 'px',
  199. height: 2 * _minR + 'px'
  200. });
  201. return _animator.animate(_wave, {
  202. top: y - r,
  203. left: x - r,
  204. height: 2 * r,
  205. width: 2 * r
  206. }, duration);
  207. }
  208. }, {
  209. key: '_updateParent',
  210. value: function _updateParent() {
  211. if (!this._parentUpdated && this.parentNode) {
  212. var computedStyle = window.getComputedStyle(this.parentNode);
  213. if (computedStyle.getPropertyValue('position') === 'static') {
  214. this.parentNode.style.position = 'relative';
  215. }
  216. this._parentUpdated = true;
  217. }
  218. }
  219. }, {
  220. key: '_onTap',
  221. value: function _onTap(e) {
  222. var _this2 = this;
  223. if (!this.disabled && !e.ripple) {
  224. e.ripple = true;
  225. this._updateParent();
  226. this._rippleAnimation(e.gesture.srcEvent).then(function () {
  227. _this2._animator.fade(_this2._wave);
  228. _this2._animator.fade(_this2._background);
  229. });
  230. }
  231. }
  232. }, {
  233. key: '_onHold',
  234. value: function _onHold(e) {
  235. if (!this.disabled && !e.ripple) {
  236. e.ripple = true;
  237. this._updateParent();
  238. this._holding = this._rippleAnimation(e.gesture.srcEvent, 2000);
  239. document.addEventListener('release', this._onRelease);
  240. }
  241. }
  242. }, {
  243. key: '_onRelease',
  244. value: function _onRelease(e) {
  245. var _this3 = this;
  246. if (this._holding && !e.ripple) {
  247. e.ripple = true;
  248. this._holding.speed(300).then(function () {
  249. _this3._animator.stopAll({ stopNext: true });
  250. _this3._animator.fade(_this3._wave);
  251. _this3._animator.fade(_this3._background);
  252. });
  253. this._holding = false;
  254. }
  255. document.removeEventListener('release', this._onRelease);
  256. }
  257. }, {
  258. key: '_onDragStart',
  259. value: function _onDragStart(e) {
  260. if (this._holding) {
  261. return this._onRelease(e);
  262. }
  263. if (['left', 'right'].indexOf(e.gesture.direction) != -1) {
  264. this._onTap(e);
  265. }
  266. }
  267. }, {
  268. key: 'connectedCallback',
  269. value: function connectedCallback() {
  270. this._parentNode = this.parentNode;
  271. if (internal.config.animationsDisabled) {
  272. this.disabled = true;
  273. } else {
  274. this._parentNode.addEventListener('tap', this._onTap);
  275. this._parentNode.addEventListener('hold', this._onHold);
  276. this._parentNode.addEventListener('dragstart', this._onDragStart);
  277. }
  278. }
  279. }, {
  280. key: 'disconnectedCallback',
  281. value: function disconnectedCallback() {
  282. var pn = this._parentNode || this.parentNode;
  283. pn.removeEventListener('tap', this._onTap);
  284. pn.removeEventListener('hold', this._onHold);
  285. pn.removeEventListener('dragstart', this._onDragStart);
  286. }
  287. }, {
  288. key: 'attributeChangedCallback',
  289. value: function attributeChangedCallback(name, last, current) {
  290. var _this4 = this;
  291. switch (name) {
  292. case 'class':
  293. util.restoreClass(this, defaultClassName, scheme);
  294. break;
  295. case 'modifier':
  296. ModifierUtil.onModifierChanged(last, current, this, scheme);
  297. break;
  298. case 'start-radius':
  299. this._minR = Math.max(0, parseFloat(current) || 0);
  300. break;
  301. case 'color':
  302. if (current) {
  303. contentReady(this, function () {
  304. _this4._wave.style.background = current;
  305. if (!_this4.hasAttribute('background')) {
  306. _this4._background.style.background = current;
  307. }
  308. });
  309. }
  310. break;
  311. case 'background':
  312. if (current || last) {
  313. if (current === 'none') {
  314. contentReady(this, function () {
  315. _this4._background.setAttribute('disabled', 'disabled');
  316. _this4._background.style.background = 'transparent';
  317. });
  318. } else {
  319. contentReady(this, function () {
  320. if (_this4._background.hasAttribute('disabled')) {
  321. _this4._background.removeAttribute('disabled');
  322. }
  323. _this4._background.style.background = current;
  324. });
  325. }
  326. }
  327. break;
  328. case 'center':
  329. if (name === 'center') {
  330. this._center = current != null && current != 'false';
  331. }
  332. break;
  333. }
  334. }
  335. /**
  336. * @property disabled
  337. * @type {Boolean}
  338. * @description
  339. * [en]Whether the element is disabled or not.[/en]
  340. * [ja]無効化されている場合に`true`。[/ja]
  341. */
  342. }, {
  343. key: 'disabled',
  344. set: function set(value) {
  345. return util.toggleAttribute(this, 'disabled', value);
  346. },
  347. get: function get() {
  348. return this.hasAttribute('disabled');
  349. }
  350. }], [{
  351. key: 'observedAttributes',
  352. get: function get() {
  353. return ['start-radius', 'color', 'background', 'center', 'class', 'modifier'];
  354. }
  355. }]);
  356. return RippleElement;
  357. }(BaseElement);
  358. export default RippleElement;
  359. onsElements.Ripple = RippleElement;
  360. customElements.define('ons-ripple', RippleElement);