<commit_before>export default function maExportToCsvButton ($stateParams, Papa, notification, AdminDescription, entryFormatter, ReadQueries) {
    return {
        restrict: 'E',
        scope: {
            entity: '&',
            label: '@',
            datastore: '&'
        },
        link: function(scope) {
            scope.label = scope.label || 'EXPORT';
            scope.datastore = scope.datastore();
            scope.entity = scope.entity();
            var exportView = scope.entity.exportView();
            var listView = scope.entity.listView();
            if (exportView.fields().length === 0) {
                var exportFields = listView.exportFields();
                if (exportFields === null) {
                    exportFields = listView.fields();
                }
                exportView.fields(exportFields);
                exportView.filters(listView.filters());
                exportView.name(listView.name()); // to enable reuse of sortField
            }
            scope.has_export = exportView.fields().length > 0;
            var formatEntry = entryFormatter.getFormatter(exportView.fields());

            scope.exportToCsv = function () {
                var rawEntries;

                ReadQueries.getAll(exportView, -1, $stateParams.search, $stateParams.sortField, $stateParams.sortDir)
                    .then(response => {
                        rawEntries = response.data;
                        return rawEntries;
                    })
                    .then(rawEntries => ReadQueries.getReferenceData(exportView.fields(), rawEntries))
                    .then(referenceData => {
                        const references = exportView.getReferences();
                        for (var name in referenceData) {
                            AdminDescription.getEntryConstructor().createArrayFromRest(
                                referenceData[name],
                                [references[name].targetField()],
                                references[name].targetEntity().name(),
                                references[name].targetEntity().identifier().name()
                            ).map(entry => scope.datastore.addEntry(references[name].targetEntity().uniqueId + '_values', entry));
                        }
                    })
                    .then(function () {
                        var entries = exportView.mapEntries(rawEntries);

                        // shortcut to diplay collection of entry with included referenced values
                        scope.datastore.fillReferencesValuesFromCollection(entries, exportView.getReferences(), true);

                        var results = [];
                        for (var i = entries.length - 1; i >= 0; i--) {
                            results[i] = formatEntry(entries[i]);
                        }
                        var csv = Papa.unparse(results, listView.exportOptions());
                        var fakeLink = document.createElement('a');
                        document.body.appendChild(fakeLink);

                        fakeLink.setAttribute('href', 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(csv));
                        fakeLink.setAttribute('download', scope.entity.name() + '.csv');
                        fakeLink.click();
                    }, function (error) {
                        notification.log(error.message, {addnCls: 'humane-flatty-error'});
                    });
            };
        },
        template:
`<span ng-if="has_export">
    <a class="btn btn-default" ng-click="exportToCsv()">
        <span class="glyphicon glyphicon-download" aria-hidden="true"></span>&nbsp;<span class="hidden-xs" translate="{{ ::label }}"></span>
    </a>
</span>`
    };
}

maExportToCsvButton.$inject = ['$stateParams', 'Papa', 'notification', 'AdminDescription', 'EntryFormatter', 'ReadQueries'];<commit_msg>Allow IE11 & Edge to export CSV<commit_after>export default function maExportToCsvButton ($stateParams, Papa, notification, AdminDescription, entryFormatter, ReadQueries) {
    return {
        restrict: 'E',
        scope: {
            entity: '&',
            label: '@',
            datastore: '&'
        },
        link: function(scope) {
            scope.label = scope.label || 'EXPORT';
            scope.datastore = scope.datastore();
            scope.entity = scope.entity();
            var exportView = scope.entity.exportView();
            var listView = scope.entity.listView();
            if (exportView.fields().length === 0) {
                var exportFields = listView.exportFields();
                if (exportFields === null) {
                    exportFields = listView.fields();
                }
                exportView.fields(exportFields);
                exportView.filters(listView.filters());
                exportView.name(listView.name()); // to enable reuse of sortField
            }
            scope.has_export = exportView.fields().length > 0;
            var formatEntry = entryFormatter.getFormatter(exportView.fields());

            scope.exportToCsv = function () {
                var rawEntries;

                ReadQueries.getAll(exportView, -1, $stateParams.search, $stateParams.sortField, $stateParams.sortDir)
                    .then(response => {
                        rawEntries = response.data;
                        return rawEntries;
                    })
                    .then(rawEntries => ReadQueries.getReferenceData(exportView.fields(), rawEntries))
                    .then(referenceData => {
                        const references = exportView.getReferences();
                        for (var name in referenceData) {
                            AdminDescription.getEntryConstructor().createArrayFromRest(
                                referenceData[name],
                                [references[name].targetField()],
                                references[name].targetEntity().name(),
                                references[name].targetEntity().identifier().name()
                            ).map(entry => scope.datastore.addEntry(references[name].targetEntity().uniqueId + '_values', entry));
                        }
                    })
                    .then(function () {
                        var entries = exportView.mapEntries(rawEntries);

                        // shortcut to diplay collection of entry with included referenced values
                        scope.datastore.fillReferencesValuesFromCollection(entries, exportView.getReferences(), true);

                        var results = [];
                        for (var i = entries.length - 1; i >= 0; i--) {
                            results[i] = formatEntry(entries[i]);
                        }
                        var csv = Papa.unparse(results, listView.exportOptions());
                        var fakeLink = document.createElement('a');
                        document.body.appendChild(fakeLink);

                        const blobName = `${scope.entity.name()}.csv`;

                        if (window.navigator && window.navigator.msSaveOrOpenBlob) { // Manage IE11+ & Edge
                            var blob = new Blob([csv], { type: 'text/csv' });
                            window.navigator.msSaveOrOpenBlob(blob, blobName);
                        } else {
                            fakeLink.setAttribute('href', 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(csv));
                            fakeLink.setAttribute('download', blobName);
                            fakeLink.click();
                        }
                    }, function (error) {
                        notification.log(error.message, {addnCls: 'humane-flatty-error'});
                    });
            };
        },
        template:
`<span ng-if="has_export">
    <a class="btn btn-default" ng-click="exportToCsv()">
        <span class="glyphicon glyphicon-download" aria-hidden="true"></span>&nbsp;<span class="hidden-xs" translate="{{ ::label }}"></span>
    </a>
</span>`
    };
}

maExportToCsvButton.$inject = ['$stateParams', 'Papa', 'notification', 'AdminDescription', 'EntryFormatter', 'ReadQueries'];
{'fraction_non_alphanumeric': 0.1185967467831998, 'fraction_numerical': 0.0019422189851905803, 'mean_word_length': 1.537419156144133, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7148606', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1876, 'n_words': 351}