<commit_before>var ChyrpLikes = {
    action: "like",
    failed: false,
    didPrevFinish: true,
    init: function() {
        if (Site.ajax) {
            $("div.likes a.likes").click(function(e) {
                if (!ChyrpLikes.failed) {
                    e.preventDefault();
                    ChyrpLikes.toggle($(this).attr("data-post_id"));
                }
            });
            ChyrpLikes.watch();
        }
    },
    watch: function() {
        // Watch for DOM additions on blog pages.
        if (!!window.MutationObserver && $(".post").length) {
            var target = $(".post").last().parent()[0];
            var observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    for (var i = 0; i < mutation.addedNodes.length; ++i) {
                        var item = mutation.addedNodes[i];
                        $(item).find("div.likes a.likes").click(function(e) {
                            e.preventDefault();
                            ChyrpLikes.toggle($(this).attr("data-post_id"));
                        });
                    }
                });
            });
            var config = { childList: true, subtree: true };
            observer.observe(target, config);
        }
    },
    send: function(post_id, callback, isUnlike) {
        if (!ChyrpLikes.didPrevFinish)
            return false;

        if (isUnlike == true)
            ChyrpLikes.action = "unlike";
        else
            ChyrpLikes.action = "like";

        $.ajax({
            type: "POST",
            url: Site.chyrp_url + "/includes/ajax.php",
            data: {
                "action": ChyrpLikes.action,
                "post_id": post_id
            },
            beforeSend: function() {
                ChyrpLikes.didPrevFinish = false;	
            },
            success: function(response) {
                if (isError(response)) {
                    ChyrpLikes.panic();
                    return;
                }

                if (response != "")
                    callback(response);
            },
            complete: function(response) {
                ChyrpLikes.didPrevFinish = true;
            },
            dataType: "html",
            cache: false,
            error: ChyrpLikes.panic
        })
    },
    toggle: function(post_id) {
        if ($("#likes_" + post_id + " a.liked").length)
            ChyrpLikes.unlike(post_id);
        else
            ChyrpLikes.like(post_id);
    },
    like: function(post_id) {
        ChyrpLikes.send(post_id,function(response) {
            var div = $("#likes_" + post_id);
            div.children("span.like_text").html(response);
            div.children("a.like").removeClass("like").addClass("liked");
        }, false);
    },
    unlike: function(post_id) {
        ChyrpLikes.send(post_id,function(response) {
            var div = $("#likes_" + post_id);
            div.children("span.like_text").html(response);
            div.children("a.liked").removeClass("liked").addClass("like");
        }, true);
    },
    panic: function() {
        ChyrpLikes.failed = true;
        alert('<?php echo __("Oops! Something went wrong on this web page."); ?>');
    }
};
$(document).ready(ChyrpLikes.init);
<commit_msg>Likes module: simplified JavaScript logic<commit_after>var ChyrpLikes = {
    failed: false,
    busy: false,
    init: function() {
        if (Site.ajax) {
            $("div.likes a.likes").click(function(e) {
                if (!ChyrpLikes.failed) {
                    e.preventDefault();
                    ChyrpLikes.toggle($(this).attr("data-post_id"));
                }
            });
            ChyrpLikes.watch();
        }
    },
    watch: function() {
        // Watch for DOM additions on blog pages.
        if (!!window.MutationObserver && $(".post").length) {
            var target = $(".post").last().parent()[0];
            var observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    for (var i = 0; i < mutation.addedNodes.length; ++i) {
                        var item = mutation.addedNodes[i];
                        $(item).find("div.likes a.likes").click(function(e) {
                            e.preventDefault();
                            ChyrpLikes.toggle($(this).attr("data-post_id"));
                        });
                    }
                });
            });
            var config = { childList: true, subtree: true };
            observer.observe(target, config);
        }
    },
    send: function(post_id, callback, isUnlike) {
        if (ChyrpLikes.busy)
            return false;

        $.ajax({
            type: "POST",
            url: Site.chyrp_url + "/includes/ajax.php",
            data: {
                "action": (isUnlike) ? "unlike" : "like",
                "post_id": post_id
            },
            beforeSend: function() {
                ChyrpLikes.busy = true;	
            },
            success: function(response) {
                if (isError(response)) {
                    ChyrpLikes.panic();
                    return;
                }

                if (response != "")
                    callback(response);
            },
            complete: function(response) {
                ChyrpLikes.busy = false;
            },
            dataType: "html",
            cache: false,
            error: ChyrpLikes.panic
        });
    },
    toggle: function(post_id) {
        if ($("#likes_" + post_id + " a.liked").length)
            ChyrpLikes.send(post_id, function(response) {
                var div = $("#likes_" + post_id);
                div.children("span.like_text").html(response);
                div.children("a.liked").removeClass("liked").addClass("like");
            }, true);
        else
            ChyrpLikes.send(post_id, function(response) {
                var div = $("#likes_" + post_id);
                div.children("span.like_text").html(response);
                div.children("a.like").removeClass("like").addClass("liked");
            }, false);
    },
    panic: function() {
        ChyrpLikes.failed = true;
        alert('<?php echo __("Oops! Something went wrong on this web page."); ?>');
    }
};
$(document).ready(ChyrpLikes.init);

{'fraction_non_alphanumeric': 0.1411913016073117, 'fraction_numerical': 0.0006303183107469272, 'mean_word_length': 1.4188262195121952, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 5, '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': '2947208', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1745, 'n_words': 302}