<form action="{{typeahead.action}}" data-toggle="validator">
<div class="control-group {{typeahead.class}}" style="{{typeahead.style}}">
  <div class="input-group">
    <span class="input-group-btn">
      <button class="btn btn-default" type="submit" id="{{typeahead.base_id}}_submit" data-ays-ignore="true">
        <span class="glyphicon glyphicon-search"></span>
      </button>
    </span>
  {% for k,v in pairs(typeahead.parameters or {}) do %}
    <input type="hidden" name="{{k}}" value="{{v}}" />
  {% end %}
    <input id="{{typeahead.base_id}}_query" type="hidden" name="{{typeahead.query_field}}" data-ays-ignore="true"/>
    <input id="{{typeahead.base_id}}_search_field" type="text" data-minlength="1" class="form-control search-query span2" placeholder="{{typeahead.query_title}}" data-provide="typeahead" autocomplete="off"></input>
  </div>
</div>

<script type='text/javascript'>
  var _typeahead_prototype = $.fn.typeahead.Constructor.prototype;
  var _typeahead_overridden_callbacks = {
    "click": null,
    "select": null,
  }

  /* Temporary override the typeahead click callback */
  for (var callback_name in _typeahead_overridden_callbacks) {
    /* Dump original callback */
    _typeahead_overridden_callbacks[callback_name] = _typeahead_prototype[callback_name];

    _typeahead_prototype[callback_name] = function() {
      if (this.$menu.find("> li[data-no-results]").length == 0) {
        /* invoke the original callback */
        _typeahead_overridden_callbacks[callback_name].apply(this, arguments);
      }
    };
  }

  $('#{{typeahead.base_id}}_search_field').typeahead({
    source: function (query, process) {
      /* Avoid this kind of queries which generates file path warnings into the ntopng log */
      if (query.startsWith(".")) return;

      var _get = {};
    {% for k,v in pairs(typeahead.parameters or {}) do %}
      _get["{{k}}"] = "{{v}}";
    {% end %}
      _get["query"] = query;

      return $.get("{*typeahead.query_url*}", _get, function (data) {
        var res = process(data.results);
        var menu = res.$menu;

        menu.click(function (e) { e.preventDefault(); });

        if (data.results && data.results.length > 0) {
          $("li:last", menu).addClass("hidden");
        } else {
          $("li:last", menu)
            .attr("data-no-results", "")
            .html("<span style='padding-left:1em; cursor:default;'>No results found</span>");
        }

        return res;
      });
    }, afterSelect: function(item) {
      if (! item.no_results) {
        var form = $("#{{typeahead.base_id}}_query")
          .val(item["{{typeahead.json_key}}"])
          .closest("form");

        if ({*typeahead.before_submit or "$.noop"*}(form, item) !== "false")
          form.submit();
      }
    }, addItem: {name:"", no_results:true},
  });

  $('#{{typeahead.base_id}}_submit').click(function(e) {
    if ($('#{{typeahead.base_id}}_query').val() === '') {
      /* No typeahead result has been selected by the user */
      if($('#{{typeahead.base_id}}_search_field').val() === '' || "${{typeahead.allow_partial}}" !== "true") {
        /* Do not submit if also the typeahead content is empty or if we do not allow partial inputs */
        e.preventDefault();
      } else {
        /* Populate the search field with the user submitted values */
        $("#{{typeahead.base_id}}_query").val($('#{{typeahead.base_id}}_search_field').val());
      }
    }
  });

  /* Restore the original callbacks for new objects */
  for (var callback_name in _typeahead_overridden_callbacks)
    _typeahead_prototype[callback_name] = _typeahead_overridden_callbacks[callback_name];
</script>
</form>
