<commit_before>/*global Bloodhound */

define(['ext/backbone', 'ext/jquery', 'ext/underscore', 'util',
    'ext/typeahead'],
function(RmcBackbone, $, _, _util, _typeahead) {

  var onOpenedOuter = function(self) {
    return function($e) {
      $('.tt-dropdown-menu').css('width',
          self.baseWidth + self.extraWidth);
    };
  };

  var onSelected = function(event, datum, name) {
    if (datum.type === 'course') {
      window.location.href = '/courses/' + datum.label;
    } else if (datum.type === 'friend') {
      window.location.href = '/profile/' + datum.id;
    }
  };

  var itemName = function(item) {
    if (item.type === 'course') {
      return item.label + ' - ' + item.name;
    } else if (item.type === 'friend') {
      return item.label;
    }
  };

  var customSuggestionTemplate = function(item) {
    var formatter;
    if (item.type === 'course') {
      formatter = _.template($('#course-result-item-tpl').html());
      item.department_id = item.department_id.toUpperCase();
    } else if (item.type === 'friend') {
      formatter = _.template($('#friend-result-item-tpl').html());
    }
    return formatter(item);
  };

  var initBloodhoundWithAutocomplete = function(self) {
    var engine = new Bloodhound({
      name: 'friendsAndCourses',
      local: [].concat(_util.getLocalData('friends'),
          _util.getLocalData('courses')),
      datumTokenizer: function(d) {
        return Bloodhound.tokenizers.whitespace(d.tokens.join(' '));
      },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      limit: 20
    });
    engine.initialize();
    setUpAutocomplete('.search-bar', engine, self);
  };

  var setUpAutocomplete = function(searchBarElement, engine, self) {
    $(searchBarElement).typeahead(
      {
        hint: true,
        autoselect: true,
        highlight: true,
        minLength: 2,
        limit: 20
      },
      {
        name: 'friendsAndCourses',
        displayKey: itemName,
        source: engine.ttAdapter(),
        templates: {
          empty: _.template($('#empty-results-message-tpl').html()),
          suggestion: customSuggestionTemplate
        }
      }
    )
    .on('typeahead:opened', onOpenedOuter(self))
    .on('typeahead:selected', onSelected);
  };

  var SearchBarView = RmcBackbone.View.extend({
    initialize: function() {
      this.moving = false;
      this.duration = 300;
      this.baseWidth = 200;
      this.extraWidth = $('.navbar-minus-searchbar').width();
    },

    render: function() {
      var template = _.template($('#search-bar-tpl').html());
      this.$el.html(template);
    },

    events: {
      'focus .search-bar': 'onFocus',
      'blur .search-bar': 'onBlur'
    },

    onFocus: function(event){
      var self = this;
      if (self.moving) {
        return;
      } else {
        self.moving = true;
      }
      $(".nav.pull-right").hide(self.duration);
      self.$('.search-div').animate(
        {
          width: (self.baseWidth + self.extraWidth)
        },
        self.duration,
        function() {
          self.$(".search-div").width(self.extraWidth + self.baseWidth);
          self.moving = false;
        }
      );
    },

    onBlur: function(event){
      var self = this;
      if (self.moving) {
        return;
      } else {
        self.moving = true;
      }
      $(".nav.pull-right").show(self.duration);
      self.$('.search-div').animate(
        {
          width: self.baseWidth
        },
        self.duration,
        function() {
          self.moving = false;
        }
      );<commit_msg>Final fixes from Jamie's suggestions, mostly styling<commit_after>/*global Bloodhound */

define(['ext/backbone', 'ext/jquery', 'ext/underscore', 'util',
    'ext/typeahead'],
function(RmcBackbone, $, _, _util, _typeahead) {

  var onOpenedOuter = function(context) {
    return function($e) {
      $('.tt-dropdown-menu').css('width',
          context.baseWidth + context.extraWidth);
    };
  };

  var onSelected = function(event, datum, name) {
    if (datum.type === 'course') {
      window.location.href = '/courses/' + datum.label;
    } else if (datum.type === 'friend') {
      window.location.href = '/profile/' + datum.id;
    }
  };

  var itemName = function(item) {
    if (item.type === 'course') {
      return item.label + ' - ' + item.name;
    } else if (item.type === 'friend') {
      return item.label;
    }
  };

  var customSuggestionTemplate = function(item) {
    var formatter;
    if (item.type === 'course') {
      formatter = _.template($('#course-result-item-tpl').html());
      item.department_id = item.department_id.toUpperCase();
    } else if (item.type === 'friend') {
      formatter = _.template($('#friend-result-item-tpl').html());
    }
    return formatter(item);
  };

  var initBloodhoundWithAutocomplete = function(context) {
    var engine = new Bloodhound({
      name: 'friendsAndCourses',
      local: [].concat(_util.getLocalData('friends'),
          _util.getLocalData('courses')),
      datumTokenizer: function(d) {
        return Bloodhound.tokenizers.whitespace(d.tokens.join(' '));
      },
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      limit: 20
    });
    engine.initialize();
    setUpAutocomplete('.search-bar', engine, context);
  };

  var setUpAutocomplete = function(searchBarElement, engine, context) {
    $(searchBarElement).typeahead(
      {
        hint: true,
        autoselect: true,
        highlight: true,
        minLength: 2,
        limit: 20
      },
      {
        name: 'friendsAndCourses',
        displayKey: itemName,
        source: engine.ttAdapter(),
        templates: {
          empty: _.template($('#empty-results-message-tpl').html()),
          suggestion: customSuggestionTemplate
        }
      }
    )
    .on('typeahead:opened', onOpenedOuter(context))
    .on('typeahead:selected', onSelected);
  };

  var SearchBarView = RmcBackbone.View.extend({
    initialize: function() {
      this.moving = false;
      this.duration = 300;
      this.baseWidth = 200;
      this.extraWidth = $('.navbar-minus-searchbar').width();
    },

    render: function() {
      var template = _.template($('#search-bar-tpl').html());
      this.$el.html(template);
    },

    events: {
      'focus .search-bar': 'onFocus',
      'blur .search-bar': 'onBlur'
    },

    onFocus: function(event) {
      var self = this;
      if (self.moving) {
        return;
      }
      self.moving = true;
      $(".nav.pull-right").hide(self.duration);
      self.$('.search-div').animate(
        {
          width: (self.baseWidth + self.extraWidth)
        },
        self.duration,
        function() {
          self.$(".search-div").width(self.extraWidth + self.baseWidth);
          self.moving = false;
        }
      );
    },

    onBlur: function(event) {
      var self = this;
      if (self.moving) {
        return;
      }
      self.moving = true;
      $(".nav.pull-right").show(self.duration);
      self.$('.search-div').animate(
        {
          width: self.baseWidth
        },
        self.duration,
        function() {
          self.moving = false;
        }
      );
{'fraction_non_alphanumeric': 0.16215461903432876, 'fraction_numerical': 0.0030700530281886685, 'mean_word_length': 2.2444545043005886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7564483', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2206, 'n_words': 390}