Skip to content

Instantly share code, notes, and snippets.

@seb-thomas
Created November 17, 2016 23:00
Show Gist options
  • Save seb-thomas/74279a0dc30e5e2cb38f9ebdeaf8ee27 to your computer and use it in GitHub Desktop.
Save seb-thomas/74279a0dc30e5e2cb38f9ebdeaf8ee27 to your computer and use it in GitHub Desktop.
var SearchForm = function(e) {
return this instanceof SearchForm ? (this.$el = $(e),
void this.$el.on("submit", $.proxy(this.handleSubmit, this))) : new SearchForm(e)
};
SearchForm.prototype.handleSubmit = function(e) {
e.preventDefault();
var a = $.trim(this.$el.find("#search-input").val());
a && $(document).trigger("search", a)
}
;
var SearchData = function(e) {
return this instanceof SearchData ? void 0 : new SearchData(e)
};
SearchData.prototype.fetch = function(e) {
if (!e) {
var a = $.Deferred();
return a.resolve([]),
a.promise()
}
return $.get(CONFIG.search.url, {
query: e
}).then(function(e) {
return e
})
}
;
var SearchResults = function(e) {
return this instanceof SearchResults ? void (this.el = $(e)) : new SearchResults(e)
};
SearchResults.prototype.pending = function() {
this.el.html('<li class="searching count">Searching &hellip;</li>')
}
,
SearchResults.prototype.setResults = function(e) {
var a = $('<div class="results-wrapper">')
, r = 0;
e ? (lmGlobals.searchLayer.eachLayer(function(e) {
r++;
var s = e.toGeoJSON().properties
, t = s.gid
, n = $('<li id="' + t + '" class="result">');
$(n).append("address" === s.type ? '<h4 class="name">' + s.name + '</h4><div class="zip">' + s.zip + "</div>" : '<h4 class="name">' + s.name + '</h4><div class="address">' + s.address + '</div><div class="zip">' + s.zip + "</div>"),
$(n).click(function() {
$(".svg-marker-icon").attr("class", "svg-marker-icon"),
$("." + t).find(".svg-marker-icon").attr("class", "svg-marker-icon active"),
$(this).siblings().removeClass("active"),
$(this).addClass("active"),
map.panTo(e._latlng),
s.zoomLevel ? (console.log("zooming to " + s.zoomLevel),
map.setView(e._latlng, s.zoomLevel, {
animate: !0
})) : map.panTo(e._latlng)
}),
$(a).append(n)
}),
this.el.parent(".search-container").addClass("on"),
$(a).prepend('<div class="count">Your search returned ' + r + " results</div>")) : (this.el.parent(".search-container").addClass("on"),
a = '<div class="count">Your search returned no results</div>',
void 0 !== lmGlobals.searchLayer && map.removeLayer(lmGlobals.searchLayer)),
this.el.html(a)
}
,
$(document).ready(function() {
var e = !1
, a = new SearchForm($("#search"))
, r = new SearchResults($("#results"))
, s = new SearchData;
if ($(document).on("search", function(a, t) {
if (!e) {
var n = t;
e = !0,
s.fetch(n).then(function(a) {
var s = a.features;
s.length ? (addFeatures(a, !0),
r.setResults(!0)) : r.setResults(!1),
e = !1
}),
r.pending()
}
}),
$(".reset").click(function() {
$("#search-input").val(""),
$(".search-container").removeClass("on"),
$("#results").html(""),
void 0 !== lmGlobals.searchLayer && map.removeLayer(lmGlobals.searchLayer)
}),
$("#search-icon").click(function() {
$(".search-container, .mobile-arrow").addClass("open"),
$("body").addClass("search-open")
}),
$("html").on("click", ".mobile-arrow, .mobile .result", function() {
$(".search-container, .mobile-arrow").removeClass("open"),
$("body").removeClass("search-open"),
$(".search-container").removeClass("on"),
$("#results").html("")
}),
window.location.search.indexOf("query") > -1 && $(".search-container").show(),
params.query) {
var t = params.query;
$(document).trigger("search", t),
a.$el.find("#search-input").val(decodeURIComponent(t))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment