[
  {
    "path": ".gitignore",
    "content": ".DS_Store\nsftp-config.json\nnode_modules"
  },
  {
    "path": ".gitmodules",
    "content": "[submodule \"dist/chosen\"]\n\tpath = dist/chosen\n\turl = https://github.com/harvesthq/chosen.git\n"
  },
  {
    "path": "Cakefile",
    "content": "fs      = require 'fs'\n{exec}  = require 'child_process'\nutil    = require 'util'\n{jsmin} = require 'jsmin'\n\ntargetName    = \"ajax-chosen\"\n\n###\nCoffeeScript Options\n###\ncsSrcDir      = \"src\"\ncsTargetDir   = \"lib\"\n\ntargetCoffee  = \"#{csSrcDir}/build.coffee\"\ntargetJS      = \"#{csTargetDir}/#{targetName}.js\"\ntargetMinJS   = \"#{csTargetDir}/#{targetName}.min.js\"\n\ncoffeeOpts    = \"-b -j #{targetName}.js -o #{csTargetDir} -c #{targetCoffee}\"\n\ncoffeeFiles   = [\n  \"ajax-chosen\"\n]\n\n###\nEvent System\n###\nfinishedCallback = {}\nfinished = (type) ->      \n  finishedCallback[type]() if finishedCallback[type]?\n\nfinishListener = (type, cb) ->\n  finishedCallback[type] = cb\n  \nnotify = (msg) ->\n  return if not growl?\n  growl.notify msg, {title: \"Heello Development\", image: \"Terminal\"}\n  \n###\nTasks\n###\ntask 'docs', 'Generates documentation for the coffee files', ->\n  util.log 'Invoking docco on the CoffeeScript source files'\n  \n  files = coffeeFiles\n  files[i] = \"#{csSrcDir}/#{files[i]}.coffee\" for i in [0...files.length]\n\n  exec \"docco #{files.join(' ')}\", (err, stdout, stderr) ->\n    util.log err if err\n    util.log \"Documentation built into docs/ folder.\"\n        \ntask 'watch', 'Automatically recompile the CoffeeScript files when updated', ->\n  util.log \"Watching for changes in #{csSrcDir}\"\n  \n  for jsFile in coffeeFiles then do (jsFile) ->\n    fs.watchFile \"#{csSrcDir}/#{jsFile}.coffee\", (curr, prev) ->\n      if +curr.mtime isnt +prev.mtime\n        util.log \"#{csSrcDir}/#{jsFile}.coffee updated\"\n        invoke 'build'\n        \ntask 'build', 'Compile and minify all CoffeeScript source files', ->\n  finishListener 'js', -> invoke 'minify'\n  invoke 'compile'\n\ntask 'compile', 'Compile all CoffeeScript source files', ->\n  util.log \"Building #{targetJS}\"\n  contents = []\n  remaining = coffeeFiles.length\n  \n  util.log \"Appending #{coffeeFiles.length} files to #{targetCoffee}\"\n  \n  for file, index in coffeeFiles then do (file, index) ->\n    fs.readFile \"#{csSrcDir}/#{file}.coffee\", \"utf8\", (err, fileContents) ->\n      util.log err if err\n      \n      contents[index] = fileContents\n      util.log \"[#{index + 1}] #{file}.coffee\"\n      process() if --remaining is 0\n      \n  process = ->\n    fs.writeFile targetCoffee, contents.join(\"\\n\\n\"), \"utf8\", (err) ->\n      util.log err if err\n      \n      exec \"coffee #{coffeeOpts}\", (err, stdout, stderr) ->\n        util.log err if err\n        util.log \"Compiled #{targetJS}\"\n        fs.unlink targetCoffee, (err) -> util.log err if err\n        finished('js')\n        \ntask 'minify', 'Minify the CoffeeScript files', ->\n  util.log \"Minifying #{targetJS}\"\n  fs.readFile targetJS, \"utf8\", (err, contents) ->\n    fs.writeFile targetMinJS, jsmin(contents), \"utf8\", (err) ->\n      util.log err if err\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2014 Ryan LeFevre\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "Makefile",
    "content": "all:\n\tcoffee -o lib/ -c src/"
  },
  {
    "path": "README.md",
    "content": "# Ajax-Chosen\n\n**This project is no longer maintained and is likely extremely out of date with the original chosen library. There are much better alternatives out there now.**\n\nThis project is an addition to the excellent [Chosen jQuery plugin](https://github.com/harvesthq/chosen) that makes HTML input forms more friendly.  Chosen adds search boxes to `select` HTML elements, so I felt it could use the addition of ajax autocomplete for awesomely dynamic forms.\n\nThis script bootstraps the existing Chosen plugin without making any modifications to the original code. Eventually, I would love to see this functionality built-in to the library, but until then, this seems to work pretty well.\n\n## How to Use\n\nThis plugin exposes a new jQuery function named `ajaxChosen` that we call on a `select` element. The first argument consists of the options passed to the jQuery $.ajax function. The `data` parameter is optional, and the `success` callback is also optional.\n\nThe second argument is a callback that tells the plugin what HTML `option` elements to make. It is passed the data returned from the ajax call, and you have to return an array of objects for which each item has a `value` property corresponding to the HTML `option` elements' `value` attribute, and a `text` property corresponding to the text to display for each option. In other words:\n\n\t[{\"value\": 3, \"text\": \"Ohio\"}]\n\nbecomes:\n\n\t<option value=\"3\">Ohio</option>\n\nor for grouping:\n\n\t[{\n\t\tgroup: true,\n\t\ttext: \"Europe\",\n\t\titems: [\n\t\t\t{ \"value\": \"10\", \"text\": \"Stockholm\" },\n\t\t\t{ \"value\": \"23\", \"text\": \"London\" }\n\t\t]\n\t},\n\t{\n\t\tgroup: true,\n\t\ttext: \"Asia\",\n\t\titems: [\n\t\t\t{ \"value\": \"36\", \"text\": \"Beijing\" },\n\t\t\t{ \"value\": \"20\", \"text\": \"Tokyo\" }\n\t\t]\n\t}]\n\nbecomes:\n\n        <optgroup label=\"Europe\">\n            <option value=\"10\">Stockholm</option>\n            <option value=\"23\">London</option>\n        </optgroup>\n        <optgroup label=\"Asia\">\n            <option value=\"36\">Beijing</option>\n            <option value=\"20\">Tokyo</option>\n        </optgroup>\n\nNote: \n\nDue to a bug in Chosen, it is necessary to change `choosen.css`.\n\nAdd \n\n\tdisplay: list-item;\n\nto \n\n\t.chzn-container .chzn-results .group-result {\n\nclass\n\n### Options\n\nThere are some additional ajax-chosen specific options you can pass into the first argument to control its behavior.\n\n* `minTermLength`: minimum number of characters that must be typed before an ajax call is fired\n* `afterTypeDelay`: how many milliseconds to wait after typing stops to fire the ajax call\n* `jsonTermKey`: the ajax request key to use for the search query (defaults to `term`)\n\n## Example Code\n\n``` js\n$(\"#example-input\").ajaxChosen({\n\ttype: 'GET',\n\turl: '/ajax-chosen/data.php',\n\tdataType: 'json'\n}, function (data) {\n\tvar results = [];\n\t\n\t$.each(data, function (i, val) {\n\t\tresults.push({ value: val.value, text: val.text });\n\t});\n\t\n\treturn results;\n});\n```\nTo have the results grouped in `optgroup` elements, have the function return a list of group objects instead:\n\n``` js\n$(\"#example-input\").ajaxChosen({\n\ttype: 'GET',\n\turl: '/ajax-chosen/grouped.php',\n\tdataType: 'json'\n}, function (data) {\n\tvar results = [];\n\n\t$.each(data, function (i, val) {\n\t\tvar group = { // here's a group object:\n\t\t\tgroup: true,\n\t\t\ttext: val.name, // label for the group\n\t\t\titems: [] // individual options within the group\n\t\t};\n\n\t\t$.each(val.items, function (i1, val1) {\n\t\t\tgroup.items.push({value: val1.value, text: val1.text});\n\t\t});\n\n\t\tresults.push(group);\n\t});\n\n\treturn results;\n});\n\n```\n\n## Developing ajax-chosen\n\nIn order to install development dependencies, you can run in the ajax-chosen directory:\n\n```\nnpm install -d\n```\n\najax-chosen is written in Coffeescript, so there is a Cakefile provided that will perform all necessary tasks for you. Simply run `cake` to see all available commands.\n"
  },
  {
    "path": "VERSION",
    "content": "0.2.0\n"
  },
  {
    "path": "data.json",
    "content": "{\n\t\"states\": [\n\t\t\"Alabama\",\n\t\t\"Alaska\",\n\t\t\"Arizona\",\n\t\t\"Arkansas\",\n\t\t\"California\",\n\t\t\"Colorado\",\n\t\t\"Connecticut\",\n\t\t\"Delaware\",\n\t\t\"District of Columbia\",\n\t\t\"Florida\",\n\t\t\"Georgia\",\n\t\t\"Hawaii\",\n\t\t\"Idaho\",\n\t\t\"Illinois\",\n\t\t\"Indiana\",\n\t\t\"Iowa\",\n\t\t\"Kansas\",\n\t\t\"Kentucky\",\n\t\t\"Louisiana\",\n\t\t\"Maine\",\n\t\t\"Maryland\",\n\t\t\"Massachusetts\",\n\t\t\"Michigan\",\n\t\t\"Minnesota\",\n\t\t\"Mississippi\",\n\t\t\"Missouri\",\n\t\t\"Montana\",\n\t\t\"Nebraska\",\n\t\t\"Nevada\",\n\t\t\"New Hampshire\",\n\t\t\"New Jersey\",\n\t\t\"New Mexico\",\n\t\t\"New York\",\n\t\t\"North Carolina\",\n\t\t\"North Dakota\",\n\t\t\"Ohio\",\n\t\t\"Oklahoma\",\n\t\t\"Oregon\",\n\t\t\"Pennsylvania\",\n\t\t\"Puerto Rico\",\n\t\t\"Rhode Island\",\n\t\t\"South Carolina\",\n\t\t\"South Dakota\",\n\t\t\"Tennessee\",\n\t\t\"Texas\",\n\t\t\"Utah\",\n\t\t\"Vermont\",\n\t\t\"Virginia\",\n\t\t\"Virgin Islands\",\n\t\t\"Washington\",\n\t\t\"West Virginia\",\n\t\t\"Wisconsin\",\n\t\t\"Wyoming\"\n\t]\n}"
  },
  {
    "path": "docs/ajax-chosen.html",
    "content": "<!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>\n\n  <span class=\"nv\">$.fn.ajaxChosen = </span><span class=\"nf\">(settings = {}, callback = -&gt;) -&gt;</span>\n    <span class=\"nv\">defaultOptions =</span>\n      <span class=\"nv\">minTermLength: </span><span class=\"mi\">3</span>\n      <span class=\"nv\">afterTypeDelay: </span><span class=\"mi\">500</span>\n      <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>\n    \n    <span class=\"nv\">chosenXhr = </span><span class=\"kc\">null</span>\n    </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\nof using the .chzn-autoselect class to specify input elements\nwe want to use with ajax autocomplete.</p>             </td>             <td class=\"code\">               <div class=\"highlight\"><pre>    <span class=\"nx\">@chosen</span><span class=\"p\">()</span>\n    \n    <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\nour 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>\n        <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>\n        <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\ninto 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\nif 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>\n          <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>\n          </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\nthe 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>\n          </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\nis 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\nserver 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>\n          </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\nassuming you don't want to perform a search with less than 3\ncharacters.</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>\n          </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>\n          </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>\n          <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>\n          </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\ncall 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>\n          </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\nfinished.</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>\n            </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\nones that have not been selected by the user.  For those selected\nby 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>\n            <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> \n              <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>\n                <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> \n              <span class=\"k\">else</span>\n                <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>\n                </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\nvalue => 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>\n            </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\nthe 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>\n              <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>\n                <span class=\"nx\">$</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;option /&gt;&quot;</span><span class=\"p\">)</span>\n                  <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>\n                  <span class=\"p\">.</span><span class=\"nx\">html</span><span class=\"p\">(</span><span class=\"nx\">text</span><span class=\"p\">)</span>\n                  <span class=\"p\">.</span><span class=\"nx\">appendTo</span><span class=\"p\">(</span><span class=\"nx\">select</span><span class=\"p\">)</span>\n                </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\nThis 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>\n            </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\ncall trigger above. Often, this can be very annoying (and can make some\nsearches impossible), so we add the value the user was typing back into\nthe 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\nDOES rebuild results (during liszt:updated above, it clears the input \nsearch field before scaling it.  This causes the input field width to be \nat 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\nrebuilding results without clearing the input field.  Or to call \nChosen.search<em>field</em>scale() after resetting the value above.  This isn't\npossible with the current state of Chosen.  The quick fix is to simply reset\nthe 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>\n                      </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> \n            <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>\n            <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>\n          <span class=\"p\">,</span> <span class=\"nx\">options</span><span class=\"p\">.</span><span class=\"nx\">afterTypeDelay</span>\n\n</pre></div>             </td>           </tr>                </tbody>     </table>   </div> </body> </html> "
  },
  {
    "path": "docs/docco.css",
    "content": "/*--------------------- Layout and Typography ----------------------------*/\nbody {\n  font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;\n  font-size: 15px;\n  line-height: 22px;\n  color: #252519;\n  margin: 0; padding: 0;\n}\na {\n  color: #261a3b;\n}\n  a:visited {\n    color: #261a3b;\n  }\np {\n  margin: 0 0 15px 0;\n}\nh1, h2, h3, h4, h5, h6 {\n  margin: 0px 0 15px 0;\n}\n  h1 {\n    margin-top: 40px;\n  }\n#container {\n  position: relative;\n}\n#background {\n  position: fixed;\n  top: 0; left: 525px; right: 0; bottom: 0;\n  background: #f5f5ff;\n  border-left: 1px solid #e5e5ee;\n  z-index: -1;\n}\n#jump_to, #jump_page {\n  background: white;\n  -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;\n  -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px;\n  font: 10px Arial;\n  text-transform: uppercase;\n  cursor: pointer;\n  text-align: right;\n}\n#jump_to, #jump_wrapper {\n  position: fixed;\n  right: 0; top: 0;\n  padding: 5px 10px;\n}\n  #jump_wrapper {\n    padding: 0;\n    display: none;\n  }\n    #jump_to:hover #jump_wrapper {\n      display: block;\n    }\n    #jump_page {\n      padding: 5px 0 3px;\n      margin: 0 0 25px 25px;\n    }\n      #jump_page .source {\n        display: block;\n        padding: 5px 10px;\n        text-decoration: none;\n        border-top: 1px solid #eee;\n      }\n        #jump_page .source:hover {\n          background: #f5f5ff;\n        }\n        #jump_page .source:first-child {\n        }\ntable td {\n  border: 0;\n  outline: 0;\n}\n  td.docs, th.docs {\n    max-width: 450px;\n    min-width: 450px;\n    min-height: 5px;\n    padding: 10px 25px 1px 50px;\n    overflow-x: hidden;\n    vertical-align: top;\n    text-align: left;\n  }\n    .docs pre {\n      margin: 15px 0 15px;\n      padding-left: 15px;\n    }\n    .docs p tt, .docs p code {\n      background: #f8f8ff;\n      border: 1px solid #dedede;\n      font-size: 12px;\n      padding: 0 0.2em;\n    }\n    .pilwrap {\n      position: relative;\n    }\n      .pilcrow {\n        font: 12px Arial;\n        text-decoration: none;\n        color: #454545;\n        position: absolute;\n        top: 3px; left: -20px;\n        padding: 1px 2px;\n        opacity: 0;\n        -webkit-transition: opacity 0.2s linear;\n      }\n        td.docs:hover .pilcrow {\n          opacity: 1;\n        }\n  td.code, th.code {\n    padding: 14px 15px 16px 25px;\n    width: 100%;\n    vertical-align: top;\n    background: #f5f5ff;\n    border-left: 1px solid #e5e5ee;\n  }\n    pre, tt, code {\n      font-size: 12px; line-height: 18px;\n      font-family: Monaco, Consolas, \"Lucida Console\", monospace;\n      margin: 0; padding: 0;\n    }\n\n\n/*---------------------- Syntax Highlighting -----------------------------*/\ntd.linenos { background-color: #f0f0f0; padding-right: 10px; }\nspan.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\nbody .hll { background-color: #ffffcc }\nbody .c { color: #408080; font-style: italic }  /* Comment */\nbody .err { border: 1px solid #FF0000 }         /* Error */\nbody .k { color: #954121 }                      /* Keyword */\nbody .o { color: #666666 }                      /* Operator */\nbody .cm { color: #408080; font-style: italic } /* Comment.Multiline */\nbody .cp { color: #BC7A00 }                     /* Comment.Preproc */\nbody .c1 { color: #408080; font-style: italic } /* Comment.Single */\nbody .cs { color: #408080; font-style: italic } /* Comment.Special */\nbody .gd { color: #A00000 }                     /* Generic.Deleted */\nbody .ge { font-style: italic }                 /* Generic.Emph */\nbody .gr { color: #FF0000 }                     /* Generic.Error */\nbody .gh { color: #000080; font-weight: bold }  /* Generic.Heading */\nbody .gi { color: #00A000 }                     /* Generic.Inserted */\nbody .go { color: #808080 }                     /* Generic.Output */\nbody .gp { color: #000080; font-weight: bold }  /* Generic.Prompt */\nbody .gs { font-weight: bold }                  /* Generic.Strong */\nbody .gu { color: #800080; font-weight: bold }  /* Generic.Subheading */\nbody .gt { color: #0040D0 }                     /* Generic.Traceback */\nbody .kc { color: #954121 }                     /* Keyword.Constant */\nbody .kd { color: #954121; font-weight: bold }  /* Keyword.Declaration */\nbody .kn { color: #954121; font-weight: bold }  /* Keyword.Namespace */\nbody .kp { color: #954121 }                     /* Keyword.Pseudo */\nbody .kr { color: #954121; font-weight: bold }  /* Keyword.Reserved */\nbody .kt { color: #B00040 }                     /* Keyword.Type */\nbody .m { color: #666666 }                      /* Literal.Number */\nbody .s { color: #219161 }                      /* Literal.String */\nbody .na { color: #7D9029 }                     /* Name.Attribute */\nbody .nb { color: #954121 }                     /* Name.Builtin */\nbody .nc { color: #0000FF; font-weight: bold }  /* Name.Class */\nbody .no { color: #880000 }                     /* Name.Constant */\nbody .nd { color: #AA22FF }                     /* Name.Decorator */\nbody .ni { color: #999999; font-weight: bold }  /* Name.Entity */\nbody .ne { color: #D2413A; font-weight: bold }  /* Name.Exception */\nbody .nf { color: #0000FF }                     /* Name.Function */\nbody .nl { color: #A0A000 }                     /* Name.Label */\nbody .nn { color: #0000FF; font-weight: bold }  /* Name.Namespace */\nbody .nt { color: #954121; font-weight: bold }  /* Name.Tag */\nbody .nv { color: #19469D }                     /* Name.Variable */\nbody .ow { color: #AA22FF; font-weight: bold }  /* Operator.Word */\nbody .w { color: #bbbbbb }                      /* Text.Whitespace */\nbody .mf { color: #666666 }                     /* Literal.Number.Float */\nbody .mh { color: #666666 }                     /* Literal.Number.Hex */\nbody .mi { color: #666666 }                     /* Literal.Number.Integer */\nbody .mo { color: #666666 }                     /* Literal.Number.Oct */\nbody .sb { color: #219161 }                     /* Literal.String.Backtick */\nbody .sc { color: #219161 }                     /* Literal.String.Char */\nbody .sd { color: #219161; font-style: italic } /* Literal.String.Doc */\nbody .s2 { color: #219161 }                     /* Literal.String.Double */\nbody .se { color: #BB6622; font-weight: bold }  /* Literal.String.Escape */\nbody .sh { color: #219161 }                     /* Literal.String.Heredoc */\nbody .si { color: #BB6688; font-weight: bold }  /* Literal.String.Interpol */\nbody .sx { color: #954121 }                     /* Literal.String.Other */\nbody .sr { color: #BB6688 }                     /* Literal.String.Regex */\nbody .s1 { color: #219161 }                     /* Literal.String.Single */\nbody .ss { color: #19469D }                     /* Literal.String.Symbol */\nbody .bp { color: #954121 }                     /* Name.Builtin.Pseudo */\nbody .vc { color: #19469D }                     /* Name.Variable.Class */\nbody .vg { color: #19469D }                     /* Name.Variable.Global */\nbody .vi { color: #19469D }                     /* Name.Variable.Instance */\nbody .il { color: #666666 }                     /* Literal.Number.Integer.Long */"
  },
  {
    "path": "index.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n\t<title>Ajax-Chosen: Bootstrapping a Popular jQuery Plugin to add Ajax Autocomplete</title>\n\t\n\t<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js\"></script>\n\t<script type=\"text/javascript\" src=\"/dist/chosen/chosen/chosen.jquery.min.js\"></script>\n\t<script type=\"text/javascript\" src=\"/lib/ajax-chosen.js\"></script>\n\t\n\t<link type=\"text/css\" rel=\"stylesheet\" href=\"/dist/chosen/chosen/chosen.css\" />\n\t\n\t<style type=\"text/css\">\n\t#example1, #example2, #example3 { width: 300px; display: block; }\n\t</style>\n\t\n\t<script type=\"text/javascript\">\n\t$(document).ready(function () {\n\t\t$(\"#example1,#example2,#example3\").ajaxChosen({\n\t\t\ttype: 'GET',\n\t\t\turl: '/data.json',\n\t\t\tdataType: 'json'\n\t\t}, function (data) {\n\t\t\tvar terms = {};\n\t\t\t\n\t\t\t$.each(data.states, function (i, val) {\n\t\t\t\tterms[i] = val;\n\t\t\t});\n\t\t\t\n\t\t\treturn terms;\n\t\t});\n\t});\n\t</script>\n</head>\n<body>\n\t<h1>Ajax-Chosen: Bootstrapping a jQuery Plugin</h1>\n\t\n\t<div>\n\t\t<h2>Multi Select</h2>\n\t\t<select id=\"example1\" title=\"Search for a state\" multiple></select><br /><br />\n\t\t<select id=\"example3\" title=\"Search for a state\" multiple></select>\n\t</div>\n\t<div>\n\t\t<h2>Single Select</h2>\n\t\t<select id=\"example2\" title=\"Select for a state\" search><option></option></select>\n\t</div>\n</body>\n</html>"
  },
  {
    "path": "lib/ajax-chosen.js",
    "content": "// Generated by CoffeeScript 1.4.0\n\n(function($) {\n  return $.fn.ajaxChosen = function(settings, callback, chosenOptions) {\n    var chosenXhr, defaultOptions, options, select;\n    if (settings == null) {\n      settings = {};\n    }\n    if (chosenOptions == null) {\n      chosenOptions = {};\n    }\n    defaultOptions = {\n      minTermLength: 3,\n      afterTypeDelay: 500,\n      jsonTermKey: \"term\",\n      keepTypingMsg: \"Keep typing...\",\n      lookingForMsg: \"Looking for\"\n    };\n    select = this;\n    chosenXhr = null;\n    options = $.extend({}, defaultOptions, $(select).data(), settings);\n    this.chosen(chosenOptions ? chosenOptions : {});\n    return this.each(function() {\n      return $(this).next('.chzn-container').find(\".search-field > input, .chzn-search > input\").bind('keyup', function() {\n        var field, msg, success, untrimmed_val, val;\n        untrimmed_val = $(this).val();\n        val = $.trim($(this).val());\n        msg = val.length < options.minTermLength ? options.keepTypingMsg : options.lookingForMsg + (\" '\" + val + \"'\");\n        select.next('.chzn-container').find('.no-results').text(msg);\n        if (val === $(this).data('prevVal')) {\n          return false;\n        }\n        $(this).data('prevVal', val);\n        if (this.timer) {\n          clearTimeout(this.timer);\n        }\n        if (val.length < options.minTermLength) {\n          return false;\n        }\n        field = $(this);\n        if (options.data == null) {\n          options.data = {};\n        }\n        options.data[options.jsonTermKey] = val;\n        if (options.dataCallback != null) {\n          options.data = options.dataCallback(options.data);\n        }\n        success = options.success;\n        options.success = function(data) {\n          var items, nbItems, selected_values;\n          if (data == null) {\n            return;\n          }\n          selected_values = [];\n          select.find('option').each(function() {\n            if (!$(this).is(\":selected\")) {\n              return $(this).remove();\n            } else {\n              return selected_values.push($(this).val() + \"-\" + $(this).text());\n            }\n          });\n          select.find('optgroup:empty').each(function() {\n            return $(this).remove();\n          });\n          items = callback != null ? callback(data, field) : data;\n          nbItems = 0;\n          $.each(items, function(i, element) {\n            var group, text, value;\n            nbItems++;\n            if (element.group) {\n              group = select.find(\"optgroup[label='\" + element.text + \"']\");\n              if (!group.size()) {\n                group = $(\"<optgroup />\");\n              }\n              group.attr('label', element.text).appendTo(select);\n              return $.each(element.items, function(i, element) {\n                var text, value;\n                if (typeof element === \"string\") {\n                  value = i;\n                  text = element;\n                } else {\n                  value = element.value;\n                  text = element.text;\n                }\n                if ($.inArray(value + \"-\" + text, selected_values) === -1) {\n                  return $(\"<option />\").attr('value', value).html(text).appendTo(group);\n                }\n              });\n            } else {\n              if (typeof element === \"string\") {\n                value = i;\n                text = element;\n              } else {\n                value = element.value;\n                text = element.text;\n              }\n              if ($.inArray(value + \"-\" + text, selected_values) === -1) {\n                return $(\"<option />\").attr('value', value).html(text).appendTo(select);\n              }\n            }\n          });\n          if (nbItems) {\n            select.trigger(\"liszt:updated\");\n          } else {\n            select.data().chosen.no_results_clear();\n            select.data().chosen.no_results(field.val());\n          }\n          if (settings.success != null) {\n            settings.success(data);\n          }\n          return field.val(untrimmed_val);\n        };\n        return this.timer = setTimeout(function() {\n          if (chosenXhr) {\n            chosenXhr.abort();\n          }\n          return chosenXhr = $.ajax(options);\n        }, options.afterTypeDelay);\n      });\n    });\n  };\n})(jQuery);\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"ajax-chosen\",\n  \"description\": \"A complement to the jQuery library Chosen that adds ajax autocomplete\",\n  \"version\": \"0.2.0\",\n  \"author\": {\n    \"name\": \"Ryan LeFevre\",\n    \"email\": \"meltingice8917@gmail.com\",\n    \"url\": \"http://meltingice.net\"\n  },\n  \"devDependencies\": {\n    \"coffee-script\": \">= 1.3.1\",\n    \"jsmin\": \"*\",\n    \"servedir\": \"*\"\n  },\n  \"scripts\": {\n    \"start\": \"servedir\"\n  }\n}"
  },
  {
    "path": "src/ajax-chosen.coffee",
    "content": "do ($ = jQuery) ->\n\n  $.fn.ajaxChosen = (settings = {}, callback, chosenOptions = {}) ->\n    defaultOptions =\n      minTermLength: 3\n      afterTypeDelay: 500\n      jsonTermKey: \"term\"\n      keepTypingMsg: \"Keep typing...\"\n      lookingForMsg: \"Looking for\"\n\n    # This will come in handy later.\n    select = @\n\n    chosenXhr = null\n\n    # Merge options with defaults\n    options = $.extend {}, defaultOptions, $(select).data(), settings\n\n    # Load chosen. To make things clear, I have taken the liberty\n    # of using the .chzn-autoselect class to specify input elements\n    # we want to use with ajax autocomplete.\n    @chosen(if chosenOptions then chosenOptions else {})\n\n    @each ->\n      # Now that chosen is loaded normally, we can bootstrap it with\n      # our ajax autocomplete code.\n      $(@).next('.chzn-container')\n        .find(\".search-field > input, .chzn-search > input\")\n        .bind 'keyup', ->\n          # This code will be executed every time the user types a letter\n          # into the input form that chosen has created\n\n          # Retrieve the current value of the input form\n          untrimmed_val = $(@).val()\n          val = $.trim $(@).val()\n\n          # Depending on how much text the user has typed, let them know\n          # if they need to keep typing or if we are looking for their data\n          msg = if val.length < options.minTermLength then options.keepTypingMsg else options.lookingForMsg + \" '#{val}'\"\n          select.next('.chzn-container').find('.no-results').text(msg)\n\n          # If input text has not changed ... do nothing\n          return false if val is $(@).data('prevVal')\n\n          # Set the current search term so we don't execute the ajax call if\n          # the user hits a key that isn't an input letter/number/symbol\n          $(@).data('prevVal', val)\n\n          # At this point, we have a new term/query ... the old timer\n          # is no longer valid.  Clear it.\n\n          # We delay searches by a small amount so that we don't flood the\n          # server with ajax requests.\n          clearTimeout(@timer) if @timer\n\n          # Some simple validation so we don't make excess ajax calls. I am\n          # assuming you don't want to perform a search with less than 3\n          # characters.\n          return false if val.length < options.minTermLength\n\n          # This is a useful reference for later\n          field = $(@)\n\n          # Default term key is `term`.  Specify alternative in options.options.jsonTermKey\n          options.data = {} unless options.data?\n          options.data[options.jsonTermKey] = val\n          options.data = options.dataCallback(options.data) if options.dataCallback?\n\n          # If the user provided an ajax success callback, store it so we can\n          # call it after our bootstrapping is finished.\n          success = options.success\n\n          # Create our own callback that will be executed when the ajax call is\n          # finished.\n          options.success = (data) ->\n            # Exit if the data we're given is invalid\n            return unless data?\n\n            # Go through all of the <option> elements in the <select> and remove\n            # ones that have not been selected by the user.  For those selected\n            # by the user, add them to a list to filter from the results later.\n            selected_values = []\n            select.find('option').each ->\n              if not $(@).is(\":selected\")\n                $(@).remove()\n              else\n                selected_values.push $(@).val() + \"-\" + $(@).text()\n            select.find('optgroup:empty').each ->\n              $(@).remove()\n\n\n            # Send the ajax results to the user callback so we can get an object of\n            # value => text pairs to inject as <option> elements.\n            items = if callback? then callback(data, field) else data\n\n\n            nbItems = 0\n\n            # Iterate through the given data and inject the <option> elements into\n            # the DOM if it doesn't exist in the selector already\n            $.each items, (i, element) ->\n              nbItems++\n\n              if element.group\n                group = select.find(\"optgroup[label='#{element.text}']\")\n                group = $(\"<optgroup />\") unless group.size()\n\n                group.attr('label', element.text)\n                  .appendTo(select)\n                $.each element.items, (i, element) ->\n                  if typeof element == \"string\"\n                    value = i;\n                    text = element;\n                  else\n                    value = element.value;\n                    text = element.text;\n                  if $.inArray(value + \"-\" + text, selected_values) == -1\n                    $(\"<option />\")\n                      .attr('value', value)\n                      .html(text)\n                      .appendTo(group)\n              else\n                if typeof element == \"string\"\n                  value = i;\n                  text = element;\n                else\n                  value = element.value;\n                  text = element.text;\n                if $.inArray(value + \"-\" + text, selected_values) == -1\n                  $(\"<option />\")\n                    .attr('value', value)\n                    .html(text)\n                    .appendTo(select)\n\n            if nbItems\n              # Tell chosen that the contents of the <select> input have been updated\n              # This makes chosen update its internal list of the input data.\n              select.trigger(\"liszt:updated\")\n            else\n              # If there are no results, display the no_results text\n              select.data().chosen.no_results_clear()\n              select.data().chosen.no_results field.val()\n\n            # Finally, call the user supplied callback (if it exists)\n            settings.success(data) if settings.success?\n\n            # For some reason, the contents of the input field get removed once you\n            # call trigger above. Often, this can be very annoying (and can make some\n            # searches impossible), so we add the value the user was typing back into\n            # the input field.\n            field.val(untrimmed_val)\n\n            # Because non-ajax Chosen isn't constantly re-building results, when it\n            # DOES rebuild results (during liszt:updated above, it clears the input\n            # search field before scaling it.  This causes the input field width to be\n            # at it's minimum, which is about 25px.\n\n            # The proper way to fix this would be create a new method in chosen for\n            # rebuilding results without clearing the input field.  Or to call\n            # Chosen.search_field_scale() after resetting the value above.  This isn't\n            # possible with the current state of Chosen.  The quick fix is to simply reset\n            # the width of the field after we reset the value of the input text.\n            # field.css('width','auto')\n\n          # Execute the ajax call to search for autocomplete data with a timer\n          @timer = setTimeout ->\n            chosenXhr.abort() if chosenXhr\n            chosenXhr = $.ajax(options)\n          , options.afterTypeDelay\n"
  }
]