ulvis.paste.net

Paste Search Dynamic
Recent pastes
search_query.js
  1. (function() {
  2.  
  3. var $ = jQuery; // Handle namespaced jQuery
  4. Collection which holds all of the individual facets (category: value). Used for finding and removing specific facets.
  5.  
  6.  
  7. VS.model.SearchQuery = Backbone.Collection.extend({
  8. Model holds the category and value of the facet.
  9.  
  10.  
  11.   model : VS.model.SearchFacet,
  12. Turns all of the facets into a single serialized string.
  13.  
  14.  
  15.   serialize : function() {
  16.     return this.map(function(facet){ return facet.serialize(); }).join(' ');
  17.   },
  18.  
  19.   facets : function() {
  20.     return this.map(function(facet) {
  21.       var value = {};
  22.       value[facet.get('category')] = facet.get('value');
  23.       return value;
  24.     });
  25.   },
  26. Find a facet by its category. Multiple facets with the same category is fine, but only the first is returned.
  27.  
  28.  
  29.   find : function(category) {
  30.     var facet = this.detect(function(facet) {
  31.       return facet.get('category').toLowerCase() == category.toLowerCase();
  32.     });
  33.     return facet && facet.get('value');
  34.   },
  35. Counts the number of times a specific category is in the search query.
  36.  
  37.  
  38.   count : function(category) {
  39.     return this.select(function(facet) {
  40.       return facet.get('category').toLowerCase() == category.toLowerCase();
  41.     }).length;
  42.   },
  43. Returns an array of extracted values from each facet in a category.
  44.  
  45.  
  46.   values : function(category) {
  47.     var facets = this.select(function(facet) {
  48.       return facet.get('category').toLowerCase() == category.toLowerCase();
  49.     });
  50.     return _.map(facets, function(facet) { return facet.get('value'); });
  51.   },
  52. Checks all facets for matches of either a category or both category and value.
  53.  
  54.  
  55.   has : function(category, value) {
  56.     return this.any(function(facet) {
  57.       var categoryMatched = facet.get('category').toLowerCase() == category.toLowerCase();
  58.       if (!value) return categoryMatched;
  59.       return categoryMatched && facet.get('value') == value;
  60.     });
  61.   },
  62. Used to temporarily hide specific categories and serialize the search query.
  63.  
  64.  
  65.   withoutCategory : function() {
  66.     var categories = _.map(_.toArray(arguments), function(cat) { return cat.toLowerCase(); });
  67.     return this.map(function(facet) {
  68.       if (!_.include(categories, facet.get('category').toLowerCase())) {
  69.         return facet.serialize();
  70.       };
  71.     }).join(' ');
  72.   }
  73.  
  74. });
  75.  
  76. })();
Parsed in 0.018 seconds