Created
January 30, 2013 00:30
-
-
Save oojacoboo/4669481 to your computer and use it in GitHub Desktop.
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
| /** | |
| * Documents Pagination Class | |
| * @param template | |
| * @param container | |
| * @constructor | |
| */ | |
| function DocumentsPager(template, container) { | |
| this.container = container; | |
| this.template = $(template).html(); | |
| this.data = {}; | |
| this.filteredData = {}; | |
| //following need resetting | |
| this.searchQuery = ""; | |
| this.groupId = null; | |
| this.pagerIncrement = 10; | |
| this.pagerLimit = this.pagerIncrement; | |
| /** | |
| * Will reset all the necessary members to establish a clean slate | |
| * @note don't need to reset the json set b/c we reset before filtering down in the buildJson method | |
| * @return {*} | |
| */ | |
| this.reset = function() { | |
| this.searchQuery = ""; | |
| this.groupId = null; | |
| this.pagerLimit = this.pagerIncrement; | |
| return this; | |
| }; | |
| /** | |
| * Loads the json parsed object (data) into the class | |
| * @param data object | |
| * @return {*} | |
| */ | |
| this.loadData = function(data) { | |
| this.data = data; | |
| this.filteredData = _.clone(data); //must clone b/c of assignment by reference | |
| return this; | |
| }; | |
| /** | |
| * Sets the new pagination result threshold | |
| * @param pagerLimit | |
| * @return {*} | |
| */ | |
| this.setPagerLimit = function(pagerLimit) { | |
| this.pagerLimit = pagerLimit || this.pagerLimit; | |
| return this; | |
| }; | |
| /** | |
| * SET the current search string and | |
| * @param query | |
| * @return {*} | |
| */ | |
| this.setSearch = function(query) { | |
| this.searchQuery = query; | |
| return this; | |
| }; | |
| /** | |
| * SET the currently filtered group id | |
| * @param groupId | |
| * @return {*} | |
| */ | |
| this.setFilterGroup = function(groupId) { | |
| this.groupId = groupId; | |
| return this; | |
| }; | |
| /** | |
| * Performs the actual search logic | |
| * @private | |
| * @return {*} | |
| */ | |
| this.search = function() { | |
| if(this.searchQuery.length < 1) return this; | |
| var searchPattern = new RegExp('(' + RegExp.escape(this.searchQuery) + ')', "ig"); | |
| function match(s) { | |
| return (s) ? s.match(searchPattern) : false; | |
| } | |
| function containsMatch(doc) { | |
| return match(doc.nickname) ? true : | |
| _.chain(doc.docRelations) | |
| .map(function(r) { return [r.displayName, r.unitAddress]; }) | |
| .crush() | |
| .reject(_.isUndefined) | |
| .any(match).value(); | |
| } | |
| this.filteredData.documents = _.filter(this.filteredData.documents, containsMatch); | |
| return this; | |
| }; | |
| /** | |
| * Performs the actual filter logic | |
| * @private | |
| * @return {*} | |
| */ | |
| this.filter = function() { | |
| if(!this.groupId) return this; | |
| var thisClass = this; | |
| this.filteredData.documents = _.filter(this.filteredData.documents, function(doc) { | |
| return (doc.groupId == thisClass.groupId); | |
| }); | |
| return this; | |
| }; | |
| /** | |
| * Builds the data object based on all the filters and searches | |
| * @private | |
| * @return {*} | |
| */ | |
| this.buildData = function() { | |
| if(_.isEmpty(this.data)) return this; | |
| var builtData = _.clone(this.data); | |
| //reset the filteredJson object before filtering down again | |
| this.filteredData = _.clone(this.data); | |
| //perform search and filter | |
| this.search().filter(); | |
| //if we've searched and set this.searchedJson, use this instead of our original object | |
| if(!_.isEqual(this.data, this.filteredData)) { | |
| builtData = this.filteredData; | |
| } | |
| //paginate the dataset | |
| builtData.documents = builtData.documents.slice(0, this.pagerLimit); | |
| return builtData; | |
| }; | |
| /** | |
| * Will actually render the template within the DOM | |
| * @param callback function | |
| */ | |
| this.renderTemplate = function(callback) { | |
| if(_.isEmpty(this.data)) return "Error getting data object"; | |
| var data = this.buildData(), | |
| template = _.template(this.template, data); | |
| $(this.container).html(template); | |
| if(_.isFunction(callback)) | |
| callback.call(); | |
| return this; | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment