<commit_before>angular.module('anol.featureexchange')

/**
 * @ngdoc directive
 * @name anol.featureexchange.directive:anolFeatureexchange
 *
 * @restrict A
 *
 * @param {string} templateUrl Url to template to use instead of default one
 * @param {string} filename Name of downloaded file
 * @param {function} preDownload Function executed before download provided
 * @param {function} postUpload Function executed after file uploaded
 * @param {string} srs Coordinate system to export features in / load features from
 *
 * @description
 * Download features as geojson
 */
.directive('anolFeatureexchange', ['$translate', '$rootScope', 'MapService', function($translate, $rootScope, MapService) {
    return {
        restrict: 'A',
        replace: true,
        scope: {
            layer: '=',
            filename: '=',
            preDownload: '=',
            postUpload: '=',
            srs: '@'
        },
        templateUrl: function(tElement, tAttrs) {
            var defaultUrl = 'src/modules/featureexchange/templates/featureexchange.html';
            return tAttrs.templateUrl || defaultUrl;
        },
        link: function(scope, element, attrs) {
            var format = new ol.format.GeoJSON();
            var fileselector = element.find('#fileselector');
            var uploadErrorElement = element.find('#upload-error');

            var showError = function(errorMessage) {
                uploadErrorElement.text(errorMessage);
                uploadErrorElement.removeClass('hide');
            };

            scope.download = function() {
                if(scope.layer instanceof anol.layer.Feature) {
                    var geojson = format.writeFeaturesObject(scope.layer.getFeatures(), {
                        featureProjection: MapService.getMap().getView().getProjection(),
                        dataProjection: scope.srs || 'EPSG:4326'
                    });
                    if(angular.isFunction(scope.preDownload)) {
                        geojson = scope.preDownload(geojson);
                    }
                    geojson = JSON.stringify(geojson);
                    // ie
                    if(angular.isFunction(window.navigator.msSaveBlob)) {
                        var blobObject = new Blob([geojson]);
                        window.navigator.msSaveBlob(blobObject, scope.filename);
                    // other
                    } else {
                        var a = $('<a>Foo</a>');
                        a.attr('href', 'data:application/vnd.geo+json;charset=utf-8,' + encodeURIComponent(geojson));
                        a.attr('download', 'features.geojson');
                        a.css('display', 'none');
                        $('body').append(a);
                        a[0].click();
                        a.remove();
                    }
                }
            };

            scope.upload = function() {
                if(scope.layer instanceof anol.layer.Feature) {
                    uploadErrorElement.addClass('hide');
                    uploadErrorElement.empty();
                    fileselector[0].click();
                }
            };

            fileselector.change(function(e) {
                var files = e.target.files;
                if(files.length === 0) {
                    return;
                }
                var fileReader = new FileReader();
                fileReader.onload = function(e) {
                    var featureCollection;
                    try {
                        featureCollection = JSON.parse(e.target.result);
                    } catch(err) {
                        showError(scope.errorMessages.noJsonFormat);
                        return;
                    }
                    if(angular.isUndefined(featureCollection.features) || !angular.isArray(featureCollection.features)) {
                        showError(scope.errorMessages.invalidGeoJson);
                        return;
                    }
                    if(featureCollection.features.length === 0) {
                        showError(scope.errorMessages.emptyGeoJson);
                        return;
                    }
                    if(angular.isFunction(scope.postUpload)) {<commit_msg>Clear fileselector input value before open upload file dialog<commit_after>angular.module('anol.featureexchange')

/**
 * @ngdoc directive
 * @name anol.featureexchange.directive:anolFeatureexchange
 *
 * @restrict A
 *
 * @param {string} templateUrl Url to template to use instead of default one
 * @param {string} filename Name of downloaded file
 * @param {function} preDownload Function executed before download provided
 * @param {function} postUpload Function executed after file uploaded
 * @param {string} srs Coordinate system to export features in / load features from
 *
 * @description
 * Download features as geojson
 */
.directive('anolFeatureexchange', ['$translate', '$rootScope', 'MapService', function($translate, $rootScope, MapService) {
    return {
        restrict: 'A',
        replace: true,
        scope: {
            layer: '=',
            filename: '=',
            preDownload: '=',
            postUpload: '=',
            srs: '@'
        },
        templateUrl: function(tElement, tAttrs) {
            var defaultUrl = 'src/modules/featureexchange/templates/featureexchange.html';
            return tAttrs.templateUrl || defaultUrl;
        },
        link: function(scope, element, attrs) {
            var format = new ol.format.GeoJSON();
            var fileselector = element.find('#fileselector');
            var uploadErrorElement = element.find('#upload-error');

            var showError = function(errorMessage) {
                uploadErrorElement.text(errorMessage);
                uploadErrorElement.removeClass('hide');
            };

            scope.download = function() {
                if(scope.layer instanceof anol.layer.Feature) {
                    var geojson = format.writeFeaturesObject(scope.layer.getFeatures(), {
                        featureProjection: MapService.getMap().getView().getProjection(),
                        dataProjection: scope.srs || 'EPSG:4326'
                    });
                    if(angular.isFunction(scope.preDownload)) {
                        geojson = scope.preDownload(geojson);
                    }
                    geojson = JSON.stringify(geojson);
                    // ie
                    if(angular.isFunction(window.navigator.msSaveBlob)) {
                        var blobObject = new Blob([geojson]);
                        window.navigator.msSaveBlob(blobObject, scope.filename);
                    // other
                    } else {
                        var a = $('<a>Foo</a>');
                        a.attr('href', 'data:application/vnd.geo+json;charset=utf-8,' + encodeURIComponent(geojson));
                        a.attr('download', 'features.geojson');
                        a.css('display', 'none');
                        $('body').append(a);
                        a[0].click();
                        a.remove();
                    }
                }
            };

            scope.upload = function() {
                if(scope.layer instanceof anol.layer.Feature) {
                    uploadErrorElement.addClass('hide');
                    uploadErrorElement.empty();
                    fileselector.val('');
                    fileselector[0].click();
                }
            };

            fileselector.change(function(e) {
                var files = e.target.files;
                if(files.length === 0) {
                    return;
                }
                var fileReader = new FileReader();
                fileReader.onload = function(e) {
                    var featureCollection;
                    try {
                        featureCollection = JSON.parse(e.target.result);
                    } catch(err) {
                        showError(scope.errorMessages.noJsonFormat);
                        return;
                    }
                    if(angular.isUndefined(featureCollection.features) || !angular.isArray(featureCollection.features)) {
                        showError(scope.errorMessages.invalidGeoJson);
                        return;
                    }
                    if(featureCollection.features.length === 0) {
                        showError(scope.errorMessages.emptyGeoJson);
                        return;
                    }
                    if(angular.isFunction(scope.postUpload)) {
{'fraction_non_alphanumeric': 0.10662622491833877, 'fraction_numerical': 0.002099860009332711, 'mean_word_length': 1.4599713055954089, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7147430', 'n_tokens_mistral': 2103, 'n_tokens_neox': 1956, 'n_words': 377}