index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { NoticeBarDefaultProps } from './props';
  2. import { log } from '../_util/console';
  3. import '../_util/assert-component2';
  4. Component({
  5. props: NoticeBarDefaultProps,
  6. data: {
  7. show: true,
  8. marqueeStyle: '',
  9. animatedWidth: 0,
  10. overflowWidth: 0,
  11. duration: 0,
  12. viewWidth: 0,
  13. },
  14. didMount: function () {
  15. var enableMarquee = this.props.enableMarquee;
  16. this.showError();
  17. if (enableMarquee) {
  18. this.measureText(this.startMarquee.bind(this));
  19. }
  20. },
  21. didUpdate: function () {
  22. var enableMarquee = this.props.enableMarquee;
  23. this.showError();
  24. // 这里更新处理的原因是防止notice内容在动画过程中发生改变。
  25. if (enableMarquee) {
  26. this.measureText(this.startMarquee.bind(this));
  27. }
  28. },
  29. pageEvents: {
  30. onShow: function () {
  31. this.resetState();
  32. },
  33. },
  34. methods: {
  35. resetState: function () {
  36. var _this = this;
  37. if (this.props.enableMarquee) {
  38. this.setData({
  39. marqueeStyle: '',
  40. animatedWidth: 0,
  41. overflowWidth: 0,
  42. duration: 0,
  43. viewWidth: 0,
  44. }, function () {
  45. _this.resetMarquee();
  46. _this.measureText(_this.startMarquee.bind(_this));
  47. });
  48. }
  49. },
  50. showError: function () {
  51. var actions = this.props.actions;
  52. if (!Array.isArray(actions) && typeof actions !== 'undefined') {
  53. log.warn('NoticeBar', "\u5F53\u524D\u5B9A\u4E49\u7684 actions \u7684\u7C7B\u578B\u4E3A ".concat(typeof actions, "\uFF0C\u4E0D\u7B26\u5408\u5C5E\u6027\u5B9A\u4E49\uFF0C\u5E94\u8BE5\u4E3A\u6570\u7EC4\uFF0C\u5982\uFF1Aactions=\"{{['\u503C', '\u503C']}}"));
  54. }
  55. },
  56. onTap: function () {
  57. var _a = this.props, mode = _a.mode, onTap = _a.onTap;
  58. if (mode === 'link' && typeof onTap === 'function') {
  59. return onTap();
  60. }
  61. if (mode === 'closeable' && typeof onTap === 'function') {
  62. this.setData({
  63. show: false,
  64. });
  65. return onTap();
  66. }
  67. },
  68. // 文本滚动的计算
  69. resetMarquee: function () {
  70. var loop = this.props.loop;
  71. var viewWidth = this.data.viewWidth;
  72. var showMarqueeWidth = '0px';
  73. if (loop) {
  74. showMarqueeWidth = "".concat(viewWidth, "px");
  75. }
  76. var marqueeStyle = "transform: translate3d(".concat(showMarqueeWidth, ", 0, 0); transition: 0s all linear;");
  77. this.setData({
  78. marqueeStyle: marqueeStyle,
  79. });
  80. },
  81. startMarquee: function () {
  82. var loop = this.props.loop;
  83. var leading = 500;
  84. var _a = this.data, duration = _a.duration, overflowWidth = _a.overflowWidth, viewWidth = _a.viewWidth;
  85. var marqueeScrollWidth = overflowWidth;
  86. if (loop) {
  87. marqueeScrollWidth = overflowWidth + viewWidth;
  88. }
  89. var marqueeStyle = "transform: translate3d(".concat(-marqueeScrollWidth, "px, 0, 0); transition: ").concat(duration, "s all linear ").concat(typeof leading === 'number' ? "".concat(leading / 1000, "s") : '0s', ";");
  90. if (this.data.marqueeStyle !== marqueeStyle) {
  91. this.setData({
  92. marqueeStyle: marqueeStyle,
  93. });
  94. }
  95. },
  96. onTransitionEnd: function () {
  97. var _this = this;
  98. var loop = this.props.loop;
  99. var trailing = 200;
  100. if (loop) {
  101. setTimeout(function () {
  102. _this.resetMarquee();
  103. _this.measureText(_this.startMarquee.bind(_this));
  104. }, trailing);
  105. }
  106. },
  107. measureText: function (callback) {
  108. var _this = this;
  109. var fps = 40;
  110. var loop = this.props.loop;
  111. // 计算文本所占据的宽度,计算需要滚动的宽度
  112. setTimeout(function () {
  113. my.createSelectorQuery()
  114. .select(".ant-notice-bar-marquee-".concat(_this.$id))
  115. .boundingClientRect()
  116. .select(".ant-notice-bar-content-".concat(_this.$id))
  117. .boundingClientRect()
  118. .exec(function (ret) {
  119. var _a;
  120. // eslint-disable-next-line max-len
  121. var overflowWidth = (ret &&
  122. ret[0] &&
  123. ret[1] &&
  124. ret[0].width -
  125. ret[1].width) ||
  126. 0;
  127. var viewWidth = ((_a = ret[1]) === null || _a === void 0 ? void 0 : _a.width) || 0;
  128. var marqueeScrollWidth = overflowWidth;
  129. if (loop) {
  130. marqueeScrollWidth = overflowWidth + viewWidth;
  131. }
  132. if (overflowWidth > 0) {
  133. _this.setData({
  134. overflowWidth: overflowWidth,
  135. viewWidth: viewWidth,
  136. duration: marqueeScrollWidth / fps,
  137. });
  138. callback();
  139. }
  140. });
  141. }, 0);
  142. },
  143. },
  144. });