<commit_before>Tableling.Plain = {};

Tableling.Plain.Table = Tableling.Modular.extend({

  className: 'tableling',
  modules : [ 'table', 'pageSize', 'quickSearch', 'info', 'page' ],
  template : _.template('<div class="header"><div class="pageSize" /><div class="quickSearch" /></div><div class="table" /><div class="footer"><div class="info" /><div class="page" /></div>'),

  regions : {
    tableRegion : '.table',
    pageSizeRegion : '.pageSize',
    quickSearchRegion : '.quickSearch',
    infoRegion : '.info',
    pageRegion : '.page'
  }
});

Tableling.Plain.TableView = Backbone.Marionette.CompositeView.extend({

  // FIXME: make sure these events are not overriden by mistake
  events : {
    'click thead th.sorting' : 'updateSort',
    'click thead th.sorting-asc' : 'updateSort',
    'click thead th.sorting-desc' : 'updateSort'
  },

  initialize : function(options) {
    // TODO: add auto-sort
    this.vent = options.vent;
    this.sort = [];
    this.vent.on('table:setup', this.setSort, this);
    this.vent.on('table:refreshed', this.setSort, this);
  },

  updateSort : function(ev) {

    var el = $(ev.currentTarget);
    if (!(el.hasClass('sorting') || el.hasClass('sorting-asc') || el.hasClass('sorting-desc'))) {
      return;
    }

    var field = this.fieldName(el);

    if (ev.shiftKey || this.sort.length == 1) {

      var index = -1;
      _.find(this.sort, function(item, i) {
        if (item.split(' ')[0] == field) {
          index = i;
        }
      });

      if (index >= 0) {

        var parts = this.sort[index].split(' ');
        this.sort[index] = parts[0] + ' ' + (parts[1] == 'asc' ? 'desc' : 'asc');
        this.showSort();
        return this.vent.trigger('table:update', this.config());
      }
    }

    if (!ev.shiftKey) {
      this.sort.length = 0;
    }

    this.sort.push(field + ' asc');

    this.showSort();

    this.vent.trigger('table:update', this.config());
  },

  setSort : function(config) {
    if (config && config.sort) {
      this.sort = config.sort.slice(0);
      this.showSort();
    }
  },

  showSort : function() {

    this.$el.find('thead th.sorting, thead th.sorting-asc, thead th.sorting-desc').removeClass('sorting sorting-asc sorting-desc').addClass('sorting');

    for (var i = 0; i < this.sort.length; i++) {

      var parts = this.sort[i].split(' ');
      var name = parts[0];
      var direction = parts[1];
      
      field = this.$el.find('thead [data-field="' + name + '"]');
      if (!field.length) {
        field = this.$el.find('thead th:contains("' + name + '")');
      }

      if (field.length) {
        field.removeClass('sorting').addClass(direction == 'desc' ? 'sorting-desc' : 'sorting-asc');
      }
    }
  },

  config : function() {
    return {
      page : 1,
      sort : this.sortConfig()
    };
  },

  sortConfig : function() {
    return this.sort.length ? this.sort : null;
  },

  fieldName : function(el) {
    return el.data('field') || el.text();
  }
});

