Skip to content

Commit

Permalink
reorder events for click: enter?+down+focus+up+click
Browse files Browse the repository at this point in the history
This fixes a bug that some node may find they are `"focus"`ed twice.

E.g. http://<gogs>:3123/repo/create
* the framework semantic-ui allow both `focus` and `click` events to show a popup menu
* if `focus` occurs before a `mousedown`, then it thinks the event is made by a `tab` key
* or it does nothing on `focus`
  • Loading branch information
gdh1995 committed Jan 22, 2017
1 parent 1b49946 commit cf81b93
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
10 changes: 8 additions & 2 deletions content/dom_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@ VDom.UI = {
sel.removeAllRanges();
return true;
},
click: function(element, modifiers, addFocus) {
element === VDom.lastHovered || VDom.unhoverLast(element, modifiers);
VDom.mouse(element, "mousedown", modifiers);
addFocus && element !== VEventMode.lock() && element.focus();
VDom.mouse(element, "mouseup", modifiers);
VDom.mouse(element, "click", modifiers);
},
simulateSelect: function(element, flash, suppressRepeated) {
element.focus();
VDom.simulateClick(element);
this.click(element, null, true);
flash === true && this.flash(element);
if (element !== VEventMode.lock()) { return; }
var len;
Expand Down
2 changes: 1 addition & 1 deletion content/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ var VSettings, VHUD, VPort, VEventMode;
}
linkElement.scrollIntoViewIfNeeded();
VDom.UI.flash(linkElement);
setTimeout(function() { VDom.simulateClick(linkElement); }, 0);
setTimeout(function() { VDom.UI.click(linkElement); }, 0);
return true;
},
goBy: function(relName, pattern) {
Expand Down
9 changes: 4 additions & 5 deletions content/link_hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ LEAVE: {
129: "Simulate mouse leaving link",
193: "Simulate mouse leaving continuously",
activator: function(element) {
VDom.simulateMouse(element, "mouseout");
VDom.mouse(element, "mouseout");
if (document.activeElement === element) { element.blur(); }
}
},
Expand Down Expand Up @@ -963,7 +963,7 @@ DOWNLOAD_LINK: {
if (oldDownload == null) {
link.download = "";
}
VDom.simulateClick(link, {
VDom.UI.click(link, {
altKey: true,
ctrlKey: false,
metaKey: false,
Expand Down Expand Up @@ -1011,19 +1011,18 @@ DEFAULT: {
VDom.UI.simulateSelect(link, true);
return false;
}
if (mode > 0 || link.tabIndex >= 0) { link.focus(); }
mode = this.mode & 3;
if (mode >= 2 && tag === "a") {
alterTarget = link.getAttribute('target');
link.target = "_top";
}
// NOTE: not clear last hovered item, for that it may be a menu
VDom.simulateClick(link, {
VDom.UI.click(link, {
altKey: false,
ctrlKey: mode >= 2 && !onMac,
metaKey: mode >= 2 && onMac,
shiftKey: mode === 3
});
}, mode > 0 || link.tabIndex >= 0);
if (alterTarget === undefined) {}
else if (alterTarget === null) {
link.removeAttribute("target");
Expand Down
14 changes: 3 additions & 11 deletions lib/dom_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,33 +203,25 @@ var VDom = {
for (o = el; o && o.nodeType !== eTy; o = o.previousSibling) {}
return o || (el && el.parentNode);
},
simulateMouse: function(element, type, modifiers, related) {
mouse: function(element, type, modifiers, related) {
var mouseEvent = document.createEvent("MouseEvents");
modifiers || (modifiers = this.defaultMouseKeys);
mouseEvent.initMouseEvent(type, true, true, window, 1, 0, 0, 0, 0
, modifiers.ctrlKey, modifiers.altKey, modifiers.shiftKey, modifiers.metaKey
, 0, related || null);
return element.dispatchEvent(mouseEvent);
},
_eventSequence: ["click", "mouseup", "mousedown"],
defaultMouseKeys: { altKey: false, ctrlKey: false, metaKey: false, shiftKey: false },
lastHovered: null,
unhoverLast: function(newEl, modifiers) {
var last = this.lastHovered;
if (last && this.isInDOM(last)) {
this.simulateMouse(last, "mouseout", modifiers, newEl !== last ? newEl : null);
this.mouse(last, "mouseout", modifiers, newEl !== last ? newEl : null);
} else {
last = null;
}
this.lastHovered = newEl;
newEl && this.simulateMouse(newEl, "mouseover", modifiers, last);
},
simulateClick: function(element, modifiers) {
var eventSequence = this._eventSequence, _i;
element === this.lastHovered || this.unhoverLast(element, modifiers);
for (_i = eventSequence.length; 0 <= --_i; ) {
this.simulateMouse(element, eventSequence[_i], modifiers);
}
newEl && this.mouse(newEl, "mouseover", modifiers, last);
}
},

Expand Down
2 changes: 1 addition & 1 deletion pages/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function clickLink(options, event) {
}
a.href = url;
if (window.VDom) {
VDom.simulateClick(a, event);
VDom.UI.click(a, event);
} else {
a.click();
}
Expand Down

0 comments on commit cf81b93

Please sign in to comment.