<commit_before>/*
  Block Table
*/

var _ = require('../lodash');
var $ = require('jquery');
var Block = require('../block');
var template = '' +
  '<table>' +
    '<caption contenteditable></caption>' +
    '<thead>' +
      '<tr>' +
        '<th contenteditable></th>' +
        '<th contenteditable></th>' +
      '</tr>' +
    '</thead>' +
    '<tbody>' +
      '<tr>' +
        '<td contenteditable></td>' +
        '<td contenteditable></td>' +
      '</tr>' +
    '</tbody>' +
  '</table>';
var instruction = '<span class="helper">' + i18n.t('blocks:table:helper-merge') + '</span>';

function addCell(row, cellTag) {
    var tag_template = _.template('<<%= tag %>>');

    if (cellTag === undefined) {
        cellTag = tag_template({
            tag: $(row).children().first().prop('tagName').toLowerCase()
        });
    }

    $(row).append($(cellTag, {
        contenteditable: ''
    }));
}

function addColumnHandler(ev, scope) {
    ev.preventDefault();
    scope.find('tr').each(function() {
        addCell(this);
    });
    scope.find('td').each(function() {
        if (undefined === $(this).attr('colspan')){
            $(this).attr('colspan', 1);
        }
    });
}

function mergeCellHandler(ev, scope) {
    ev.preventDefault();
    var table = scope.find('table');
    var activated = !$('.st-block-control-ui-btn--merge').hasClass('activated');

    table.find('td, th').each(function(){
        var colspanValue = parseInt($(this).attr('colspan'));

        if (isNaN(colspanValue)){
            $(this).attr('colspan', 1);
        }
    });

    if (activated === true) {
        table.find('td , th').addClass('mergeable');
        table.before('<span class="helper">' + i18n.t('blocks:table:helper-merge') + '</span>');
        table.on('click', 'td, th', function(e) {
            e.preventDefault();
            if ($(this).next('td, th').index() !== -1) {
                var nextWidth = parseInt($(this).next('td, th').attr('colspan'));

                var colspanValue = parseInt($(this).attr('colspan'));

                $(this).next('td, th').remove();

                $(this).attr('colspan', colspanValue + nextWidth);
            }
            table.find('td, th').addClass('mergeable');
        });
    }
    else {
        $('.helper').remove();
        table.off('click');
        table.find('td, th').attr('contenteditable', '');
        table.find('td, th').removeClass('mergeable');
    }

    $('.st-block-control-ui-btn').toggleClass('disabled');
    $('.st-block-control-ui-btn--merge').removeClass('disabled');
}

function unMergeCellHandler(ev, scope) {
    ev.preventDefault();
    var activated = !$('.st-block-control-ui-btn--unmerge').hasClass('activated');
    var table = scope;
    table.before('<span class="helper">' + i18n.t('blocks:table:helper-unmerge') + '</span>');

    if (activated === true){
        table.find('td, th').each(function(){
            var colspanValue = parseInt($(this).attr('colspan'));

            if (colspanValue > 1) {
                $(this).addClass('unmergeable');
            }
        });
        table.on('click', 'td, th', function() {
            var colspanValue = parseInt($(this).attr('colspan'));

            if (colspanValue > 1 && $(this).is('td')) {
                $(this).attr('colspan', colspanValue - 1);
                $(this).parent().append('<td colspan="1"></td>');
            }
            if (colspanValue > 1 && $(this).is('th')) {
                $(this).attr('colspan', colspanValue - 1);
                $(this).parent().append('<th colspan="1"></th>');
            }
            table.find('td, th').each(function(){
            colspanValue = parseInt($(this).attr('colspan'));

            if (colspanValue > 1) {
                $(this).attr('contenteditable', '');
                $(this).addClass('unmergeable');
            }
            else {
                $(this).removeClass('unmergeable');
            }

            });

        });
    }
    else {
        $('.helper').remove();
        table.off('click');
        table.find('td, th').attr('contenteditable', '');
        table.find('td, th').removeClass('unmergeable');
    }

    $('.st-block-control-ui-btn').toggleClass('disabled');
    $('.st-block-control-ui-btn--unmerge').removeClass('disabled');
}

