<commit_before>function getMatchesSelector() {
    choices = [
        Element.prototype.matches,
        Element.prototype.mozMatchesSelector,
        Element.prototype.webkitMatchesSelector,
        Element.prototype.msMatchesSelector,
        Element.prototype.oMatchesSelector,
    ];
    for (var i = 0; i <= choices.length - 1; ++i) {
        if (choices[i] !== undefined) {
            return function(elem, selector) {
                return choices[i].bind(elem, selector)();
            };
        };
    };
}

var matchesSelector = getMatchesSelector();

var $ = document.querySelector.bind(document);
var $$ = document.querySelectorAll.bind(document);

function last(collection, i) {
    if (i === undefined)
        i = 1;
    return collection[collection.length - i];
}

// Search upward from `elem` for an element matched by `selector`.
function $up(elem, selector) {
    var target;
    while (elem = elem.parentElement) {
        if (matchesSelector(elem, selector)) {
            return elem;
        };
    };
    // Didn't find it: return undefined.
}

function forEach(collection, f) {
    for (var i = 0; i < collection.length; ++i)
        f(collection[i]);
}

function handle(selector, type, f) {
    handler = function(elem) {
        elem.addEventListener(type, function() { f(elem); });
    }

    forEach($$(selector), handler);

    return handler;
}

function parse(s) {
    t = document.createElement('template');
    t.innerHTML = s;
    return document.importNode(t.content.firstChild, true);
}

function create_item(description, callback) {
    data = new FormData();
    data.append('description', description);

    r = new XMLHttpRequest();
    r.onreadystatechange = function() {
        if (r.readyState == r.DONE) {
            if (r.status == 200) {
                container = $('#checklist-item-container');
                item = parse(r.responseText);
                item = container.insertBefore(
                    item, last(container.children));
                handle_remove(item.querySelector('button.remove-item'));
                handle_check(item.querySelector('button.check'));
                if (callback)
                    callback();
            };
        };
    };
    r.open('POST', '/create');
    r.send(data);
}

var done_text = '\u2611';
var not_done_text = '\u2610';

function edit_item(id, done, callback) {
    data = new FormData();
    data.append('id', id);
    data.append('done', done);

    r = new XMLHttpRequest();
    r.onreadystatechange = function() {
        if (r.readyState == r.DONE) {
            if (r.status == 204) {
                text_node = $(
                    'table#checklist tr[data-item-id="' + id +
                    '"] button.check').childNodes[0];
                if (done)
                    text_node.nodeValue = done_text;
                else
                    text_node.nodeValue = not_done_text;

                if (callback)
                    callback();
            };
        };
    };
    r.open('POST', '/edit');
    r.send(data);
}

function remove_item(id, callback) {
    data = new FormData();
    data.append('id', id);

    r = new XMLHttpRequest();
    r.onreadystatechange = function() {
        if (r.readyState == r.DONE) {
            if (r.status == 204) {
                $('table#checklist tr[data-item-id="' + id + '"]').remove();
                if (callback)
                    callback;
            };
        };
    };
    r.open('POST', '/remove');
    r.send(data);
}

var handle_remove;
var handle_check;

document.addEventListener('DOMContentLoaded', function() {
    handle_remove = handle(
            'table#checklist button.remove-item', 'click', function(elem) {
        remove_item($up(elem, 'tr').getAttribute('data-item-id'));
    });

    handle_check = handle(
            'table#checklist button.check', 'click', function(elem) {
        text_node = elem.childNodes[0];

        edit_item(
            $up(elem, 'tr').getAttribute('data-item-id'),
            !(text_node.nodeValue === done_text));

    });

    handle('button#new-item', 'click', function(elem) {
        description_input = $('#new-item-container input[name=description]');
        description = description_input.value;
        if (description !== '') {
            create_item(description, function() {
                description_input.value = '';
            });
        };
    });
})<commit_msg>Use `bind(elem)` to set value of `this`<commit_after>function getMatchesSelector() {
    choices = [
        Element.prototype.matches,
        Element.prototype.mozMatchesSelector,
        Element.prototype.webkitMatchesSelector,
        Element.prototype.msMatchesSelector,
        Element.prototype.oMatchesSelector,
    ];
    for (var i = 0; i <= choices.length - 1; ++i) {
        if (choices[i] !== undefined) {
            return function(elem, selector) {
                return choices[i].bind(elem, selector)();
            };
        };
    };
}

