diff --git a/UI/Input/Mouse/EventType.enum.js b/UI/Input/Mouse/EventType.enum.js new file mode 100644 index 0000000..3470e9c --- /dev/null +++ b/UI/Input/Mouse/EventType.enum.js @@ -0,0 +1,19 @@ +/** + * Click type. + * + * @author OMS Development Team + * @author Dennis Eichhorn + * @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 || {})); diff --git a/UI/Input/Mouse/MouseManager.js b/UI/Input/Mouse/MouseManager.js index a818725..ee4a991 100644 --- a/UI/Input/Mouse/MouseManager.js +++ b/UI/Input/Mouse/MouseManager.js @@ -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(); }