message.js 6.2 KB

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