bar.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { on, off } from 'element-ui/src/utils/dom';
  2. import { renderThumbStyle, BAR_MAP } from './util';
  3. /* istanbul ignore next */
  4. export default {
  5. name: 'Bar',
  6. props: {
  7. vertical: Boolean,
  8. size: String,
  9. move: Number
  10. },
  11. computed: {
  12. bar() {
  13. return BAR_MAP[this.vertical ? 'vertical' : 'horizontal'];
  14. },
  15. wrap() {
  16. return this.$parent.wrap;
  17. }
  18. },
  19. render(h) {
  20. const { size, move, bar } = this;
  21. return (
  22. <div
  23. class={ ['el-scrollbar__bar', 'is-' + bar.key] }
  24. onMousedown={ this.clickTrackHandler } >
  25. <div
  26. ref="thumb"
  27. class="el-scrollbar__thumb"
  28. onMousedown={ this.clickThumbHandler }
  29. style={ renderThumbStyle({ size, move, bar }) }>
  30. </div>
  31. </div>
  32. );
  33. },
  34. methods: {
  35. clickThumbHandler(e) {
  36. // prevent click event of right button
  37. if (e.ctrlKey || e.button === 2) {
  38. return;
  39. }
  40. this.startDrag(e);
  41. this[this.bar.axis] = (e.currentTarget[this.bar.offset] - (e[this.bar.client] - e.currentTarget.getBoundingClientRect()[this.bar.direction]));
  42. },
  43. clickTrackHandler(e) {
  44. const offset = Math.abs(e.target.getBoundingClientRect()[this.bar.direction] - e[this.bar.client]);
  45. const thumbHalf = (this.$refs.thumb[this.bar.offset] / 2);
  46. const thumbPositionPercentage = ((offset - thumbHalf) * 100 / this.$el[this.bar.offset]);
  47. this.wrap[this.bar.scroll] = (thumbPositionPercentage * this.wrap[this.bar.scrollSize] / 100);
  48. },
  49. startDrag(e) {
  50. e.stopImmediatePropagation();
  51. this.cursorDown = true;
  52. on(document, 'mousemove', this.mouseMoveDocumentHandler);
  53. on(document, 'mouseup', this.mouseUpDocumentHandler);
  54. document.onselectstart = () => false;
  55. },
  56. mouseMoveDocumentHandler(e) {
  57. if (this.cursorDown === false) return;
  58. const prevPage = this[this.bar.axis];
  59. if (!prevPage) return;
  60. const offset = ((this.$el.getBoundingClientRect()[this.bar.direction] - e[this.bar.client]) * -1);
  61. const thumbClickPosition = (this.$refs.thumb[this.bar.offset] - prevPage);
  62. const thumbPositionPercentage = ((offset - thumbClickPosition) * 100 / this.$el[this.bar.offset]);
  63. this.wrap[this.bar.scroll] = (thumbPositionPercentage * this.wrap[this.bar.scrollSize] / 100);
  64. },
  65. mouseUpDocumentHandler(e) {
  66. this.cursorDown = false;
  67. this[this.bar.axis] = 0;
  68. off(document, 'mousemove', this.mouseMoveDocumentHandler);
  69. document.onselectstart = null;
  70. }
  71. },
  72. destroyed() {
  73. off(document, 'mouseup', this.mouseUpDocumentHandler);
  74. }
  75. };