Skip to content

Instantly share code, notes, and snippets.

@glamrock
Created September 8, 2016 15:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save glamrock/f539dead923d0131c665dcf70358bd50 to your computer and use it in GitHub Desktop.
(function(a) {
a.fn.swiftfilter = function(b) {
jQuery(this).each(function() {
jQuery(this)._swiftfilter()
})
};
a.fn._swiftfilter = function(l) {
l = a.extend(l, a(this).data());
var c = a.extend({
container: "html",
searchIn: "article",
child: "*",
childOnly: false,
autocomplete: true,
typeEnd: 0,
show: "fadeTo",
hide: "fadeTo",
durationShow: 500,
durationHide: 500,
opacity: 0.2,
showParam: "",
hideParam: "",
connect: null,
compare: "in",
}, l);
var b = c.showParam == "" ? c.durationShow : c.showParam;
var d = c.hideParam == "" ? c.durationHide : c.hideParam;
c.show = c.hide == "fadeTo" ? "fadeTo" : c.show;
if (c.show == "toggleClass" || c.hide == "toggleClass") {
c.show = "removeClass";
c.hide = "addClass";
b = c.showParam;
d = c.hideParam
}
for (var e in c) {
a(this).attr("data-" + e.replace(/([A-Z]+)/g, function(i) {
return "-" + i.toLowerCase()
}), c[e])
}
a("[data-keywords]").each(function() {
a(this).attr("data-keywords", " " + a(this).attr("data-keywords") + " ")
});
var j = this;
var g = [];
var f = [];
this.push_field = function(i) {
a(i).each(function() {
g.push(a(this))
})
};
this.get_autocomplete_list = function(m, u) {
var o = [];
var p = [];
var q = new RegExp("^" + m, "i");
var r = [];
for (var n in u) {
var i = u[n].split(" ");
for (var s in i) {
r.push(i[s].replace(/\W+/g, ""))
}
}
for (var t in r) {
if (r[t].match(q)) {
if (a.inArray(r[t], o) == -1) {
o.push(r[t]);
p.push(a("<li>", {
text: r[t],
}).css("cursor", "pointer"))
}
}
if (p.length >= 10) {
return p
}
}
return p
};
if (c.autocomplete && a(j).prop("type") == "text") {
a(j).each(function() {
var o = a(this).width() + parseInt(a(this).css("padding-left").replace(/[^\d]+/g, "")) + parseInt(a(this).css("padding-right").replace(/[^\d]+/g, ""));
var n = a(this).css("background-color") || "#fff";
var m = a(this).css("color") || "#000";
var i = a(this).css("font-size") || 12;
var p = a("<ul>", {
"class": "swiftfilter-autocomplete-container"
}).insertAfter(this);
a(p).css("position", "absolute");
a(p).css("z-index", 999999);
a(p).css("width", o);
a(p).css("background", n);
a(p).css("color", m);
a(p).css("opacity", 0.9);
a(p).css("font-size", i);
a(p).css("margin", 0);
a(p).css("padding", "5px");
a(p).css("list-style-type", "none");
a(p).css("display", "none");
a(document).on("click", ".swiftfilter-autocomplete-container li", function(q) {
a(j).val(a(this).text());
a(j).trigger("change")
});
a(this).on("blur", function() {
setTimeout(function() {
a(j).next().css("display", "none")
}, 200)
});
a(this).on("keydown", function(q) {
if (q.which == 40) {
var r = a(p).find("li.sf-ac-selected").length > 0 && a(p).find("li.sf-ac-selected").next().length > 0 ? a(p).find("li.sf-ac-selected").next() : a(p).find("li:first");
a(p).find("li.sf-ac-selected").css("background", a(p).css("background"));
a(p).find("li.sf-ac-selected").css("color", a(p).css("color"));
a(p).find("li.sf-ac-selected").removeClass("sf-ac-selected");
a(r).css("background", "#2D84DF");
a(r).css("color", "#FFF");
a(r).addClass("sf-ac-selected")
} else {
if (q.which == 38) {
var r = a(p).find("li.sf-ac-selected").length > 0 && a(p).find("li.sf-ac-selected").prev().length > 0 ? a(p).find("li.sf-ac-selected").prev() : a(p).find("li:last");
a(p).find("li.sf-ac-selected").css("background", a(p).css("background"));
a(p).find("li.sf-ac-selected").css("color", a(p).css("color"));
a(p).find("li.sf-ac-selected").removeClass("sf-ac-selected");
a(r).css("background", "#2D84DF");
a(r).css("color", "#FFF");
a(r).addClass("sf-ac-selected")
} else {
if (q.which == 9 || q.which == 13) {
if (a(p).find("li.sf-ac-selected").length > 0) {
q.preventDefault();
a(this).val(a(p).find("li.sf-ac-selected").text());
a(this).trigger("blur");
return false
}
} else {
if (q.which == 27) {
a(this).trigger("blur")
}
}
}
}
})
})
}
this.push_field(a(this));
a(this).attr("data-compare", c.compare);
if (c.connect != null) {
var k = c.connect.split(",");
for (var e in k) {
this.push_field(a(k[e]))
}
a(this).on
}
if (Boolean(c.autocomplete) === true) {
var h = [];
a(c.container + " " + c.searchIn).each(function() {
var i = a(this).index(c.container + " " + c.searchIn);
h[i] = ((Boolean(c.childOnly) === true ? "" : (a(this).text() ? a(this).text() : "") + (a(this).attr("data-keywords") ? a(this).attr("data-keywords") : "")) + (a(this).find(c.child).text() ? a(this).find(c.child).text() : "") + (a(this).find(c.child).attr("data-keywords") ? a(this).find(c.child).attr("data-keywords") : "")).toLowerCase().replace(/\s+/g, " ")
})
}
this.do_filter = function() {
a(c.container + " " + c.searchIn).each(function() {
var n = true;
for (var p in g) {
var t = ((a(g[p]).attr("data-child-only") == "true" ? "" : a(this).text() + (typeof a(this).attr("data-keywords") !== "undefined" ? a(this).attr("data-keywords") : "")) + a(this).find(a(g[p]).attr("data-child")).text() + (typeof a(this).find(a(g[p]).attr("data-child")).attr("data-keywords") !== "undefined" ? a(this).find(a(g[p]).attr("data-child")).attr("data-keywords") : "")).toLowerCase().replace(/\s+/g, " ");
var q = a(g[p]).val().toLowerCase();
if (a(g[p]).attr("data-compare") == "in") {
var u = (null === t.match(q) ? false : true)
} else {
if (a(g[p]).attr("data-compare") == "not_in") {
var u = (null === t.match(q) || q == "" ? true : false)
} else {
if (a(g[p]).attr("data-compare") == "regexp") {
var u = (t.match(new RegExp(q)) || q == "")
} else {
if (a(g[p]).attr("data-compare") == "equals") {
var u = (t == q || q == "")
} else {
if (a(g[p]).attr("data-compare") == "gt") {
console.log(((t.replace(/[^\d\.]+/g, "")) * 1));
var u = (((t.replace(/[^\d\.]+/g, "")) * 1) > (q * 1) || q == "")
} else {
if (a(g[p]).attr("data-compare") == "gte") {
var u = ((t.replace(/[^\d\.]+/g, "")) * 1 >= q * 1 || q == "")
} else {
if (a(g[p]).attr("data-compare") == "lt") {
var u = ((t.replace(/[^\d\.]+/g, "")) * 1 < q * 1 || q == "")
} else {
if (a(g[p]).attr("data-compare") == "lte") {
var u = ((t.replace(/[^\d\.]+/g, "")) * 1 <= q * 1 || q == "")
} else {
if (a(g[p]).attr("data-compare") == "between") {
var r = q.split("-");
r[0] == "" ? r[0] = -9999999999 : r[0];
r[1] == "" ? r[1] = 9999999999 : r[1];
var m = r[0] * 1 < r[1] * 1 ? r[1] * 1 : r[0] * 1;
var o = r[0] * 1 > r[1] * 1 ? r[1] * 1 : r[0] * 1;
var u = (((t.replace(/[^\d\.]+/g, "")) * 1 >= o && (t.replace(/[^\d\.]+/g, "")) <= m) || q == "")
}
}
}
}
}
}
}
}
}
n = n && (u || ((a(g[p]).prop("type") == "checkbox" || a(g[p]).prop("type") == "radio") && !a(g[p]).prop("checked")))
}
if (n) {
if (c.show == "fadeTo") {
a(this).stop()[c.show](c.durationShow, 1)
} else {
a(this).stop()[c.show](b)
}
} else {
if (c.hide == "fadeTo") {
a(this).stop()[c.hide](c.durationHide, c.opacity)
} else {
a(this).stop()[c.hide](d)
}
}
})
};
a(this).on("keyup change", function(i) {
if (i.which != 40 && i.which != 38 && a(j).prop("type") == "text") {
if (c.autocomplete && a(this).val() != "" && (c.compare == "in" || c.compare == "not_in")) {
a(this).next().css("display", "block");
a(this).next().empty().append(j.get_autocomplete_list(a(this).val(), h))
} else {
a(this).next().css("display", "none")
}
}
var m = (a(this).val() == "" || (a(this).prop("type") != "text") ? 0 : parseInt(c.typeEnd));
clearTimeout(j.type_timeout);
j.type_timeout = setTimeout(j.do_filter, m)
});
return this
}
}(jQuery));
jQuery(function() {
jQuery(".swiftfilter").each(function() {
jQuery(this).swiftfilter()
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment