Created
February 11, 2009 01:26
-
-
Save louisremi/61752 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------------------- | |
// To be added to CmdUtils API | |
// ----------------------------------------------------------------- | |
// ** {{{ CmdUtils.getUbiquity( ) }}} ** | |
// | |
// Get a reference to Ubiquity. This is used by | |
// {{{ CmdUtils.closeUbiquity() }}} and {{{ CmdUtils.getCommandByName() }}}, but | |
// probably doesn't need to be used directly by command feeds. | |
CmdUtils.getUbiquity = function getUbiquity() { | |
// TODO: Understand why it doesn't work with a simple test. | |
try { | |
return context.chromeWindow.gUbiquity; | |
} catch(e) { | |
return Utils.currentChromeWindow.gUbiquity; | |
} | |
// TODO: make this function available from mouse-based Ubiquity (see | |
// http://azarask.in/verbs/mouse/mouse.js) | |
}; | |
// ** {{{ CmdUtils.getCommandByName( ) }}} ** | |
// | |
// Get a reference to a Ubiquity command. This is used by | |
// alias related functions, but probably doesn't need to be used directly | |
// by command feeds. | |
// | |
// {{{ name }}} The name of the command. | |
CmdUtils.getCommandByName = function getCommandByName( name ) { | |
return CmdUtils.getUbiquity().__cmdManager.__cmdSource.getCommand( name ); | |
}; | |
// ** {{{ CmdUtils.executeCommand }}} ** | |
// | |
// Execute an existing Ubiquity command. | |
// | |
// {{{ command }}} either the name of the Ubiquity command that will be executed | |
// or a direct reference to the command itself. | |
// | |
// {{{ directObject }}} An object containing the words that will be passed to | |
// the execute function of the target command. Example: | |
// {data: 'en', text: 'english'} | |
// | |
// {{{ modifiersValues }}} An object containing the modifiers values that will | |
// be passed to the execute function of the target command. Example: | |
// {from: {data: 'en', text: 'english'}, to; {data: 'fr', text: 'french'}} | |
CmdUtils.executeCommand = function executeCommand( command, | |
directObject, | |
modifiersValues) { | |
return (command.constructor == String? | |
CmdUtils.getCommandByName(command) : | |
command | |
).execute( | |
context || {}, | |
directObject, | |
modifiersValues || {} | |
); | |
}; | |
// ** {{{ CmdUtils.previewCommand }}} ** | |
// | |
// Runs the preview function of an existing Ubiquity command and returns the | |
// resulting previewBlock. | |
// | |
// {{{ command }}} either the name of the Ubiquity command that will run or a | |
// reference to the command itself. | |
// | |
// {{{ previewBlock }}} A reference to the preview display element. | |
// | |
// {{{ directObject }}} An object containing the words that will be passed to | |
// the preview function of the target command. Example: | |
// {data: 'en', text: 'english'} | |
// | |
// {{{ modifiersValues }}} An object containing the modifiers values that will | |
// be passed to the preview function of the target command. Example: | |
// {from: {data: 'en', text: 'english'}, to; {data: 'fr', text: 'french'}} | |
CmdUtils.previewCommand = function previewCommand( command, | |
previewBlock, | |
directObject, | |
modifiersValues) { | |
return (command.constructor == String? | |
CmdUtils.getCommandByName(command) : | |
command | |
).preview( | |
context || {}, | |
directObject, | |
modifiersValues || {}, | |
previewBlock | |
); | |
}; | |
// ** {{{ CmdUtils.previewCommand }}} ** | |
// | |
// create an alias for an existing Ubiquity command without modifying its | |
// source code. The alias can be defined with pre-filled arguments. | |
// | |
// {{{ options }}} is a dictionnary object which ** must ** have the following | |
// properties | |
// | |
// {{{ options.for }}} The name of the exisiting Ubiquity command that this | |
// alias is created for. | |
// | |
// ** The following properties are used if you want your alias pre-fill | |
// arguments for the original Ubiquity command. ** | |
// | |
// {{{ options.modifiers }}} An object containing the modifiers values that will | |
// be passed both to the preview and the execute function of the target command. | |
// Example: | |
// {from: {data: 'en', text: 'english'}, to; {data: 'fr', text: 'french'}} | |
CmdUtils.CreateAlias = function CreateAlias( options ) { | |
var cmd = CmdUtils.getCommandByName(options.for); | |
if (cmd) { | |
var o = { name: options.name }; | |
if(cmd.execute) | |
o.execute = function(directObject, modifiers) { | |
CmdUtils.executeCommand( | |
options.for, | |
// TODO: if directObj is "empty", try to use | |
// options.directObj instead. | |
directObject, | |
jQuery.extend(options.modifiers, modifiers) | |
); | |
}; | |
if(cmd.preview) | |
o.preview = function(pBlock, directObject, modifiers) { | |
return CmdUtils.previewCommand( | |
options.for, | |
pBlock, | |
// Same as execute | |
directObject, | |
jQuery.extend(options.modifiers, modifiers) | |
); | |
}; | |
// Copy the original "takes" and "modifiers" object | |
o.DOType = cmd.DOType; | |
o.DOLabel = cmd.DOLabel; | |
o.modifiers = cmd.modifiers; | |
// Not sure whether the following two lines are useful or not | |
/*o.takes = {}; | |
o.takes[cmd.DOLabel] = cmd.DOType._name;*/ | |
CmdUtils.CreateCommand(o); | |
} | |
}; | |
// ----------------------------------------------------------------- | |
// Create-alias command | |
// ----------------------------------------------------------------- | |
CmdUtils.CreateCommand({ | |
name: "create-alias", | |
author: { name: "lrbabe", email: "public@lrbabe.com", homepage: "http://www.lrbabe.com"}, | |
description: "Defines an alias for an existing command, possibly pre-filling arguments.", | |
help: 'This command allows you to define an alias for an existing commands.'+ | |
'For example: "create-alias en-fr for translate from french to en".'+ | |
'The aliased command is parsed as you type, just like any normal command.'+ | |
'You can even specify arguments for this command and they will be pre-filled each'+ | |
'time you will use the alias. You will still be able to overwrite those pre-filled'+ | |
'arguments, though.', | |
takes: {"alias name": noun_arb_text}, | |
modifiers: {"for": noun_arb_text}, | |
preview: function(pblock, words, modifiers) { | |
if(modifiers.for.text) { | |
var msg = | |
'{for r in result}'+ | |
'<p style="{if r_index == 0}font-size:1.3em;{elseif}color:#999;{/if}">'+ | |
'${r.for} '+ | |
'{for a in r.filledArgs}'+ | |
'<b>${a_index} ${a.text}</b> '+ | |
'{/for}{for a in r.emptyArgs}'+ | |
'<i>(${a_index} ${a})</i> '+ | |
'{/for}'+ | |
'</p>'+ | |
'{forelse}'+ | |
'<p>No command are available for this sentence.</p>'+ | |
'{/for}', | |
result = this._parseSuggestions(this._getSuggestions(modifiers.for.text)); | |
pblock.innerHTML = CmdUtils.renderTemplate(msg, {result: result}); | |
} else | |
pblock.innerHTML = "creates an alias for an existing command"; | |
}, | |
execute: function(words, modifiers) { | |
if(modifiers.for.text) { | |
var suggestion = this._getSuggestions(modifiers.for.text); | |
if(suggestion[0]) { | |
var msg = | |
' \n \n//Note: This command was automatically generated by the create-alias command.\n'+ | |
'CmdUtils.CreateAlias({\n'+ | |
' name: "${result.name}",\n'+ | |
' for: "${result.for}",\n'+ | |
' modifiers: {\n'+ | |
'{for a in result.filledArgs}'+ | |
' ${a_index}: {\n'+ | |
' data: "${a.data}",\n'+ | |
' summary: "${a.summary}",\n'+ | |
' text: "${a.text}"\n'+ | |
' html: "${a.html.replace(/"/g, \'\\"\')}"\n'+ | |
' },\n'+ | |
'{/for}'+ | |
' }\n'+ | |
'});', | |
result = this._parseSuggestions([suggestion[0]]); | |
result[0].name = words.text; | |
CmdUtils.UserCode.appendCode(CmdUtils.renderTemplate(msg, {result: result[0]})); | |
displayMessage( | |
"You have created the alias: " + words.text + | |
" for the command " + result[0].for + ".\n" + | |
"You can edit its source-code with the command-editor command."); | |
} | |
} | |
}, | |
_getSuggestions: function(query) { | |
if(!this._clone) { | |
var parser = CmdUtils.getUbiquity().__cmdManager.__nlParser; | |
// We have to clone the parser or we will only be able | |
// to execute the function we try to alias. | |
this._clone = {}; | |
for(var i in parser) | |
this._clone[i] = parser[i]; | |
CmdUtils.log('clone'); | |
} | |
this._clone.updateSuggestionList(query, {}); | |
return this._clone._suggestionList; | |
}, | |
_parseSuggestions: function(suggestions) { | |
var results = []; | |
for(var s in suggestions) { | |
// only the first five results are parsed | |
if(s < 6) { | |
var r = {for: suggestions[s]._verb._name, filledArgs: {}, emptyArgs: {}}; | |
for(var a in suggestions[s]._verb._arguments) { | |
// We are not using directObject | |
if(a != 'direct_object') { | |
if(suggestions[s]._argSuggs[a].data) | |
r.filledArgs[a] = suggestions[s]._argSuggs[a]; | |
else | |
r.emptyArgs[a] = suggestions[s]._verb._arguments[a].type._name; | |
} | |
} | |
results.push(r); | |
} | |
} | |
return results; | |
} | |
}); | |
// ----------------------------------------------------------------- | |
// Tests for alias related functions from the CmdUtils API | |
// ----------------------------------------------------------------- | |
// Test low level alias related functions: previewCommand and executeCommand | |
// => Seems to work fine. | |
CmdUtils.CreateCommand({ | |
name: "fren", | |
takes: {"few words to translate": noun_arb_text}, | |
modifiers: {"from": noun_type_language, "to": noun_type_language }, | |
preview: function(pblock, words) { | |
CmdUtils.previewCommand('translate', pblock, words, { | |
from: {data: 'fr', text: 'french'}, | |
to: {data: 'en', text: 'english'} | |
}); | |
}, | |
execute: function(pblock, words) { | |
CmdUtils.executeCommand('translate', words, { | |
from: {data: 'fr', text: 'french'}, | |
to: {data: 'en', text: 'english'} | |
}); | |
} | |
}); | |
// Test high level alias related function: CreateAlias | |
// fails for command defined in the same file. | |
CmdUtils.CreateAlias({ | |
name: 'enfr', | |
for: 'translate', | |
modifiers: { | |
from: {data: 'en', text: 'english'}, | |
to: {data: 'fr', text: 'french'} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment