collapse.js 1.33 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8
/*global gettext*/
(function($) {
    'use strict';
    $(document).ready(function() {
        // Add anchor tag for Show/Hide link
        $("fieldset.collapse").each(function(i, elem) {
            // Don't hide if fields in this fieldset have errors
            if ($(elem).find("div.errors").length === 0) {
9 10 11 12
                $(elem).addClass("collapsed")
                    .find("h2")
                    .first()
                    .append(' (<a id="fieldsetcollapser' +
Administrator committed
13 14 15 16 17 18
                    i + '" class="collapse-toggle" href="#">' + gettext("Show") +
                    '</a>)');
            }
        });
        // Add toggle to anchor tag
        $("fieldset.collapse a.collapse-toggle").click(function(ev) {
19 20
            if ($(this).closest("fieldset")
                .hasClass("collapsed")) {
Administrator committed
21
                // Show
22 23 24 25
                $(this).text(gettext("Hide"))
                    .closest("fieldset")
                    .removeClass("collapsed")
                    .trigger("show.fieldset", [$(this).attr("id")]);
Administrator committed
26 27
            } else {
                // Hide
28 29 30 31
                $(this).text(gettext("Show"))
                    .closest("fieldset")
                    .addClass("collapsed")
                    .trigger("hide.fieldset", [$(this).attr("id")]);
Administrator committed
32
            }
33

Administrator committed
34 35 36 37
            return false;
        });
    });
})(django.jQuery);