index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { __awaiter, __generator } from "tslib";
  2. import { Component, triggerEventOnly, getValueFromProps, } from '../_util/simply';
  3. import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
  4. import { NoticeBarDefaultProps } from './props';
  5. Component(NoticeBarDefaultProps, {
  6. getInstance: function () {
  7. if (this.$id) {
  8. return my;
  9. }
  10. return this;
  11. },
  12. getBoundingClientRectWithId: function (prefix) {
  13. return __awaiter(this, void 0, void 0, function () {
  14. return __generator(this, function (_a) {
  15. switch (_a.label) {
  16. case 0: return [4 /*yield*/, getInstanceBoundingClientRect(this.getInstance(), "".concat(prefix).concat(this.$id ? "-".concat(this.$id) : ''))];
  17. case 1: return [2 /*return*/, _a.sent()];
  18. }
  19. });
  20. });
  21. },
  22. onTap: function () {
  23. var mode = getValueFromProps(this, 'mode');
  24. if (mode === 'link') {
  25. triggerEventOnly(this, 'tap');
  26. }
  27. if (mode === 'closeable') {
  28. if (typeof this.props.onTap !== 'function') {
  29. return;
  30. }
  31. this.setData({
  32. show: false,
  33. });
  34. triggerEventOnly(this, 'tap');
  35. }
  36. },
  37. startMarquee: function (state) {
  38. var loop = getValueFromProps(this, 'loop');
  39. var leading = 500;
  40. var duration = state.duration, overflowWidth = state.overflowWidth, viewWidth = state.viewWidth;
  41. var marqueeScrollWidth = overflowWidth;
  42. if (loop) {
  43. marqueeScrollWidth = overflowWidth + viewWidth;
  44. }
  45. var newMarqueeStyle = "transform: translate3d(".concat(-marqueeScrollWidth, "px, 0, 0); transition: ").concat(duration, "s all linear ").concat(typeof leading === 'number' ? "".concat(leading / 1000, "s") : '0s', ";");
  46. this.setData({
  47. marqueeStyle: newMarqueeStyle,
  48. });
  49. return newMarqueeStyle;
  50. },
  51. measureText: function (callback) {
  52. var _this = this;
  53. var fps = 40;
  54. var loop = getValueFromProps(this, 'loop');
  55. // 计算文本所占据的宽度,计算需要滚动的宽度
  56. setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
  57. var marqueeSize, contentSize, overflowWidth, viewWidth, marqueeScrollWidth;
  58. return __generator(this, function (_a) {
  59. switch (_a.label) {
  60. case 0: return [4 /*yield*/, this.getBoundingClientRectWithId('.ant-notice-bar-marquee')];
  61. case 1:
  62. marqueeSize = _a.sent();
  63. return [4 /*yield*/, this.getBoundingClientRectWithId('.ant-notice-bar-content')];
  64. case 2:
  65. contentSize = _a.sent();
  66. overflowWidth = (marqueeSize &&
  67. contentSize &&
  68. marqueeSize.width - contentSize.width) ||
  69. 0;
  70. viewWidth = (contentSize === null || contentSize === void 0 ? void 0 : contentSize.width) || 0;
  71. marqueeScrollWidth = overflowWidth;
  72. if (loop) {
  73. marqueeScrollWidth = overflowWidth + viewWidth;
  74. }
  75. if (overflowWidth > 0) {
  76. callback({
  77. overflowWidth: overflowWidth,
  78. viewWidth: viewWidth,
  79. duration: marqueeScrollWidth / fps,
  80. });
  81. }
  82. return [2 /*return*/];
  83. }
  84. });
  85. }); }, 0);
  86. },
  87. // 文本滚动的计算
  88. resetMarquee: function (state) {
  89. var loop = getValueFromProps(this, 'loop');
  90. var viewWidth = state.viewWidth;
  91. var showMarqueeWidth = '0px';
  92. if (loop) {
  93. showMarqueeWidth = "".concat(viewWidth, "px");
  94. }
  95. var marqueeStyle = "transform: translate3d(".concat(showMarqueeWidth, ", 0, 0); transition: 0s all linear;");
  96. this.setData({
  97. marqueeStyle: marqueeStyle,
  98. });
  99. },
  100. onTransitionEnd: function () {
  101. var _this = this;
  102. var loop = getValueFromProps(this, 'loop');
  103. var trailing = 200;
  104. if (loop) {
  105. setTimeout(function () {
  106. _this.measureText(function (state) {
  107. _this.resetMarquee.call(_this, state);
  108. });
  109. }, trailing);
  110. }
  111. },
  112. }, {
  113. show: true,
  114. marqueeStyle: '',
  115. }, undefined, {
  116. didMount: function () {
  117. var _this = this;
  118. var enableMarquee = this.props.enableMarquee;
  119. if (enableMarquee) {
  120. this.measureText(function (state) {
  121. _this.startMarquee.call(_this, state);
  122. });
  123. }
  124. },
  125. didUpdate: function () {
  126. var _this = this;
  127. var enableMarquee = this.props.enableMarquee;
  128. // 这里更新处理的原因是防止notice内容在动画过程中发生改变。
  129. if (enableMarquee) {
  130. console.log(enableMarquee);
  131. this.measureText(function (state) {
  132. _this.startMarquee.call(_this, state);
  133. });
  134. }
  135. },
  136. pageEvents: {
  137. onShow: function () {
  138. var _this = this;
  139. if (this.props.enableMarquee) {
  140. this.setData({ marqueeStyle: '' });
  141. this.resetMarquee({
  142. overflowWidth: 0,
  143. duration: 0,
  144. viewWidth: 0,
  145. });
  146. this.measureText(function (state) {
  147. _this.startMarquee.call(_this, state);
  148. });
  149. }
  150. },
  151. },
  152. });