<commit_before>var $ = require('jquery');
var cdb = require('cartodb.js');
cdb.admin = require('cdb.admin');
var LocalStorage = require('../common/local_storage');
var HeaderView = require('../common/views/dashboard_header_view');
var SupportView = require('../common/support_view');
var MamufasImportView = require('../common/mamufas_import/mamufas_import_view');
var BackgroundPollingView = require('../common/background_polling/background_polling_view');
var DashboardBackgroundPollingModel = require('./background_polling_model');
var ContentControllerView = require('./content_controller_view');
var HeaderViewModel = require('./header_view_model');

module.exports = cdb.core.View.extend({

  events: {
    'click': '_onClick'
  },

  initialize: function() {
    this._initModels();
    this._initViews();
    this._initBindings();
  },

  _initBindings: function() {
    this.router.model.bind('change', this._onRouterChange, this);
    this.add_related_model(this.router.model);
  },

  _initModels: function() {
    this.user = this.options.user;
    this.router = this.options.router;
    this.localStorage = new LocalStorage();

    // Update order and category attribute to router model
    this.router.model.set('order',    this.localStorage.get('dashboard.order'), { silent: true });
    this.router.model.set('category', this.localStorage.get('dashboard.category'), { silent: true });
  },

  _onRouterChange: function(m, changes) {
    this._fetchCollection(m, changes);

    // Only create a visualization from an import if user is in maps section
    this._backgroundPollingView.createVis = this.router.model.isMaps();
  },

  _fetchCollection: function(m, changes) {
    var params = this.router.model.attributes;

    // Get order from localStorage if it is not defined or
    // come from other type (tables or visualizations)
    var order = this.localStorage.get("dashboard.order") || 'updated_at';
    // Maps doesn't have size order, so if that order is set
    // in maps section we will show with 'updated_at' order
    if (params.content_type === "maps" && order === "size") {
      order = 'updated_at'
    }

    var types = params.content_type === "datasets" ? 'table' : 'derived';

    // Requesting data library items?
    if (params.library) {
      types = 'remote';
    }

    // Supporting search in data library and user datasets at the same time
    if ((params.q || params.tag) && params.content_type === "datasets") {
      types = 'table,remote';
    }

    // TODO: review, should collection params really be set here?
    this.collection.options.set({
      q:              params.q,
      page:           params.page || 1,
      tags:           params.tag,
      per_page:       this.collection[ "_" + ( params.content_type === "datasets" ? 'TABLES' : 'ITEMS') + '_PER_PAGE'],
      shared:         params.shared,
      locked:         params.liked ? '' : params.locked, // If not locked liked items are not rendered
      only_liked:     params.liked,
      order:          order,
      types:          types,
      type:           ''
    });

    this.collection.fetch();
  },

  _initViews: function() {
    var backgroundPollingModel = new DashboardBackgroundPollingModel({
      showGeocodingDatasetURLButton: true,
      geocodingsPolling: true,
      importsPolling: true
    }, {
      user: this.user
    });

    this._backgroundPollingView = new BackgroundPollingView({
      model: backgroundPollingModel,
      // Only create a visualization from an import if user is in maps section
      createVis: this.router.model.isMaps(),
      user: this.user
    });
    backgroundPollingModel.bind('importCompleted', function() {
      this.collection.fetch();
      this.user.fetch();
    }, this);
    this.$el.append(this._backgroundPollingView.render().el);

    this.addView(this._backgroundPollingView);

    var mamufasView = new MamufasImportView({
      el: this.$el,
      user: this.user
    });

    cdb.god.bind('dialogOpened', function() {
      mamufasView.disable();
    }, this);
    cdb.god.bind('dialogClosed', function() {
      mamufasView.enable();
    }, this);

    mamufasView.render();
    mamufasView.enable();

    var headerView = new HeaderView({
      el:             this.$('#header'), //pre-rendered in DOM by Rails app
      model:          this.user,
      viewModel:      new HeaderViewModel(this.router),
      router:         this.router,
      collection:     this.collection,
      localStorage:   this.localStorage
    });
    headerView.render();

    this.controllerView = new ContentControllerView({
      el:           this.$('#content-controller'),
      // Pass the whole element for only calculating
      // the height is not "fair"
      headerHeight: this.$('#header').height(),
      user:         this.user,
      router:       this.router,
      collection:   this.collection,
      localStorage: this.localStorage
    });

    this.controllerView.render();

    var supportView = new SupportView({
      el: this.$('#support-banner'),
      user: this.user
    });

    supportView.render();
  },

  _onClick: function(e) {
    // Clicks outside of any dialog "body" will fire a closeDialogs event
    if (this._closeTo(e, '.Dialog')) {
      cdb.god.trigger('closeDialogs');

      // If click outside the filters view should also unselect any selected items
      if (this._closeTo(e, '.js-skip-unselect-all')) {
        cdb.god.trigger('unselectAllItems');
      }
    }
  },

  _closeTo: function(ev, selector) {
    return $(ev.target).closest(selector).length === 0;
  }

});<commit_msg>Change to a more representative method name<commit_after>var $ = require('jquery');
var cdb = require('cartodb.js');
cdb.admin = require('cdb.admin');
var LocalStorage = require('../common/local_storage');
var HeaderView = require('../common/views/dashboard_header_view');
var SupportView = require('../common/support_view');
var MamufasImportView = require('../common/mamufas_import/mamufas_import_view');
var BackgroundPollingView = require('../common/background_polling/background_polling_view');
var DashboardBackgroundPollingModel = require('./background_polling_model');
var ContentControllerView = require('./content_controller_view');
var HeaderViewModel = require('./header_view_model');

