<commit_before>var cdb = require('cartodb.js');
var Utils = require('cdb.Utils');
var BaseDialog = require('../../views/base_dialog/view');
var ViewFactory = require('../../view_factory');
var randomQuote = require('../../view_helpers/random_quote');

module.exports = BaseDialog.extend({

  _CARD_WIDTH: 288,
  _CARD_HEIGHT: 170,
  _TABS_PER_ROW: 3,
  _MAX_ROWS: 9000,
  _MAX_COLS: 60,
  _REJECT: 0,
  _KEEP: 1,
  _GET_BBOX_FROM_THE_GEOM: false,
  _EXCLUDED_COLUMNS: ['cartodb_id', 'lat', 'lon', 'lng', 'long', 'latitude', 'longitude', 'shape_length', 'shape_area', 'objectid', 'id', 'country', 'state', 'created_at', 'updated_at', 'iso2', 'iso3', 'x_coord', 'y_coord', 'xcoord', 'ycoord'],

  events: cdb.core.View.extendEvents({
    "click .js-goPrev": "_prevPage",
    "click .js-goNext": "_nextPage",
    "click .js-skip"  : "_onSkipClick"
  }),

  initialize: function() {
    this.elder('initialize');

    if (!this.options.map) {
      throw new Error('map is required');
    }

    if (!this.options.table) {
      throw new Error('table is required');
    }

    this._initModels();
    this._initViews();
    this._initBinds();
  },

  _check: function() {
    var tableData     = this.options.table.data();
    var geometryTypes = tableData.table && tableData.table.get("stats_geometry_types");
    var hasGeometries = geometryTypes && geometryTypes.length > 0 ? true : false;

    var row_count = tableData.length;
    var hasRows = row_count > 0 && row_count < this._MAX_ROWS;

    var col_count = _(tableData.query_schema).size();
    var hasColumns = col_count > 0 && col_count < this._MAX_COLS;

    if (hasGeometries && hasRows && hasColumns) {
      return true;
    } else {
      return false;
    }
  },

  render_content: function() {
    return this._panes.getActivePane().render().el;
  },

  render: function() {
    BaseDialog.prototype.render.apply(this, arguments);
    return this;
  },

  _initModels: function() {
    this.columns = new Backbone.Collection();
    this.model = new cdb.core.Model({ page: 1, maxPages: 0 });
  },

  _initViews: function() {

    _.bindAll(this, "_addCard", "_generateThumbnail", "_analyzeColumn", "_analyzeColumns", "_refreshMapList", "_onLoadWizard", "_analyzeStats");

    this.table = this.options.table;
    this.user  = this.options.user;
    this.map   = this.options.map;
    //this.bbox  = this.options.map.getViewBounds();
    this.query_schema = this.table.data().query_schema;

    this._panes = new cdb.ui.common.TabPane({
      el: this.el
    });

    this.addView(this._panes);

    this._panes.addTab('vis',
      ViewFactory.createByTemplate('common/dialogs/pecan/template', {
      })
    );

    this._panes.addTab('loading',
      ViewFactory.createByTemplate('common/templates/loading', {
        title: 'Analyzing your data…',
        quote: randomQuote()
      })
    );

    this._panes.addTab('applying',
      ViewFactory.createByTemplate('common/templates/loading', {
        title: 'Applying style…',
        quote: randomQuote()
      })
    );

    if (this._check()) {
      this._panes.active('loading');
      this._start();
    } else {
      this.close();
    }
  },

  _onSkipClick: function(e) {
    this.killEvent(e);
    this.model.set("disabled", true);
    this.close(); // TODO: trigger a skip after the manual testing is finished
  },

  _nextPage: function() {
    var page = this.model.get('page');
    var maxPages = this.model.get('maxPages');

    if (page < maxPages) {
      this.model.set('page', page + 1);
    }
  },

  _prevPage: function() {
    var page = this.model.get('page');
    if (page > 1) {
      this.model.set('page', page - 1);
    }
  },

  _moveTabsNavigation: function() {
    var page = this.model.get('page');
    var rowWidth = 990;

    var p = rowWidth * (page - 1);
    this.$('.js-map-list').css('margin-left', '-' + p + 'px');
    this._refreshNavigation();
  },

  _refreshNavigation: function() {
    var page = this.model.get('page');
    var maxPages = this.model.get('maxPages');

    this.$('.js-goPrev')[ page > 1 ? 'removeClass' : 'addClass' ]('is-disabled');
    this.$('.js-goNext')[ page < maxPages ? 'removeClass' : 'addClass' ]('is-disabled');
  },

  _hideNavigation: function() {
    this.$('.js-navigation').addClass("is-hidden");
  },

  _initBinds: function() {
    this.model.bind('change:page', this._moveTabsNavigation, this);
    this.columns.bind('change:analyzed, change:success', this._onAnalyzeColumn, this);
    this._panes.bind('tabEnabled', this.render, this);
  },

  _onAnalyzeColumn: function(column) {
    var self = this;
    var coltype = column.get("type");
    if (column.get("success")){
      if(coltype !== 'date'){
        this._guessColumn(column);
      }
      else{
        column.set("pending", true);
      }
    }

    var analyzedColumnsCount    = this._getAnalyzedColumns().length;
    var successfullColumnsCount = this._getSuccessColumns().length;

    if (analyzedColumnsCount === this.columns.length) {

      if (successfullColumnsCount === 0) {
        this.close();
        return;
      }
      else {
        this._getPending().forEach(function(c){
          if(c.get("type" === "date")){
            console.log("Adding torque");
            var options = {torque: {} };
            for(var i = 0; i < successfulColumns.length; i++){
              var thisCol = successfulColumns[i];
              if(thisCol.get("type") === "string"){
                options.torque.type = "category";<commit_msg>caps max torques at 3<commit_after>var cdb = require('cartodb.js');
var Utils = require('cdb.Utils');
var BaseDialog = require('../../views/base_dialog/view');
var ViewFactory = require('../../view_factory');
var randomQuote = require('../../view_helpers/random_quote');

module.exports = BaseDialog.extend({

  _CARD_WIDTH: 288,
  _CARD_HEIGHT: 170,
  _TABS_PER_ROW: 3,
  _MAX_ROWS: 9000,
  _MAX_COLS: 60,
  _REJECT: 0,
  _KEEP: 1,
  _GET_BBOX_FROM_THE_GEOM: false,
  _EXCLUDED_COLUMNS: ['cartodb_id', 'lat', 'lon', 'lng', 'long', 'latitude', 'longitude', 'shape_length', 'shape_area', 'objectid', 'id', 'country', 'state', 'created_at', 'updated_at', 'iso2', 'iso3', 'x_coord', 'y_coord', 'xcoord', 'ycoord'],

  events: cdb.core.View.extendEvents({
    "click .js-goPrev": "_prevPage",
    "click .js-goNext": "_nextPage",
    "click .js-skip"  : "_onSkipClick"
  }),

  initialize: function() {
    this.elder('initialize');

    if (!this.options.map) {
      throw new Error('map is required');
    }

    if (!this.options.table) {
      throw new Error('table is required');
    }

    this._initModels();
    this._initViews();
    this._initBinds();
  },

  _check: function() {
    var tableData     = this.options.table.data();
    var geometryTypes = tableData.table && tableData.table.get("stats_geometry_types");
    var hasGeometries = geometryTypes && geometryTypes.length > 0 ? true : false;

    var row_count = tableData.length;
    var hasRows = row_count > 0 && row_count < this._MAX_ROWS;

    var col_count = _(tableData.query_schema).size();
    var hasColumns = col_count > 0 && col_count < this._MAX_COLS;

    if (hasGeometries && hasRows && hasColumns) {
      return true;
    } else {
      return false;
    }
  },

  render_content: function() {
    return this._panes.getActivePane().render().el;
  },

  render: function() {
    BaseDialog.prototype.render.apply(this, arguments);
    return this;
  },

  _initModels: function() {
    this.columns = new Backbone.Collection();
    this.model = new cdb.core.Model({ page: 1, maxPages: 0 });
  },

  _initViews: function() {

    _.bindAll(this, "_addCard", "_generateThumbnail", "_analyzeColumn", "_analyzeColumns", "_refreshMapList", "_onLoadWizard", "_analyzeStats");

    this.table = this.options.table;
    this.user  = this.options.user;
    this.map   = this.options.map;
    //this.bbox  = this.options.map.getViewBounds();
    this.query_schema = this.table.data().query_schema;

    this._panes = new cdb.ui.common.TabPane({
      el: this.el
    });

    this.addView(this._panes);

    this._panes.addTab('vis',
      ViewFactory.createByTemplate('common/dialogs/pecan/template', {
      })
    );

    this._panes.addTab('loading',
      ViewFactory.createByTemplate('common/templates/loading', {
        title: 'Analyzing your data…',
        quote: randomQuote()
      })
    );

    this._panes.addTab('applying',
      ViewFactory.createByTemplate('common/templates/loading', {
        title: 'Applying style…',
        quote: randomQuote()
      })
    );

    if (this._check()) {
      this._panes.active('loading');
      this._start();
    } else {
      this.close();
    }
  },

  _onSkipClick: function(e) {
    this.killEvent(e);
    this.model.set("disabled", true);
    this.close(); // TODO: trigger a skip after the manual testing is finished
  },

  _nextPage: function() {
    var page = this.model.get('page');
    var maxPages = this.model.get('maxPages');

    if (page < maxPages) {
      this.model.set('page', page + 1);
    }
  },

  _prevPage: function() {
    var page = this.model.get('page');
    if (page > 1) {
      this.model.set('page', page - 1);
    }
  },

  _moveTabsNavigation: function() {
    var page = this.model.get('page');
    var rowWidth = 990;

    var p = rowWidth * (page - 1);
    this.$('.js-map-list').css('margin-left', '-' + p + 'px');
    this._refreshNavigation();
  },

  _refreshNavigation: function() {
    var page = this.model.get('page');
    var maxPages = this.model.get('maxPages');

    this.$('.js-goPrev')[ page > 1 ? 'removeClass' : 'addClass' ]('is-disabled');
    this.$('.js-goNext')[ page < maxPages ? 'removeClass' : 'addClass' ]('is-disabled');
  },

  _hideNavigation: function() {
    this.$('.js-navigation').addClass("is-hidden");
  },

  _initBinds: function() {
    this.model.bind('change:page', this._moveTabsNavigation, this);
    this.columns.bind('change:analyzed, change:success', this._onAnalyzeColumn, this);
    this._panes.bind('tabEnabled', this.render, this);
  },

  _onAnalyzeColumn: function(column) {
    var self = this;
    var coltype = column.get("type");
    if (column.get("success")){
      if(coltype !== 'date'){
        this._guessColumn(column);
      }
      else{
        column.set("pending", true);
      }
    }

    var analyzedColumnsCount    = this._getAnalyzedColumns().length;
    var successfullColumnsCount = this._getSuccessColumns().length;

    if (analyzedColumnsCount === this.columns.length) {

      if (successfullColumnsCount === 0) {
        this.close();
        return;
      }
      else {
        this._getPending().forEach(function(c){
          if(c.get("type" === "date")){
            console.log("Adding torque");
            var options = {torque: {} };
            for(var i = 0; i < 3; i++){
              var thisCol = successfulColumns[i];
              if(thisCol.get("type") === "string"){
                options.torque.type = "category";
{'fraction_non_alphanumeric': 0.16974606352962593, 'fraction_numerical': 0.006007099299171748, 'mean_word_length': 2.9496764917325664, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, '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': '3109103', 'n_tokens_mistral': 3719, 'n_tokens_neox': 3576, 'n_words': 693}