notice-bar.js 7.6 KB

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