winbox-ui-next
Version:
We Design Vue 2.0: A Vue.js 3 UI Library
128 lines (127 loc) • 3.29 kB
JavaScript
;
var vue = require("vue");
var evtd = require("evtd");
var seemly = require("seemly");
var utils = require("./utils.js");
const HANDLE_SIZE = "12px";
const HANDLE_SIZE_NUM = 12;
const RADIUS = "6px";
var AlphaSlider = vue.defineComponent({
name: "AlphaSlider",
props: {
clsPrefix: {
type: String,
required: true
},
rgba: {
type: Array,
default: null
},
alpha: {
type: Number,
default: 0
},
onUpdateAlpha: {
type: Function,
required: true
},
onComplete: Function
},
setup(props) {
const railRef = vue.ref(null);
function handleMouseDown(e) {
if (!railRef.value || !props.rgba)
return;
evtd.on("mousemove", document, handleMouseMove);
evtd.on("mouseup", document, handleMouseUp);
handleMouseMove(e);
}
function handleMouseMove(e) {
const {
value: railEl
} = railRef;
if (!railEl)
return;
const {
width,
left
} = railEl.getBoundingClientRect();
const newAlpha = (e.clientX - left) / (width - HANDLE_SIZE_NUM);
props.onUpdateAlpha(utils.normalizeAlpha(newAlpha));
}
function handleMouseUp() {
var _a;
evtd.off("mousemove", document, handleMouseMove);
evtd.off("mouseup", document, handleMouseUp);
(_a = props.onComplete) == null ? void 0 : _a.call(props);
}
return {
railRef,
railBackgroundImage: vue.computed(() => {
const {
rgba
} = props;
if (!rgba)
return "";
return `linear-gradient(to right, rgba(${rgba[0]}, ${rgba[1]}, ${rgba[2]}, 0) 0%, rgba(${rgba[0]}, ${rgba[1]}, ${rgba[2]}, 1) 100%)`;
}),
handleMouseDown
};
},
render() {
const {
clsPrefix
} = this;
return vue.createVNode("div", {
"class": `${clsPrefix}-color-picker-slider`,
"ref": "railRef",
"style": {
height: HANDLE_SIZE,
borderRadius: RADIUS
},
"onMousedown": this.handleMouseDown
}, [vue.createVNode("div", {
"style": {
borderRadius: RADIUS,
position: "absolute",
left: 0,
right: 0,
top: 0,
bottom: 0,
overflow: "hidden"
}
}, [vue.createVNode("div", {
"class": `${clsPrefix}-color-picker-checkboard`
}, null), vue.createVNode("div", {
"class": `${clsPrefix}-color-picker-slider__image`,
"style": {
backgroundImage: this.railBackgroundImage
}
}, null)]), this.rgba && vue.createVNode("div", {
"style": {
position: "absolute",
left: RADIUS,
right: RADIUS,
top: 0,
bottom: 0
}
}, [vue.createVNode("div", {
"class": `${clsPrefix}-color-picker-handle`,
"style": {
left: `calc(${this.alpha * 100}% - ${RADIUS})`,
borderRadius: RADIUS,
width: HANDLE_SIZE,
height: HANDLE_SIZE
}
}, [vue.createVNode("div", {
"class": `${clsPrefix}-color-picker-handle__fill`,
"style": {
backgroundColor: seemly.toRgbaString(this.rgba),
borderRadius: RADIUS,
width: HANDLE_SIZE,
height: HANDLE_SIZE
}
}, null)])])]);
}
});
module.exports = AlphaSlider;