Last active
August 10, 2017 14:53
-
-
Save brettburwell/e2bb93840bb6171324606d517a88ab13 to your computer and use it in GitHub Desktop.
Entry Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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