Code coverage report for api/search/Search.js

Statements: 77.78% (42 / 54)      Branches: 71.79% (28 / 39)      Functions: 83.33% (10 / 12)      Lines: 77.78% (42 / 54)      Ignored: none     

All files » api/search/ » Search.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571 1                           1 9 9   9 9     1                                             1 1                   31     2 2 2 1 1 1               79     11     11       11 11               6 5   6 6           6       6 6       6                   27       27     6   2 2 2   2 2     6 6     6                         1  
var FieldDBObject = require("./../FieldDBObject").FieldDBObject;
var DataList = require("./../data_list/DataList").DataList;
 
/**
 * @class Search progressively searches a corpus and updates a search/data list
 *  view as a user types keywords in the search box. Both intersection and
 *  union search is possible. It highlights search keywords in the list view.
 *
 * @property {String} searchQuery
 * @property {DataList} A list of data which fulfill the search query
 *
 * @name  Search
 * @extends FieldDBObject
 * @constructs
 */
var Search = function Search(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "Search";
  }
  this.debug("Constructing Search length: ", options);
  FieldDBObject.apply(this, arguments);
};
 
Search.prototype = Object.create(FieldDBObject.prototype, /** @lends Search.prototype */ {
  constructor: {
    value: Search
  },
 
  defaults: {
    value: {
      searchQuery: ""
    }
  },
 
  INTERNAL_MODELS: {
    value: {
      datalist: DataList
    }
  },
 
  searchKeywords: {
    get: function() {
      this.warn("searchKeywords is deprecated, use searchQuery instead.");
      return this.searchQuery;
    },
    set: function(value) {
      this.warn("searchKeywords is deprecated, use searchQuery instead.");
      this.searchQuery = value;
    }
  },
 
  /**
   *  Search id is a url encoded version of the current search which means the id will change over time.
   */
  id: {
    configurable: true,
    get: function() {
      return encodeURI(this.searchQuery);
    },
    set: function(value) {
      var decoded = decodeURI(value);
      Eif (this.searchQuery !== value && this.searchQuery !== decoded) {
        if (value === decoded) {
          this.searchQuery = value;
        } else Eif (value !== decoded) {
          this.searchQuery = decoded;
        }
      }
    }
  },
 
  searchQuery: {
    get: function() {
      return this._searchQuery || this.defaults.searchQuery;
    },
    set: function(value) {
      Iif (value === this._searchQuery) {
        return;
      }
      Iif (!value) {
        delete this._searchQuery;
        return;
      }
      this._searchQuery = value.trim();
      this._id = encodeURI(this._searchQuery);
    }
  },
 
  search: {
    configurable: true,
    writable: true,
    value: function(value) {
      if (value !== this.searchQuery) {
        this.searchQuery = value;
      }
      this.prepareDataListForSearchQuery(value);
      this.warn("Search has not been set up for this app." + value);
    }
  },
 
  prepareDataListForSearchQuery: {
    value: function(searchQuery) {
      Iif (searchQuery && this.datalist && this.datalist.id === encodeURI(searchQuery)) {
        this.warn("Search data list was already set up for " + searchQuery);
        return;
      }
      var title = "";
      Iif (this.corpus && this.corpus.dbname) {
        title = this.corpus.title || this.corpus.dbname;
        title = " in " + title;
      }
      this.datalist = {
        id: this.id,
        title: searchQuery,
        description: "This is the result of searching for : " + searchQuery + title + " on " + new Date()
      };
    }
  },
 
  datalist: {
    get: function() {
      this.debug("Getting datalist " + this.searchQuery, this._datalist);
      // if (!this._datalist || !(this._datalist instanceof DataList) || typeof this._datalist.reindexFromApi !== "function") {
      //   this.initializeDatalist();
      // }
      return this._datalist;
    },
    set: function(value) {
      if (value && value.id && this._datalist && this._datalist.id && value.id !== this._datalist.id && this._datalist.length) {
        // add to previous searches
        Eif (!this.previousSearchDataLists) {
          this.previousSearchDataLists = {};
          this.previousSearchDataListsCount = 0;
        }
        this.previousSearchDataLists[this._datalist.id] = this._datalist;
        this.previousSearchDataListsCount++;
      }
 
      this.debug("Setting datalist on search ", this._datalist, value);
      this.ensureSetViaAppropriateType("datalist", value);
 
      // Setting encodeURI search query as the id.
      this._datalist.id = this.id;
    }
  },
 
  clearPreviousSearchDataLists: {
    value: function() {
      this.warn("Releasing memory for" + this.previousSearchDataListsCount + " previousSearchDataLists ");
      delete this.previousSearchDataLists;
      delete this.previousSearchDataListsCount;
    }
  }
 
});
exports.Search = Search;