Sha256: cf870474ee9bb4306a87782546ecdc5fa1f799180fc9061361e56950ee0824df

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

class Finder {

  static find(query){
    $.ajax({
        url: "/find",
        type: 'GET',
        data: { query: query }
    }).done(function(result) {
      $("#header .results").html(result);
    });
  }

  static do_find(url,query,html){
    $.ajax({
        url: url,
        type: 'GET',
        data: { query: query }
    }).done(function(result) {
      $(html).html(result);
    });
  }
}

var last_query = null;
var header_input_timeout = null;
$.xhrPool = [];

$.xhrPool.abortAll = function() {
    $(this).each(function(idx, jqXHR) {
        jqXHR.abort();
    });
    $(this).each(function(idx, jqXHR) {
        var index = $.inArray(jqXHR, $.xhrPool);
        if (index > -1) {
            $.xhrPool.splice(index, 1);
        }
    });
};

$.ajaxSetup({
    beforeSend: function(jqXHR) {
        $.xhrPool.push(jqXHR);
    },
    complete: function(jqXHR) {
        var index = $.inArray(jqXHR, $.xhrPool);
        if (index > -1) {
            $.xhrPool.splice(index, 1);
        }
    }
});

$(document).ready(function(){

  $("#header input").focusin(function() {
    $("#header .results").addClass("toggled");
  })
  $("#header input").focusout(function() {
    $("#header .results").removeClass("toggled");
  })
  $('#header .results').click(function() {
    $("#header input").focus();
  });
  $('#header input').keyup(function() {
      clearTimeout(header_input_timeout);

      if($('#header input').val().length > 2 && $.trim($('#header input').val())) {
        header_input_timeout = setTimeout(function() {
          $.xhrPool.abortAll();
          if (last_query != $.trim($('#header input').val())) {
            last_query = $.trim($('#header input').val());
            console.log(last_query);
            if (last_query.length > 2) {
              Finder.find(last_query);
            }
          }
        }, 1000);
      }
  });
});

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinatra-hexacta-0.5.0 lib/sinatra/public/js/finder.js
sinatra-hexacta-0.4.3 lib/sinatra/public/js/finder.js
sinatra-hexacta-0.4.2 lib/sinatra/public/js/finder.js