<commit_before>/*jslint nomen: true */
/*globals define: true */

define([
  'jquery',
  'lib/lodash',
  'backbone',
  'moment',
  'lib/tinypubsub',
  'lib/kissmetrics',

  // LocalData
  'settings',
  'api',

  // Models
  'models/responses',

  // Views
  'views/map',
  'views/responses/filter',
  'views/responses/list',
  'views/surveys/count',

  // Templates
  'text!templates/responses/map-list.html'
],

function($, _, Backbone, moment, events, _kmq, settings, api,
  Responses,
  MapView,
  FilterView,
  ResponseListView,
  ResponseCountView,
  mapListTemplate) {

  'use strict';

  var ResponseViews = {};

  ResponseViews.MapAndListView = Backbone.View.extend({
    filters: {},
    firstRun: true,
    mapView: null,
    listView: null,
    responses: null,
    survey: null,

    template: _.template(mapListTemplate),

    el: '#response-view-container',

    events: {
      'click .refresh': 'getNew'
    },

    initialize: function(options) {
      _.bindAll(this,
        'render',
        'update',

        // Filtering
        'showFilters',
        'hideFilters',

        // Updating
        'getNew',
        'lastUpdated',

        'mapClickHandler'
      );

      this.responses = options.responses;

      this.forms = options.forms;
      this.forms.on('reset', this.updateFilterChoices, this);

      this.survey = options.survey;

      this.render();
    },

    render: function() {
      console.log("Rendering response view");
      if (this.firstRun) {
        this.firstRun = false;
      } else {
        // If this is not the first time, then we need to remove the current
        // map and list views, since we'll create new ones.
        this.mapView.remove();
        this.mapView = null;
        this.listView.remove();
        this.listView = null;
      }

      // Actually render the page
      var context = {
        survey: this.survey.toJSON()
      };
      this.$el.html(this.template(context));

      // Set up the map view, now that the root exists.
      if (this.mapView === null) {
        this.mapView = new MapView({
          el: $('#map-view-container'),
          survey: this.survey,
          clickHandler: this.mapClickHandler
        });
      }

      // Render the map
      this.mapView.render();

      this.filterView = new FilterView({
        collection: this.responses,
        survey: this.survey,
        forms: this.forms,
        map: this.mapView
      });
      $("#filter-view-container").html(this.filterView.$el);

      // Set up the response count view.
      this.countView = new ResponseCountView({
        model: this.survey
      }).render();
      $("#response-count-container").html(this.countView.$el);

      // Listen for new responses
      this.survey.on('change', this.mapView.update);
    },

    mapClickHandler: function(event) {
      if (_.isUndefined(event.data) || _.isUndefined(event.data.object_id)) {
        return;
      }

      var rc = new Responses.Collection({
        surveyId: this.survey.get('id'),
        objectId: event.data.object_id
      });

      var selectedItemListView = new ResponseListView({
        el: '#responses-list-container',
        collection: rc,
        labels: this.forms.getQuestions()
      });

      selectedItemListView.on('delete', function() {
        this.mapView.deselectObject();
      }.bind(this));
    },

    update: function() {
      if (this.firstRun) {
        this.render();
      }<commit_msg>Fix a check for undefined instead of null<commit_after>/*jslint nomen: true */
/*globals define: true */

define([
  'jquery',
  'lib/lodash',
  'backbone',
  'moment',
  'lib/tinypubsub',
  'lib/kissmetrics',

  // LocalData
  'settings',
  'api',

  // Models
  'models/responses',

  // Views
  'views/map',
  'views/responses/filter',
  'views/responses/list',
  'views/surveys/count',

  // Templates
  'text!templates/responses/map-list.html'
],

function($, _, Backbone, moment, events, _kmq, settings, api,
  Responses,
  MapView,
  FilterView,
  ResponseListView,
  ResponseCountView,
  mapListTemplate) {

  'use strict';

  var ResponseViews = {};

  ResponseViews.MapAndListView = Backbone.View.extend({
    filters: {},
    firstRun: true,
    mapView: null,
    listView: null,
    responses: null,
    survey: null,

    template: _.template(mapListTemplate),

    el: '#response-view-container',

    events: {
      'click .refresh': 'getNew'
    },

    initialize: function(options) {
      _.bindAll(this,
        'render',
        'update',

        // Filtering
        'showFilters',
        'hideFilters',

        // Updating
        'getNew',
        'lastUpdated',

        'mapClickHandler'
      );

      this.responses = options.responses;

      this.forms = options.forms;
      this.forms.on('reset', this.updateFilterChoices, this);

      this.survey = options.survey;

      this.render();
    },

    render: function() {
      console.log("Rendering response view");
      if (this.firstRun) {
        this.firstRun = false;
      } else {
        // If this is not the first time, then we need to remove the current
        // map and list views, since we'll create new ones.
        this.mapView.remove();
        this.mapView = null;
        this.listView.remove();
        this.listView = null;
      }

      // Actually render the page
      var context = {
        survey: this.survey.toJSON()
      };
      this.$el.html(this.template(context));

      // Set up the map view, now that the root exists.
      if (this.mapView === null) {
        this.mapView = new MapView({
          el: $('#map-view-container'),
          survey: this.survey,
          clickHandler: this.mapClickHandler
        });
      }

      // Render the map
      this.mapView.render();

      this.filterView = new FilterView({
        collection: this.responses,
        survey: this.survey,
        forms: this.forms,
        map: this.mapView
      });
      $("#filter-view-container").html(this.filterView.$el);

      // Set up the response count view.
      this.countView = new ResponseCountView({
        model: this.survey
      }).render();
      $("#response-count-container").html(this.countView.$el);

      // Listen for new responses
      this.survey.on('change', this.mapView.update);
    },

    mapClickHandler: function(event) {
      if (!event.data || !event.data.object_id) {
        return;
      }

      var rc = new Responses.Collection({
        surveyId: this.survey.get('id'),
        objectId: event.data.object_id
      });

      var selectedItemListView = new ResponseListView({
        el: '#responses-list-container',
        collection: rc,
        labels: this.forms.getQuestions()
      });

      selectedItemListView.on('delete', function() {
        this.mapView.deselectObject();
      }.bind(this));
    },

    update: function() {
      if (this.firstRun) {
        this.render();
      }
{'fraction_non_alphanumeric': 0.1389370306181398, 'fraction_numerical': 0.0, 'mean_word_length': 2.153460837887067, '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': '1991499', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2106, 'n_words': 453}