зеркало из https://github.com/mozilla/ubiquity.git
CreateCommand: executeDefault() now handles the case where options.execute isn't a function. Renamed cmd._previewString to cmd.previewHtml. Added cmd.global.
This commit is contained in:
Родитель
e4bddcdfcd
Коммит
b290cc51c1
|
@ -19,6 +19,7 @@
|
|||
*
|
||||
* Contributor(s):
|
||||
* Michael Yoshitaka Erlewine <mitcho@mitcho.com>
|
||||
* Satoshi Murakami <murky.satyr@gmail.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -47,13 +48,16 @@ function displayTemplate(feedUri, feedKey) {
|
|||
"",
|
||||
'msgid ""',
|
||||
'msgstr ""',
|
||||
'"Project-Id-Version: Ubiquity 0.5\\n"',
|
||||
'"Project-Id-Version: Ubiquity ' + UbiquitySetup.version + '\\n"',
|
||||
'"POT-Creation-Date: ' + potCreationDate(new Date) + '\\n"',
|
||||
"\n\n"].join("\n");
|
||||
|
||||
po += "#. [Globals]\n";
|
||||
for each (let gKey in LocalizationUtils.getFeedGlobals(feedUri))
|
||||
po += potMsgs("", gKey);
|
||||
|
||||
po += ("#. [Commands]\n" +
|
||||
"#. For *.names, use | to separate multiple name values.\n");
|
||||
for each (let cmd in feed.commands)
|
||||
po += cmdTemplate(cmd);
|
||||
|
||||
|
@ -66,42 +70,37 @@ var localizableProperties = ["names", "help", "description"];
|
|||
var contexts = ["preview", "execute"];
|
||||
|
||||
function cmdTemplate(cmd) {
|
||||
var po = "\n#. " + cmd.referenceName + " command:\n";
|
||||
var po = "\n#. " + cmd.referenceName + "\n";
|
||||
for each (let key in localizableProperties)
|
||||
po += cmdPropertyLine(cmd, key);
|
||||
for each (let key in contexts) {
|
||||
var fn = cmd.proto[key];
|
||||
if (typeof fn === "function")
|
||||
po += cmdInlineLine(cmd, fn + "", key);
|
||||
po += cmdInlineLine(cmd, fn.toSource(-1), key);
|
||||
}
|
||||
return po;
|
||||
}
|
||||
|
||||
function cmdPropertyLine(cmd, property) {
|
||||
if (!(property in cmd)) return "";
|
||||
var help = (property === "names"
|
||||
? "#. use | to separate multiple name values:\n"
|
||||
: "");
|
||||
|
||||
var value = cmd[property];
|
||||
if (typeof value.join === "function") value = value.join("|");
|
||||
return help + potMsgs(cmd.referenceName + "." + property, value);
|
||||
return potMsgs(cmd.referenceName + "." + property, value);
|
||||
}
|
||||
|
||||
function cmdInlineLine(cmd, cmdCode, context) {
|
||||
if (context === "preview" && "_previewString" in cmd)
|
||||
return cmdPreviewString(cmd);
|
||||
if (context === "preview" && "previewHtml" in cmd)
|
||||
return potMsgs(cmd.referenceName + ".preview",
|
||||
cmd.previewHtml);
|
||||
|
||||
var inlineChecker = /\W_\(("[^\\\"]*(?:\\.[^\\\"]*)*")/g;
|
||||
inlineChecker.lastIndex = 0;
|
||||
var po = "";
|
||||
while (inlineChecker.test(cmdCode))
|
||||
var po = "", underScores = /\b_\(("[^\\\"]+(?:\\.[^\\\"]*)*")/g;
|
||||
underScores.lastIndex = "function ()".length;
|
||||
while (underScores.test(cmdCode))
|
||||
po += potMsgs(cmd.referenceName + "." + context, JSON.parse(RegExp.$1));
|
||||
return po;
|
||||
}
|
||||
|
||||
function cmdPreviewString(cmd) potMsgs(cmd.referenceName + ".preview",
|
||||
cmd._previewString);
|
||||
|
||||
function potMsgs(context, id) (
|
||||
(context && 'msgctxt "' + quoteString(context) + '"\n') +
|
||||
'msgid "'+ quoteString(id).replace(/\n/g, '\\n"\n"') + '"\n' +
|
||||
|
|
|
@ -77,8 +77,7 @@ var CmdUtils = {
|
|||
//
|
||||
// Ubiquity 0.1.x only supports parser version 1, while
|
||||
// Ubiquity 0.5.x supports parser versions 1 and 2.
|
||||
get parserVersion()
|
||||
Utils.prefs.get("extensions.ubiquity.parserVersion", 1),
|
||||
get parserVersion() Utils.prefs.get("extensions.ubiquity.parserVersion", 1),
|
||||
|
||||
// === {{{ CmdUtils.maxSuggestions }}} ===
|
||||
// The current number of max suggestions.
|
||||
|
@ -342,7 +341,7 @@ function onPageLoad(callback, includes, excludes) {
|
|||
// the chrome window associated with it.
|
||||
|
||||
function onUbiquityLoad(callback) {
|
||||
this.__globalObject.ubiquityLoadFuncs.push(callback);
|
||||
return this.__globalObject.ubiquityLoadFuncs.push(callback);
|
||||
}
|
||||
|
||||
// ** {{{ CmdUtils.setLastResult(result) }}} **
|
||||
|
@ -643,14 +642,15 @@ function retrieveLogins(name) [
|
|||
// based off the URL from which the feed is being retrieved.
|
||||
|
||||
function CreateCommand(options) {
|
||||
var me = this;
|
||||
var global = this.__globalObject;
|
||||
var command = {
|
||||
__proto__: options,
|
||||
proto: options,
|
||||
global: global,
|
||||
previewDefault: CreateCommand.previewDefault,
|
||||
};
|
||||
|
||||
var me = this;
|
||||
function toNounType(obj, key) {
|
||||
var val = obj[key];
|
||||
if (!val) return;
|
||||
|
@ -697,9 +697,9 @@ function CreateCommand(options) {
|
|||
else if (!Utils.isArray(args)) {
|
||||
// arguments: {role: noun, ...}
|
||||
// arguments: {"role label": noun, ...}
|
||||
let a = [];
|
||||
let a = [], re = /^[a-z]+(?=(?:[$_:\s]([^]+))?)/;
|
||||
for (let key in args) {
|
||||
let [role, label] = /^[a-z]+(?=(?:[$_:\s]([^]+))?)/(key) || 0;
|
||||
let [role, label] = re(key) || 0;
|
||||
if (role) a.push({role: role, label: label, nountype: args[key]});
|
||||
}
|
||||
args = a;
|
||||
|
@ -708,47 +708,45 @@ function CreateCommand(options) {
|
|||
command.arguments = args;
|
||||
}
|
||||
{ let {execute, preview} = options;
|
||||
if (typeof execute !== "function") {
|
||||
let uri;
|
||||
try { uri = global.Utils.url(execute) } catch(e) {}
|
||||
command.execute = uri ? function executeOpen() {
|
||||
Utils.focusUrlInBrowser(uri.spec);
|
||||
} : function executeDisplay() {
|
||||
//errorToLocalize
|
||||
global.displayMessage(execute || "No action defined.");
|
||||
};
|
||||
}
|
||||
if (preview == null)
|
||||
if (typeof execute !== "function")
|
||||
command.execute = CreateCommand.executeDefault;
|
||||
if (typeof preview !== "function") {
|
||||
if (preview != null) command.previewHtml = String(preview);
|
||||
command.preview = CreateCommand.previewDefault;
|
||||
else if (typeof preview !== "function") {
|
||||
// wrap it in a function that does what you'd expect it to.
|
||||
command.preview = function previewHtml(pblock) {
|
||||
pblock.innerHTML = this._previewString;
|
||||
};
|
||||
command._previewString = String(preview);
|
||||
}
|
||||
}
|
||||
|
||||
if ("previewUrl" in options && !options.__lookupGetter__("previewUrl"))
|
||||
// Call our "patched" Utils.url(), which has the ability
|
||||
// Call our "patched" Utils.uri(), which has the ability
|
||||
// to base a relative URL on the current feed's URL.
|
||||
command.previewUrl = global.Utils.url(options.previewUrl);
|
||||
command.previewUrl = global.Utils.uri(options.previewUrl);
|
||||
|
||||
global.commands.push(command);
|
||||
return command;
|
||||
}
|
||||
CreateCommand.executeDefault = function executeDefault() {
|
||||
var {proto: {execute}, global} = this, uri;
|
||||
try { uri = global.Utils.uri(execute) } catch ([]) {}
|
||||
if (uri) return Utils.focusUrlInBrowser(uri.spec);
|
||||
|
||||
if (execute == null) execute = "No action defined."; //ToLocalize
|
||||
global.displayMessage(execute, this);
|
||||
};
|
||||
CreateCommand.previewDefault = function previewDefault(pb) {
|
||||
var html = "";
|
||||
if ("description" in this)
|
||||
html += '<div class="description">' + this.description + '</div>';
|
||||
if ("help" in this)
|
||||
html += '<p class="help">' + this.help + '</p>';
|
||||
if (!html)
|
||||
html = ('Executes the <b class="name">' +
|
||||
Utils.escapeHtml(this.name) + '</b> command.');
|
||||
html = '<div class="default">' + html + '</div>';
|
||||
if (pb) pb.innerHTML = html;
|
||||
return html;
|
||||
if ("previewHtml" in this) html = this.previewHtml;
|
||||
else {
|
||||
if ("description" in this)
|
||||
html += '<div class="description">' + this.description + '</div>';
|
||||
if ("help" in this)
|
||||
html += '<p class="help">' + this.help + '</p>';
|
||||
if (!html)
|
||||
//ToLocalize
|
||||
html = ('Executes the <b class="name">' +
|
||||
Utils.escapeHtml(this.name) + '</b> command.');
|
||||
html = '<div class="default">' + html + '</div>';
|
||||
}
|
||||
return (pb || 0).innerHTML = html;
|
||||
};
|
||||
|
||||
// == COMMAND ALIAS CREATION ==
|
||||
|
@ -941,13 +939,13 @@ makeSearchCommand.preview = function searchPreview(pblock, {object: {text}}) {
|
|||
pblock.innerHTML =
|
||||
"<div class='search-command'>" + Array.join(arguments, "") + "</div>";
|
||||
}
|
||||
var {parser} = this, html = Utils.escapeHtml(text);
|
||||
var {parser, global} = this, html = Utils.escapeHtml(text);
|
||||
put(L("ubiquity.cmdutils.searchcmd", Utils.escapeHtml(this.name), html),
|
||||
//ToLocalize
|
||||
parser ? "<p class='loading'>Loading results...</p>" : "");
|
||||
if (!parser) return;
|
||||
|
||||
var {__parent__: global, type, keys} = parser;
|
||||
var {type, keys} = parser;
|
||||
var params = {
|
||||
url: makeSearchCommand.query(parser.url || this.url, text, this.charset),
|
||||
dataType: parser.type || "text",
|
||||
|
|
|
@ -147,12 +147,11 @@ var LocalizationUtils = {
|
|||
if (val) cmd[key] = val;
|
||||
}
|
||||
|
||||
if (cmd._previewString) {
|
||||
let context = cmd.referenceName + ".preview";
|
||||
let key = cmd._previewString;
|
||||
let rv =
|
||||
LocalizationUtils.getLocalizedStringFromContext(feedKey, context, key);
|
||||
if (rv !== key) cmd._previewString = rv;
|
||||
if ("previewHtml" in cmd) {
|
||||
let key = cmd.previewHtml;
|
||||
let rv = LocalizationUtils.getLocalizedStringFromContext(
|
||||
feedKey, cmd.referenceName + ".preview", key);
|
||||
if (rv !== key) cmd.previewHtml = rv;
|
||||
}
|
||||
|
||||
cmd.name = cmd.names[0];
|
||||
|
|
Загрузка…
Ссылка в новой задаче