/**
 * Patterns autosuggest - suggestion/completion support
 *
 * Copyright 2012-2013 Florian Friesdorf
 * Copyright 2012 JC Brand
 * Copyright 2013 Marko Durkovic
 */
define([
    "jquery",
    "pat-parser",
    "pat-registry",
    "select2"
], function($, Parser, registry) {
    var parser = new Parser("autosuggest");
    parser.add_argument("words", "");
    parser.add_argument("pre-fill", function($el) { return $el.val(); });
    parser.add_argument("placeholder", function($el) {
        return $el.attr("placeholder") || "Enter text";
    });

    var _ = {
        name: "autosuggest",
        trigger: "input.pat-autosuggest",
        init: function($el, opts) {
            if ($el.length > 1)
                return $el.each(function() { _.init($(this), opts); });

            var cfg = parser.parse($el, opts);
            $el.val(cfg.preFill.split(','));
            $el.select2({
                placeholder: $el.attr("readonly") ? "" : cfg.placeholder,
                tags: cfg.words.split(/\s*,\s*/),
                tokenSeparators: [","],
                openOnEnter: false
            });

            // suppress propagation for second input field
            $el.prev().on("input-change input-defocus input-change-delayed",
                function(e) { e.stopPropagation(); }
            );

            // Clear the values when a reset button is pressed
            $el.closest('form').find('button[type=reset]').on('click', function () {
                $el.select2('val', '');
            });
            return $el;
        },
        destroy: function($el) {
            $el.off(".pat-autosuggest");
            $el.select2("destroy");
        },
        transform: function($content) {
            $content.findInclusive('input[type=text].pat-autosuggest').each(function() {
                var $src = $(this),
                    $dest = $('<input type="hidden"/>').insertAfter($src);

                $src.detach();
                $.each($src.prop('attributes'), function() {
                    if (this.name !== 'type') {
                        $dest.attr(this.name, this.value);
                    }
                });
                $src.remove();
            });
        }
    };
    registry.register(_);
    return _;
});

// jshint indent: 4, browser: true, jquery: true, quotmark: double
// vim: sw=4 expandtab

@@ -349,24 +349,162 @@
 ords%22, %22%22);%0A
+    parser.add_argument(%22ajax-url%22, %22%22);%0A    parser.add_argument(%22ajax-data-type%22, %22%22);%0A    parser.add_argument(%22ajax-search-index%22, %22%22);%0A
     parser.a
@@ -561,24 +561,24 @@
 .val(); %7D);%0A
-
     parser.a
@@ -837,16 +837,18 @@
 gth %3E 1)
+ %7B
 %0A       
@@ -908,25 +908,38 @@
  opts); %7D);%0A
+            %7D
 %0A
-
             
@@ -1018,16 +1018,17 @@
 (','));%0A
+%0A
         
@@ -1031,28 +1031,29 @@
         
-$el.select2(
+var config = 
 %7B%0A      
@@ -1245,16 +1245,16 @@
 : false%0A
-
         
@@ -1254,24 +1254,1145 @@
            %7D
+;%0A%0A            if ((cfg.ajax) && (cfg.ajax.url)) %7B%0A                config = $.extend(true, %7B%0A                    minimumInputLength: 1,%0A                    ajax: %7B%0A                        url: cfg.ajax.url,%0A                        dataType: cfg.ajax%5B'data-type'%5D,%0A                        type: 'POST',%0A                        quietMillis: 400,%0A                        data: function (term, page) %7B%0A                            return %7B%0A                                index: cfg.ajax%5B'search-index'%5D,%0A                                q: term, // search term%0A                                page_limit: 10,%0A                                page: page%0A                            %7D;%0A                        %7D,%0A                        results: function (data, page) %7B%0A                            // parse the results into the format expected by Select2.%0A                            // data must be a list of objects with keys 'id' and 'text'%0A                            return %7Bresults: data, page: page%7D;%0A                        %7D%0A                    %7D%0A                %7D, config);%0A            %7D%0A%0A            $el.select2(config
 );%0A%0A        

Add Ajax functionality to pat-autosuggest