module.exports = cdb.core.View.extend({

  events: {
    'click': '_onClick'
  },

  initialize: function() {
    this._initModels();
    this._initViews();
    this._initBindings();
  },

  _initBindings: function() {
    this.router.model.bind('change', this._onRouterChange, this);
    this.add_related_model(this.router.model);
  },

  _initModels: function() {
    this.user = this.options.user;
    this.router = this.options.router;
    this.localStorage = new LocalStorage();

    // Update order and category attribute to router model
    this.router.model.set('order',    this.localStorage.get('dashboard.order'), { silent: true });
    this.router.model.set('category', this.localStorage.get('dashboard.category'), { silent: true });
  },

  _onRouterChange: function(m, changes) {
    this._fetchCollection(m, changes);

    // Only create a visualization from an import if user is in maps section
    this._backgroundPollingView.createVis = this.router.model.isMaps();
  },

  _fetchCollection: function(m, changes) {
    var params = this.router.model.attributes;

    // Get order from localStorage if it is not defined or
    // come from other type (tables or visualizations)
    var order = this.localStorage.get("dashboard.order") || 'updated_at';
    // Maps doesn't have size order, so if that order is set
    // in maps section we will show with 'updated_at' order
    if (params.content_type === "maps" && order === "size") {
      order = 'updated_at'
    }

    var types = params.content_type === "datasets" ? 'table' : 'derived';

    // Requesting data library items?
    if (params.library) {
      types = 'remote';
    }

    // Supporting search in data library and user datasets at the same time
    if ((params.q || params.tag) && params.content_type === "datasets") {
      types = 'table,remote';
    }

    // TODO: review, should collection params really be set here?
    this.collection.options.set({
      q:              params.q,
      page:           params.page || 1,
      tags:           params.tag,
      per_page:       this.collection[ "_" + ( params.content_type === "datasets" ? 'TABLES' : 'ITEMS') + '_PER_PAGE'],
      shared:         params.shared,
      locked:         params.liked ? '' : params.locked, // If not locked liked items are not rendered
      only_liked:     params.liked,
      order:          order,
      types:          types,
      type:           ''
    });

    this.collection.fetch();
  },

  _initViews: function() {
    var backgroundPollingModel = new DashboardBackgroundPollingModel({
      showGeocodingDatasetURLButton: true,
      geocodingsPolling: true,
      importsPolling: true
    }, {
      user: this.user
    });

    this._backgroundPollingView = new BackgroundPollingView({
      model: backgroundPollingModel,
      // Only create a visualization from an import if user is in maps section
      createVis: this.router.model.isMaps(),
      user: this.user
    });
    backgroundPollingModel.bind('importCompleted', function() {
      this.collection.fetch();
      this.user.fetch();
    }, this);
    this.$el.append(this._backgroundPollingView.render().el);

    this.addView(this._backgroundPollingView);

    var mamufasView = new MamufasImportView({
      el: this.$el,
      user: this.user
    });

    cdb.god.bind('dialogOpened', function() {
      mamufasView.disable();
    }, this);
    cdb.god.bind('dialogClosed', function() {
      mamufasView.enable();
    }, this);

    mamufasView.render();
    mamufasView.enable();

    var headerView = new HeaderView({
      el:             this.$('#header'), //pre-rendered in DOM by Rails app
      model:          this.user,
      viewModel:      new HeaderViewModel(this.router),
      router:         this.router,
      collection:     this.collection,
      localStorage:   this.localStorage
    });
    headerView.render();

    this.controllerView = new ContentControllerView({
      el:           this.$('#content-controller'),
      // Pass the whole element for only calculating
      // the height is not "fair"
      headerHeight: this.$('#header').height(),
      user:         this.user,
      router:       this.router,
      collection:   this.collection,
      localStorage: this.localStorage
    });

    this.controllerView.render();

    var supportView = new SupportView({
      el: this.$('#support-banner'),
      user: this.user
    });

    supportView.render();
  },

  _onClick: function(e) {
    // Clicks outside of any dialog "body" will fire a closeDialogs event
    if (this._isEventFromWithin(e, '.Dialog')) {
      cdb.god.trigger('closeDialogs');

      // If click outside the filters view should also unselect any selected items
      if (this._isEventFromWithin(e, '.js-skip-unselect-all')) {
        cdb.god.trigger('unselectAllItems');
      }
    }
  },

  _isEventFromWithin: function(ev, selector) {
    return $(ev.target).closest(selector).length === 0;
  }

});
{'fraction_non_alphanumeric': 0.1330373001776199, 'fraction_numerical': 0.00035523978685612787, 'mean_word_length': 2.8446568794810516, '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': '3950738', 'n_tokens_mistral': 3340, 'n_tokens_neox': 3226, 'n_words': 776}