emergencyindex / emergencyindex.com

AN ANNUAL DOCUMENT OF PERFORMANCE PRACTICE
http://emergencyindex.com
GNU General Public License v3.0
2 stars 2 forks source link

cleaned up and streamlined a bit #31

Closed whatcoloris closed 5 years ago

whatcoloris commented 5 years ago

cleaned it - still sure there's a more graceful method....

edwardsharp commented 5 years ago

so, this seems to work much better:

var initAutocomplete = function(){
  var dataObj = {};
  var hrefObj = {};

  for(var proj of data){
    if(proj.title != ''){
      var _key = proj.title;
      if(proj.contributor && proj.contributor !== ''){
        _key = _key + ' -- ' + proj.contributor;
      }
      if(proj.place && proj.place !== ''){
        _key = _key + ' -- ' + proj.place;
      }
      dataObj[_key.trim()] = null;
      hrefObj[_key.trim()] = proj.url;
    }
  }

  M.Autocomplete.init(document.querySelectorAll('input.autocomplete'), {
    data: dataObj,
    limit: 50,
    onAutocomplete: function(val) {
      var projKey = Object.keys(hrefObj).filter(function(i){return i === val})[0]
      if(projKey && hrefObj[projKey]){
        window.location = hrefObj[projKey];
      }
    },
    minLength: 1
  });
}
edwardsharp commented 5 years ago

@whatcoloris i just pushed a fix to master