<commit_before>var Dict = require('./dict').Dict;

var condense = (function () {

var exports = {};

var _message_content_height_cache = new Dict();

function show_more_link(row) {
    row.find(".message_condenser").hide();
    row.find(".message_expander").show();
}

function show_condense_link(row) {
    row.find(".message_expander").hide();
    row.find(".message_condenser").show();
}

function condense_row(row) {
    var content = row.find(".message_content");
    content.addClass("condensed");
    show_more_link(row);
}

function uncondense_row(row) {
    var content = row.find(".message_content");
    content.removeClass("condensed");
    show_condense_link(row);
}

exports.uncollapse = function (row) {
    // Uncollapse a message, restoring the condensed message [More] or
    // [Condense] link if necessary.
    var message = current_msg_list.get(rows.id(row));
    message.collapsed = false;
    message_flags.save_uncollapsed(message);

    var process_row = function process_row(row) {
        var content = row.find(".message_content");
        content.removeClass("collapsed");

        if (message.condensed === true) {
            // This message was condensed by the user, so re-show the
            // [More] link.
            condense_row(row);
        } else if (message.condensed === false) {
            // This message was un-condensed by the user, so re-show the
            // [Condense] link.
            uncondense_row(row);
        } else if (content.hasClass("could-be-condensed")) {
            // By default, condense a long message.
            condense_row(row);
        } else {
            // This was a short message, no more need for a [More] link.
            row.find(".message_expander").hide();
        }
    };

    // We also need to collapse this message in the home view
    var home_row = home_msg_list.get_row(rows.id(row));

    process_row(row);
    process_row(home_row);
};

exports.collapse = function (row) {
    // Collapse a message, hiding the condensed message [More] or
    // [Condense] link if necessary.
    var message = current_msg_list.get(rows.id(row));
    message.collapsed = true;

    if (message.locally_echoed) {
        // Trying to collapse a locally echoed message is
        // very rare, and in our current implementation the
        // server response overwrites the flag, so we just
        // punt for now.
        return;
    }

    message_flags.save_collapsed(message);

    var process_row = function process_row(row) {
        row.find(".message_content").addClass("collapsed");
        show_more_link(row);
    };

    // We also need to collapse this message in the home view
    var home_row = home_msg_list.get_row(rows.id(row));

    process_row(row);
    process_row(home_row);
};

exports.toggle_collapse = function (message) {
    if (message.is_me_message) {
        // Disabled temporarily because /me messages don't have a
        // styling for collapsing /me messages (they only recently
        // added multi-line support).  See also popovers.js.
        return;
    }

    var row = current_msg_list.get_row(message.id);
    if (!row) {
        return;
    }
    var condensed = row.find(".could-be-condensed");
    if (message.collapsed) {
        message.condensed = true;
        condense.uncollapse(row);
        condensed.addClass("condensed");
        exports.show_message_expander(row);
        row.find(".message_condenser").hide();
    } else if (!message.collapsed && condensed.hasClass("condensed")) {
        message.condensed = false;
        condensed.removeClass("condensed");
        exports.hide_message_expander(row);
        row.find(".message_condenser").show();
    } else if (!message.collapsed && !condensed.hasClass("condensed")) {
        condense.collapse(row);
    }<commit_msg>condense: Rewrite condense.toggle_collapse to be readable and correct.<commit_after>/*
This library implements two related, similar concepts:

- condensing, i.e. cutting off messages taller than about a half
  screen so that they aren't distractingly tall (and offering a button
  to uncondense them).

- Collapsing, i.e. taking a message and reducing its height to a
  single line, with a button to see the content.

*/

var Dict = require('./dict').Dict;

var condense = (function () {

var exports = {};

var _message_content_height_cache = new Dict();

function show_more_link(row) {
    row.find(".message_condenser").hide();
    row.find(".message_expander").show();
}

function show_condense_link(row) {
    row.find(".message_expander").hide();
    row.find(".message_condenser").show();
}

function condense_row(row) {
    var content = row.find(".message_content");
    content.addClass("condensed");
    show_more_link(row);
}

function uncondense_row(row) {
    var content = row.find(".message_content");
    content.removeClass("condensed");
    show_condense_link(row);
}

exports.uncollapse = function (row) {
    // Uncollapse a message, restoring the condensed message [More] or
    // [Condense] link if necessary.
    var message = current_msg_list.get(rows.id(row));
    message.collapsed = false;
    message_flags.save_uncollapsed(message);

    var process_row = function process_row(row) {
        var content = row.find(".message_content");
        content.removeClass("collapsed");

        if (message.condensed === true) {
            // This message was condensed by the user, so re-show the
            // [More] link.
            condense_row(row);
        } else if (message.condensed === false) {
            // This message was un-condensed by the user, so re-show the
            // [Condense] link.
            uncondense_row(row);
        } else if (content.hasClass("could-be-condensed")) {
            // By default, condense a long message.
            condense_row(row);
        } else {
            // This was a short message, no more need for a [More] link.
            row.find(".message_expander").hide();
        }
    };

    // We also need to collapse this message in the home view
    var home_row = home_msg_list.get_row(rows.id(row));

    process_row(row);
    process_row(home_row);
};

exports.collapse = function (row) {
    // Collapse a message, hiding the condensed message [More] or
    // [Condense] link if necessary.
    var message = current_msg_list.get(rows.id(row));
    message.collapsed = true;

    if (message.locally_echoed) {
        // Trying to collapse a locally echoed message is
        // very rare, and in our current implementation the
        // server response overwrites the flag, so we just
        // punt for now.
        return;
    }

    message_flags.save_collapsed(message);

    var process_row = function process_row(row) {
        row.find(".message_content").addClass("collapsed");
        show_more_link(row);
    };

    // We also need to collapse this message in the home view
    var home_row = home_msg_list.get_row(rows.id(row));

    process_row(row);
    process_row(home_row);
};

exports.toggle_collapse = function (message) {
    if (message.is_me_message) {
        // Disabled temporarily because /me messages don't have a
        // styling for collapsing /me messages (they only recently
        // added multi-line support).  See also popovers.js.
        return;
    }

    // This function implements a multi-way toggle, to try to do what
    // the user wants for messages:
    //
    // * If the message is currently showing any [More] link, either
    //   because it was previously condensed or collapsed, fully display it.
    // * If the message is fully visible, either because it's too short to
    //   condense or because it's already uncondensed, collapse it

    var row = current_msg_list.get_row(message.id);
    if (!row) {
        return;
    }

    var content = row.find(".message_content");
    var is_condensable = content.hasClass("could-be-condensed");
    var is_condensed = content.hasClass("condensed");
    if (message.collapsed) {
        if (is_condensable) {
            message.condensed = true;
            content.addClass("condensed");
            exports.show_message_expander(row);
            row.find(".message_condenser").hide();
        }
        condense.uncollapse(row);
    } else {
        if (is_condensed) {
            message.condensed = false;
            content.removeClass("condensed");
            exports.hide_message_expander(row);
            row.find(".message_condenser").show();
        } else {
            condense.collapse(row);
        }
    }
{'fraction_non_alphanumeric': 0.11830853929951973, 'fraction_numerical': 0.0, 'mean_word_length': 2.627017841971113, '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': '731579', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2462, 'n_words': 659}