aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/dom_utils.coffee41
-rw-r--r--lib/utils.coffee1
2 files changed, 29 insertions, 13 deletions
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee
index ee7d415f..7473df17 100644
--- a/lib/dom_utils.coffee
+++ b/lib/dom_utils.coffee
@@ -223,23 +223,38 @@ DomUtils =
handlerStack.bubbleEvent "click", target: element
else
element.focus()
- # If the cursor is at the start of the element's contents, send it to the end. Motivation:
- # * the end is a more useful place to focus than the start,
- # * this way preserves the last used position (except when it's at the beginning), so the user can
- # 'resume where they left off'.
- # NOTE(mrmr1993): Some elements throw an error when we try to access their selection properties, so
- # wrap this with a try.
- try
- if element.selectionStart == 0 and element.selectionEnd == 0
- element.setSelectionRange element.value.length, element.value.length
-
-
+ if element.tagName.toLowerCase() != "textarea"
+ # If the cursor is at the start of the (non-textarea) element's contents, send it to the end. Motivation:
+ # * the end is a more useful place to focus than the start,
+ # * this way preserves the last used position (except when it's at the beginning), so the user can
+ # 'resume where they left off'.
+ # NOTE(mrmr1993): Some elements throw an error when we try to access their selection properties, so
+ # wrap this with a try.
+ try
+ if element.selectionStart == 0 and element.selectionEnd == 0
+ element.setSelectionRange element.value.length, element.value.length
+
+ simulateUnhover: (element, modifiers) -> @simulateMouseEvent "mouseout", element, modifiers
simulateClick: (element, modifiers) ->
- modifiers ||= {}
-
eventSequence = ["mouseover", "mousedown", "mouseup", "click"]
for event in eventSequence
+ @simulateMouseEvent event, element, modifiers
+
+ simulateMouseEvent: do ->
+ lastHoveredElement = undefined
+ (event, element, modifiers = {}) ->
+
+ if event == "mouseout"
+ element ?= lastHoveredElement # Allow unhovering the last hovered element by passing undefined.
+ lastHoveredElement = undefined
+ return unless element?
+
+ else if event == "mouseover"
+ # Simulate moving the mouse off the previous element first, as if we were a real mouse.
+ @simulateMouseEvent "mouseout", undefined, modifiers
+ lastHoveredElement = element
+
mouseEvent = document.createEvent("MouseEvents")
mouseEvent.initMouseEvent(event, true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, modifiers.altKey,
modifiers.shiftKey, modifiers.metaKey, 0, null)
diff --git a/lib/utils.coffee b/lib/utils.coffee
index b69b926b..c255102e 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -281,6 +281,7 @@ Array.copy = (array) -> Array.prototype.slice.call(array, 0)
String::startsWith = (str) -> @indexOf(str) == 0
String::ltrim = -> @replace /^\s+/, ""
String::rtrim = -> @replace /\s+$/, ""
+String::reverse = -> @split("").reverse().join ""
globalRoot = window ? global
globalRoot.extend = (hash1, hash2) ->