message-item.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 '../message/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. multipleSlots: true,
  32. };
  33. this.properties = Object.assign({}, props);
  34. this.data = {
  35. prefix,
  36. classPrefix: name,
  37. loop: -1,
  38. animation: [],
  39. showAnimation: [],
  40. wrapTop: -999,
  41. fadeClass: '',
  42. };
  43. this.closeTimeoutContext = 0;
  44. this.nextAnimationContext = 0;
  45. this.resetAnimation = wx.createAnimation({
  46. duration: 0,
  47. timingFunction: 'linear',
  48. });
  49. this.observers = {
  50. marquee(val) {
  51. if (JSON.stringify(val) === '{}' || JSON.stringify(val) === 'true') {
  52. this.setData({
  53. marquee: {
  54. speed: 50,
  55. loop: -1,
  56. delay: 0,
  57. },
  58. });
  59. }
  60. },
  61. 'icon, theme'(icon, theme) {
  62. this.setData({
  63. _icon: calcIcon(icon, THEME_ICON[theme]),
  64. });
  65. },
  66. link(v) {
  67. const _link = isObject(v) ? Object.assign({}, v) : { content: v };
  68. this.setData({ _link });
  69. },
  70. closeBtn(v) {
  71. this.setData({
  72. _closeBtn: calcIcon(v, 'close'),
  73. });
  74. },
  75. };
  76. this.lifetimes = {
  77. ready() {
  78. this.memoInitialData();
  79. },
  80. detached() {
  81. this.clearMessageAnimation();
  82. },
  83. };
  84. }
  85. memoInitialData() {
  86. this.initialData = Object.assign(Object.assign({}, this.properties), this.data);
  87. }
  88. resetData(cb) {
  89. this.setData(Object.assign({}, this.initialData), cb);
  90. }
  91. checkAnimation() {
  92. const { marquee } = this.properties;
  93. if (!marquee || marquee.loop === 0) {
  94. return;
  95. }
  96. const speeding = marquee.speed;
  97. if (this.data.loop > 0) {
  98. this.data.loop -= 1;
  99. }
  100. else if (this.data.loop === 0) {
  101. this.setData({ animation: this.resetAnimation.translateX(0).step().export() });
  102. return;
  103. }
  104. if (this.nextAnimationContext) {
  105. this.clearMessageAnimation();
  106. }
  107. const warpID = `#${name}__text-wrap`;
  108. const nodeID = `#${name}__text`;
  109. Promise.all([getRect(this, nodeID), getRect(this, warpID)]).then(([nodeRect, wrapRect]) => {
  110. this.setData({
  111. animation: this.resetAnimation.translateX(wrapRect.width).step().export(),
  112. }, () => {
  113. const durationTime = ((nodeRect.width + wrapRect.width) / speeding) * 1000;
  114. const nextAnimation = wx
  115. .createAnimation({
  116. duration: durationTime,
  117. })
  118. .translateX(-nodeRect.width)
  119. .step()
  120. .export();
  121. setTimeout(() => {
  122. this.nextAnimationContext = setTimeout(this.checkAnimation.bind(this), durationTime);
  123. this.setData({ animation: nextAnimation });
  124. }, 20);
  125. });
  126. });
  127. }
  128. clearMessageAnimation() {
  129. clearTimeout(this.nextAnimationContext);
  130. this.nextAnimationContext = 0;
  131. }
  132. show(offsetHeight = 0) {
  133. const { duration, marquee, offset, id } = this.properties;
  134. this.setData({
  135. visible: true,
  136. loop: marquee.loop || this.data.loop,
  137. fadeClass: `${name}__fade`,
  138. wrapTop: unitConvert(offset[0]) + offsetHeight,
  139. });
  140. this.reset();
  141. this.checkAnimation();
  142. if (duration && duration > 0) {
  143. this.closeTimeoutContext = setTimeout(() => {
  144. this.hide();
  145. this.triggerEvent('duration-end', { self: this });
  146. }, duration);
  147. }
  148. const wrapID = id ? `#${id}` : `#${name}`;
  149. getRect(this, wrapID).then((wrapRect) => {
  150. this.setData({ height: wrapRect.height }, () => {
  151. this.setData({
  152. fadeClass: ``,
  153. });
  154. });
  155. });
  156. }
  157. hide() {
  158. this.reset();
  159. this.setData({
  160. fadeClass: `${name}__fade`,
  161. });
  162. setTimeout(() => {
  163. this.setData({ visible: false, animation: [] });
  164. }, SHOW_DURATION);
  165. if (typeof this.onHide === 'function') {
  166. this.onHide();
  167. }
  168. }
  169. reset() {
  170. if (this.nextAnimationContext) {
  171. this.clearMessageAnimation();
  172. }
  173. clearTimeout(this.closeTimeoutContext);
  174. this.closeTimeoutContext = 0;
  175. }
  176. handleClose() {
  177. this.hide();
  178. this.triggerEvent('close-btn-click');
  179. }
  180. handleLinkClick() {
  181. this.triggerEvent('link-click');
  182. }
  183. };
  184. Message = __decorate([
  185. wxComponent()
  186. ], Message);
  187. export default Message;