2008-07-09 11:30:32 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Spatial Navigation.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Corporation
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Doug Turner <dougt@meer.net> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Import this module through
|
|
|
|
*
|
|
|
|
* Components.utils.import("resource://gre/modules/SpatialNavigation.js");
|
|
|
|
*
|
2008-08-05 16:31:06 -07:00
|
|
|
* Usage: (Literal class)
|
2008-07-09 11:30:32 -07:00
|
|
|
*
|
2008-08-05 16:31:06 -07:00
|
|
|
* SpatialNavigation(browser_element, optional_callback);
|
2008-07-09 11:30:32 -07:00
|
|
|
*
|
|
|
|
* optional_callback will be called when a new element is focused.
|
|
|
|
*
|
|
|
|
* function optional_callback(element) {}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["SpatialNavigation"];
|
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
var SpatialNavigation = {
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
init: function(browser, callback) {
|
|
|
|
browser.addEventListener("keypress", function (event) { _onInputKeyPress(event, callback) }, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function() {
|
|
|
|
}
|
2008-07-09 11:30:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Private stuff
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
function dump(msg)
|
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
var console = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
|
|
|
|
console.logStringMessage("*** SNAV: " + msg);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var gDirectionalBias = 10;
|
|
|
|
var gRectFudge = 1;
|
|
|
|
|
|
|
|
function _onInputKeyPress (event, callback) {
|
2008-07-23 10:47:31 -07:00
|
|
|
|
|
|
|
// If it isn't an arrow key, bail.
|
|
|
|
if (event.keyCode != event.DOM_VK_LEFT &&
|
|
|
|
event.keyCode != event.DOM_VK_RIGHT &&
|
|
|
|
event.keyCode != event.DOM_VK_UP &&
|
|
|
|
event.keyCode != event.DOM_VK_DOWN )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// check to see if we are in a textarea or text input element, and if so,
|
|
|
|
// ensure that we let the arrow keys work properly.
|
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
var target = event.target;
|
|
|
|
|
|
|
|
if (target instanceof Ci.nsIDOMHTMLHtmlElement) {
|
|
|
|
_focusNextUsingCmdDispatcher(event, callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if ((target instanceof Ci.nsIDOMHTMLInputElement && (target.type == "text" || target.type == "password")) ||
|
|
|
|
target instanceof Ci.nsIDOMHTMLTextAreaElement ) {
|
|
|
|
|
|
|
|
// if there is any selection at all, just ignore
|
|
|
|
if (target.selectionEnd - target.selectionStart > 0)
|
|
|
|
return;
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// if there is no text, there is nothing special to do.
|
|
|
|
|
|
|
|
if (target.textLength > 0) {
|
|
|
|
|
|
|
|
if (event.keyCode == event.DOM_VK_RIGHT || event.keyCode == event.DOM_VK_DOWN ) {
|
|
|
|
// we are moving forward into the document
|
|
|
|
if (target.textLength != target.selectionEnd)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we are at the start of the text, okay to move
|
|
|
|
if (target.selectionStart != 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if we are in a select
|
|
|
|
if (target instanceof Ci.nsIDOMHTMLSelectElement)
|
|
|
|
{
|
|
|
|
if (event.keyCode == event.DOM_VK_DOWN ) {
|
|
|
|
if (target.selectedIndex + 1 < target.length)
|
2008-07-16 14:54:50 -07:00
|
|
|
return;
|
2008-07-23 10:47:31 -07:00
|
|
|
}
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (event.keyCode == event.DOM_VK_UP) {
|
|
|
|
if (target.selectedIndex > 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function snavfilter(node) {
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (node instanceof Ci.nsIDOMHTMLLinkElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLAnchorElement) {
|
|
|
|
// if a anchor doesn't have a href, don't target it.
|
|
|
|
if (node.href == "")
|
2008-07-09 11:30:32 -07:00
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_SKIP;
|
2008-07-23 10:47:31 -07:00
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_ACCEPT;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if ((node instanceof Ci.nsIDOMHTMLButtonElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLInputElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLLinkElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLOptGroupElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLSelectElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLTextAreaElement) &&
|
|
|
|
node.disabled == false)
|
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_ACCEPT;
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_SKIP;
|
|
|
|
}
|
|
|
|
var bestElementToFocus = null;
|
|
|
|
var distanceToBestElement = Infinity;
|
|
|
|
var focusedRect = _inflateRect(target.getBoundingClientRect(),
|
|
|
|
- gRectFudge);
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var doc = target.ownerDocument;
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var treeWalker = doc.createTreeWalker(doc, Ci.nsIDOMNodeFilter.SHOW_ELEMENT, snavfilter, false);
|
|
|
|
var nextNode;
|
|
|
|
|
|
|
|
while ((nextNode = treeWalker.nextNode())) {
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (nextNode == target)
|
|
|
|
continue;
|
2008-07-16 10:00:27 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var nextRect = _inflateRect(nextNode.getBoundingClientRect(),
|
|
|
|
- gRectFudge);
|
|
|
|
|
|
|
|
if (! _isRectInDirection(event, focusedRect, nextRect))
|
|
|
|
continue;
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var distance = _spatialDistance(event, focusedRect, nextRect);
|
2008-08-05 15:44:53 -07:00
|
|
|
|
|
|
|
//dump("looking at: " + nextNode + " " + distance);
|
2008-07-23 10:47:31 -07:00
|
|
|
|
|
|
|
if (distance <= distanceToBestElement && distance > 0) {
|
|
|
|
distanceToBestElement = distance;
|
|
|
|
bestElementToFocus = nextNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestElementToFocus != null) {
|
2008-08-05 15:44:53 -07:00
|
|
|
//dump("focusing element " + bestElementToFocus.nodeName + " " + bestElementToFocus) + "id=" + bestElementToFocus.getAttribute("id");
|
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// Wishing we could do element.focus()
|
|
|
|
doc.defaultView.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).focus(bestElementToFocus);
|
2008-07-16 14:54:50 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// if it is a text element, select all.
|
|
|
|
if((bestElementToFocus instanceof Ci.nsIDOMHTMLInputElement && (bestElementToFocus.type == "text" || bestElementToFocus.type == "password")) ||
|
|
|
|
bestElementToFocus instanceof Ci.nsIDOMHTMLTextAreaElement ) {
|
|
|
|
bestElementToFocus.selectionStart = 0;
|
|
|
|
bestElementToFocus.selectionEnd = bestElementToFocus.textLength;
|
2008-07-16 14:54:50 -07:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
|
|
|
|
if (callback != undefined)
|
|
|
|
callback(bestElementToFocus);
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
} else {
|
|
|
|
// couldn't find anything. just advance and hope.
|
2008-08-05 15:44:53 -07:00
|
|
|
_focusNextUsingCmdDispatcher(event, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _focusNextUsingCmdDispatcher(event, callback) {
|
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var windowMediator = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
|
|
|
|
var window = windowMediator.getMostRecentWindow("navigator:browser");
|
2008-08-05 15:44:53 -07:00
|
|
|
|
|
|
|
if (event.keyCode == event.DOM_VK_RIGHT || event.keyCode == event.DOM_VK_DOWN ) {
|
2008-07-23 10:47:31 -07:00
|
|
|
window.document.commandDispatcher.advanceFocus();
|
2008-08-05 15:44:53 -07:00
|
|
|
} else {
|
2008-07-23 10:47:31 -07:00
|
|
|
window.document.commandDispatcher.rewindFocus();
|
2008-08-05 15:44:53 -07:00
|
|
|
}
|
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (callback != undefined)
|
|
|
|
callback(null);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function _isRectInDirection(event, focusedRect, anotherRect)
|
|
|
|
{
|
|
|
|
if (event.keyCode == event.DOM_VK_LEFT) {
|
2008-07-23 10:47:31 -07:00
|
|
|
return (anotherRect.left < focusedRect.left);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode == event.DOM_VK_RIGHT) {
|
2008-07-23 10:47:31 -07:00
|
|
|
return (anotherRect.right > focusedRect.right);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode == event.DOM_VK_UP) {
|
2008-07-23 10:47:31 -07:00
|
|
|
return (anotherRect.top < focusedRect.top);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode == event.DOM_VK_DOWN) {
|
2008-07-23 10:47:31 -07:00
|
|
|
return (anotherRect.bottom > focusedRect.bottom);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _inflateRect(rect, value)
|
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
var newRect = new Object();
|
|
|
|
|
|
|
|
newRect.left = rect.left - value;
|
|
|
|
newRect.top = rect.top - value;
|
|
|
|
newRect.right = rect.right + value;
|
|
|
|
newRect.bottom = rect.bottom + value;
|
|
|
|
return newRect;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function _containsRect(a, b)
|
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
return ( (b.left <= a.right) &&
|
|
|
|
(b.right >= a.left) &&
|
|
|
|
(b.top <= a.bottom) &&
|
|
|
|
(b.bottom >= a.top) );
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function _spatialDistance(event, a, b)
|
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
var inlineNavigation = false;
|
|
|
|
var mx, my, nx, ny;
|
|
|
|
|
|
|
|
if (event.keyCode == event.DOM_VK_LEFT) {
|
|
|
|
|
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
|
|
|
// |---| |---|
|
|
|
|
// |---| |---|
|
|
|
|
//
|
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.top > b.bottom) {
|
|
|
|
// the b rect is above a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.right;
|
|
|
|
ny = b.bottom;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
else if (a.bottom < b.top) {
|
|
|
|
// the b rect is below a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.right;
|
|
|
|
ny = b.top;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mx = a.left;
|
|
|
|
my = 0;
|
|
|
|
nx = b.right;
|
|
|
|
ny = 0;
|
|
|
|
}
|
|
|
|
} else if (event.keyCode == event.DOM_VK_RIGHT) {
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
|
|
|
// |---| |---|
|
|
|
|
// |---| |---|
|
|
|
|
//
|
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.top > b.bottom) {
|
|
|
|
// the b rect is above a.
|
|
|
|
mx = a.right;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.left;
|
|
|
|
ny = b.bottom;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
else if (a.bottom < b.top) {
|
|
|
|
// the b rect is below a.
|
|
|
|
mx = a.right;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.left;
|
|
|
|
ny = b.top;
|
|
|
|
} else {
|
|
|
|
mx = a.right;
|
|
|
|
my = 0;
|
|
|
|
nx = b.left;
|
|
|
|
ny = 0;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
} else if (event.keyCode == event.DOM_VK_UP) {
|
|
|
|
|
|
|
|
// |---| |---| |---|
|
|
|
|
// |---| |---| |---|
|
|
|
|
//
|
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.left > b.right) {
|
|
|
|
// the b rect is to the left of a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.right;
|
|
|
|
ny = b.bottom;
|
|
|
|
} else if (a.right < b.left) {
|
|
|
|
// the b rect is to the right of a
|
|
|
|
mx = a.right;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.left;
|
|
|
|
ny = b.bottom;
|
|
|
|
} else {
|
|
|
|
// both b and a share some common x's.
|
|
|
|
mx = 0;
|
|
|
|
my = a.top;
|
|
|
|
nx = 0;
|
|
|
|
ny = b.bottom;
|
|
|
|
}
|
|
|
|
} else if (event.keyCode == event.DOM_VK_DOWN) {
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
|
|
|
// |---| |---| |---|
|
|
|
|
// |---| |---| |---|
|
|
|
|
//
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.left > b.right) {
|
|
|
|
// the b rect is to the left of a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.right;
|
|
|
|
ny = b.top;
|
|
|
|
} else if (a.right < b.left) {
|
|
|
|
// the b rect is to the right of a
|
|
|
|
mx = a.right;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.left;
|
|
|
|
ny = b.top;
|
|
|
|
} else {
|
|
|
|
// both b and a share some common x's.
|
|
|
|
mx = 0;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = 0;
|
|
|
|
ny = b.top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var scopedRect = _inflateRect(a, gRectFudge);
|
|
|
|
|
|
|
|
if (event.keyCode == event.DOM_VK_LEFT ||
|
|
|
|
event.keyCode == event.DOM_VK_RIGHT) {
|
|
|
|
scopedRect.left = 0;
|
|
|
|
scopedRect.right = Infinity;
|
|
|
|
inlineNavigation = _containsRect(scopedRect, b);
|
|
|
|
}
|
|
|
|
else if (event.keyCode == event.DOM_VK_UP ||
|
|
|
|
event.keyCode == event.DOM_VK_DOWN) {
|
|
|
|
scopedRect.top = 0;
|
|
|
|
scopedRect.bottom = Infinity;
|
|
|
|
inlineNavigation = _containsRect(scopedRect, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
var d = Math.pow((mx-nx), 2) + Math.pow((my-ny), 2);
|
|
|
|
|
|
|
|
// prefer elements directly aligned with the focused element
|
|
|
|
if (inlineNavigation)
|
|
|
|
d /= gDirectionalBias;
|
|
|
|
|
|
|
|
return d;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|