<commit_before>/* global define */

define([
  'controllers/base-controller',
  'controllers/archive-controller',
  'views/search-view',
  'collections/post-collection',
  'helpers/post-filter',
  'buses/command-bus',
  'buses/navigator'
], function (BaseController, ArchiveController, SearchView, Posts, PostFilter, CommandBus, Navigator) {
  'use strict';

  var SearchController = BaseController.extend({
    busEvents: {
      'search:view:search:term':   'searchTerm',
      'search:view:search:submit': 'navigateSearchUrl',
      'search:view:search:empty':  'teardownSearch'
    },

    initialize: function (options) {
      options     = options || {};
      this.filter = options.filter || new PostFilter();
      this.posts  = options.posts || new Posts(null, { filter: this.filter });
      this._handleCommands();
    },

    showSearch: function (options) {
      this.show(new SearchView(), { region: options.region });
    },

    onBeforeDestroy: function () {
      this.archive.mainView.destroy();
    },

    searchTerm: function (options) {
      var page = options.paged || 1;

      this.filter.bySearchingFor(options.search).onPage(page);
      if (this.posts.length > 0) {
        this._archiveController().triggerMethod('search:term');
      } else {
        this._archiveController().showArchive();
      }
    },

    navigateSearchUrl: function (options) {
      var page = null, trigger = false;<commit_msg>Avoid destroying an unset view<commit_after>/* global define */

define([
  'controllers/base-controller',
  'controllers/archive-controller',
  'views/search-view',
  'collections/post-collection',
  'helpers/post-filter',
  'buses/command-bus',
  'buses/navigator'
], function (BaseController, ArchiveController, SearchView, Posts, PostFilter, CommandBus, Navigator) {
  'use strict';

  var SearchController = BaseController.extend({
    busEvents: {
      'search:view:search:term':   'searchTerm',
      'search:view:search:submit': 'navigateSearchUrl',
      'search:view:search:empty':  'teardownSearch'
    },

    initialize: function (options) {
      options     = options || {};
      this.filter = options.filter || new PostFilter();
      this.posts  = options.posts || new Posts(null, { filter: this.filter });
      this._handleCommands();
    },

    showSearch: function (options) {
      this.show(new SearchView(), { region: options.region });
    },

    onBeforeDestroy: function () {
      if (this.archive) {
        this.archive.mainView.destroy();
      }
    },

    searchTerm: function (options) {
      var page = options.paged || 1;

      this.filter.bySearchingFor(options.search).onPage(page);
      if (this.posts.length > 0) {
        this._archiveController().triggerMethod('search:term');
      } else {
        this._archiveController().showArchive();
      }
    },

    navigateSearchUrl: function (options) {
      var page = null, trigger = false;
{'fraction_non_alphanumeric': 0.14519986334130508, 'fraction_numerical': 0.0013665869490946361, 'mean_word_length': 3.206896551724138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2746908', 'n_tokens_mistral': 872, 'n_tokens_neox': 851, 'n_words': 163}