<commit_before>import he from 'he';

import dbServer from '../model/db-server';
import Editor from '../utils/editor';
import exportXML from '../utils/export-xml';
import exportJSON from '../utils/export-json';
import notify from '../view/notify';


class TableBuilder {

    /**
     * Initialise.
     */
    constructor(tableElem, xsltFilename) {
        this.tableElem = tableElem;
        this.xsltFilename = xsltFilename
    }

    /**
     * Return the dataset.
     */
    getDataset(records) {
        return records.map((el) => {
            return el[this.xsltFilename].TEI;
        })
    }

    /**
     * Return the table columns.
     */
    getColumns() {
        let columns = [{data: null}];
        $('th:not(:first-child)').each(function() {
            columns.push({data: $(this).text().replace(/\s/g, "")});
        });
        return columns;
    }

    /**
     * Build the table.
     */
    build(dataSet) {
        return new Promise((resolve, reject) => {
            const columns = this.getColumns(),
                  table   = this.tableElem.DataTable({
                "data": dataSet,
                "dom": "Bfrtip",
                "deferRender": true,
                "colReorder": {
                    "fixedColumnsLeft": 1,
                },
                "columnDefs": [
                    {
                        "targets": "_all",
                        "render": function (data, type, full, meta) {
                            return he.decode(data.toString());  // Decode HTML entities
                        }
                    },
                    {
                        "searchable": false,
                        "orderable": false,
                        "targets": 0,
                        "className": "bg-faded",
                    }
                ],
                "columns": columns,
                "buttons": [
                    {
                        "extend": "collection",
                        "text": "Export",
                        "autoClose": true,
                        "buttons": [
                            {
                                "extend": "excelHtml5",
                                "title": "buttons-excel-export",
                                "exportOptions": {
                                    "columns": function (idx, data, node) {
                                        return idx !== 0 && idx !== 1;
                                    }
                                }
                            },
                            {
                                "extend": "csvHtml5",
                                "title": "buttons-csv-export",
                                "exportOptions": {
                                    "columns": function (idx, data, node) {
                                        return idx !== 0 && idx !== 1;
                                    }
                                }
                            },
                            {
                                "text": "XML",
                                "className": "buttons-xml-export",
                                "action": function (evt, dt, node, conf) {
                                    dbServer.getAll().then(function(records) {
                                        exportXML(records);
                                    }).catch(function(err) {
                                        notify(err.message, 'error');
                                        throw err;
                                    });
                                }
                            },
                            {
                                "text": "JSON",
                                "className": "buttons-json-export",
                                "action": function (evt, dt, node, conf) {
                                    dbServer.getAll().then(function(records) {
                                        exportJSON(dataSet, false);
                                    }).catch(function(err) {
                                        notify(err.message, 'error');
                                        throw err;
                                    });
                                }
                            },
                            {
                                "text": "JSONP",
                                "className": "buttons-jsonp-export",
                                "action": function (evt, dt, node, conf) {
                                    dbServer.getAll().then(function(records) {
                                        exportJSON(dataSet, true);
                                    }).catch(function(err) {
                                        notify(err.message, 'error');
                                        throw err;
                                    });
                                }
                            }
                        ]
                    },
                    {
                        "extend": "collection",
                        "text": "Edit",
                        "autoClose": true,
                        "buttons": [
                            {
                                "text": "Select All",
                                "extend": "selectAll"
                            },
                            {
                                "text": "Deselect",
                                "extend": "selectNone"
                            },
                            {
                                "text": "Delete",
                                "className": "buttons-delete",
                                "action": function (evt, dt, node, conf) {
                                    $('tbody tr.selected').each(function() {
                                        const id = $(this).attr('id');
                                        dbServer.remove(id).then(function() {
                                            dt.rows('#' + id).remove().draw();
                                        }).catch(function(err) {
                                            notify(err.message, 'error');
                                            throw err;
                                        });
                                    });
                                }
                            },
                            {
                                "text": "Delete All",
                                "className": "buttons-delete-all",
                                "action": function (evt, dt, node, conf) {
                                    $('tbody tr').each(function() {
                                        const id = $(this).attr('id');
                                        dbServer.remove(id).then(function() {
                                            dt.rows('#' + id).remove().draw();
                                        }).catch(function(err) {
                                            notify(err.message, 'error');
                                            throw err;
                                        });
                                    });
                                }
                            },
                            {
                                "text": "XML Editor",
                                "className": "buttons-xml-editor",
                                "action": function (evt, dt, node, conf) {
                                    if ($('tr.selected').length !== 1) {
                                        notify('Please select a single row to edit', 'info');
                                    } else {
                                        const id = parseInt($('tr.selected').attr('id'));

                                        // Load the record into the editor modal
                                        dbServer.get(id).then(function(record) {
                                            const container = $('#editor-modal .modal-body')[0],<commit_msg>Fix delete all table rows<commit_after>import he from 'he';

import dbServer from '../model/db-server';
import Editor from '../utils/editor';
import exportXML from '../utils/export-xml';
import exportJSON from '../utils/export-json';
import notify from '../view/notify';


class TableBuilder {

    /**
     * Initialise.
     */
    constructor(tableElem, xsltFilename) {
        this.tableElem = tableElem;
        this.xsltFilename = xsltFilename
    }

    /**
     * Return the dataset.
     */
    getDataset(records) {
        return records.map((el) => {
            return el[this.xsltFilename].TEI;
        })
    }

    /**
     * Return the table columns.
     */
    getColumns() {
        let columns = [{data: null}];
        $('th:not(:first-child)').each(function() {
            columns.push({data: $(this).text().replace(/\s/g, "")});
        });
        return columns;
    }

    /**
     * Build the table.
     */
    build(dataSet) {
        return new Promise((resolve, reject) => {
            const columns = this.getColumns(),
                  table   = this.tableElem.DataTable({
                "data": dataSet,
                "dom": "Bfrtip",
                "deferRender": true,
                "colReorder": {
                    "fixedColumnsLeft": 1,
                },
                "columnDefs": [
                    {
                        "targets": "_all",
                        "render": function (data, type, full, meta) {
                            return he.decode(data.toString());  // Decode HTML entities
                        }
                    },
                    {
                        "searchable": false,
                        "orderable": false,
                        "targets": 0,
                        "className": "bg-faded",
                    }
                ],
                "columns": columns,
                "buttons": [
                    {
                        "extend": "collection",
                        "text": "Export",
                        "autoClose": true,
                        "buttons": [
                            {
                                "extend": "excelHtml5",
                                "title": "buttons-excel-export",
                                "exportOptions": {
                                    "columns": function (idx, data, node) {
                                        return idx !== 0 && idx !== 1;
                                    }
                                }
                            },
                            {
                                "extend": "csvHtml5",
                                "title": "buttons-csv-export",
                                "exportOptions": {
                                    "columns": function (idx, data, node) {
                                        return idx !== 0 && idx !== 1;
                                    }
                                }
                            },
                            {
                                "text": "XML",
                                "className": "buttons-xml-export",
                                "action": function (evt, dt, node, conf) {
                                    dbServer.getAll().then(function(records) {
                                        exportXML(records);
                                    }).catch(function(err) {
                                        notify(err.message, 'error');
                                        throw err;
                                    });
                                }
                            },
                            {
                                "text": "JSON",
                                "className": "buttons-json-export",
                                "action": function (evt, dt, node, conf) {
                                    dbServer.getAll().then(function(records) {
                                        exportJSON(dataSet, false);
                                    }).catch(function(err) {
                                        notify(err.message, 'error');
                                        throw err;
                                    });
                                }
                            },
                            {
                                "text": "JSONP",
                                "className": "buttons-jsonp-export",
                                "action": function (evt, dt, node, conf) {
                                    dbServer.getAll().then(function(records) {
                                        exportJSON(dataSet, true);
                                    }).catch(function(err) {
                                        notify(err.message, 'error');
                                        throw err;
                                    });
                                }
                            }
                        ]
                    },
                    {
                        "extend": "collection",
                        "text": "Edit",
                        "autoClose": true,
                        "buttons": [
                            {
                                "text": "Select All",
                                "extend": "selectAll"
                            },
                            {
                                "text": "Deselect",
                                "extend": "selectNone"
                            },
                            {
                                "text": "Delete",
                                "className": "buttons-delete",
                                "action": function (evt, dt, node, conf) {
                                    $('tbody tr.selected').each(function() {
                                        const id = $(this).attr('id');
                                        dbServer.remove(id).then(function() {
                                            dt.rows('#' + id).remove().draw();
                                        }).catch(function(err) {
                                            notify(err.message, 'error');
                                            throw err;
                                        });
                                    });
                                }
                            },
                            {
                                "text": "Delete All",
                                "className": "buttons-delete-all",
                                "action": function (evt, dt, node, conf) {
                                    $('tbody tr').each(function() {
                                        const id = $(this).attr('id');
                                        dbServer.clear().then(function() {
                                            dt.rows().remove().draw();
                                        }).catch(function(err) {
                                            notify(err.message, 'error');
                                            throw err;
                                        });
                                    });
                                }
                            },
                            {
                                "text": "XML Editor",
                                "className": "buttons-xml-editor",
                                "action": function (evt, dt, node, conf) {
                                    if ($('tr.selected').length !== 1) {
                                        notify('Please select a single row to edit', 'info');
                                    } else {
                                        const id = parseInt($('tr.selected').attr('id'));

                                        // Load the record into the editor modal
                                        dbServer.get(id).then(function(record) {
                                            const container = $('#editor-modal .modal-body')[0],
{'fraction_non_alphanumeric': 0.10504071104481323, 'fraction_numerical': 0.0012430853378084406, 'mean_word_length': 0.5982914473030695, 'pattern_counts': {'":': 104, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 114, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4376191', 'n_tokens_mistral': 3131, 'n_tokens_neox': 2938, 'n_words': 588}