User:Nikki/AddTermboxLanguage.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
* This script makes it easier to add/edit labels to an item in a language other
* than the ones shown by default.
* It adds an input field below the termbox. Enter the language code of the
* language you would like to add/edit and press enter. If the language code is
* supported, it will add an entry for that language to the termbox. If it is
* not supported, it will show an error message.
*
* To use it, add the following to your common.js:
* mw.loader.load("//www.wikidata.org/w/index.php?title=User:Nikki/AddTermboxLanguage.js&action=raw&ctype=text/javascript");
*
* @license CC0-1.0
*/
(function () {
"use strict";
if (mw.config.get("wgNamespaceNumber") !== 0 && mw.config.get("wgNamespaceNumber") !== 120) {
// Not a supported entity type
return;
}
function addTermboxLanguage() {
const translations = {
"en": {
"termbox-addlanguage-placeholder": "add language",
"termbox-addlanguage-unsupported": "The language code \"$1\" is not supported.",
"termbox-addlanguage-alreadyshown": "Labels for the language code \"$1\" are already shown."
},
"de": {
"termbox-addlanguage-placeholder": "Sprache hinzufügen",
"termbox-addlanguage-unsupported": "Der Sprachcode „$1“ ist nicht unterstützt.",
"termbox-addlanguage-alreadyshown": "Bezeichnungen für den Sprachcode „$1“ werden schon gezeigt."
},
"nb": {
"termbox-addlanguage-placeholder": "legg til språk",
"termbox-addlanguage-unsupported": "Språk-koden \"$1\" er ikke støttet.",
"termbox-addlanguage-alreadyshown": "Etiketter for språk-koden \"$1\" vises allerede."
},
};
$.i18n().load(translations);
mw.util.addCSS(`
.termbox-addlanguage {
float: right;
}
.rtl .termbox-addlanguage {
float: left;
}
.termbox-addlanguage input {
padding: 0.25em 0.5em;
margin: 0.25em 0;
}
`);
function addLanguages(text) {
let langs = text.split(/,/);
let remainder = [];
for (let lang of langs) {
if (!addLanguage(lang)) {
// Language couldn't be added because it's not supported
remainder.push(lang);
}
}
// Update the input field to remove all codes that are now shown
let newtext = remainder.join(",");
if (newtext !== text) {
input.value = newtext;
}
}
function addLanguage(lang) {
lang = lang.trim().toLowerCase();
// Check whether the language code is in the list of language codes allowed for term languages
if (!wb.WikibaseContentLanguages.getTermLanguages()._languageCodes.includes(lang)) {
mw.notify($.i18n("termbox-addlanguage-unsupported", lang), { type: "error" });
return false;
}
// Check that the language isn't already shown
if (self.options.userLanguages.includes(lang)) {
mw.notify($.i18n("termbox-addlanguage-alreadyshown", lang), { type: "warn" });
return true;
}
// This determines which languages are not hidden when toggling "All entered languages"
self.options.userLanguages.push(lang);
// Add the row for this language
let $item = listview.addItem(self._getValueForLanguage(lang));
mw.hook("termbox-language-added").fire(lang);
if (self.isInEditMode()) {
lia.liInstance($item).startEditing();
}
return true;
}
let self = $(".wikibase-entitytermsforlanguagelistview").data("entitytermsforlanguagelistview");
let listview = $(".wikibase-entitytermsforlanguagelistview").data("entitytermsforlanguagelistview").$listview.data("listview");
let lia = listview.listItemAdapter();
let input = document.createElement("input");
input.placeholder = $.i18n("termbox-addlanguage-placeholder");
input.onkeydown = function (event) {
if (event.key === "Enter") {
event.stopPropagation();
event.preventDefault();
addLanguages(input.value);
}
};
let span = document.createElement("span");
span.classList.add("termbox-addlanguage");
span.insertAdjacentElement("afterbegin", input);
document.querySelector(".wikibase-entitytermsforlanguagelistview").insertAdjacentElement("afterend", span);
}
mw.hook('wikibase.entityPage.entityView.rendered').add(addTermboxLanguage);
})();