index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 _icon = _interopRequireDefault(require("../icon"));
  7. var _use = (0, _utils.use)('notice-bar'),
  8. sfc = _use[0],
  9. bem = _use[1];
  10. var _default = sfc({
  11. props: {
  12. text: String,
  13. mode: String,
  14. color: String,
  15. leftIcon: String,
  16. wrapable: Boolean,
  17. background: String,
  18. delay: {
  19. type: [String, Number],
  20. "default": 1
  21. },
  22. scrollable: {
  23. type: Boolean,
  24. "default": true
  25. },
  26. speed: {
  27. type: Number,
  28. "default": 50
  29. }
  30. },
  31. data: function data() {
  32. return {
  33. wrapWidth: 0,
  34. firstRound: true,
  35. duration: 0,
  36. offsetWidth: 0,
  37. showNoticeBar: true,
  38. animationClass: ''
  39. };
  40. },
  41. watch: {
  42. text: {
  43. handler: function handler() {
  44. var _this = this;
  45. this.$nextTick(function () {
  46. var _this$$refs = _this.$refs,
  47. wrap = _this$$refs.wrap,
  48. content = _this$$refs.content;
  49. if (!wrap || !content) {
  50. return;
  51. }
  52. var wrapWidth = wrap.getBoundingClientRect().width;
  53. var offsetWidth = content.getBoundingClientRect().width;
  54. if (_this.scrollable && offsetWidth > wrapWidth) {
  55. _this.wrapWidth = wrapWidth;
  56. _this.offsetWidth = offsetWidth;
  57. _this.duration = offsetWidth / _this.speed;
  58. _this.animationClass = bem('play');
  59. }
  60. });
  61. },
  62. immediate: true
  63. }
  64. },
  65. methods: {
  66. onClickIcon: function onClickIcon() {
  67. if (this.mode === 'closeable') {
  68. this.showNoticeBar = false;
  69. this.$emit('close');
  70. }
  71. },
  72. onAnimationEnd: function onAnimationEnd() {
  73. var _this2 = this;
  74. this.firstRound = false;
  75. this.$nextTick(function () {
  76. _this2.duration = (_this2.offsetWidth + _this2.wrapWidth) / _this2.speed;
  77. _this2.animationClass = bem('play--infinite');
  78. });
  79. }
  80. },
  81. render: function render(h) {
  82. var _this3 = this;
  83. var mode = this.mode;
  84. var iconName = mode === 'closeable' ? 'cross' : mode === 'link' ? 'arrow' : '';
  85. var barStyle = {
  86. color: this.color,
  87. background: this.background
  88. };
  89. var contentStyle = {
  90. paddingLeft: this.firstRound ? 0 : this.wrapWidth + 'px',
  91. animationDelay: (this.firstRound ? this.delay : 0) + 's',
  92. animationDuration: this.duration + 's'
  93. };
  94. return h("div", {
  95. "directives": [{
  96. name: "show",
  97. value: this.showNoticeBar
  98. }],
  99. "class": bem({
  100. withicon: mode,
  101. wrapable: this.wrapable
  102. }),
  103. "style": barStyle,
  104. "on": {
  105. "click": function click() {
  106. _this3.$emit('click');
  107. }
  108. }
  109. }, [this.leftIcon && h(_icon["default"], {
  110. "class": bem('left-icon'),
  111. "attrs": {
  112. "name": this.leftIcon
  113. }
  114. }), h("div", {
  115. "ref": "wrap",
  116. "class": bem('wrap')
  117. }, [h("div", {
  118. "ref": "content",
  119. "class": [bem('content'), this.animationClass, {
  120. 'van-ellipsis': !this.scrollable && !this.wrapable
  121. }],
  122. "style": contentStyle,
  123. "on": {
  124. "animationend": this.onAnimationEnd,
  125. "webkitAnimationEnd": this.onAnimationEnd
  126. }
  127. }, [this.slots() || this.text])]), iconName && h(_icon["default"], {
  128. "class": bem('right-icon'),
  129. "attrs": {
  130. "name": iconName
  131. },
  132. "on": {
  133. "click": this.onClickIcon
  134. }
  135. })]);
  136. }
  137. });
  138. exports["default"] = _default;