<commit_before>"use strict";

var $ = require('jquery'),
    Bacon = require('baconjs'),
    _ = require('lodash'),

    csrf = require('treemap/csrf'),
    BU = require('treemap/baconUtils');

var dom = {
    filterButtons: '[data-comments-filter] li a',
    columnHeaders: '[data-comments-sort] th a',
    pagingButtons: '.pagination li a'
};

$.ajaxSetup(csrf.jqueryAjaxSetupOptions);

module.exports = function(options) {
    var $container = $(options.container),

        lessOrMoreElementStream = $container.asEventStream('click', '[data-less-more]')
            .doAction('.preventDefault')
            .map('.target')
            .map($),

        // We use event delegation on the outer container, because we replace
        // the entire table including the pagination and filter section.

        singleActionStream = $container.asEventStream('click', '[data-comment-single-action] a'),
        batchActionStream = $container.asEventStream('click', '[data-comment-batch] a'),
        toggleAllEventStream = $container.asEventStream('click', '[data-comment-toggle-all]'),

        actionUrlStream = Bacon.mergeAll(singleActionStream, batchActionStream)
            .doAction('.preventDefault')
            .map('.target.href')
            .filter(BU.isDefinedNonEmpty)
            .map(function(url) {
                // We have to look this up every time because it changes based
                // on the current filter/sort/page
                var params = $container.find('[data-comments-params]').attr('data-comments-params');
                return url + '?' + params;
            }),

        singleActionIdStream = singleActionStream
            .map('.target')
            .map($)
            .map(function($elem) {
                return [$elem.parents('[data-comment-id]').attr('data-comment-id')];
            }),

        getSelectedCommentIds = function() {
            var $selectedCheckboxes = $container
                .find('[data-comment-id]')
                .has('[data-batch-action-checkbox]:checked');

            return _.map($selectedCheckboxes, function(elem) {
                return $(elem).attr('data-comment-id');
            });
        },

        batchActionIdStream = batchActionStream
            .map(getSelectedCommentIds),

        actionIdStream = Bacon.mergeAll(singleActionIdStream, batchActionIdStream)
            .map(function(array) {
                return {'comment-ids': array.join(',')};
            }),

        actionResultStream = Bacon.zipAsArray(actionUrlStream, actionIdStream);

    lessOrMoreElementStream.onValue(function($elem) {
        $elem.text($elem.text() == 'less' ? 'more' : 'less');
        var $text = $elem.prev();
        $text.toggleClass('less');
    });

    BU.reloadContainerOnClick(
        $container, dom.pagingButtons, dom.filterButtons, dom.columnHeaders);

    toggleAllEventStream
        .map($container, 'find', '[data-batch-action-checkbox]')
        .onValue(function($elems) {
            var checked = $container.find('[data-comment-toggle-all]').is(':checked');
            $elems.prop('checked', checked);
        });

    var containerUpdatedStream = actionResultStream.flatMap(function(req) {
        var promise = $.post(req[0], req[1], function(html) {
            $container.html(html);
        });
        return Bacon.fromPromise(promise);
    });

    return containerUpdatedStream;
};<commit_msg>Fix comment moderation UI tests.<commit_after>"use strict";

var $ = require('jquery'),
    Bacon = require('baconjs'),
    _ = require('lodash'),

    csrf = require('treemap/csrf'),
    BU = require('treemap/baconUtils');

var dom = {
    filterButtons: '[data-comments-filter] li a',
    columnHeaders: '[data-comments-sort] th a',
    pagingButtons: '.pagination li a'
};

$.ajaxSetup(csrf.jqueryAjaxSetupOptions);

module.exports = function(options) {
    var $container = $(options.container),

        lessOrMoreElementStream = $container.asEventStream('click', '[data-less-more]')
            .doAction('.preventDefault')
            .map('.target')
            .map($),

        // We use event delegation on the outer container, because we replace
        // the entire table including the pagination and filter section.

        singleActionStream = $container.asEventStream('click', '[data-comment-single-action] a'),
        batchActionStream = $container.asEventStream('click', '[data-comment-batch] a'),
        toggleAllEventStream = $container.asEventStream('click', '[data-comment-toggle-all]'),

        actionUrlStream = Bacon.mergeAll(singleActionStream, batchActionStream)
            .doAction('.preventDefault')
            .map('.target.href')
            .filter(BU.isDefinedNonEmpty)
            .map(function(url) {
                // We have to look this up every time because it changes based
                // on the current filter/sort/page
                var params = $container.find('[data-comments-params]').attr('data-comments-params');
                return url + '?' + params;
            }),

        singleActionIdStream = singleActionStream
            .map('.target')
            .map($)
            .map(function($elem) {
                return [$elem.parents('[data-comment-id]').attr('data-comment-id')];
            }),

        getSelectedCommentIds = function() {
            var $selectedCheckboxes = $container
                .find('[data-comment-id]')
                .has('[data-batch-action-checkbox]:checked');

            return _.map($selectedCheckboxes, function(elem) {
                return $(elem).attr('data-comment-id');
            });
        },

        batchActionIdStream = batchActionStream
            .map(getSelectedCommentIds),

        actionIdStream = Bacon.mergeAll(singleActionIdStream, batchActionIdStream)
            .map(function(array) {
                return {'comment-ids': array.join(',')};
            }),

        actionResultStream = Bacon.zipAsArray(actionUrlStream, actionIdStream);

    lessOrMoreElementStream.onValue(function($elem) {
        $elem.text($elem.text() == 'less' ? 'more' : 'less');
        var $text = $elem.prev();
        $text.toggleClass('less');
    });

    BU.reloadContainerOnClick(
        $container, dom.pagingButtons, dom.filterButtons, dom.columnHeaders);

    toggleAllEventStream
        .map($container, 'find', '[data-batch-action-checkbox]')
        .onValue(function($elems) {
            var checked = $container.find('[data-comment-toggle-all]').is(':checked');
            $elems.prop('checked', checked);
        });

    var containerUpdatedStream = actionResultStream.flatMap(function(req) {
        var promise = $.post(req[0], req[1], function(html) {
            $container.html(html);
        });
        return Bacon.fromPromise(promise);
    });

    // We need an onValue to force evaluation of the lazy stream
    containerUpdatedStream.onValue($.noop);

    return containerUpdatedStream;
};
{'fraction_non_alphanumeric': 0.14376169232983163, 'fraction_numerical': 0.0005756223917110375, 'mean_word_length': 2.4542743538767398, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5904157', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1863, 'n_words': 348}