var matchesSelector = getMatchesSelector();

var $ = document.querySelector.bind(document);
var $$ = document.querySelectorAll.bind(document);

function last(collection, i) {
    if (i === undefined)
        i = 1;
    return collection[collection.length - i];
}

// Search upward from `elem` for an element matched by `selector`.
function $up(elem, selector) {
    var target;
    while (elem = elem.parentElement) {
        if (matchesSelector(elem, selector)) {
            return elem;
        };
    };
    // Didn't find it: return undefined.
}

function forEach(collection, f) {
    for (var i = 0; i < collection.length; ++i)
        f(collection[i]);
}

function handle(selector, type, f) {
    handler = function(elem) {
        elem.addEventListener(type, f.bind(elem));
    }

    forEach($$(selector), handler);

    return handler;
}

function parse(s) {
    t = document.createElement('template');
    t.innerHTML = s;
    return document.importNode(t.content.firstChild, true);
}

function create_item(description, callback) {
    data = new FormData();
    data.append('description', description);

    r = new XMLHttpRequest();
    r.onreadystatechange = function() {
        if (r.readyState == r.DONE) {
            if (r.status == 200) {
                container = $('#checklist-item-container');
                item = parse(r.responseText);
                item = container.insertBefore(
                    item, last(container.children));
                handle_remove(item.querySelector('button.remove-item'));
                handle_check(item.querySelector('button.check'));
                if (callback)
                    callback();
            };
        };
    };
    r.open('POST', '/create');
    r.send(data);
}

var done_text = '\u2611';
var not_done_text = '\u2610';

function edit_item(id, done, callback) {
    data = new FormData();
    data.append('id', id);
    data.append('done', done);

    r = new XMLHttpRequest();
    r.onreadystatechange = function() {
        if (r.readyState == r.DONE) {
            if (r.status == 204) {
                text_node = $(
                    'table#checklist tr[data-item-id="' + id +
                    '"] button.check').childNodes[0];
                if (done)
                    text_node.nodeValue = done_text;
                else
                    text_node.nodeValue = not_done_text;

                if (callback)
                    callback();
            };
        };
    };
    r.open('POST', '/edit');
    r.send(data);
}

function remove_item(id, callback) {
    data = new FormData();
    data.append('id', id);

    r = new XMLHttpRequest();
    r.onreadystatechange = function() {
        if (r.readyState == r.DONE) {
            if (r.status == 204) {
                $('table#checklist tr[data-item-id="' + id + '"]').remove();
                if (callback)
                    callback;
            };
        };
    };
    r.open('POST', '/remove');
    r.send(data);
}

var handle_remove;
var handle_check;

document.addEventListener('DOMContentLoaded', function() {
    handle_remove = handle(
            'table#checklist button.remove-item', 'click', function() {
        remove_item($up(this, 'tr').getAttribute('data-item-id'));
    });

    handle_check = handle(
            'table#checklist button.check', 'click', function() {
        text_node = this.childNodes[0];

        edit_item(
            $up(this, 'tr').getAttribute('data-item-id'),
            !(text_node.nodeValue === done_text));

    });

    handle('button#new-item', 'click', function() {
        description_input = $('#new-item-container input[name=description]');
        description = description_input.value;
        if (description !== '') {
            create_item(description, function() {
                description_input.value = '';
            });
        };
    });
})
{'fraction_non_alphanumeric': 0.1473209249858996, 'fraction_numerical': 0.005188945290468133, 'mean_word_length': 2.064638783269962, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4969105', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2507, 'n_words': 513}