Jump to content

User:Mike Dillon/Scripts/edittools.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* <pre><nowiki> */

// XXX: This should be in a regular CSS file
(function () {
    var head = document.getElementsByTagName("head")[0];
    if (head) {
        var style = document.createElement("style");
        style.setAttribute("type", "text/css")
        style.appendChild(document.createTextNode(
            "div#edittools_main {"  
            "    font-size: inherit;"  
            "    border-bottom: 1px solid #aaa;"  
            "}"  
            "div.edittools-group {"  
            "    font-size: smaller;"  
            "}"
        ));
        head.appendChild(style);
    }
})();

function EdittoolsGroup(name, label) {
    this.label = label || name;

    this.elements = [];

    function addElement(group, type, e) {
        group.elements[group.elements.length] = [ type, e ];
        return group;
    }

    this.addLabel = function (label) {
        var b = document.createElement("b");
        b.appendChild(document.createTextNode(label));
        return addElement(this, "label", b);
    };

    this.addInsert = function (open, close, sample) {
        if (!open) open = '';
        if (!close) close = '';
        if (!sample) sample = '';

        var display = open   close;
        if (display.match(/^[\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]/)) {
            display = "\u034F"   display;
        }

        var a = document.createElement("a");
        a.setAttribute("href", "#");
        a.setAttribute("title", "Insert text: "   display);
        a.appendChild(document.createTextNode(display));
        a.onclick = function () {
            insertTags(open, close, sample);
            return false;
        };
        return addElement(this, "insert", a);
    };

    this.addInsertList = function (chars) {
        if (!chars) return;
        var list = chars.split(/\s /);
        for (var n in list) {
            if (list[n] != '') this.addInsert(list[n]);
        }
        return this;
    };

    this.addGap = function () {
        return addElement(this, "gap", document.createTextNode("\xA0"));
    };

    this.addBullet = function () {
        return addElement(this, "bullet", document.createTextNode("\xA0•\xA0"));
    };

    this.addWikiLink = function (page, label) {
        var parts = page.split(/#/, 2);
        var url = wgArticlePath.replace(/\$1/, encodeURIComponent(parts[0]));
        if (parts[1]) {
            url  = "#"   encodeURIComponent(parts[1]).replace(/%/g, ".");
        }

        var a = document.createElement("a");
        a.setAttribute("href", url);
        a.setAttribute("title", page.replace(/_/g, " "));
        a.appendChild(document.createTextNode(label || page));

        var span = document.createElement("span");
        span.appendChild(document.createTextNode("("));
        span.appendChild(a);
        span.appendChild(document.createTextNode(")"));

        return addElement(this, "link", span);
    };
}

var Edittools = new function () {
    this.style = "full";
    this.clear = true;
    this.groups = {};

    this.createGroup = function (name, label) {
        if (!this.groups[name]) {
            this.groups[name] = new EdittoolsGroup(name, label);
        }
        return this.groups[name];
    };

    this.getGroup = function (name) {
        return this.groups[name];
    };

    this.removeGroup = function (name) {
        delete this.groups[name];
    };

    this.removeAllGroups = function () {
        this.groups = {};
    };

    this.install = function (container) {
        if (container && container.nodeType == null) {
            container = document.getElementById(container);
        }
        if (!container) return;

        if (this.clear) {
            while (container.firstChild) container.removeChild(container.firstChild);
        }

        var groupEls = {};

        if (this.style == 'compact') {
            var select = document.createElement("select");
            select.onchange = function () {
                var sel = this.options[this.selectedIndex].value;
                for (var name in groupEls) {
                    groupEls[name].style.display = (name == sel) ? "inline" : "none";
                }
            };
            container.appendChild(select);
            container.appendChild(document.createTextNode(" "));
        }

        var first = true;
        var elType = (this.style == 'compact') ? "span" : "div";
        for (var name in this.groups) {
            var group = this.groups[name];

            if (this.style == 'compact') {
                var opt = document.createElement("option");
                opt.setAttribute("value", name);
                opt.appendChild(document.createTextNode(group.label));
                select.appendChild(opt);
            }

            var groupEl = document.createElement(elType);
            groupEl.setAttribute("id", "edittools_"   name);
            groupEl.setAttribute("class", "edittools-group");
            if (!first && this.style == 'compact') {
                groupEl.setAttribute("style", "display: none");
            }

            for (var n in group.elements) {
                var element = group.elements[n];
                if (element[0] == 'label' && this.style == 'compact') continue;
                if (groupEl.childNodes.length) {
                    groupEl.appendChild(document.createTextNode(" "));
                }
                groupEl.appendChild(element[1]);
            }
            container.appendChild(groupEl);
            groupEls[name] = groupEl;
            first = false;
        }
    };
};

with (Edittools) {
    // Main group
    var main = createGroup("main", "Standard");
    with (main) {
        addLabel("Insert:");
        addInsertList("– — … ° ≈ ≠ ≤ ≥ ± − × ÷ ← → · §").addGap();
        addLabel("Sign your username:");
        addInsert("~~"   "~~");
    }

    // Wiki markup
    var wikimarkup = createGroup("wikimarkup", "Wiki markup");
    with (wikimarkup) {
        addLabel("Wiki markup:");
        addInsert("{{", "}}").addGap();
        addInsert("|").addGap();
        addInsert("[", "]").addGap();
        addInsert("[[", "]]").addGap();
        addInsert("[[Category:", "]]").addGap();
        addInsert("#REDIRECT[[", "]]").addGap();
        addInsert("<s>", "</s>").addGap();
        addInsert("<sup>", "</sup>").addGap();
        addInsert("<sub>", "</sub>").addGap();
        addInsert("<code>", "</code>").addGap();
        addInsert("<blockquote>", "</blockquote>").addGap();
        addInsert("<ref>", "</ref>").addGap();
        addInsert("{{Reflist}}").addGap();
        addInsert("<references/>").addGap();
        addInsert("<includeonly>", "</includeonly>").addGap();
        addInsert("<noinclude>", "</noinclude>").addGap();
        addInsert("{{DEFAULTSORT:", "}}").addGap();
        addInsert("<no"   "wiki>", "</no"   "wiki>").addGap();
        addInsert("<!-- ", " -->").addGap();
        addInsert("<span class=\"plainlinks\">", "</span>");
        addBullet();
        addWikiLink("Wikipedia:Template messages", "templates");
    }

    // Symbols
    var symbols = createGroup("symbols", "Symbols");
    with (symbols) {
        addLabel("Symbols:");
        addInsertList("~ | ¡ ¿ † ‡ ↔ ↑ ↓ • ¶").addGap();
        addInsertList("# ¹ ² ³ ½ ⅓ ⅔ ¼ ¾ ⅛ ⅜ ⅝ ⅞ ∞").addGap();
        addInsertList("‘ “ ’ ”").addGap();
        addInsertList("¤ ₳ ฿ ₵ ¢ ₡ ₢ $ ₫ ₯ € ₠ ₣ ƒ ₴ ₭ ₤ ℳ ₥ ₦ ₧ ₰");
        addInsertList("£ ៛ ₨ ₪ ৳ ₮ ₩ ¥").addGap();
        addInsertList("♠ ♣ ♥ ♦");
    }

    // Characters
    var characters = createGroup("characters", "Characters");
    with (characters) {
        addLabel("Characters:");
        addInsertList("Á á Ć ć É é Í í Ĺ ĺ Ń ń Ó ó Ŕ ŕ Ś ś Ú ú Ý ý Ź ź").addGap();
        addInsertList("À à È è Ì ì Ò ò Ù ù").addGap();
        addInsertList("Â â Ĉ ĉ Ê ê Ĝ ĝ Ĥ ĥ Î î Ĵ ĵ Ô ô Ŝ ŝ Û û Ŵ ŵ Ŷ ŷ").addGap();
        addInsertList("Ä ä Ë ë Ï ï Ö ö Ü ü Ÿ ÿ").addGap();
        addInsertList("ß").addGap();
        addInsertList("Ã ã Ẽ ẽ Ĩ ĩ Ñ ñ Õ õ Ũ ũ Ỹ ỹ").addGap();
        addInsertList("Ç ç Ģ ģ Ķ ķ Ļ ļ Ņ ņ Ŗ ŗ Ş ş Ţ ţ").addGap();
        addInsertList("Đ đ").addGap();
        addInsertList("Ů ů").addGap();
        addInsertList("Ǎ ǎ Č č Ď ď Ě ě Ǐ ǐ Ľ ľ Ň ň Ǒ ǒ Ř ř Š š Ť ť Ǔ ǔ Ž ž").addGap();
        addInsertList("Ā ā Ē ē Ī ī Ō ō Ū ū Ȳ ȳ Ǣ ǣ").addGap();
        addInsertList("ǖ ǘ ǚ ǜ").addGap();
        addInsertList("Ă ă Ĕ ĕ Ğ ğ Ĭ ĭ Ŏ ŏ Ŭ ŭ").addGap();
        addInsertList("Ċ ċ Ė ė Ġ ġ İ ı Ż ż").addGap();
        addInsertList("Ą ą Ę ę Į į Ǫ ǫ Ų ų").addGap();
        addInsertList("Ḍ ḍ Ḥ ḥ Ḷ ḷ Ḹ ḹ Ṃ ṃ Ṇ ṇ Ṛ ṛ Ṝ ṝ Ṣ ṣ Ṭ ṭ").addGap();
        addInsertList("Ł ł").addGap();
        addInsertList("Ő ő Ű ű").addGap();
        addInsertList("Ŀ ŀ").addGap();
        addInsertList("Ħ ħ").addGap();
        addInsertList("Ð ð Þ þ").addGap();
        addInsertList("Œ œ").addGap();
        addInsertList("Æ æ Ø ø Å å").addGap();
        addInsertList("Ə ə");
        addBullet();
        addInsert("{{Unicode|", "}}");
    }

    // Greek
    var greek = createGroup("greek", "Greek");
    with (greek) {
        addLabel("Greek:");
        addInsertList("Ά ά Έ έ Ή ή Ί ί Ό ό Ύ ύ Ώ ώ").addGap();
        addInsertList("Α α Β β Γ γ Δ δ").addGap();
        addInsertList("Ε ε Ζ ζ Η η Θ θ").addGap();
        addInsertList("Ι ι Κ κ Λ λ Μ μ").addGap();
        addInsertList("Ν ν Ξ ξ Ο ο Π π").addGap();
        addInsertList("Ρ ρ Σ σ ς Τ τ Υ υ").addGap();
        addInsertList("Φ φ Χ χ Ψ ψ Ω ω").addGap();
        addInsertList("");
        addBullet();
        addInsert("{{Polytonic|", "}}");
        addBullet();
        addWikiLink("Polytonic_orthography#Examples_of_polytonic_characters", "polytonic");
    }

    // Cyrillic
    var cyrillic = createGroup("cyrillic", "Cyrillic");
    with (cyrillic) {
        addLabel("Cyrillic:");
        addInsertList("А а Б б В в Г г").addGap();
        addInsertList("Ґ ґ Ѓ ѓ Д д Ђ ђ").addGap();
        addInsertList("Е е Ё ё Є є Ж ж").addGap();
        addInsertList("З з Ѕ ѕ И и І і").addGap();
        addInsertList("Ї ї Й й Ј ј К к").addGap();
        addInsertList("Ќ ќ Л л Љ љ М м").addGap();
        addInsertList("Н н Њ њ О о П п").addGap();
        addInsertList("Р р С с Т т Ћ ћ").addGap();
        addInsertList("У у Ў ў Ф ф Х х").addGap();
        addInsertList("Ц ц Ч ч Џ џ Ш ш").addGap();
        addInsertList("Щ щ Ъ ъ Ы ы Ь ь").addGap();
        addInsertList("Э э Ю ю Я я");
    }

    // IPA
    var ipa = createGroup("ipa", "IPA");
    with (ipa) {
        addLabel("IPA:");
        addInsertList("t̪ d̪ ʈ ɖ ɟ ɡ ɢ ʡ ʔ").addGap();
        addInsertList("ɸ ʃ ʒ ɕ ʑ ʂ ʐ ʝ ɣ ʁ ʕ ʜ ʢ ɦ").addGap();
        addInsertList("ɱ ɳ ɲ ŋ ɴ").addGap();
        addInsertList("ʋ ɹ ɻ ɰ").addGap();
        addInsertList("ʙ ʀ ɾ ɽ").addGap();
        addInsertList("ɫ ɬ ɮ ɺ ɭ ʎ ʟ").addGap();
        addInsertList("ɥ ʍ ɧ").addGap();
        addInsertList("ɓ ɗ ʄ ɠ ʛ").addGap();
        addInsertList("ʘ ǀ ǃ ǂ ǁ").addGap();
        addInsertList("ɨ ʉ ɯ").addGap();
        addInsertList("ɪ ʏ ʊ").addGap();
        addInsertList("ɘ ɵ ɤ").addGap();
        addInsertList("ə ɚ").addGap();
        addInsertList("ɛ ɜ ɝ ɞ ʌ ɔ").addGap();
        addInsertList("ɐ ɶ ɑ ɒ").addGap();
        addInsertList("ʰ ʷ ʲ ˠ ˤ ⁿ ˡ").addGap();
        addInsertList("ˈ ˌ ː ˑ  ̪");
        addBullet();
        addInsert("{{IPA|", "}}");
    }
}

addOnloadHook(function () {
    Edittools.install("editpage-specialchars");
});

/* </nowiki></pre> */