notice-bar.js 7.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 { getRect, getAnimationFrame, calcIcon } 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. prefixIcon(prefixIcon) {
  54. this.setPrefixIcon(prefixIcon);
  55. },
  56. suffixIcon(v) {
  57. this.setData({
  58. _suffixIcon: calcIcon(v),
  59. });
  60. },
  61. content() {
  62. this.clearNoticeBarAnimation();
  63. this.initAnimation();
  64. },
  65. };
  66. this.lifetimes = {
  67. created() {
  68. this.resetAnimation = wx.createAnimation({
  69. duration: 0,
  70. timingFunction: 'linear',
  71. });
  72. },
  73. detached() {
  74. this.clearNoticeBarAnimation();
  75. },
  76. ready() {
  77. this.show();
  78. },
  79. };
  80. this.methods = {
  81. initAnimation() {
  82. const warpID = `.${name}__content-wrap`;
  83. const nodeID = `.${name}__content`;
  84. getAnimationFrame(() => {
  85. Promise.all([getRect(this, nodeID), getRect(this, warpID)]).then(([nodeRect, wrapRect]) => {
  86. const { marquee } = this.properties;
  87. if (nodeRect == null || wrapRect == null || !nodeRect.width || !wrapRect.width) {
  88. return;
  89. }
  90. if (marquee || wrapRect.width < nodeRect.width) {
  91. const speeding = marquee.speed || 50;
  92. const delaying = marquee.delay || 0;
  93. const loops = marquee.loop - 1 || -1;
  94. const animationDuration = ((wrapRect.width + nodeRect.width) / speeding) * 1000;
  95. const firstAnimationDuration = (nodeRect.width / speeding) * 1000;
  96. this.setData({
  97. wrapWidth: Number(wrapRect.width),
  98. nodeWidth: Number(nodeRect.width),
  99. animationDuration: animationDuration,
  100. delay: delaying,
  101. loop: loops,
  102. firstAnimationDuration: firstAnimationDuration,
  103. });
  104. this.startScrollAnimation(true);
  105. }
  106. });
  107. });
  108. },
  109. startScrollAnimation(isFirstScroll = false) {
  110. this.clearNoticeBarAnimation();
  111. const { wrapWidth, nodeWidth, firstAnimationDuration, animationDuration, delay } = this.data;
  112. const delayTime = isFirstScroll ? delay : 0;
  113. const durationTime = isFirstScroll ? firstAnimationDuration : animationDuration;
  114. this.setData({
  115. animationData: this.resetAnimation
  116. .translateX(isFirstScroll ? 0 : wrapWidth)
  117. .step()
  118. .export(),
  119. });
  120. getAnimationFrame(() => {
  121. this.setData({
  122. animationData: wx
  123. .createAnimation({ duration: durationTime, timingFunction: 'linear', delay: delayTime })
  124. .translateX(-nodeWidth)
  125. .step()
  126. .export(),
  127. });
  128. });
  129. this.nextAnimationContext = setTimeout(() => {
  130. if (this.data.loop > 0) {
  131. this.data.loop -= 1;
  132. this.startScrollAnimation();
  133. }
  134. else if (this.data.loop === 0) {
  135. this.setData({ animationData: this.resetAnimation.translateX(0).step().export() });
  136. }
  137. else if (this.data.loop < 0) {
  138. this.startScrollAnimation();
  139. }
  140. }, durationTime + delayTime);
  141. },
  142. show() {
  143. this.clearNoticeBarAnimation();
  144. this.setPrefixIcon(this.properties.prefixIcon);
  145. this.initAnimation();
  146. },
  147. clearNoticeBarAnimation() {
  148. this.nextAnimationContext && clearTimeout(this.nextAnimationContext);
  149. this.nextAnimationContext = null;
  150. },
  151. setPrefixIcon(v) {
  152. this.setData({
  153. _prefixIcon: calcIcon(v, 'error-circle-filled'),
  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;