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