index.js 3.3 KB

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