<commit_before>---
---
$(function() {
    $('#file-input').change(loadFile);
    $('#url-form').submit(loadURL);
    $('#url-form a').click(fillUrlInput);
});

// Respond to a user choosing a file to upload. If successful,
// send the user to the transformation page.
function loadFile() {
    var myFile = this.files[0];
    readXML(myFile).then(function(fileText) {
        // We could use the filename as the identifier. However
        // multiple clients could have conflicts on filenames for
        // files that are not identical to each other. This has the
        // potential to be confusing if URLs are copied/pasted
        // between users/sessions.
        var newId = uniqueId();
        sessionStorage.setItem(newId, fileText);
        sessionStorage.setItem(newId+'_name', myFile.name);

        location.href = '{{ site.github.url }}/transform.html?h=' + newId;
    }).catch(function(err) {
        console.log(err);
    });
}

// A Promise that reads a file from the user and provides the string
// text on success.
function readXML(file) {
    return new Promise (function(resolve, reject) {
        if(!file.type.match(/.*xml/)) {
            reject(Error('Filetype not supported. Please select an XML file.'));
        } else {
            var reader = new FileReader();
            reader.onload = function(e) {
                resolve(e.target.result);
            };
            reader.onerror = function() {
                reject(Error('There was a problem loading the file.'));
            }
            reader.readAsText(file);
        }
    });
}

// Respond to a user choosing a URL to access. If successful,
// send the user to the transformation page.
function loadURL(e) {
    e.preventDefault();
    var url = $('#url-input').val();
    getContentType(url).then(function(contentType) {
        if(contentType.match(/.*xml/)) {
            location.href = '{{ site.github.url }}/transform.html?f=' + encodeURIComponent(url);
        } else {
            console.log('The URL provided doesn\'t appear to be an IRS XML e-file document.');
        }
    }).catch(function(error) {
        console.log(error);
    });
}

// A Promise that checks the Content-Type of a request
function getContentType(url) {
    return new Promise (function(resolve, reject) {
        if(!url) {
            reject(Error('No file was requested.'));
            return;
        }

        var request = new XMLHttpRequest();
        request.open('HEAD', url);
        request.onload = function() {
            if(request.status === 200) {
                resolve(request.getResponseHeader('Content-Type'));
            } else {
                reject(Error('Error Code: ' + request.status + ' (' + request.statusText + ')'));
            }
        };
        request.onerror = function() {
            reject(Error('There was a network error.'));
        };
        request.send();
    });
}

// Pre-load the search box with a URL that will work
function fillUrlInput(e) {
    e.preventDefault();
    $('#url-input').val(e.target.href);
}
<commit_msg>auto-focus on textbox after selecting demo link<commit_after>---
---
$(function() {
    $('#file-input').change(loadFile);
    $('#url-form').submit(loadURL);
    $('#url-form a').click(fillUrlInput);
});

// Respond to a user choosing a file to upload. If successful,
// send the user to the transformation page.
function loadFile() {
    var myFile = this.files[0];
    readXML(myFile).then(function(fileText) {
        // We could use the filename as the identifier. However
        // multiple clients could have conflicts on filenames for
        // files that are not identical to each other. This has the
        // potential to be confusing if URLs are copied/pasted
        // between users/sessions.
        var newId = uniqueId();
        sessionStorage.setItem(newId, fileText);
        sessionStorage.setItem(newId+'_name', myFile.name);

        location.href = '{{ site.github.url }}/transform.html?h=' + newId;
    }).catch(function(err) {
        console.log(err);
    });
}

// A Promise that reads a file from the user and provides the string
// text on success.
function readXML(file) {
    return new Promise (function(resolve, reject) {
        if(!file.type.match(/.*xml/)) {
            reject(Error('Filetype not supported. Please select an XML file.'));
        } else {
            var reader = new FileReader();
            reader.onload = function(e) {
                resolve(e.target.result);
            };
            reader.onerror = function() {
                reject(Error('There was a problem loading the file.'));
            }
            reader.readAsText(file);
        }
    });
}

// Respond to a user choosing a URL to access. If successful,
// send the user to the transformation page.
function loadURL(e) {
    e.preventDefault();
    var url = $('#url-input').val();
    getContentType(url).then(function(contentType) {
        if(contentType.match(/.*xml/)) {
            location.href = '{{ site.github.url }}/transform.html?f=' + encodeURIComponent(url);
        } else {
            console.log('The URL provided doesn\'t appear to be an IRS XML e-file document.');
        }
    }).catch(function(error) {
        console.log(error);
    });
}

// A Promise that checks the Content-Type of a request
function getContentType(url) {
    return new Promise (function(resolve, reject) {
        if(!url) {
            reject(Error('No file was requested.'));
            return;
        }

        var request = new XMLHttpRequest();
        request.open('HEAD', url);
        request.onload = function() {
            if(request.status === 200) {
                resolve(request.getResponseHeader('Content-Type'));
            } else {
                reject(Error('Error Code: ' + request.status + ' (' + request.statusText + ')'));
            }
        };
        request.onerror = function() {
            reject(Error('There was a network error.'));
        };
        request.send();
    });
}

// Pre-load the search box with a URL that will work
function fillUrlInput(e) {
    e.preventDefault();
    $('#url-input').val(e.target.href);
    $('#url-input').focus();
}

{'fraction_non_alphanumeric': 0.14057198254968492, 'fraction_numerical': 0.0012926159314913556, 'mean_word_length': 2.2022762545266423, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3651260', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1703, 'n_words': 479}