• Welcome to the Kancolle Wiki!
  • If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord

Difference between revisions of "MediaWiki:DetailToggle.js"

From Kancolle Wiki
Jump to navigation Jump to search
(Support multiple .toggle elements)
(Add .mw-collapsible togglers)
Line 4: Line 4:
  
 
     $(document).ready(function() {
 
     $(document).ready(function() {
 +
 +
        // custom hidable content and togglers supported by local storage
  
 
         if (typeof(Storage) === "undefined") {
 
         if (typeof(Storage) === "undefined") {
Line 58: Line 60:
  
 
         function updateContent() {
 
         function updateContent() {
 +
 
             $(".toggle").each(function() {
 
             $(".toggle").each(function() {
 
                 var toggle_element = $(this);
 
                 var toggle_element = $(this);
Line 67: Line 70:
 
                 });
 
                 });
 
             });
 
             });
 +
 +
            // .mw-collapsible togglers
 +
 +
            $(".mw-collapsible-expand-all").each(function() {
 +
                var toggle_element = $(this);
 +
                toggle_element.off("click");
 +
                toggle_element.click(function() {
 +
                    $(".mw-collapsible-toggle.mw-collapsible-toggle-collapsed").click();
 +
                });
 +
            });
 +
 +
            $(".mw-collapsible-collapse-all").each(function() {
 +
                var toggle_element = $(this);
 +
                toggle_element.off("click");
 +
                toggle_element.click(function() {
 +
                    $(".mw-collapsible-toggle.mw-collapsible-toggle-expanded").click();
 +
                });
 +
            });
 +
 +
            $(".mw-collapsible-toggle-all").each(function() {
 +
                var toggle_element = $(this);
 +
                toggle_element.off("click");
 +
                toggle_element.click(function() {
 +
                    $(".mw-collapsible-toggle").click();
 +
                });
 +
            });
 +
 
         }
 
         }
  

Revision as of 16:54, 19 July 2016

(function(mw, $) {

    "use strict";

    $(document).ready(function() {

        // custom hidable content and togglers supported by local storage

        if (typeof(Storage) === "undefined") {
            return;
        }

        var ToggleStorage = function(property, property_default) {
            if (!localStorage[property]) {
                localStorage[property] = property_default;
            }
            return {
                toggle: function() {
                    localStorage[property] = localStorage[property] === "show" ? "hide" : "show";
                },
                on: function() {
                    return localStorage[property] === "show";
                }
            };
        }

        function update(toggle_element, toggle) {
            var property = toggle_element.data("target") || "default",
                property_anti = toggle_element.data("anti-target"),
                property_default = toggle_element.data("default") || "show",
                target = "toggle-target-" + property,
                target_class = "." + target,
                anti_target_class,
                toggleStorage = ToggleStorage(target, property_default);
            if (property_anti) {
                anti_target_class = ".toggle-anti-target-" + property;
            }
            if (toggle) {
                toggleStorage.toggle();
            }
            var toggle_show_selector = '.toggle[data-target="' + property + '"] .toggle-show',
                toggle_hide_selector = '.toggle[data-target="' + property + '"] .toggle-hide';
            if (toggleStorage.on()) {
                $(toggle_show_selector).show();
                $(toggle_hide_selector).hide();
                $(target_class).show();
                if (anti_target_class) {
                    $(anti_target_class).hide();
                }
            } else {
                $(toggle_show_selector).hide();
                $(toggle_hide_selector).show();
                $(target_class).hide();
                if (anti_target_class) {
                    $(anti_target_class).show();
                }
            }
            $(window).trigger("WideTablesScan");
        }

        function updateContent() {

            $(".toggle").each(function() {
                var toggle_element = $(this);
                toggle_element.show();
                update(toggle_element);
                toggle_element.off("click");
                toggle_element.click(function() {
                    update(toggle_element, true);
                });
            });

            // .mw-collapsible togglers

            $(".mw-collapsible-expand-all").each(function() {
                var toggle_element = $(this);
                toggle_element.off("click");
                toggle_element.click(function() {
                    $(".mw-collapsible-toggle.mw-collapsible-toggle-collapsed").click();
                });
            });

            $(".mw-collapsible-collapse-all").each(function() {
                var toggle_element = $(this);
                toggle_element.off("click");
                toggle_element.click(function() {
                    $(".mw-collapsible-toggle.mw-collapsible-toggle-expanded").click();
                });
            });

            $(".mw-collapsible-toggle-all").each(function() {
                var toggle_element = $(this);
                toggle_element.off("click");
                toggle_element.click(function() {
                    $(".mw-collapsible-toggle").click();
                });
            });

        }

        updateContent();
        mw.hook("wikipage.content").add(updateContent);

    });

}(mediaWiki, jQuery));