iep-ui
Version:
An enterprise-class UI design language and Vue-based implementation
106 lines (89 loc) • 3.13 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _dom = require('../vc-util/Dom/dom');
var _util = require('./util');
/* istanbul ignore next */
exports['default'] = {
name: 'Bar',
props: {
vertical: Boolean,
size: String,
prefixCls: String,
move: Number
},
computed: {
bar: function bar() {
return _util.BAR_MAP[this.vertical ? 'vertical' : 'horizontal'];
},
wrap: function wrap() {
return this.$parent.wrap;
}
},
render: function render(h) {
var size = this.size,
move = this.move,
bar = this.bar,
prefixCls = this.prefixCls;
var cls = prefixCls + '__thumb';
return h(
'div',
{ 'class': [prefixCls + '__bar', 'is-' + bar.key], on: {
'mousedown': this.clickTrackHandler
}
},
[h('div', {
ref: 'thumb',
'class': cls,
on: {
'mousedown': this.clickThumbHandler
},
style: (0, _util.renderThumbStyle)({ size: size, move: move, bar: bar })
})]
);
},
methods: {
clickThumbHandler: function clickThumbHandler(e) {
// prevent click event of right button
if (e.ctrlKey || e.button === 2) {
return;
}
this.startDrag(e);
this[this.bar.axis] = e.currentTarget[this.bar.offset] - (e[this.bar.client] - e.currentTarget.getBoundingClientRect()[this.bar.direction]);
},
clickTrackHandler: function clickTrackHandler(e) {
var offset = Math.abs(e.target.getBoundingClientRect()[this.bar.direction] - e[this.bar.client]);
var thumbHalf = this.$refs.thumb[this.bar.offset] / 2;
var thumbPositionPercentage = (offset - thumbHalf) * 100 / this.$el[this.bar.offset];
this.wrap[this.bar.scroll] = thumbPositionPercentage * this.wrap[this.bar.scrollSize] / 100;
},
startDrag: function startDrag(e) {
e.stopImmediatePropagation();
this.cursorDown = true;
(0, _dom.on)(document, 'mousemove', this.mouseMoveDocumentHandler);
(0, _dom.on)(document, 'mouseup', this.mouseUpDocumentHandler);
document.onselectstart = function () {
return false;
};
},
mouseMoveDocumentHandler: function mouseMoveDocumentHandler(e) {
if (this.cursorDown === false) return;
var prevPage = this[this.bar.axis];
if (!prevPage) return;
var offset = (this.$el.getBoundingClientRect()[this.bar.direction] - e[this.bar.client]) * -1;
var thumbClickPosition = this.$refs.thumb[this.bar.offset] - prevPage;
var thumbPositionPercentage = (offset - thumbClickPosition) * 100 / this.$el[this.bar.offset];
this.wrap[this.bar.scroll] = thumbPositionPercentage * this.wrap[this.bar.scrollSize] / 100;
},
mouseUpDocumentHandler: function mouseUpDocumentHandler(e) {
this.cursorDown = false;
this[this.bar.axis] = 0;
(0, _dom.off)(document, 'mousemove', this.mouseMoveDocumentHandler);
document.onselectstart = null;
}
},
destroyed: function destroyed() {
(0, _dom.off)(document, 'mouseup', this.mouseUpDocumentHandler);
}
};