<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Musings of a Trained Monkey: Typo Administration: article tagging III...</title>
    <link>http://www.stevelongdo.com/articles/2006/03/15/typo-administration-article-tagging-iii</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Typo Administration: article tagging III...</title>
      <description>Last time I alluded to using an &lt;a href="http://www.adaptivepath.com/publications/essays/archives/000385.php"&gt;AJAX-less&lt;/a&gt; solution for the tagging auto completion feature. In fact in the &lt;code&gt;controls.js&lt;/code&gt; JavaScript file distributed with &lt;a href="http://typosphere.org"&gt;Typo&lt;/a&gt; there is a "class", Autocompleter.Local, that does what we are after.   As the name implies it does completion from a local JavaScript array. &lt;br /&gt;&lt;br /&gt;
We need to coerce the &lt;span style="background-color:#EEEEEE; color: #77BBBB;"&gt;@tags&lt;/span&gt; attribute we set up in the content_controller last time, into a JavaScript array. Then hook up the Autocompleter.Local to the Keywords field on the Article form and pass it the JavaScript array of tags.  The Autocompleter.Local needs a &amp;lt;DIV&gt; element to update with matches for what we type into the Keywords field.  Also for everything to work properly, turn off the browser's native "autocomplete" feature by setting it to "off" in the text_field method's options hash for the Keywords field. Now that we know what the next steps are lets code:
&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;app/views/admin/content/_form.rhtml&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="ieislame"&gt;&lt;pre&gt;
...
&amp;lt;p&gt;
  &amp;lt;label for="article_keywords"&gt;Keywords:&amp;lt;/label&gt;&amp;lt;br /&gt;
  &amp;lt;%= text_field 'article', 'keywords', :autocomplete =&gt; 'off'  %&gt;&amp;lt;br /&gt;
  &amp;lt;div id="completer" style="max-width: 75px; display: none; overflow: auto; border: 1px solid black; background-color: white;"&gt;&amp;lt;/div&gt;
&amp;lt;script type="text/javascript" language="javascript" charset="utf-8"&gt;
  opts = new Array(&amp;lt;%= @tags.sort_by{|t| t.display_name}.collect!{|t1| '"' + t1.display_name + '",'} -%&gt;'');
// &amp;lt;![CDATA[  
  new Autocompleter.Local('article_keywords','completer', opts
  , { tokens: new Array(' ',',','\n'), partialChars: 1, choices: 20, partialSearch: false});
// ]]&gt;
&amp;lt;/script&gt;
&amp;lt;/p&gt;
...
&lt;/pre&gt;&lt;/div&gt;
This code reads well, it is &lt;a href="http://ruby-lang.org"&gt;Ruby&lt;/a&gt; after all, but the line right after the &amp;lt;script&gt; element may deserve some special explanation.  That dense little line of &lt;a href="http://ruby-lang.org"&gt;Ruby&lt;/a&gt; code sorts our &lt;span style="background-color:#EEEEEE; color: #77BBBB;"&gt;@tags&lt;/span&gt; by their display_name and then collects them together into a String that will look like this: ("tag1","tag2",...,''). This String will  in turn be evaluated into a JavaScript Array.  The options being passed to the Autocompleter {tokens,choices,etc.} are documented in the &lt;code&gt;controls.js&lt;/code&gt; JavaScript file distributed with &lt;a href="http://typosphere.org"&gt;Typo&lt;/a&gt;. 
&lt;br /&gt; Let's create a new article and check out the fruits of our labor thus far by seeing what happens when we enter the letter 'a':
&lt;div style="margin: 0 auto; padding: 0 auto;"&gt;
&lt;a href="/files/tagging2.png" rel="lightbox"&gt; &lt;img title="ugly tag complete" alt="ugly tag complete" src="/files/tagging2.png" height="200px"&gt;&lt;/a&gt;
&lt;/div&gt;&lt;br /&gt;
The CSS styling provided is pulled from the admin panel's CSS stylesheet, hence the redness.  Also you can see that my guess at making the completer &amp;lt;DIV&gt; element's &lt;span style="background-color:#eeeeee"&gt;&lt;span style="color: #994444;"&gt;max-width:&lt;/span&gt; &lt;span style="color: #BBBB77;"&gt;75px&lt;/span&gt;&lt;/span&gt;, didn't work out too well.  Using &lt;a href="http://chrispederick.com/work/webdeveloper/"&gt;Chris Pedrick's WebDeveloper Extension&lt;/a&gt; to view the generated source of our completer &amp;lt;DIV&gt; element, the HTML markup provided by the Autocompleter is pretty basic. We'll work on augmenting it and making the CSS styling better looking next time.
</description>
      <pubDate>Wed, 15 Mar 2006 03:47:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d4a1e692-971c-494f-b37f-d2d985f8cdf2</guid>
      <author>Steve Longdo</author>
      <link>http://www.stevelongdo.com/articles/2006/03/15/typo-administration-article-tagging-iii</link>
      <category>ajax</category>
      <category>css</category>
      <category>javascript</category>
      <category>rails</category>
      <category>ruby</category>
      <category>tag</category>
      <category>typo</category>
      <trackback:ping>http://www.stevelongdo.com/articles/trackback/97</trackback:ping>
    </item>
  </channel>
</rss>
