<commit_before>
/**
*  Tables list
*
*  It will show the user tables in a list
*
*  Usage example:
*
*  var tableList = new cdb.admin.dashboard.TableList({
*    el: this.$('#tablelist'),
*    collection: this.tables,
*    user: this.user   // it needs it to know if the user has limitations or not
*  });
*
*/

cdb.admin.dashboard.TableList = cdb.core.View.extend({

  tagName: 'ul',

  initialize: function() {
    this.collection.bind('add remove reset', this.render, this);
  },

  render: function() {
    this.clearSubViews();
    var self = this;

    this.collection.each(function(table) {
      var table_item = new cdb.admin.dashboard.TableItem({
        model: table,
        user: self.options.user,
        table_options: self.collection.options
      })
      .bind('remove', function() {

        self.trigger('showLoader');

        var xhr = this.model.destroy();
        $.when(xhr).done(function() {
          self.trigger('remove');
        });
      });

      self.$el.append(table_item.render().el);
      self.addView(table_item);

    });
  }

});

/**
* Single table item in dashboard tables list
*
* Usage example:
*
*  var li = new cdb.admin.dashboard.TableItem({
*    model: table_model,
*    limitation: !this.options.user.get("private_tables")
*  });
*
*/

cdb.admin.dashboard.TableItem = cdb.core.View.extend({

  tagName: 'li',

  _TAGS_PER_ITEM: 6,

  _PRIVACY_VALUES: ['public','private'],

  _TEXTS: {
    sync: {
      state: {
        enabled: 'SYNCED <%= ran_at %>',
        error: 'ERROR WITH SYNC'
      },
      msg: {
        enabled: '',
        error: 'The source is not available anymore. Please check its location'
      }  
    }
  },

  events: {
    'click a.delete': '_deleteTable',
    'click a.status': '_changePrivacy'
  },

  initialize: function() {
  _.bindAll(this, "_changePrivacy", "_showDeleteDialog");

    this.template = cdb.templates.getTemplate('dashboard/views/table_list_item');

    this.model.bind('change:privacy',     this._setPrivacy,     this);
  },

  _generateTagList: function(tags, selected_tag) {

    if (!tags) return;

    var tags_count = this._TAGS_PER_ITEM;

    tags = tags.slice(0, tags_count);

    var template = _.template('<a href="/dashboard/table/tag/<%= tag %>" data-tag="<%= tag %>"><%= tag %></a>');

    if (selected_tag) {
      var selected_tag_included = _.contains(tags, selected_tag);
      if (!selected_tag_included) tags_count--;
    }

    // Render the tags
    var result = _.map(tags, function(t) {
      return template({ tag: t });
    }).slice(0, tags_count);

    // Add the selected_tag in case it wasn't already added
    if (!selected_tag_included && selected_tag) {
      result.push(template({ tag: selected_tag }));
    }

    return result.reverse().join(" ");

  },

  _cleanString: function(s, n) {

    if (s) {
      s = s.replace(/<(?:.|\n)*?>/gm, ''); // strip HTML tags
      s = s.substr(0, n-1) + (s.length > n ? '&hellip;' : ''); // truncate string
    }

    return s;

  },

  render: function() {
    var attrs = this.model.toJSON();

    attrs.table_size = this._bytesToSize(attrs.table.size);

    var selected_tag = this.options.table_options.get("tags")

    var tags             = this._generateTagList(attrs.tags, selected_tag);
    var name             = this._cleanString(this.model.get("name"), 30);
    var description      = this.model.get("description");
    var pretty_row_count = cdb.Utils.formatNumber(this.model.attributes.table.row_count);

    var extra_tags_count = attrs.tags.length - this._TAGS_PER_ITEM;

    var template_vars = {
      sync_status: "",
      order: this.options.table_options.get("order"),
      row_count: pretty_row_count,
      name: name,
      description: description,
      tags: tags,
      extra_tags_count: extra_tags_count
    };

    // Check sync status
    // If table is under synchronization, it will show the info
    // available about the sync.
    if (!_.isEmpty(this.model.get("synchronization"))) {
      
      var sync_attrs = _.clone(this.model.get("synchronization"));
      sync_attrs.ran_at = moment(sync_attrs.ran_at || new Date()).fromNow();
      var state = sync_attrs.state;

      _.extend(
        template_vars,
        {
          sync_status: state,
          sync_message: _.template(this._TEXTS.sync.state[state == "failure" ? "error" : "enabled" ])(sync_attrs),
          sync_error_message: this._TEXTS.sync.msg[state == "failure" ? "error" : "enabled" ]
        }
      );
    }

    this.$el.append(this.template(_.extend(attrs, template_vars)));

    this.$el.find(".feed.failure a").tipsy({
      fade: true,
      gravity: "s",
      className: 'feed',
      offset: 20,
      title: function() {
        return $(this).attr("data-tipsy")
      }
    });

    this.$('a.delete').tipsy({ gravity: 's', fade: true });

    return this;
  },<commit_msg>CDB-7723 #fixes Reduces the legth of the table name so it doesn't touch the option list in the right side of the item<commit_after>
/**
*  Tables list
*
*  It will show the user tables in a list
*
*  Usage example:
*
*  var tableList = new cdb.admin.dashboard.TableList({
*    el: this.$('#tablelist'),
*    collection: this.tables,
*    user: this.user   // it needs it to know if the user has limitations or not
*  });
*
*/

cdb.admin.dashboard.TableList = cdb.core.View.extend({

  tagName: 'ul',

  initialize: function() {
    this.collection.bind('add remove reset', this.render, this);
  },

  render: function() {
    this.clearSubViews();
    var self = this;

    this.collection.each(function(table) {
      var table_item = new cdb.admin.dashboard.TableItem({
        model: table,
        user: self.options.user,
        table_options: self.collection.options
      })
      .bind('remove', function() {

        self.trigger('showLoader');

        var xhr = this.model.destroy();
        $.when(xhr).done(function() {
          self.trigger('remove');
        });
      });

      self.$el.append(table_item.render().el);
      self.addView(table_item);

    });
  }

});

/**
* Single table item in dashboard tables list
*
* Usage example:
*
*  var li = new cdb.admin.dashboard.TableItem({
*    model: table_model,
*    limitation: !this.options.user.get("private_tables")
*  });
*
*/

cdb.admin.dashboard.TableItem = cdb.core.View.extend({

  tagName: 'li',

  _TAGS_PER_ITEM: 6,

  _PRIVACY_VALUES: ['public','private'],

  _TEXTS: {
    sync: {
      state: {
        enabled: 'SYNCED <%= ran_at %>',
        error: 'ERROR WITH SYNC'
      },
      msg: {
        enabled: '',
        error: 'The source is not available anymore. Please check its location'
      }
    }
  },

  events: {
    'click a.delete': '_deleteTable',
    'click a.status': '_changePrivacy'
  },

  initialize: function() {
  _.bindAll(this, "_changePrivacy", "_showDeleteDialog");

    this.template = cdb.templates.getTemplate('dashboard/views/table_list_item');

    this.model.bind('change:privacy',     this._setPrivacy,     this);
  },

  _generateTagList: function(tags, selected_tag) {

    if (!tags) return;

    var tags_count = this._TAGS_PER_ITEM;

    tags = tags.slice(0, tags_count);

    var template = _.template('<a href="/dashboard/table/tag/<%= tag %>" data-tag="<%= tag %>"><%= tag %></a>');

    if (selected_tag) {
      var selected_tag_included = _.contains(tags, selected_tag);
      if (!selected_tag_included) tags_count--;
    }

    // Render the tags
    var result = _.map(tags, function(t) {
      return template({ tag: t });
    }).slice(0, tags_count);

    // Add the selected_tag in case it wasn't already added
    if (!selected_tag_included && selected_tag) {
      result.push(template({ tag: selected_tag }));
    }

    return result.reverse().join(" ");

  },

  _cleanString: function(s, n) {

    if (s) {
      s = s.replace(/<(?:.|\n)*?>/gm, ''); // strip HTML tags
      s = s.substr(0, n-1) + (s.length > n ? '&hellip;' : ''); // truncate string
    }

    return s;

  },

  render: function() {
    var attrs = this.model.toJSON();

    attrs.table_size = this._bytesToSize(attrs.table.size);

    var selected_tag = this.options.table_options.get("tags")

    var tags             = this._generateTagList(attrs.tags, selected_tag);
    var name             = this._cleanString(this.model.get("name"), 26);
    var description      = this.model.get("description");
    var pretty_row_count = cdb.Utils.formatNumber(this.model.attributes.table.row_count);

    var extra_tags_count = attrs.tags.length - this._TAGS_PER_ITEM;

    var template_vars = {
      sync_status: "",
      order: this.options.table_options.get("order"),
      row_count: pretty_row_count,
      name: name,
      description: description,
      tags: tags,
      extra_tags_count: extra_tags_count
    };

    // Check sync status
    // If table is under synchronization, it will show the info
    // available about the sync.
    if (!_.isEmpty(this.model.get("synchronization"))) {

      var sync_attrs = _.clone(this.model.get("synchronization"));
      sync_attrs.ran_at = moment(sync_attrs.ran_at || new Date()).fromNow();
      var state = sync_attrs.state;

      _.extend(
        template_vars,
        {
          sync_status: state,
          sync_message: _.template(this._TEXTS.sync.state[state == "failure" ? "error" : "enabled" ])(sync_attrs),
          sync_error_message: this._TEXTS.sync.msg[state == "failure" ? "error" : "enabled" ]
        }
      );
    }

    this.$el.append(this.template(_.extend(attrs, template_vars)));

    this.$el.find(".feed.failure a").tipsy({
      fade: true,
      gravity: "s",
      className: 'feed',
      offset: 20,
      title: function() {
        return $(this).attr("data-tipsy")
      }
    });

    this.$('a.delete').tipsy({ gravity: 's', fade: true });

    return this;
  },
{'fraction_non_alphanumeric': 0.15588773642464918, 'fraction_numerical': 0.0022371364653243847, 'mean_word_length': 2.64529280948851, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '136486', 'n_tokens_mistral': 3270, 'n_tokens_neox': 3171, 'n_words': 680}