储能智慧云小程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

notice-bar.js 7.4KB

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