Changes

Jump to: navigation, search

Labs/Ubiquity/Ubiquity Source Tip Author Tutorial

1,758 bytes removed, 05:04, 22 June 2009
Color: Creating Bounded Noun Types
jQuery is a powerful tool. With it, you can fairly effortlessly cherry-pick the data you need from RSS feeds, XML, and all sorts of other data formats. It also makes doing in-preview animations a breeze.
== Color: Creating Bounded Noun Types ==
Suppose you're writing a set of commands for artists and
web designers, and you know that several of the commands will operate
on colors. You'd like to be able to specify that certain commands
expect names of colors as arguments. Since there are a finite number of
named colors, you can define a custom noun type for them based on a list
of strings, like so:
 
<pre>
noun_type_color = new CmdUtils.NounType( "Color",
["red", "orange", "yellow", "green", "blue", "violet", "black", "white",
"grey", "brown", "beige", "magenta", "cerulean", "puce"] // etc...
);
</pre>
 
Note that we gave the new object a name starting with "<code>noun_</code>".
The Ubiquity command loader automatically detects objects starting with
"<code>noun_</code>" as custom noun-types, in the same way as it auto-detects
functions starting with "<code>cmd_</code>" as custom commands.
 
Once you've defined a custom noun-type, you can use it in as many commands
as you like, thus:
 
<pre>
CmdUtils.CreateCommand({
name: "get-color-code",
takes: {"color": noun_type_color},
preview: "Inserts the HTML hex-code for the given color.",
// etc...
</pre>
 
One benefit of creating the custom color noun-type is that if the user
has entered "get-color bl", for instance, Ubiquity will be able to suggest
"black" and "blue" as the two valid completions based on the input.
 
Of course, not every type of noun you'd be interested in can be represented
as a finite list. If you want to be able to accept or reject input based
on some algorithmic test, you can do so by creating your own noun-type
implementation instead of instantiating <code>CmdUtils.NounType</code>. There is an example of this in the section on the tab commands, below.
== Replace: Commands With Multiple Arguments ==
1,007
edits

Navigation menu