Added mouse type events

Longpress, click, context
This commit is contained in:
Dennis Eichhorn 2016-05-14 10:55:01 +02:00
parent 3072b20d2a
commit 9696363f5e
2 changed files with 58 additions and 14 deletions

View File

@ -0,0 +1,19 @@
/**
* Click type.
*
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0 * @since 1.0.0
*/
(function (jsOMS, undefined)
{
jsOMS.Autoloader.defineNamespace('jsOMS.UI.Input.Mouse');
jsOMS.UI.Input.Mouse.EventType = Object.freeze({
CONTEXT: 0,
LONGPRESS: 1,
CLICK: 2
});
}(window.jsOMS = window.jsOMS || {}));

View File

@ -5,28 +5,55 @@
jsOMS.UI.Input.Mouse.MouseManager = function ()
{
this.elements = {};
this.down = [];
this.click = {time: 0};
};
jsOMS.UI.Input.Mouse.MouseManager.prototype.add = function (element, callback, exact)
jsOMS.UI.Input.Mouse.MouseManager.prototype.add = function (element, type, button, callback, exact)
{
if (typeof this.elements[element] === 'undefined') {
this.elements[element] = [];
this.bind(element);
}
this.elements[element].push({callback: callback, exact: exact});
this.bind(element, type);
this.elements[element].push({callback: callback, type: type, button: button, exact: exact});
};
jsOMS.UI.Input.Mouse.MouseManager.prototype.bind = function (element)
jsOMS.UI.Input.Mouse.MouseManager.prototype.bind = function (element, type)
{
let self = this;
let self = this,
e = document.getElementById(element);
document.getElementById(element).addEventListener('contextmenu', function (event)
{
self.run(element, event);
}, false);
if(e === null) {
return;
}
if (type === jsOMS.UI.Input.Mouse.EventType.CONTEXT) {
e.addEventListener('contextmenu', function (event)
{
self.run(element, event);
}, false);
} else if (type === jsOMS.UI.Input.Mouse.EventType.LONGPRESS) {
e.addEventListener('mousedown', function (event)
{
self.click.time = new Date().getTime();
}, false);
e.addEventListener('mouseup', function (event)
{
let duration = new Date().getTime() - self.click.time;
if (duration > 650) {
self.run(element, event);
}
self.click.time = 0;
}, false);
} else if (type === jsOMS.UI.Input.Mouse.EventType.CLICK) {
e.addEventListener('click', function (event)
{
self.run(element, event);
}, false);
}
};
jsOMS.UI.Input.Mouse.MouseManager.prototype.run = function (element, event)
@ -38,10 +65,8 @@
let actions = this.elements[element],
length = actions.length;
console.log();
for (let i = 0; i < length; i++) {
if(!actions[i].exact || event.target.getAttribute('id') === element) {
if ((!actions[i].exact || event.target.getAttribute('id') === element) && actions[i].button === event.button) {
jsOMS.preventAll(event);
actions[i].callback();
}