message.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { getRect, unitConvert, calcIcon, isObject } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-message`;
  13. const SHOW_DURATION = 500;
  14. const THEME_ICON = {
  15. info: 'info-circle-filled',
  16. success: 'check-circle-filled',
  17. warning: 'info-circle-filled',
  18. error: 'error-circle-filled',
  19. };
  20. let Message = class Message extends SuperComponent {
  21. constructor() {
  22. super(...arguments);
  23. this.externalClasses = [
  24. `${prefix}-class`,
  25. `${prefix}-class-content`,
  26. `${prefix}-class-icon`,
  27. `${prefix}-class-link`,
  28. `${prefix}-class-close-btn`,
  29. ];
  30. this.options = {
  31. styleIsolation: 'apply-shared',
  32. multipleSlots: true,
  33. };
  34. this.properties = Object.assign({}, props);
  35. this.data = {
  36. prefix,
  37. classPrefix: name,
  38. loop: -1,
  39. animation: [],
  40. showAnimation: [],
  41. wrapTop: -999,
  42. };
  43. this.observers = {
  44. marquee(val) {
  45. if (JSON.stringify(val) === '{}' || JSON.stringify(val) === 'true') {
  46. this.setData({
  47. marquee: {
  48. speed: 50,
  49. loop: -1,
  50. delay: 0,
  51. },
  52. });
  53. }
  54. },
  55. 'icon, theme'(icon, theme) {
  56. this.setData({
  57. _icon: calcIcon(icon, THEME_ICON[theme]),
  58. });
  59. },
  60. link(v) {
  61. const _link = isObject(v) ? Object.assign({}, v) : { content: v };
  62. this.setData({ _link });
  63. },
  64. closeBtn(v) {
  65. this.setData({
  66. _closeBtn: calcIcon(v, 'close'),
  67. });
  68. },
  69. };
  70. this.closeTimeoutContext = 0;
  71. this.nextAnimationContext = 0;
  72. this.resetAnimation = wx.createAnimation({
  73. duration: 0,
  74. timingFunction: 'linear',
  75. });
  76. }
  77. ready() {
  78. this.memoInitalData();
  79. }
  80. memoInitalData() {
  81. this.initalData = Object.assign(Object.assign({}, this.properties), this.data);
  82. }
  83. resetData(cb) {
  84. this.setData(Object.assign({}, this.initalData), cb);
  85. }
  86. detached() {
  87. this.clearMessageAnimation();
  88. }
  89. checkAnimation() {
  90. const { marquee } = this.properties;
  91. if (!marquee || marquee.loop === 0) {
  92. return;
  93. }
  94. const speeding = marquee.speed;
  95. if (this.data.loop > 0) {
  96. this.data.loop -= 1;
  97. }
  98. else if (this.data.loop === 0) {
  99. this.setData({ animation: this.resetAnimation.translateX(0).step().export() });
  100. return;
  101. }
  102. if (this.nextAnimationContext) {
  103. this.clearMessageAnimation();
  104. }
  105. const warpID = `#${name}__text-wrap`;
  106. const nodeID = `#${name}__text`;
  107. Promise.all([getRect(this, nodeID), getRect(this, warpID)]).then(([nodeRect, wrapRect]) => {
  108. this.setData({
  109. animation: this.resetAnimation.translateX(wrapRect.width).step().export(),
  110. }, () => {
  111. const durationTime = ((nodeRect.width + wrapRect.width) / speeding) * 1000;
  112. const nextAnimation = wx
  113. .createAnimation({
  114. duration: durationTime,
  115. })
  116. .translateX(-nodeRect.width)
  117. .step()
  118. .export();
  119. setTimeout(() => {
  120. this.nextAnimationContext = setTimeout(this.checkAnimation.bind(this), durationTime);
  121. this.setData({ animation: nextAnimation });
  122. }, 20);
  123. });
  124. });
  125. }
  126. clearMessageAnimation() {
  127. clearTimeout(this.nextAnimationContext);
  128. this.nextAnimationContext = 0;
  129. }
  130. show() {
  131. const { duration, marquee, offset } = this.properties;
  132. this.setData({ visible: true, loop: marquee.loop || this.data.loop });
  133. this.reset();
  134. this.checkAnimation();
  135. if (duration && duration > 0) {
  136. this.closeTimeoutContext = setTimeout(() => {
  137. this.hide();
  138. this.triggerEvent('duration-end', { self: this });
  139. }, duration);
  140. }
  141. const wrapID = `#${name}`;
  142. getRect(this, wrapID).then((wrapRect) => {
  143. this.setData({ wrapTop: -wrapRect.height }, () => {
  144. this.setData({
  145. showAnimation: wx
  146. .createAnimation({ duration: SHOW_DURATION, timingFunction: 'ease' })
  147. .translateY(wrapRect.height + unitConvert(offset[0]))
  148. .step()
  149. .export(),
  150. });
  151. });
  152. });
  153. }
  154. hide() {
  155. this.reset();
  156. this.setData({
  157. showAnimation: wx
  158. .createAnimation({ duration: SHOW_DURATION, timingFunction: 'ease' })
  159. .translateY(this.data.wrapTop)
  160. .step()
  161. .export(),
  162. });
  163. setTimeout(() => {
  164. this.setData({ visible: false, animation: [] });
  165. }, SHOW_DURATION);
  166. }
  167. reset() {
  168. if (this.nextAnimationContext) {
  169. this.clearMessageAnimation();
  170. }
  171. clearTimeout(this.closeTimeoutContext);
  172. this.closeTimeoutContext = 0;
  173. }
  174. handleClose() {
  175. this.hide();
  176. this.triggerEvent('close-btn-click');
  177. }
  178. handleLinkClick() {
  179. this.triggerEvent('link-click');
  180. }
  181. };
  182. Message = __decorate([
  183. wxComponent()
  184. ], Message);
  185. export default Message;