Created
January 12, 2016 18:18
-
-
Save a-am/0edf0753877e098b7b3b to your computer and use it in GitHub Desktop.
Swift Search Methods
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
tm.Search.init = function () { | |
if (!$("body").hasClass('search-page')) { | |
var inputs = [ | |
"st-input-access", | |
"st-input-header", | |
"st-input-header-stick", | |
"st-input-mobile" | |
]; | |
for (var i = inputs.length - 1; i >= 0; i--) { | |
if($("#" + inputs[i]).length > 0){ | |
var elm = $("#" + inputs[i]), | |
filter = elm.data('filter'); | |
tm.Search.add({el:elm,filter:filter}); | |
} | |
}; | |
$("form[action='/search-results']").on('submit', function(evt){ | |
evt.preventDefault(); | |
var url = $(this).data('url') || ""; | |
var query = $(this).find('.search-input').val(); | |
window.location = url + "search-results/#stq=" + encodeURIComponent(query); | |
}); | |
}; | |
}; | |
tm.Search.add = function (params) { | |
var input = params.el, | |
//meta = $("meta.swiftype[name='source']").attr('content'), | |
meta = $("body").data("campus"), | |
source = meta !== "" ? meta : "", | |
types = params.filter !== '' ? params.filter : "", | |
filters = null, | |
campuses = { | |
'watermark-dallas': 'Dallas Campus,', | |
'watermark-fort-worth': 'Fort Worth Campus,', | |
'watermark-plano': 'Plano Campus,', | |
'watermark-church': '' | |
}; | |
if (types !== "") { | |
filters = {'page': {'type': types, 'source':[source,'watermarkall'] }}; | |
}else if( source !== ""){ | |
filters = {'page': {'source': [source,'watermarkall'] }}; | |
}; | |
var customRenderFunction = function(document_type, item) { | |
var title = item.highlight['title'], | |
image = "", | |
campusTitle = campuses[item['campus']]; | |
if (item.highlight['title'] == undefined) { | |
title = item['title']; | |
}; | |
if (item['image'] !== "") { | |
image = '<img src="' + item['image'] + '" width="70px" height="70px" />'; | |
}; | |
var out = image.concat('<p class="title">' + title + '</p>'); | |
return out.concat('<p class="sections">' + campusTitle + ' ' + item['type'] + '</p>'); | |
}; | |
var params = { | |
renderFunction: customRenderFunction, | |
searchFields: ['title','body'], | |
resultLimit: 6, | |
engineKey: 'L_KuK3ycctWwBT-oL9dF', | |
sortField: "published_at", | |
sortDirection: "asc" | |
}; | |
if (filters !== null) { | |
params['filters'] = filters; | |
}; | |
input.swiftype(params); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment