message.js 6.0 KB

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