Tableling.Plain.PageSizeView = Tableling.Plain.Table.prototype.pageSizeView = Tableling.FieldModule.extend({

  // TODO: update current page intelligently
  name : 'pageSize',
  template : function(data) {
    return _.template('<select name="pageSize" /> <%- entries %>', data);
  },

  i18n : {
    entries : 'entries per page'
  },
  sizes : [ 10, 15, 20, 25, 50 ],

  ui : {
    field : 'select'
  },

  initialize : function(options) {
    Tableling.FieldModule.prototype.initialize.call(this, options);
    this.sizes = _.clone(options.sizes || this.sizes);
  },

  onRender : function() {
    this.ui.field.empty();
    _.each(this.sizes, _.bind(this.addSize, this));
  },

  addSize : function(size) {
    $('<option />').text(size).appendTo(this.ui.field);
  },

  setupValue : function(value) {
    if (value) {
      Tableling.FieldModule.prototype.setupValue.apply(this, Array.prototype.slice.call(arguments));<commit_msg>Make sure table view events are not overriden. Also added initialization function.<commit_after>Tableling.Plain = {};

Tableling.Plain.Table = Tableling.Modular.extend({

  className: 'tableling',
  modules : [ 'table', 'pageSize', 'quickSearch', 'info', 'page' ],
  template : _.template('<div class="header"><div class="pageSize" /><div class="quickSearch" /></div><div class="table" /><div class="footer"><div class="info" /><div class="page" /></div>'),

  regions : {
    tableRegion : '.table',
    pageSizeRegion : '.pageSize',
    quickSearchRegion : '.quickSearch',
    infoRegion : '.info',
    pageRegion : '.page'
  }
});

// TODO: make table view a module
Tableling.Plain.TableView = Backbone.Marionette.CompositeView.extend({

  moduleEvents : {
    'click thead th.sorting' : 'updateSort',
    'click thead th.sorting-asc' : 'updateSort',
    'click thead th.sorting-desc' : 'updateSort'
  },

  // TODO: add auto-sort
  initialize : function(options) {

    this.vent = options.vent;
    this.sort = [];
    this.vent.on('table:setup', this.setSort, this);
    this.vent.on('table:refreshed', this.setSort, this);
    this.events = _.extend({}, this.events || {}, this.moduleEvents);

    if (typeof(this.initializeModule) == 'function') {
      this.initializeModule(options);
    }
  },

  updateSort : function(ev) {

    var el = $(ev.currentTarget);
    if (!(el.hasClass('sorting') || el.hasClass('sorting-asc') || el.hasClass('sorting-desc'))) {
      return;
    }

    var field = this.fieldName(el);

    if (ev.shiftKey || this.sort.length == 1) {

      var index = -1;
      _.find(this.sort, function(item, i) {
        if (item.split(' ')[0] == field) {
          index = i;
        }
      });

      if (index >= 0) {

        var parts = this.sort[index].split(' ');
        this.sort[index] = parts[0] + ' ' + (parts[1] == 'asc' ? 'desc' : 'asc');
        this.showSort();
        return this.vent.trigger('table:update', this.config());
      }
    }

    if (!ev.shiftKey) {
      this.sort.length = 0;
    }

    this.sort.push(field + ' asc');

    this.showSort();

    this.vent.trigger('table:update', this.config());
  },

  setSort : function(config) {
    if (config && config.sort) {
      this.sort = config.sort.slice(0);
      this.showSort();
    }
  },

  showSort : function() {

    this.$el.find('thead th.sorting, thead th.sorting-asc, thead th.sorting-desc').removeClass('sorting sorting-asc sorting-desc').addClass('sorting');

    for (var i = 0; i < this.sort.length; i++) {

      var parts = this.sort[i].split(' ');
      var name = parts[0];
      var direction = parts[1];
      
      field = this.$el.find('thead [data-field="' + name + '"]');
      if (!field.length) {
        field = this.$el.find('thead th:contains("' + name + '")');
      }

      if (field.length) {
        field.removeClass('sorting').addClass(direction == 'desc' ? 'sorting-desc' : 'sorting-asc');
      }
    }
  },

  config : function() {
    return {
      page : 1,
      sort : this.sortConfig()
    };
  },

  sortConfig : function() {
    return this.sort.length ? this.sort : null;
  },

  fieldName : function(el) {
    return el.data('field') || el.text();
  }
});

Tableling.Plain.PageSizeView = Tableling.Plain.Table.prototype.pageSizeView = Tableling.FieldModule.extend({

  // TODO: update current page intelligently
  name : 'pageSize',
  template : function(data) {
    return _.template('<select name="pageSize" /> <%- entries %>', data);
  },

  i18n : {
    entries : 'entries per page'
  },
  sizes : [ 10, 15, 20, 25, 50 ],

  ui : {
    field : 'select'
  },

  initialize : function(options) {
    this.sizes = _.clone(options.sizes || this.sizes);
    Tableling.FieldModule.prototype.initialize.call(this, options);
  },

  onRender : function() {
    this.ui.field.empty();
    _.each(this.sizes, _.bind(this.addSize, this));
  },

  addSize : function(size) {
    $('<option />').text(size).appendTo(this.ui.field);
  },

  setupValue : function(value) {
    if (value) {
      Tableling.FieldModule.prototype.setupValue.apply(this, Array.prototype.slice.call(arguments));
{'fraction_non_alphanumeric': 0.18550185873605948, 'fraction_numerical': 0.005947955390334572, 'mean_word_length': 2.8251184834123224, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '3357774', 'n_tokens_mistral': 2771, 'n_tokens_neox': 2657, 'n_words': 516}