notice-bar.js 7.1 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 { getRect, getAnimationFrame } from '../common/utils';
  9. import props from './props';
  10. import config from '../common/config';
  11. const { prefix } = config;
  12. const name = `${prefix}-notice-bar`;
  13. let NoticeBar = class NoticeBar extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [
  17. `${prefix}-class`,
  18. `${prefix}-class-content`,
  19. `${prefix}-class-prefix-icon`,
  20. `${prefix}-class-extra`,
  21. `${prefix}-class-suffix-icon`,
  22. ];
  23. this.options = {
  24. styleIsolation: 'apply-shared',
  25. multipleSlots: true,
  26. };
  27. this.properties = props;
  28. this.data = {
  29. prefix,
  30. classPrefix: name,
  31. loop: -1,
  32. };
  33. this.observers = {
  34. marquee(val) {
  35. if (JSON.stringify(val) === '{}' || JSON.stringify(val) === 'true') {
  36. this.setData({
  37. marquee: {
  38. speed: 50,
  39. loop: -1,
  40. delay: 0,
  41. },
  42. });
  43. }
  44. },
  45. visible(visible) {
  46. if (visible) {
  47. this.show();
  48. }
  49. else {
  50. this.clearNoticeBarAnimation();
  51. }
  52. },
  53. };
  54. this.lifetimes = {
  55. created() {
  56. this.resetAnimation = wx.createAnimation({
  57. duration: 0,
  58. timingFunction: 'linear',
  59. });
  60. },
  61. detached() {
  62. this.clearNoticeBarAnimation();
  63. },
  64. ready() {
  65. this.show();
  66. },
  67. };
  68. this.methods = {
  69. initAnimation() {
  70. const warpID = `.${name}__content-wrap`;
  71. const nodeID = `.${name}__content`;
  72. getAnimationFrame(() => {
  73. Promise.all([getRect(this, nodeID), getRect(this, warpID)]).then(([nodeRect, wrapRect]) => {
  74. const { marquee } = this.properties;
  75. if (nodeRect == null || wrapRect == null || !nodeRect.width || !wrapRect.width) {
  76. return;
  77. }
  78. if (marquee || wrapRect.width < nodeRect.width) {
  79. const speeding = marquee.speed || 50;
  80. const delaying = marquee.delay || 0;
  81. const loops = marquee.loop - 1 || -1;
  82. const animationDuration = ((wrapRect.width + nodeRect.width) / speeding) * 1000;
  83. const firstAnimationDuration = (nodeRect.width / speeding) * 1000;
  84. this.setData({
  85. wrapWidth: Number(wrapRect.width),
  86. nodeWidth: Number(nodeRect.width),
  87. animationDuration: animationDuration,
  88. delay: delaying,
  89. loop: loops,
  90. firstAnimationDuration: firstAnimationDuration,
  91. });
  92. this.startScrollAnimation(true);
  93. }
  94. });
  95. });
  96. },
  97. startScrollAnimation(isFirstScroll = false) {
  98. this.clearNoticeBarAnimation();
  99. const { wrapWidth, nodeWidth, firstAnimationDuration, animationDuration, delay } = this.data;
  100. const delayTime = isFirstScroll ? delay : 0;
  101. const durationTime = isFirstScroll ? firstAnimationDuration : animationDuration;
  102. this.setData({
  103. animationData: this.resetAnimation
  104. .translateX(isFirstScroll ? 0 : wrapWidth)
  105. .step()
  106. .export(),
  107. });
  108. getAnimationFrame(() => {
  109. this.setData({
  110. animationData: wx
  111. .createAnimation({ duration: durationTime, timingFunction: 'linear', delay: delayTime })
  112. .translateX(-nodeWidth)
  113. .step()
  114. .export(),
  115. });
  116. });
  117. this.nextAnimationContext = setTimeout(() => {
  118. if (this.data.loop > 0) {
  119. this.data.loop -= 1;
  120. this.startScrollAnimation();
  121. }
  122. else if (this.data.loop === 0) {
  123. this.setData({ animationData: this.resetAnimation.translateX(0).step().export() });
  124. }
  125. else if (this.data.loop < 0) {
  126. this.startScrollAnimation();
  127. }
  128. }, durationTime + delayTime);
  129. },
  130. show() {
  131. this.clearNoticeBarAnimation();
  132. this.setIcon();
  133. this.initAnimation();
  134. },
  135. clearNoticeBarAnimation() {
  136. this.nextAnimationContext && clearTimeout(this.nextAnimationContext);
  137. this.nextAnimationContext = null;
  138. },
  139. setIcon() {
  140. const { prefixIcon, theme } = this.properties;
  141. if (prefixIcon) {
  142. this.setData({
  143. iconName: prefixIcon !== 'null' ? `${prefixIcon}` : '',
  144. });
  145. }
  146. else {
  147. const themeNoticeBar = {
  148. info: 'error-circle-filled',
  149. success: 'check-circle-filled',
  150. warning: 'error-circle-filled',
  151. error: 'close-circle-filled',
  152. };
  153. this.setData({ iconName: themeNoticeBar[theme] });
  154. }
  155. },
  156. clickPrefixIcon() {
  157. this.triggerEvent('click', { trigger: 'prefix-icon' });
  158. },
  159. clickContent() {
  160. this.triggerEvent('click', { trigger: 'content' });
  161. },
  162. clickSuffixIcon() {
  163. this.triggerEvent('click', { trigger: 'suffix-icon' });
  164. },
  165. clickExtra() {
  166. this.triggerEvent('click', { trigger: 'extra' });
  167. },
  168. };
  169. }
  170. };
  171. NoticeBar = __decorate([
  172. wxComponent()
  173. ], NoticeBar);
  174. export default NoticeBar;