index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _utils = require("../utils");
  6. var _raf = require("../utils/dom/raf");
  7. var _bindEvent = require("../mixins/bind-event");
  8. var _icon = _interopRequireDefault(require("../icon"));
  9. var _createNamespace = (0, _utils.createNamespace)('notice-bar'),
  10. createComponent = _createNamespace[0],
  11. bem = _createNamespace[1];
  12. var _default = createComponent({
  13. mixins: [(0, _bindEvent.BindEventMixin)(function (bind) {
  14. // fix cache issues with forwards and back history in safari
  15. // see: https://guwii.com/cache-issues-with-forwards-and-back-history-in-safari/
  16. bind(window, 'pageshow', this.start);
  17. })],
  18. props: {
  19. text: String,
  20. mode: String,
  21. color: String,
  22. leftIcon: String,
  23. wrapable: Boolean,
  24. background: String,
  25. scrollable: {
  26. type: Boolean,
  27. default: null
  28. },
  29. delay: {
  30. type: [Number, String],
  31. default: 1
  32. },
  33. speed: {
  34. type: [Number, String],
  35. default: 50
  36. }
  37. },
  38. data: function data() {
  39. return {
  40. show: true,
  41. offset: 0,
  42. duration: 0,
  43. wrapWidth: 0,
  44. contentWidth: 0
  45. };
  46. },
  47. watch: {
  48. scrollable: 'start',
  49. text: {
  50. handler: 'start',
  51. immediate: true
  52. }
  53. },
  54. activated: function activated() {
  55. this.start();
  56. },
  57. methods: {
  58. onClickIcon: function onClickIcon(event) {
  59. if (this.mode === 'closeable') {
  60. this.show = false;
  61. this.$emit('close', event);
  62. }
  63. },
  64. onTransitionEnd: function onTransitionEnd() {
  65. var _this = this;
  66. this.offset = this.wrapWidth;
  67. this.duration = 0; // wait for Vue to render offset
  68. // using nextTick won't work in iOS14
  69. (0, _raf.raf)(function () {
  70. // use double raf to ensure animation can start
  71. (0, _raf.doubleRaf)(function () {
  72. _this.offset = -_this.contentWidth;
  73. _this.duration = (_this.contentWidth + _this.wrapWidth) / _this.speed;
  74. _this.$emit('replay');
  75. });
  76. });
  77. },
  78. reset: function reset() {
  79. this.offset = 0;
  80. this.duration = 0;
  81. this.wrapWidth = 0;
  82. this.contentWidth = 0;
  83. },
  84. start: function start() {
  85. var _this2 = this;
  86. var delay = (0, _utils.isDef)(this.delay) ? this.delay * 1000 : 0;
  87. this.reset();
  88. clearTimeout(this.startTimer);
  89. this.startTimer = setTimeout(function () {
  90. var _this2$$refs = _this2.$refs,
  91. wrap = _this2$$refs.wrap,
  92. content = _this2$$refs.content;
  93. if (!wrap || !content || _this2.scrollable === false) {
  94. return;
  95. }
  96. var wrapWidth = wrap.getBoundingClientRect().width;
  97. var contentWidth = content.getBoundingClientRect().width;
  98. if (_this2.scrollable || contentWidth > wrapWidth) {
  99. (0, _raf.doubleRaf)(function () {
  100. _this2.offset = -contentWidth;
  101. _this2.duration = contentWidth / _this2.speed;
  102. _this2.wrapWidth = wrapWidth;
  103. _this2.contentWidth = contentWidth;
  104. });
  105. }
  106. }, delay);
  107. }
  108. },
  109. render: function render() {
  110. var _this3 = this;
  111. var h = arguments[0];
  112. var slots = this.slots,
  113. mode = this.mode,
  114. leftIcon = this.leftIcon,
  115. onClickIcon = this.onClickIcon;
  116. var barStyle = {
  117. color: this.color,
  118. background: this.background
  119. };
  120. var contentStyle = {
  121. transform: this.offset ? "translateX(" + this.offset + "px)" : '',
  122. transitionDuration: this.duration + 's'
  123. };
  124. function LeftIcon() {
  125. var slot = slots('left-icon');
  126. if (slot) {
  127. return slot;
  128. }
  129. if (leftIcon) {
  130. return h(_icon.default, {
  131. "class": bem('left-icon'),
  132. "attrs": {
  133. "name": leftIcon
  134. }
  135. });
  136. }
  137. }
  138. function RightIcon() {
  139. var slot = slots('right-icon');
  140. if (slot) {
  141. return slot;
  142. }
  143. var iconName;
  144. if (mode === 'closeable') {
  145. iconName = 'cross';
  146. } else if (mode === 'link') {
  147. iconName = 'arrow';
  148. }
  149. if (iconName) {
  150. return h(_icon.default, {
  151. "class": bem('right-icon'),
  152. "attrs": {
  153. "name": iconName
  154. },
  155. "on": {
  156. "click": onClickIcon
  157. }
  158. });
  159. }
  160. }
  161. return h("div", {
  162. "attrs": {
  163. "role": "alert"
  164. },
  165. "directives": [{
  166. name: "show",
  167. value: this.show
  168. }],
  169. "class": bem({
  170. wrapable: this.wrapable
  171. }),
  172. "style": barStyle,
  173. "on": {
  174. "click": function click(event) {
  175. _this3.$emit('click', event);
  176. }
  177. }
  178. }, [LeftIcon(), h("div", {
  179. "ref": "wrap",
  180. "class": bem('wrap'),
  181. "attrs": {
  182. "role": "marquee"
  183. }
  184. }, [h("div", {
  185. "ref": "content",
  186. "class": [bem('content'), {
  187. 'van-ellipsis': this.scrollable === false && !this.wrapable
  188. }],
  189. "style": contentStyle,
  190. "on": {
  191. "transitionend": this.onTransitionEnd
  192. }
  193. }, [this.slots() || this.text])]), RightIcon()]);
  194. }
  195. });
  196. exports.default = _default;