Created
February 28, 2013 18:07
-
-
Save TechnotronicOz/5058792 to your computer and use it in GitHub Desktop.
Search
This file contains 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
CPAR.ajaxSearch = CPAR.ajaxSearch || (CPAR.ajaxSearch = { | |
$searchResults: $('#search-results'), | |
$searchButton: $('#search-button'), | |
$searchInput: $('#searchText'), | |
$searchCountry: $('#countryCollection'), | |
loadingDiv: '.loading', | |
parseResults: function(data) { | |
var dataString = ''; | |
if ( !data || typeof(data[0]) === 'undefined' || data[0].pageList.length === 0 || data[0].pageSize === 0) { | |
dataString = '<strong>No results found.</strong>'; | |
return false; | |
} | |
dataString += '<hr /><ul class="search_res">'; | |
for ( var i=0; i < data[0].pageList.length; i++) { | |
var resultsContent = data[0].pageList[i]; | |
dataString += '<li><a href="' + resultsContent.link + '">' + resultsContent.title + '</a><p>' + resultsContent.description + '</p></li>'; | |
} | |
dataString += '</ul>'; | |
return this.insertResults(dataString); | |
}, | |
insertResults: function(data) { | |
this.$searchResults.find(this.loadingDiv).remove(); | |
return this.$searchResults.append(data); | |
}, | |
searchSubmit: function() { | |
if ( this.$searchInput.val() === '' ) { | |
this.$searchInput.val('Enter a search term.'); | |
return; | |
} | |
if ( this.$searchResults.find('ul').children().length >= 1 ) { | |
this.$searchResults.empty(); | |
} | |
if ( this.$searchResults.find(this.loadingDiv).length ) { | |
return; | |
} else { | |
this.$searchResults.append('<h6 class="loading">Loading...</h6>'); | |
return this.grabJSON(this.$searchInput.val(), this.$searchCountry.val()); | |
} | |
}, | |
grabJSON: function(searchString, searchLocal) { | |
var _this = this; | |
return $.getJSON('http://vml.dev.colgate.com/app/Search/services/api/search/annualreport/search?method=com.colgate.search.rest.annualreport.Search&json=true&searchText=' + searchString + '&countryCollection=' + searchLocal + '&callback=?', function(data) { | |
_this.parseResults(data.rest.results.pagedListHolders); | |
}); | |
}, | |
getUrlVars: function() { | |
var vars = {}; | |
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { | |
vars[key] = value; | |
}); | |
return vars; | |
}, | |
searchFromURLString: function(searchString) { | |
if ( !searchString ) { | |
return this.$searchInput.val('Enter a search term.'); | |
} | |
var _this = this; | |
return setTimeout(function() { | |
_this.$searchInput.val(searchString); | |
_this.searchSubmit(); | |
}, 500); | |
}, | |
init: function() { | |
if ( this.getUrlVars()['searchInput'] ) { | |
var inputVal = this.getUrlVars()['searchInput']; | |
return this.searchFromURLString(inputVal); | |
} | |
var _this = this; | |
$('#restform').submit(function(e) { | |
e.preventDefault(); | |
return _this.searchSubmit(); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment