index.js 4.9 KB

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