new longpress on files

This commit is contained in:
Eike Ahmels authored and Stefan Dej committed 2021-03-03 20:30:20 +01:00
1 parent 5fc2d5c775
commit d470d42b6d
3 files changed
+81 -12

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
import Vue from 'vue';
Vue.directive('longpress', {
bind: function (el, binding, vNode) {
// Make sure expression provided is a function
if (typeof binding.value !== 'function') {
// Fetch name of component
const compName = vNode.context.name
// pass warning to console
let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be`
if (compName) { warn += ` Found in component '${compName}' ` }
console.warn(warn)
}
// Run Function
const handler = (e) => {
e.preventDefault();
e.stopPropagation();
e.cancelBubble = true;
binding.value(e)
return false;
}
// Define variable
let pressTimer = null
// Define funtion handlers
// Create timeout ( run function after 1s )
let start = (e) => {
if (e.type === 'click' && e.button !== 0) {
return;
}
if (pressTimer === null) {
pressTimer = setTimeout(() => {
// Run function
handler(e)
}, 1000)
}
}
// Cancel Timeout
let cancel = () => {
// Check if timer has a value or not
if (pressTimer !== null) {
clearTimeout(pressTimer)
pressTimer = null
}
}
// Add Event listeners
el.addEventListener("mousedown", start);
el.addEventListener("touchstart", start);
// Cancel timeouts if this events happen
el.addEventListener("click", cancel);
el.addEventListener("mouseout", cancel);
el.addEventListener("touchend", cancel);
el.addEventListener("touchcancel", cancel);
}
});