Skip to content

Commit

Permalink
Updated dropdown to filter client side (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeblau authored Mar 28, 2017
1 parent b581843 commit aa10843
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
29 changes: 15 additions & 14 deletions Public/js/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
'use strict';

$(".ignore-search").select2({
ajax: {
url: "/dropdown/templates.json",
data: function(params) {
return { term: params.term.toLowerCase() }
$.ajax('/dropdown/templates.json').success(function(data) {
$(".ignore-search").select2({
sorter: function(results) {
var query = $('.select2-search__field').val().toLowerCase();
return results.sort(function(a, b) {
return a.text.toLowerCase().indexOf(query) -
b.text.toLowerCase().indexOf(query);
});
},
processResults: function (data) {
return { results: data }
},
cache: true
},
placeholder: "Search Operating Systems, IDEs, or Programming Languages",
minimumInputLength: 1,
theme: "bootstrap",
multiple: true
placeholder: "Search Operating Systems, IDEs, or Programming Languages",
minimumInputLength: 1,
theme: "bootstrap",
multiple: true,
data: data
});
});


// Delete selecitons by tag instead of individual letter
$(".ignore-search").on("select2:unselect", () => {
$(".ignore-search").on("select2:open", () => {
Expand Down
11 changes: 7 additions & 4 deletions Sources/GitignoreIOServer/RouteHandlers/SiteRouteHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ internal class SiteHandlers {
])
}
}

/// Create dropdown template JSON list
///
/// - Parameter drop: Vapor server side Swift droplet
internal func createDropdownTemplates(drop: Droplet) {
drop.get("/dropdown/templates.json") { request in
guard let queryString = request.query?["term"]?.string?.lowercased() else {
return try JSON(node: Node.null)
return try JSON(node: self.createSortedDropdownTemplates())
}
return try JSON(node: self.createSortedDropdownTemplates(query: queryString))
}
Expand All @@ -83,11 +83,14 @@ internal class SiteHandlers {
/// - Parameter templates: Template controller template dictionary
///
/// - Returns: JSON array containing all templates
private func createSortedDropdownTemplates(query: String) -> Node {
private func createSortedDropdownTemplates(query: String? = nil) -> Node {
return Node.array(templates
.values
.filter({ (templateModel) -> Bool in
templateModel.key.contains(query)
guard let query = query else {
return true
}
return templateModel.key.contains(query)
})
.sorted(by: { $0.key < $1.key })
.sorted(by: { $0.key.characters.count < $1.key.characters.count })
Expand Down

0 comments on commit aa10843

Please sign in to comment.