Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
71 lines (67 sloc) 22.714 kB
<!DOCTYPE html> <html> <head> <title>ajax-chosen.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> ajax-chosen.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="nx">do</span> <span class="nf">($ = jQuery) -&gt;</span>
<span class="nv">$.fn.ajaxChosen = </span><span class="nf">(settings = {}, callback = -&gt;) -&gt;</span>
<span class="nv">defaultOptions =</span>
<span class="nv">minTermLength: </span><span class="mi">3</span>
<span class="nv">afterTypeDelay: </span><span class="mi">500</span>
<span class="nv">jsonTermKey: </span><span class="s2">&quot;term&quot;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>This will come in handy later.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">select = </span><span class="err">@</span>
<span class="nv">chosenXhr = </span><span class="kc">null</span>
</pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Merge options with defaults</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">options = </span><span class="nx">$</span><span class="p">.</span><span class="nx">extend</span> <span class="p">{},</span> <span class="nx">defaultOptions</span><span class="p">,</span> <span class="nx">settings</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Load chosen. To make things clear, I have taken the liberty
of using the .chzn-autoselect class to specify input elements
we want to use with ajax autocomplete.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@chosen</span><span class="p">()</span>
<span class="nx">@each</span> <span class="o">-&gt;</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Now that chosen is loaded normally, we can bootstrap it with
our ajax autocomplete code.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="nx">next</span><span class="p">(</span><span class="s1">&#39;.chzn-container&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="s2">&quot;.search-field &gt; input, .chzn-search &gt; input&quot;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">bind</span> <span class="s1">&#39;keyup&#39;</span><span class="p">,</span> <span class="o">-&gt;</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>This code will be executed every time the user types a letter
into the input form that chosen has created</p> </td> <td class="code"> <div class="highlight"><pre> </pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Retrieve the current value of the input form</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">val = </span><span class="nx">$</span><span class="p">.</span><span class="nx">trim</span> <span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;value&#39;</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Depending on how much text the user has typed, let them know
if they need to keep typing or if we are looking for their data</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">msg = </span><span class="k">if</span> <span class="nx">val</span><span class="p">.</span><span class="nx">length</span> <span class="o">&lt;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">minTermLength</span> <span class="k">then</span> <span class="s2">&quot;Keep typing...&quot;</span> <span class="k">else</span> <span class="s2">&quot;Looking for &#39;&quot;</span> <span class="o">+</span> <span class="nx">val</span> <span class="o">+</span> <span class="s2">&quot;&#39;&quot;</span>
<span class="nx">select</span><span class="p">.</span><span class="nx">next</span><span class="p">(</span><span class="s1">&#39;.chzn-container&#39;</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;.no-results&#39;</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="nx">msg</span><span class="p">)</span>
</pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>If input text has not changed ... do nothing</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">val</span> <span class="o">is</span> <span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="nx">data</span><span class="p">(</span><span class="s1">&#39;prevVal&#39;</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Set the current search term so we don't execute the ajax call if
the user hits a key that isn't an input letter/number/symbol</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="nx">data</span><span class="p">(</span><span class="s1">&#39;prevVal&#39;</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span>
</pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>At this point, we have a new term/query ... the old timer
is no longer valid. Clear it.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>We delay searches by a small amount so that we don't flood the
server with ajax requests.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">@timer</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@timer</span>
</pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Some simple validation so we don't make excess ajax calls. I am
assuming you don't want to perform a search with less than 3
characters.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">val</span><span class="p">.</span><span class="nx">length</span> <span class="o">&lt;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">minTermLength</span>
</pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>This is a useful reference for later</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">field = </span><span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">)</span>
</pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Default term key is <code>term</code>. Specify alternative in options.options.jsonTermKey</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">options.data = </span><span class="p">{}</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">options</span><span class="p">.</span><span class="nx">data</span><span class="o">?</span>
<span class="nx">options</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="nx">options</span><span class="p">.</span><span class="nx">jsonTermKey</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span>
</pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>If the user provided an ajax success callback, store it so we can
call it after our bootstrapping is finished.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">success</span> <span class="o">?=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">success</span>
</pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Create our own callback that will be executed when the ajax call is
finished.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">options.success = </span><span class="nf">(data) -&gt;</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Exit if the data we're given is invalid</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">data</span><span class="o">?</span>
</pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>Go through all of the <option> elements in the <select> and remove
ones that have not been selected by the user. For those selected
by the user, add them to a list to filter from the results later.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">selected_values = </span><span class="p">[]</span>
<span class="nx">select</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="s1">&#39;option&#39;</span><span class="p">).</span><span class="nx">each</span> <span class="o">-&gt;</span>
<span class="k">if</span> <span class="o">not</span> <span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="o">is</span><span class="p">(</span><span class="s2">&quot;:selected&quot;</span><span class="p">)</span>
<span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="nx">remove</span><span class="p">()</span>
<span class="k">else</span>
<span class="nx">selected_values</span><span class="p">.</span><span class="nx">push</span> <span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="nx">val</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot;-&quot;</span> <span class="o">+</span> <span class="nx">$</span><span class="p">(</span><span class="err">@</span><span class="p">).</span><span class="nx">text</span><span class="p">()</span>
</pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Send the ajax results to the user callback so we can get an object of
value => text pairs to inject as <option> elements.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">items = </span><span class="nx">callback</span> <span class="nx">data</span>
</pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>Iterate through the given data and inject the <option> elements into
the DOM if it doesn't exist in the selector already</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">$</span><span class="p">.</span><span class="nx">each</span> <span class="nx">items</span><span class="p">,</span> <span class="nf">(value, text) -&gt;</span>
<span class="k">if</span> <span class="nx">$</span><span class="p">.</span><span class="nx">inArray</span><span class="p">(</span><span class="nx">value</span> <span class="o">+</span> <span class="s2">&quot;-&quot;</span> <span class="o">+</span> <span class="nx">text</span><span class="p">,</span> <span class="nx">selected_values</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span>
<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;option /&gt;&quot;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;value&#39;</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span>
<span class="p">.</span><span class="nx">html</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span>
<span class="p">.</span><span class="nx">appendTo</span><span class="p">(</span><span class="nx">select</span><span class="p">)</span>
</pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>Tell chosen that the contents of the <select> input have been updated
This makes chosen update its internal list of the input data.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">select</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s2">&quot;liszt:updated&quot;</span><span class="p">)</span>
</pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>Finally, call the user supplied callback (if it exists)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">success</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="k">if</span> <span class="nx">success</span><span class="o">?</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>For some reason, the contents of the input field get removed once you
call trigger above. Often, this can be very annoying (and can make some
searches impossible), so we add the value the user was typing back into
the input field.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">field</span><span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;value&#39;</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <p>Because non-ajax Chosen isn't constantly re-building results, when it
DOES rebuild results (during liszt:updated above, it clears the input
search field before scaling it. This causes the input field width to be
at it's minimum, which is about 25px. </p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>The proper way to fix this would be create a new method in chosen for
rebuilding results without clearing the input field. Or to call
Chosen.search<em>field</em>scale() after resetting the value above. This isn't
possible with the current state of Chosen. The quick fix is to simply reset
the width of the field after we reset the value of the input text.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">field</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span><span class="s1">&#39;width&#39;</span><span class="p">,</span><span class="s1">&#39;auto&#39;</span><span class="p">)</span>
</pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>Execute the ajax call to search for autocomplete data with a timer</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@timer = </span><span class="nx">setTimeout</span> <span class="o">-&gt;</span>
<span class="nx">chosenXhr</span><span class="p">.</span><span class="nx">abort</span><span class="p">()</span> <span class="k">if</span> <span class="nx">chosenXhr</span>
<span class="nv">chosenXhr = </span><span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span>
<span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">afterTypeDelay</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
Jump to Line
Something went wrong with that request. Please try again.