From f441d561a232e51e91182ebf276f46d592fbbf60 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Tue, 16 Jul 2019 01:37:35 -0700 Subject: [PATCH 1/2] Refactor duplicate code --- src/webui/www/private/scripts/contextmenu.js | 62 ++++++++------------ 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/src/webui/www/private/scripts/contextmenu.js b/src/webui/www/private/scripts/contextmenu.js index e6599cbac..600f0f377 100644 --- a/src/webui/www/private/scripts/contextmenu.js +++ b/src/webui/www/private/scripts/contextmenu.js @@ -39,7 +39,6 @@ var ContextMenu = new Class({ menu: 'menu_id', stopEvent: true, targets: 'body', - trigger: 'contextmenu', offsets: { x: 0, y: 0 @@ -142,50 +141,41 @@ var ContextMenu = new Class({ } }, - addTarget: function(t) { - this.targets[this.targets.length] = t; - t.addEvent(this.options.trigger, function(e) { - //enabled? - if (!this.options.disabled) { - //prevent default, if told to - if (this.options.stopEvent) { - e.stop(); - } - //record this as the trigger - this.options.element = $(t); - this.adjustMenuPosition(e); - //show the menu - this.show(); - } + setupEventListeners: function(elem) { + elem.addEvent('contextmenu', function(e) { + this.triggerMenu(e, elem); }.bind(this)); - t.addEvent('click', function(e) { + elem.addEvent('click', function(e) { this.hide(); }.bind(this)); }, + addTarget: function(t) { + this.targets[this.targets.length] = t; + this.setupEventListeners(t); + }, + + triggerMenu: function(e, el) { + if (this.options.disabled) + return; + + //prevent default, if told to + if (this.options.stopEvent) { + e.stop(); + } + //record this as the trigger + this.options.element = $(el); + this.adjustMenuPosition(e); + //show the menu + this.show(); + }, + //get things started startListener: function() { /* all elements */ this.targets.each(function(el) { - /* show the menu */ - el.addEvent(this.options.trigger, function(e) { - //enabled? - if (!this.options.disabled) { - //prevent default, if told to - if (this.options.stopEvent) { - e.stop(); - } - //record this as the trigger - this.options.element = $(el); - this.adjustMenuPosition(e); - //show the menu - this.show(); - } - }.bind(this)); - el.addEvent('click', function(e) { - this.hide(); - }.bind(this)); - }, this); + this.setupEventListeners(el); + }.bind(this), this); /* menu items */ this.menu.getElements('a').each(function(item) { From 6af01cfcbe7a54b67674c010640b89d159fd6e1c Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Tue, 16 Jul 2019 02:21:36 -0700 Subject: [PATCH 2/2] Add WebUI support for triggering context menus on mobile --- src/webui/www/private/scripts/contextmenu.js | 18 +++++++++++++++++- src/webui/www/private/scripts/dynamicTable.js | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/webui/www/private/scripts/contextmenu.js b/src/webui/www/private/scripts/contextmenu.js index 600f0f377..899204c42 100644 --- a/src/webui/www/private/scripts/contextmenu.js +++ b/src/webui/www/private/scripts/contextmenu.js @@ -46,7 +46,8 @@ var ContextMenu = new Class({ onShow: $empty, onHide: $empty, onClick: $empty, - fadeSpeed: 200 + fadeSpeed: 200, + touchTimer: 600 }, //initialization @@ -148,6 +149,21 @@ var ContextMenu = new Class({ elem.addEvent('click', function(e) { this.hide(); }.bind(this)); + + elem.addEvent('touchstart', function(e) { + e.preventDefault(); + clearTimeout(this.touchstartTimer); + this.hide(); + + const touchstartEvent = e; + this.touchstartTimer = setTimeout(function() { + this.triggerMenu(touchstartEvent, elem); + }.bind(this), this.options.touchTimer); + }.bind(this)); + elem.addEvent('touchend', function(e) { + e.preventDefault(); + clearTimeout(this.touchstartTimer); + }.bind(this)); }, addTarget: function(t) { diff --git a/src/webui/www/private/scripts/dynamicTable.js b/src/webui/www/private/scripts/dynamicTable.js index 784bdd7d0..0871c08eb 100644 --- a/src/webui/www/private/scripts/dynamicTable.js +++ b/src/webui/www/private/scripts/dynamicTable.js @@ -706,6 +706,13 @@ var DynamicTable = new Class({ } return false; }); + tr.addEvent('touchstart', function(e) { + if (!this._this.isRowSelected(this.rowId)) { + this._this.deselectAll(); + this._this.selectRow(this.rowId); + } + return false; + }); this.setupTr(tr);