Source: site/search.js

// Generated by CoffeeScript 1.12.7

/**
 * Search functionality
 * @mixin site/search
 */
'use strict';
app.controller('site/search', [
  '$scope', '$location', 'constantService', 'displayService', 'modelService', 'searchService', 'parties', 'volumes', function($scope, $location, constants, display, models, Search, parties, volumes) {
    var Default, Number, Quoted, Range, ageLogOffset, ageSliderValue, any, f, fields, finite, handlers, mi, n, offset, params, process, rangeRegex, ref, spellcheck, startTag, type, v;
    display.title = 'Search';
    spellcheck = {};
    process = function(r) {
      var i, j, list, ref, sug;
      list = r.response.docs;
      list.count = r.response.numFound;
      if ((sug = r.spellcheck) && (sug = sug.suggestions)) {
        for (i = j = 1, ref = sug.length; j <= ref; i = j += 2) {
          spellcheck[sug[i - 1]] = sug[i].suggestion;
          $scope.spellcheck = spellcheck;
        }
      }
      return list;
    };
    if (parties) {
      $scope.parties = process(parties);
      if (!volumes) {
        type = Search.Party;
        $scope.count = $scope.parties.count;
      }
    }
    if (volumes) {
      $scope.volumes = process(volumes);
      if (!parties) {
        type = Search.Volume;
        $scope.count = $scope.volumes.count;
      }
    }
    params = $location.search();
    $scope.query = params.q || '';
    if (type) {
      offset = parseInt(params.offset, 10) || 0;
      $scope.pageCurrent = 1 + (offset / type.limit);
      $scope.pageCount = Math.ceil($scope.count / type.limit);
    }
    finite = function(x) {
      return (x != null) && isFinite(x);
    };
    Default = {
      parse: function(x) {
        return x;
      },
      print: function(x) {
        if (x) {
          return x;
        }
      }
    };
    Number = {
      parse: function(x) {
        if (x !== '*') {
          return parseFloat(x);
        }
      },
      print: function(x) {
        if (finite(x)) {
          return x;
        } else {
          return '*';
        }
      }
    };
    rangeRegex = /^\[([-+0-9.e]+|\*) TO ([-+0-9.e]+|\*)\]$/i;
    Range = {
      set: function(x) {
        return x && (finite(x[0]) || finite(x[1]));
      },
      parse: function(x) {
        var ref, ref1;
        x = rangeRegex.exec(x) || [];
        return [(ref = Number.parse(x[1])) != null ? ref : -2e308, (ref1 = Number.parse(x[2])) != null ? ref1 : 2e308];
      },
      print: function(x) {
        if (x) {
          if (!(x[0] > this.range[0])) {
            x[0] = -2e308;
          }
          if (!(x[1] < this.range[1])) {
            x[1] = 2e308;
          }
        }
        if (Range.set(x)) {
          return '[' + Number.print(x[0]) + ' TO ' + Number.print(x[1]) + ']';
        }
      }
    };
    Quoted = {
      parse: function(x) {
        return x.substring(x.startsWith('"'), x.length - x.endsWith('"'));
      },
      print: function(x) {
        if (x) {
          return '"' + x + '"';
        }
      }
    };
    $scope.years = [1925, (new Date()).getFullYear()];
    handlers = {
      record_age: {
        parse: Range.parse,
        print: Range.print,
        range: [0, constants.age.limit]
      },
      container_date: {
        parse: function(x) {
          x = Range.parse(x);
          if (Range.set(x)) {
            fields.container_top = 'false';
          }
          return x;
        },
        print: function(x) {
          if (fields.container_top) {
            return Range.print.call(this, x);
          }
        },
        range: $scope.years
      },
      numeric: Range,
      tag_name: Quoted
    };
    $scope.fields = fields = {};
    for (f in handlers) {
      v = handlers[f];
      if (v.range) {
        fields[f] = [v.range[0], v.range[1]];
      }
    }
    for (f in params) {
      v = params[f];
      if (f.startsWith('f.')) {
        n = f.substr(2);
        fields[n] = ((ref = handlers[n]) != null ? ref : Default).parse(v);
      } else if (f.startsWith('m.')) {
        mi = f.substr(2);
      }
    }
    any = function(o) {
      for (f in o) {
        v = o[f];
        if (v != null) {
          return true;
        }
      }
      return false;
    };
    $scope.search = function(offset) {
      var q, ref1;
      q = {
        q: $scope.query || void 0,
        offset: offset,
        volume: type != null ? type.volume : void 0
      };
      for (f in fields) {
        v = fields[f];
        q['f.' + f] = ((ref1 = handlers[f]) != null ? ref1 : Default).print(v);
      }
      if (q['f.container_date']) {
        delete q['f.container_top'];
      }
      $location.search(q);
    };
    $scope.searchParties = function(auth, inst) {
      fields = {};
      fields.party_authorization = auth;
      fields.party_is_institution = inst;
      type = Search.Party;
      return $scope.search(0);
    };
    $scope.searchVolumes = function() {
      type = Search.Volume;
      return $scope.search(0);
    };
    $scope.searchSpellcheck = function(w, s) {
      $scope.query = $scope.query.replace(w, s);
      return $scope.search();
    };
    $scope.searchPage = function(n) {
      return $scope.search(type.limit * (n - 1));
    };
    startTag = fields.tag_name;
    $scope.tagSearch = function(input) {
      if (input === startTag) {
        return input;
      }
      return models.Tag.search(input).then(function(data) {
        var j, len, results, tag;
        results = [];
        for (j = 0, len = data.length; j < len; j++) {
          tag = data[j];
          results.push({
            text: tag,
            select: function() {
              fields.tag_name = this.text;
              $scope.searchVolumes();
              return this.text;
            }
          });
        }
        return results;
      });
    };
    $scope.formats = _.filter(constants.format, function(f) {
      return !f.transcodable || f.id < 0;
    });
    ageSliderValue = [0, 0];
    ageLogOffset = 16;
    $scope.ageSlider = function(value) {
      var x, y;
      if (value != null) {
        fields.record_age[0] = Math.round(Math.exp(value[0]) - ageLogOffset);
        return fields.record_age[1] = Math.round(Math.exp(value[1]) - ageLogOffset);
      } else {
        x = Math.log(ageLogOffset + Math.max(0, fields.record_age[0]));
        y = Math.log(ageLogOffset + Math.min(constants.age.limit, fields.record_age[1]));
        if (x !== ageSliderValue[0] || y !== ageSliderValue[1]) {
          ageSliderValue = [x, y];
        }
        return ageSliderValue;
      }
    };
    $scope.ageRange = [Math.log(ageLogOffset), Math.log(ageLogOffset + constants.age.limit)];
    $scope.clearAge = function() {
      return fields.record_age = [-2e308, 2e308];
    };
    $scope.expandVolume = function(v) {
      var ref1;
      if (v && ((ref1 = $scope.expanded) != null ? ref1.volume : void 0) === v) {
        $scope.expanded = void 0;
        return;
      }
      $scope.expanded = {
        volume: v
      };
      if (!v) {
        return;
      }
      v.get(['excerpts']);
    };
    if ($scope.fields.content_type === 'excerpt' && !$scope.expanded) {
      $scope.expandVolume($scope.volumes[0]);
    }
  }
]);

//# sourceMappingURL=search.js.map