Skip to content

Instantly share code, notes, and snippets.

@brettburwell
Last active August 10, 2017 14:53
Show Gist options
  • Save brettburwell/e2bb93840bb6171324606d517a88ab13 to your computer and use it in GitHub Desktop.
Save brettburwell/e2bb93840bb6171324606d517a88ab13 to your computer and use it in GitHub Desktop.
Entry Search
var EventEmitter = require('eventEmitter');
var Debounce = require('debounce');
var PubSub = require('pubsub-js');
module.exports = function(options) {
/*
DESCRIPTION
Entry search/filtering via search input.
OPTIONS:
$searchInput The form input where the search will be entered
[jQuery Object] (required)
*/
//
// Private Vars
//
//////////////////////////////////////////////////////////////////////
var self = $.extend(new EventEmitter(), {}, {
qsRegex: null
});
//
// Public Vars
//
//////////////////////////////////////////////////////////////////////
self.settings = $.extend({
}, options);
//
// Private Methods
//
//////////////////////////////////////////////////////////////////////
var _init = function() {
_addEventListeners();
};
var _addEventListeners = function() {
// On key press
self.settings.$searchInput.keyup(_debounce(_onChange, 1000));
// Prevent default form submission
$('form').submit(_onSubmitNoTouch);
}
};
var _debounce = function(fn, threshold) {
var timeout;
return function debounced() {
if (timeout) {
clearTimeout(timeout);
}
function delayed() {
fn();
timeout = null;
}
timeout = setTimeout(delayed, threshold || 10);
}
}
var _onChange = function(evt) {
self.emitEvent('submit');
};
//
// Public Methods
//
//////////////////////////////////////////////////////////////////////
self.getValue = function() {
return self.settings.$searchInput.val();
};
//
// Initialize
//
//////////////////////////////////////////////////////////////////////
_init();
// Return the Object
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment