satyr, thank you so much! The only bug left is that
event.preventDefault() doesn't seem to be preventing the Alt-Up and
Alt-Down keystrokes from scrolling through the command list... but my
temporary workaround is asking my coworkers to set max suggestions to
1 because they're not using Ubiquity for anything else. I'll get a
real solution in the near future, though! (Didn't catch it in testing
because I keep my max suggestions on 1 anyway.)
For reference, here's the command that I ended up with (boring parts
removed and comments added):
preview: function preview(pblock, args) {
// scope is a beautiful thing
var index = 0;
var reports;
var numReports;
// Given a single result, display its information.
function showResult(result) {
var output = "";
output += "<b>" + result.reportId + "</b> - " +
result.reportName + "<br />";
output += "Owner: " + (null == report.owner ? "Nobody" :
report.owner) + "<br />";
output += "Status: " + (null == report.status ? "No action
yet" : report.status);
if (numReports > 1) { output += "<br /><br />Viewing result " +
(index + 1) + " of " + numReports; }
pblock.innerHTML = output;
}
function changeResult(event){
// ignore keystrokes that are just Alt key or don't include Alt
key
if(!event.altKey || event.view.KeyEvent.DOM_VK_ALT ===
event.which) return;
// increment index if Alt-Down (loop back if too high) and
update view
if (40 == event.which) {
index++; if (index >= numReports) index = 0;
showResult(reports[index]);
}
// decrement index if Alt-Up (loop back if too low) and update
view
else if (38 == event.which) {
index--; if (index < 0) index = numReports - 1;
showResult(reports[index]);
}
else return;
event.preventDefault();
}
// attach event handlers as appropriate, thanks satyr!
var tbox = context.chromeWindow.gUbiquity.textBox;
tbox.addEventListener('keydown', changeResult, true);
pblock.addEventListener('preview-change', function update() {
tbox.removeEventListener('keydown', changeResult, true);
pblock.removeEventListener('preview-change', update, false);
}, false);
// set initial conditions and make sure we have enough text to
narrow by
pblock.innerHTML = "Enter a partial name for results...";
if (args.object.text.length < 5) return;
// process search text; the ^ and $ stuff could've been processed
on the PHP side, but oh well
var searchText;
if (/^\^/.test(args.object.text)) { searchText = escape
(args.object.text.slice(1) + "%"); }
else if (/\$$/.test(args.object.text)) { searchText = escape("%" +
args.object.text.slice(0, -1)); }
else { searchText = escape("%" + args.object.text + "%"); }
// detail.php can also accept parameters of type=id and value=
[digits] for a separate Ubiquity command
var url = "http://localhost/detail.php?type=name&value=" +
searchText;
// the output from detail.php is JSON, { "reports" : [array of
report objects] }
CmdUtils.previewGet(pblock, url, function (json) {
index = 0;
numReports = 0;
response = Utils.json.decode(json);
reports = response.reports;
for (i in reports) { numReports++; }
showResult(reports[index]);
});
}
On Jan 28, 8:34 pm, satyr <[email protected]> wrote:
> On Thu, Jan 28, 2010 at 4:38 AM, Cyranix <[email protected]> wrote:
> > If anyone has an example of correctly binding a
> > key listener to part of the Ubiquity panel, I would greatly appreciate
> > seeing it.
>
> const ID = 'something_unique';
> CmdUtils.CreateCommand({
> name: 'test new results on keys',
> preview: function pv(pb){
> if(pb.ownerDocument.getElementById(ID)) return;
> function onkd(ev){
> if(!ev.shiftKey || ev.which === ev.view.KeyEvent.DOM_VK_SHIFT) return;
> pb.ownerDocument.getElementById(ID).textContent = ev.which;
> ev.preventDefault();
> }
> var tb = context.chromeWindow.gUbiquity.textBox;
> tb.addEventListener('keydown', onkd, true);
> pb.addEventListener('preview-change', function onpc(){
> tb.removeEventListener('keydown', onkd, true);
> pb.removeEventListener('preview-change', onpc, false);
> }, false);
> pb.innerHTML = <>shift + key<h1 id={ID}>code</h1></>;
> },
>
> });
> > I suppose I could hack at an accesskey solution, wiring up some faux-
> > button links to ^P/^N and tying into a function for cycling through
> > the results, if push comes to shove. But unlike "google image search"
> > I'm not trying to activate a currently visible result; I'm trying to
> > get different results to appear.
>
> Also see check-google-calendar.
--
You received this message because you are subscribed to the Google Groups
"ubiquity-firefox" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/ubiquity-firefox?hl=en.