<commit_before>/*jslint node: true */
/*jshint laxbreak: true */
"use strict";

var $ = require('jquery');
var _ = require('underscore');
var Constants = require('./Constants');
var JSZip = require('jszip');
var FileSaver = require('file-saver');

    var DownloadDataLoader = function() {
    return {
        get: function(accession, format) {
            var zip = new JSZip();
            zip.file('readme', 'Protein sequence features for ' + accession + '\n\n' +
                'This zipped file contains protein sequence features provided by different data sources (' +
                _.pluck(Constants.getDataSources(), 'url').join() + '). \n\nCurrently, all download formats (' +
                _.pluck(Constants.getDownloadFormats(), 'text').join() + ') are supported for UniProt data sources' +
                ' while only JSON is guaranteed for any other data source. \n\nWe cannot guarantee the availability ' +
                'of any data source at the download time.');

            var delegates = [];
            _.each(Constants.getDataSources(), function() {
                var delegate = $.Deferred();
                delegates.push(delegate);
            });
            _.each(Constants.getDataSources(), function(source, index) {
                var extension = source.source === Constants.getUniProtSource() ? '' : '.' + format;
                var loader = $.ajax({
                    accepts: { gff: 'text/x-gff', json: 'application/json', xml: 'application/xml' },
                    converters: {
                        'text gff': function(result) { return result; },
                        'text json': function(result) { return result; },
                        'text xml': function(result) { return result; }
                    },
                    dataType: format,
                    url: source.url + accession + extension
                }).done(function(d) {
                    if (loader.getResponseHeader('Content-type').indexOf(format) !== -1) {
                        var fileName = source.source + (source.category ? source.category : '') + '.' + format;
                        zip.file(fileName, d);
                    } else {
                        zip.file('warning_' + index + '.txt', source.url + ' response does not correspond to the ' +
                            'required format ' + format + '. Data has not been processed.');
                    }
                }).fail(function (e) {
                    zip.file('error_' + index + '.txt', source.url + ' responded with an error code for the ' +
                        'required format ' + format + '. Data has not been retrieved.');
                }).always(function() {
                    delegates[index].resolve();
                });
            });
            //when all done
            $.when.apply(null, delegates).always(function () {
                zip.generateAsync({type:"blob"})
                    .then(function(content) {
                        // TODO FileSaver does not necessarily work in Safari -- see content type
                        FileSaver.saveAs(content, "protVistaData.zip");
                    });
            });
        }
    };
}();

module.exports = DownloadDataLoader;
<commit_msg>Save solution working but for safari.<commit_after>/*jslint node: true */
/*jshint laxbreak: true */
"use strict";

var $ = require('jquery');
var _ = require('underscore');
var Constants = require('./Constants');
var JSZip = require('jszip');
var FileSaver = require('file-saver');

    var DownloadDataLoader = function() {
    return {
        get: function(accession, format) {
            var zip = new JSZip();
            zip.file('readme', 'Protein sequence features for ' + accession + '\n\n' +
                'This zipped file contains protein sequence features provided by different data sources (' +
                _.pluck(Constants.getDataSources(), 'url').join() + '). \n\nCurrently, all download formats (' +
                _.pluck(Constants.getDownloadFormats(), 'text').join() + ') are supported for UniProt data sources' +
                ' while only JSON is guaranteed for any other data source. \n\nWe cannot guarantee the availability ' +
                'of any data source at the download time.');
            var delegates = [];
            _.each(Constants.getDataSources(), function() {
                var delegate = $.Deferred();
                delegates.push(delegate);
            });
            _.each(Constants.getDataSources(), function(source, index) {
                var extension = source.source === Constants.getUniProtSource() ? '' : '.' + format;
                var loader = $.ajax({
                    accepts: { gff: 'text/x-gff', json: 'application/json', xml: 'application/xml' },
                    converters: {
                        'text gff': function(result) { return result; },
                        'text json': function(result) { return result; },
                        'text xml': function(result) { return result; }
                    },
                    dataType: format,
                    url: source.url + accession + extension
                }).done(function(d) {
                    if (loader.getResponseHeader('Content-type').indexOf(format) !== -1) {
                        var fileName = source.source + (source.category ? source.category : '') + '.' + format;
                        zip.file(fileName, d);
                    } else {
                        zip.file('warning_' + index + '.txt', source.url + ' response does not correspond to the ' +
                            'required format ' + format + '. Data has not been processed.');
                    }
                }).fail(function (e) {
                    zip.file('error_' + index + '.txt', source.url + ' responded with an error code for the ' +
                        'required format ' + format + '. Data has not been retrieved.');
                }).always(function() {
                    delegates[index].resolve();
                });
            });
            //when all done
            //TODO async download does not work in safari
            $.when.apply(null, delegates).always(function () {
                zip.generateAsync({type:'blob'})
                    .then(function(content) {
                        FileSaver.saveAs(content, "protVistaData.zip");
                    });
            });
        }
    };
}();

module.exports = DownloadDataLoader;

{'fraction_non_alphanumeric': 0.13026052104208416, 'fraction_numerical': 0.0003083089255433945, 'mean_word_length': 1.60457647531112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119219', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1533, 'n_words': 428}