Commit 4c8b2096 authored by coderus's avatar coderus

Handling LongTap events by js.

Element for context menu now "closest"
parent ffec4ca8
......@@ -19,6 +19,10 @@ let HTMLFrameElement = Ci.nsIDOMHTMLFrameElement;
XPCOMUtils.defineLazyServiceGetter(this, "DOMUtils",
"@mozilla.org/inspector/dom-utils;1", "inIDOMUtils");
XPCOMUtils.defineLazyServiceGetter(Services, "embedlite",
"@mozilla.org/embedlite-app-service;1",
"nsIEmbedAppService");
const kStateActive = 0x00000001; // :active pseudoclass for elements
dump("###################################### embedhelper.js loaded\n");
......@@ -40,6 +44,8 @@ EmbedHelper.prototype = {
dump("Init Called:" + this + "\n");
Services.prefs.setBoolPref("embedlite.azpc.handle.singletap", false);
Services.prefs.setBoolPref("embedlite.azpc.json.singletap", true);
Services.prefs.setBoolPref("embedlite.azpc.handle.longtap", false);
Services.prefs.setBoolPref("embedlite.azpc.json.longtap", true);
addEventListener("touchstart", this, false);
addEventListener("touchmove", this, false);
addEventListener("touchend", this, false);
......@@ -70,6 +76,40 @@ EmbedHelper.prototype = {
_touchElement: null,
getLinkHrefForElement: function getLinkHrefForElement(target) {
let element = null;
while (target) {
if (target instanceof Ci.nsIDOMHTMLAnchorElement ||
target instanceof Ci.nsIDOMHTMLAreaElement ||
target instanceof Ci.nsIDOMHTMLLinkElement) {
if (target.hasAttribute("href"))
element = target;
}
target = target.parentNode;
}
if (element && element.hasAttribute("href"))
return element.href;
else
return null;
},
getImageSrcForElement: function getImageSrcForElement(target) {
let element = null;
while (target) {
if (target instanceof Ci.nsIDOMHTMLImageElement) {
if (target.hasAttribute("src"))
element = target;
}
target = target.parentNode;
}
if (element && element.hasAttribute("src"))
return element.src;
else
return null;
},
receiveMessage: function receiveMessage(aMessage) {
switch (aMessage.name) {
case "Gesture:SingleTap": {
......@@ -92,6 +132,19 @@ EmbedHelper.prototype = {
}
break;
}
case "Gesture:LongTap": {
let element = this._touchElement;
if (element) {
let linkHref = this.getLinkHrefForElement(element);
let imageSrc = this.getImageSrcForElement(element);
if (linkHref || imageSrc) {
let window = element.ownerDocument.defaultView;
var winid = Services.embedlite.getIDByWindow(window);
Services.embedlite.sendAsyncMessage(winid, "context:info", JSON.stringify({LinkHref: linkHref ? linkHref : "", ImageSrc: imageSrc ? imageSrc : ""}));
}
}
this._touchElement = null;
}
default: {
dump("Child Script: Message: name:" + aMessage.name + ", json:" + JSON.stringify(aMessage.json) + "\n");
}
......
......@@ -68,119 +68,6 @@ void EmbedTouchListener::HandleSingleTap(const nsIntPoint& aPoint)
void EmbedTouchListener::HandleLongTap(const nsIntPoint& aPoint)
{
LOGT("pt[%i,%i]", aPoint.x, aPoint.y);
nsresult rv;
nsCOMPtr<nsIDOMElement> element;
gfxRect retRect(0,0,0,0);
AnyElementFromPoint(DOMWindow, aPoint.x, aPoint.y, getter_AddRefs(element));
if (!element) {
// Clicked some unknown area without elements... return here
return;
}
nsAutoString localName;
nsAutoString aHRef;
nsAutoString aSrc;
element->GetLocalName(localName);
nsCOMPtr<nsIDOMElement> linkContent;
if (localName.LowerCaseEqualsLiteral("a") ||
localName.LowerCaseEqualsLiteral("area") ||
localName.LowerCaseEqualsLiteral("link")) {
bool hasAttr;
element->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
if (hasAttr) {
linkContent = element;
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
if (anchor){
anchor->GetHref(aHRef);
}
else {
nsCOMPtr<nsIDOMHTMLAreaElement> area(do_QueryInterface(linkContent));
if (area){
area->GetHref(aHRef);
}
else {
nsCOMPtr<nsIDOMHTMLLinkElement> link(do_QueryInterface(linkContent));
if (link){
link->GetHref(aHRef);
}
}
}
}
}
else if (localName.LowerCaseEqualsLiteral("img")) {
bool hasAttr;
element->HasAttribute(NS_LITERAL_STRING("src"), &hasAttr);
if (hasAttr) {
linkContent = element;
nsCOMPtr<nsIDOMHTMLImageElement> image(do_QueryInterface(linkContent));
if (image){
image->GetSrc(aSrc);
}
}
}
nsCOMPtr<nsIDOMNode> curr;
element->GetParentNode(getter_AddRefs(curr));
while (curr) {
element = do_QueryInterface(curr);
if (!element)
break;
element->GetLocalName(localName);
if (localName.LowerCaseEqualsLiteral("a") ||
localName.LowerCaseEqualsLiteral("area") ||
localName.LowerCaseEqualsLiteral("link")) {
bool hasAttr;
element->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);
if (hasAttr) {
linkContent = element;
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor(do_QueryInterface(linkContent));
if (anchor) {
anchor->GetHref(aHRef);
}
else {
nsCOMPtr<nsIDOMHTMLAreaElement> area(do_QueryInterface(linkContent));
if (area){
area->GetHref(aHRef);
}
else {
nsCOMPtr<nsIDOMHTMLLinkElement> link(do_QueryInterface(linkContent));
if (link){
link->GetHref(aHRef);
}
}
}
}
else
linkContent = nullptr; // Links can't be nested.
break;
}
else if (localName.LowerCaseEqualsLiteral("img")) {
bool hasAttr;
element->HasAttribute(NS_LITERAL_STRING("src"), &hasAttr);
if (hasAttr) {
linkContent = element;
nsCOMPtr<nsIDOMHTMLImageElement> image(do_QueryInterface(linkContent));
if (image)
image->GetSrc(aSrc);
}
else
linkContent = nullptr; // Links can't be nested.
break;
}
nsCOMPtr<nsIDOMNode> temp = curr;
temp->GetParentNode(getter_AddRefs(curr));
}
if (!aHRef.IsEmpty() || !aSrc.IsEmpty()) {
nsString sendString;
nsCOMPtr<nsIEmbedLiteJSON> json = do_GetService("@mozilla.org/embedlite-json;1");
nsCOMPtr<nsIWritablePropertyBag2> root;
json->CreateObject(getter_AddRefs(root));
root->SetPropertyAsAString(NS_LITERAL_STRING("LinkHref"), aHRef);
root->SetPropertyAsAString(NS_LITERAL_STRING("ImageSrc"), aSrc);
json->CreateJSON(root, sendString);
mService->SendAsyncMessage(mTopWinid, NS_LITERAL_STRING("context:info").get(), sendString.get());
}
}
void EmbedTouchListener::SendAsyncScrollDOMEvent(const mozilla::gfx::Rect& aRect,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment