]> git.ipfire.org Git - thirdparty/Font-Awesome.git/commitdiff
Getting all highlighted matches
authorTravis Chase <travis@travischase.me>
Tue, 1 Sep 2015 16:06:49 +0000 (11:06 -0500)
committerTravis Chase <travis@travischase.me>
Tue, 1 Sep 2015 16:10:19 +0000 (11:10 -0500)
src/assets/js/search.js
src/assets/less/site/search.less
src/icons.html

index 5dbcbb81812b651c26133cd0de0b7e8429fa34db..1353ffe2ec76a5b53f373bf93dcc43f5b9c540e7 100644 (file)
@@ -1,8 +1,5 @@
 $(function() {
     var SearchView = Backbone.View.extend({
-
-        template: _.template($("#results-template").html()),
-
         events: {
             "keyup #search-input": "search",
             "click #search-clear": "clear"
@@ -11,6 +8,7 @@ $(function() {
         initialize: function() {
             this.algolia = algoliasearch("M19DXW5X0Q", "c79b2e61519372a99fa5890db070064c");
             this.algoliaHelper = algoliasearchHelper(this.algolia, "font_awesome");
+            this.template = _.template($("#results-template").html());
 
             this.$searchInput = this.$("#search-input");
             this.$searchResultsSection = this.$("#search-results");
@@ -41,7 +39,14 @@ $(function() {
             this.$searchResultsSection.removeClass("hide");
             this.$searchInputClear.removeClass("hide");
             this.$iconsSection.addClass("hide");
-            this.$searchResultsSection.html(this.template({results: content}));
+
+            var results = [];
+
+            _.each(content.hits, function(result) {
+                results.push(new SearchResultView({ result: result }).render())
+            });
+
+            this.$searchResultsSection.html(this.template({ content: content, results: results.join("") }));
         },
 
         clearResults: function() {
@@ -53,6 +58,32 @@ $(function() {
         }
     });
 
+    var SearchResultView = Backbone.View.extend({
+        initialize: function(options) {
+            this.template = _.template($("#result-template").html());
+            this.result = options.result
+        },
+
+        render: function() {
+            var matches = [];
+
+            this.pullMatches(matches, this.result._highlightResult.aliases);
+            this.pullMatches(matches, this.result._highlightResult.synonyms);
+
+            return this.template({ result: this.result, matches: matches.join(", ") });
+        },
+
+        pullMatches: function(matches, list) {
+            if (list !== undefined) {
+                _.each(list, function(highlight) {
+                    if (highlight.matchLevel !== "none") {
+                        matches.push(highlight.value)
+                    }
+                })
+            }
+        }
+    });
+
 
     var $searchViewElement = $("[data-view=search]");
 
index ccac069ae1b1384491fa168650f249bcfd6a31ae..7778518cc1763a474bc13e785c99bcd77d15ced1 100644 (file)
   }
 }
 
+#search-results {
+  em {
+    font-style: normal;
+    background: #1fa67a;
+  }
+}
+
 .algolia {
   margin-top: -20px;
   padding-top: 45px;
index 326660af26e3fb302e35fb42d7cd9b8b9072f999..215eecdf93e8abb6d7ea254de958328f1990ffe7 100644 (file)
@@ -56,16 +56,10 @@ relative_path: ../
     {% include icons/medical.html %}
   </div>
   <script type="text/template" id="results-template">
-    <h2 class="page-header">Search for '<span class="text-color-default"><%= results.query %></span>'</h2>
-    <% if (results.nbHits > 0) { %>
+    <h2 class="page-header">Search for '<span class="text-color-default"><%= content.query %></span>'</h2>
+    <% if (content.nbHits > 0) { %>
       <div class="row fontawesome-icon-list">
-      <% _.each(results.hits, function(icon) { %>
-        <div class="fa-hover col-md-3 col-sm-4">
-          <a href="{{ page.relative_path }}icon/<%= icon.name %>">
-            <i class="fa <%= icon.css_class %>"></i> <%= icon.name %>
-          </a>
-        </div>
-      <% }); %>
+        <%= results %>
       </div>
     <% } else { %>
       <div class="alert alert-danger text-lg" role="alert">
@@ -90,4 +84,11 @@ relative_path: ../
       </div>
     <% } %>
   </script>
+  <script type="text/template" id="result-template">
+    <div class="fa-hover col-md-3 col-sm-4">
+      <a href="{{ page.relative_path }}icon/<%= result.name %>">
+        <i class="fa <%= result.css_class %>"></i> <%= result._highlightResult.name.value %> <% if (matches !== "") { %>(<%= matches %>)<% } %>
+      </a>
+    </div>
+  </script>
 </div>