function deleteColumnHandler(ev, scope) {
    ev.preventDefault();
    scope.find('tr').each(function() {
        if ($(this).children().length > 1) {
            var colspanValue = parseInt($(this).children().last().attr('colspan'));
            if (colspanValue > 1) {
                $(this).children().last().attr('colspan', colspanValue - 1);
            }
            else {
                $(this).children().last().remove();
            }
        }
    });
}

function addRowHandler(ev, scope) {
    var row = $('<tr>');

    scope.find('th').each(function() {
        addCell(row, '<td>');
    });

    scope.find('tbody').append(row);
    //Really important allow us to merge
    scope.find('td').each(function() {
        if (undefined === $(this).attr('colspan')){
            $(this).attr('colspan', 1);
        }
    });
}

function deleteRowHandler(ev, block) {
    ev.preventDefault();

    if (block.find('tbody tr').length > 1) {
        block.find('tbody tr:last').remove();
    }
}

function changeTheme(ev, block) {
    var table = block.find('table');
    var themeSelector = $('.theme-selector select');

    themeSelector.on('change', function() {
        var themeClass = $(this).find(':selected').val();
        table.removeClass();
        table.addClass(themeClass);
    });
}

module.exports = Block.extend({
    type: 'table',<commit_msg>Removed bug, after colums merge, new rows was empty<commit_after>/*
  Block Table
*/

var _ = require('../lodash');
var $ = require('jquery');
var Block = require('../block');
var template = '' +
  '<table>' +
    '<caption contenteditable></caption>' +
    '<thead>' +
      '<tr>' +
        '<th contenteditable></th>' +
        '<th contenteditable></th>' +
      '</tr>' +
    '</thead>' +
    '<tbody>' +
      '<tr>' +
        '<td contenteditable></td>' +
        '<td contenteditable></td>' +
      '</tr>' +
    '</tbody>' +
  '</table>';
var instruction = '<span class="helper">' + i18n.t('blocks:table:helper-merge') + '</span>';


function countLineColspan(scope, count) {
    scope.find('th').each(function() {
        if (!isNaN($(this).attr('colspan'))) {
            count += $(this).attr('colspan');
        }
        else {
            count++;
        }
    });
    return count;
}

function addCell(row, cellTag) {
    var tag_template = _.template('<<%= tag %>>');

    if (cellTag === undefined) {
        cellTag = tag_template({
            tag: $(row).children().first().prop('tagName').toLowerCase()
        });
    }

    $(row).append($(cellTag, {
        contenteditable: ''
    }));
}

function addColumnHandler(ev, scope) {
    ev.preventDefault();
    var count = countLineColspan(scope,'th', 0);
    scope.find('tr').each(function() {
        addCell(this);
    });

    scope.find('td').each(function() {
        if (undefined === $(this).attr('colspan')){
            $(this).attr('colspan', 1);
        }
    });
}

function mergeCellHandler(ev, scope) {
    ev.preventDefault();
    var table = scope.find('table');
    var activated = !$('.st-block-control-ui-btn--merge').hasClass('activated');

    table.find('td, th').each(function(){
        var colspanValue = parseInt($(this).attr('colspan'));

        if (isNaN(colspanValue)){
            $(this).attr('colspan', 1);
        }
    });

    if (activated === true) {
        table.find('td , th').addClass('mergeable');
        table.before('<span class="helper">' + i18n.t('blocks:table:helper-merge') + '</span>');
        table.on('click', 'td, th', function(e) {
            e.preventDefault();
            if ($(this).next('td, th').index() !== -1) {
                var nextWidth = parseInt($(this).next('td, th').attr('colspan'));

                var colspanValue = parseInt($(this).attr('colspan'));

                $(this).next('td, th').remove();

                $(this).attr('colspan', colspanValue + nextWidth);
            }
            table.find('td, th').addClass('mergeable');
        });
    }
    else {
        $('.helper').remove();
        table.off('click');
        table.find('td, th').attr('contenteditable', '');
        table.find('td, th').removeClass('mergeable');
    }

    $('.st-block-control-ui-btn').toggleClass('disabled');
    $('.st-block-control-ui-btn--merge').removeClass('disabled');
}

function unMergeCellHandler(ev, scope) {
    ev.preventDefault();
    var activated = !$('.st-block-control-ui-btn--unmerge').hasClass('activated');
    var table = scope;
    table.before('<span class="helper">' + i18n.t('blocks:table:helper-unmerge') + '</span>');

    if (activated === true){
        table.find('td, th').each(function(){
            var colspanValue = parseInt($(this).attr('colspan'));

            if (colspanValue > 1) {
                $(this).addClass('unmergeable');
            }
        });
        table.on('click', 'td, th', function() {
            var colspanValue = parseInt($(this).attr('colspan'));

            if (colspanValue > 1 && $(this).is('td')) {
                $(this).attr('colspan', colspanValue - 1);
                $(this).parent().append('<td colspan="1"></td>');
            }
            if (colspanValue > 1 && $(this).is('th')) {
                $(this).attr('colspan', colspanValue - 1);
                $(this).parent().append('<th colspan="1"></th>');
            }
            table.find('td, th').each(function(){
            colspanValue = parseInt($(this).attr('colspan'));

            if (colspanValue > 1) {
                $(this).attr('contenteditable', '');
                $(this).addClass('unmergeable');
            }
            else {
                $(this).removeClass('unmergeable');
            }

            });

        });
    }
    else {
        $('.helper').remove();
        table.off('click');
        table.find('td, th').attr('contenteditable', '');
        table.find('td, th').removeClass('unmergeable');
    }

    $('.st-block-control-ui-btn').toggleClass('disabled');
    $('.st-block-control-ui-btn--unmerge').removeClass('disabled');
}

function deleteColumnHandler(ev, scope) {
    ev.preventDefault();
    scope.find('tr').each(function() {
        if ($(this).children().length > 1) {
            var colspanValue = parseInt($(this).children().last().attr('colspan'));
            if (colspanValue > 1) {
                $(this).children().last().attr('colspan', colspanValue - 1);
            }
            else {
                $(this).children().last().remove();
            }
        }
    });
}


function addRowHandler(ev, scope) {

    // Check all <tr> if  there is missind td, add whatever needed
    scope.find('tr').each(function() {
        if(count - $(this).children().length != 0  ) {
            missing = count - $(this).children().length;
            for(var i = 1; i < missing; i++) {
                addCell(this, '<td>');
            }
        }
    });

    var row = $('<tr>');
    var count = countLineColspan(scope, 0);
    for(var i = 0; i< count; i++){
        addCell(row, '<td>');
    }

    scope.find('tbody').append(row);
    //Really important allow us to merge
    scope.find('td').each(function() {
        if (undefined === $(this).attr('colspan')){
            $(this).attr('colspan', 1);
        }
    });
}

function deleteRowHandler(ev, block) {
    ev.preventDefault();

    if (block.find('tbody tr').length > 1) {
        block.find('tbody tr:last').remove();
    }
}

function changeTheme(ev, block) {
    var table = block.find('table');
    var themeSelector = $('.theme-selector select');

    themeSelector.on('change', function() {
        var themeClass = $(this).find(':selected').val();
        table.removeClass();
        table.addClass(themeClass);
    });
}

module.exports = Block.extend({
    type: 'table',
{'fraction_non_alphanumeric': 0.2126284388465363, 'fraction_numerical': 0.004060324825986079, 'mean_word_length': 2.2321906802356724, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 87, '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': '191567', 'n_tokens_mistral': 3682, 'n_tokens_neox': 3599, 'n_words': 596}