Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  jquery.suggest 1.1b - 2007-08-06
  3.  * Patched by Mark Jaquith with Alexander Dick's "multiple items" patch to allow for auto-suggesting of more than one tag before submitting
  4.  * See: http://www.vulgarisoip.com/2007/06/29/jquerysuggest-an-alternative-jquery-based-autocomplete-library/#comment-7228
  5.  *
  6.  *  Uses code and techniques from following libraries:
  7.  *  1. http://www.dyve.net/jquery/?autocomplete
  8.  *  2. http://dev.jquery.com/browser/trunk/plugins/interface/iautocompleter.js
  9.  *
  10.  *  All the new stuff written by Peter Vulgaris (www.vulgarisoip.com)
  11.  *  Feel free to do whatever you want with this file
  12.  *
  13.  */
  14.  
  15. (function($) {
  16.  
  17.     $.suggest = function(input, options) {
  18.         var $input, $results, timeout, prevLength, cache, cacheSize;
  19.  
  20.         $input = $(input).attr("autocomplete", "off");
  21.         $results = $("<ul/>");
  22.  
  23.         timeout = false;        // hold timeout ID for suggestion results to appear
  24.         prevLength = 0;         // last recorded length of $input.val()
  25.         cache = [];             // cache MRU list
  26.         cacheSize = 0;          // size of cache in chars (bytes?)
  27.  
  28.         $results.addClass(options.resultsClass).appendTo('body');
  29.  
  30.  
  31.         resetPosition();
  32.         $(window)
  33.             .load(resetPosition)        // just in case user is changing size of page while loading
  34.             .resize(resetPosition);
  35.  
  36.         $input.blur(function() {
  37.             setTimeout(function() { $results.hide() }, 200);
  38.         });
  39.  
  40.         $input.keydown(processKey);
  41.  
  42.         function resetPosition() {
  43.             // requires jquery.dimension plugin
  44.             var offset = $input.offset();
  45.             $results.css({
  46.                 top: (offset.top + input.offsetHeight) + 'px',
  47.                 left: offset.left + 'px'
  48.             });
  49.         }
  50.  
  51.  
  52.         function processKey(e) {
  53.  
  54.             // handling up/down/escape requires results to be visible
  55.             // handling enter/tab requires that AND a result to be selected
  56.             if ((/27$|38$|40$/.test(e.keyCode) && $results.is(':visible')) ||
  57.                 (/^13$|^9$/.test(e.keyCode) && getCurrentResult())) {
  58.  
  59.                 if (e.preventDefault)
  60.                     e.preventDefault();
  61.                 if (e.stopPropagation)
  62.                     e.stopPropagation();
  63.  
  64.                 e.cancelBubble = true;
  65.                 e.returnValue = false;
  66.  
  67.                 switch(e.keyCode) {
  68.  
  69.                     case 38: // up
  70.                         prevResult();
  71.                         break;
  72.  
  73.                     case 40: // down
  74.                         nextResult();
  75.                         break;
  76.  
  77.                     case 9:  // tab
  78.                     case 13: // return
  79.                         selectCurrentResult();
  80.                         break;
  81.  
  82.                     case 27: // escape
  83.                         $results.hide();
  84.                         break;
  85.  
  86.                 }
  87.  
  88.             } else if ($input.val().length != prevLength) {
  89.  
  90.                 if (timeout)
  91.                     clearTimeout(timeout);
  92.                 timeout = setTimeout(suggest, options.delay);
  93.                 prevLength = $input.val().length;
  94.  
  95.             }
  96.  
  97.  
  98.         }
  99.  
  100.  
  101.         function suggest() {
  102.  
  103.             var q = $.trim($input.val()), multipleSepPos, items;
  104.  
  105.             if ( options.multiple ) {
  106.                 multipleSepPos = q.lastIndexOf(options.multipleSep);
  107.                 if ( multipleSepPos != -1 ) {
  108.                     q = $.trim(q.substr(multipleSepPos + options.multipleSep.length));
  109.                 }
  110.             }
  111.             if (q.length >= options.minchars) {
  112.  
  113.                 cached = checkCache(q);
  114.  
  115.                 if (cached) {
  116.  
  117.                     displayItems(cached['items']);
  118.  
  119.                 } else {
  120.  
  121.                     var checked_vals = [];
  122.                     jQuery("input[name='post_category[]']:checked").each(function(){
  123.                         checked_vals.push( $(this).val() );
  124.                     });
  125.  
  126.                     $.get(options.source + "&category=" + checked_vals.join(','), {q: q}, function(txt) {
  127.  
  128.                         $results.hide();
  129.  
  130.                         items = parseTxt(txt, q);
  131.  
  132.                         displayItems(items);
  133.                         addToCache(q, items, txt.length);
  134.  
  135.                     });
  136.  
  137.                 }
  138.  
  139.             } else {
  140.  
  141.                 $results.hide();
  142.  
  143.             }
  144.  
  145.         }
  146.  
  147.  
  148.         function checkCache(q) {
  149.             var i;
  150.             for (i = 0; i < cache.length; i++)
  151.                 if (cache[i]['q'] == q) {
  152.                     cache.unshift(cache.splice(i, 1)[0]);
  153.                     return cache[0];
  154.                 }
  155.  
  156.             return false;
  157.  
  158.         }
  159.  
  160.         function addToCache(q, items, size) {
  161.             var cached;
  162.             while (cache.length && (cacheSize + size > options.maxCacheSize)) {
  163.                 cached = cache.pop();
  164.                 cacheSize -= cached['size'];
  165.             }
  166.  
  167.             cache.push({
  168.                 q: q,
  169.                 size: size,
  170.                 items: items
  171.                 });
  172.  
  173.             cacheSize += size;
  174.  
  175.         }
  176.  
  177.         function displayItems(items) {
  178.             var html = '', i;
  179.             if (!items)
  180.                 return;
  181.  
  182.             if (!items.length) {
  183.                 $results.hide();
  184.                 return;
  185.             }
  186.  
  187.             resetPosition(); // when the form moves after the page has loaded
  188.  
  189.             for (i = 0; i < items.length; i++)
  190.                 html += '<li>' + items[i] + '</li>';
  191.  
  192.             $results.html(html).show();
  193.  
  194.             $results
  195.                 .children('li')
  196.                 .mouseover(function() {
  197.                     $results.children('li').removeClass(options.selectClass);
  198.                     $(this).addClass(options.selectClass);
  199.                 })
  200.                 .click(function(e) {
  201.                     e.preventDefault();
  202.                     e.stopPropagation();
  203.                     selectCurrentResult();
  204.                 });
  205.  
  206.         }
  207.  
  208.         function parseTxt(txt, q) {
  209.  
  210.             var items = [], tokens = txt.split(options.delimiter), i, token;
  211.  
  212.             // parse returned data for non-empty items
  213.             for (i = 0; i < tokens.length; i++) {
  214.                 token = $.trim(tokens[i]);
  215.                 if (token) {
  216.                     token = token.replace(
  217.                         new RegExp(q, 'ig'),
  218.                         function(q) { return '<span class="' + options.matchClass + '">' + q + '</span>' }
  219.                         );
  220.                     items[items.length] = token;
  221.                 }
  222.             }
  223.  
  224.             return items;
  225.         }
  226.  
  227.         function getCurrentResult() {
  228.             var $currentResult;
  229.             if (!$results.is(':visible'))
  230.                 return false;
  231.  
  232.             $currentResult = $results.children('li.' + options.selectClass);
  233.  
  234.             if (!$currentResult.length)
  235.                 $currentResult = false;
  236.  
  237.             return $currentResult;
  238.  
  239.         }
  240.  
  241.         function selectCurrentResult() {
  242.  
  243.             $currentResult = getCurrentResult();
  244.  
  245.             if ($currentResult) {
  246.                 if ( options.multiple ) {
  247.                     if ( $input.val().indexOf(options.multipleSep) != -1 ) {
  248.                         $currentVal = $input.val().substr( 0, ( $input.val().lastIndexOf(options.multipleSep) + options.multipleSep.length ) ) + ' ';
  249.                     } else {
  250.                         $currentVal = "";
  251.                     }
  252.                     $input.val( $currentVal + $currentResult.text() + options.multipleSep + ' ' );
  253.                     $input.focus();
  254.                 } else {
  255.                     $input.val($currentResult.text());
  256.                 }
  257.                 $results.hide();
  258.                 $input.trigger('change');
  259.  
  260.                 if (options.onSelect)
  261.                     options.onSelect.apply($input[0]);
  262.  
  263.             }
  264.  
  265.         }
  266.  
  267.         function nextResult() {
  268.  
  269.             $currentResult = getCurrentResult();
  270.  
  271.             if ($currentResult)
  272.                 $currentResult
  273.                     .removeClass(options.selectClass)
  274.                     .next()
  275.                         .addClass(options.selectClass);
  276.             else
  277.                 $results.children('li:first-child').addClass(options.selectClass);
  278.  
  279.         }
  280.  
  281.         function prevResult() {
  282.             var $currentResult = getCurrentResult();
  283.  
  284.             if ($currentResult)
  285.                 $currentResult
  286.                     .removeClass(options.selectClass)
  287.                     .prev()
  288.                         .addClass(options.selectClass);
  289.             else
  290.                 $results.children('li:last-child').addClass(options.selectClass);
  291.  
  292.         }
  293.     }
  294.  
  295.     $.fn.suggest = function(source, options) {
  296.  
  297.         if (!source)
  298.             return;
  299.  
  300.         options = options || {};
  301.         options.multiple = options.multiple || false;
  302.         options.multipleSep = options.multipleSep || ",";
  303.         options.source = source;
  304.         options.delay = options.delay || 100;
  305.         options.resultsClass = options.resultsClass || 'ac_results';
  306.         options.selectClass = options.selectClass || 'ac_over';
  307.         options.matchClass = options.matchClass || 'ac_match';
  308.         options.minchars = options.minchars || 2;
  309.         options.delimiter = options.delimiter || '\n';
  310.         options.onSelect = options.onSelect || false;
  311.         options.maxCacheSize = options.maxCacheSize || 65536;
  312.  
  313.         this.each(function() {
  314.             new $.suggest(this, options);
  315.         });
  316.  
  317.         return this;
  318.  
  319.     };
  320.  
  321. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement