User:Nikki/LexemeP31Menu.js

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(function () {
	"use strict";

	var p31 = {
		"Q1084": {
			"Q604984": "singulare tantum",
			"Q138246": "plurale tantum",
			"Q7884789": "toponym",
			"Q3109261": "linguonym",
		},
		"Q147276": {
			"Q604984": "singulare tantum",
			"Q138246": "plurale tantum",
			"Q7884789": "toponym",
		},
		"Q24905": {
			"Q4129241": "strong verb",
			"Q60655": "weak verb",
			"Q70235": "irregular verb",
			"Q3254028": "separable verb",
			"Q105714288": "inseparable verb",
		},
		"Q34698": {
			"Q58691134": "indeclinable adjective (no case endings)",
			"Q332375": "absolute adjective (no comparative/superlative)",
		},
		"Q36224": {
			"Q97441379": "neopronoun",
		},
		"Q1050744": {
			"Q97441379": "neopronoun",
		},
		"Q36484": { // conjunction
			"Q28833099": "coordinating conjunction",
			"Q11655558": "subordinating conjunction",
			"Q1167104": "conjunctive adverb",
		},
	};
	let map = {
		"Q24905": [ // verb 
			{ "label": "sein", "p": "P5401", "v": "L1761", "type": "lexeme" },
			{ "label": "haben", "p": "P5401", "v": "L4179", "type": "lexeme" },
		],
		"Q102047": [ // suffix
			{ "label": "noun", "p": "P5923", "v": "Q1084", "type": "item" },
			{ "label": "verb", "p": "P5923", "v": "Q24905", "type": "item" },
			{ "label": "adjective", "p": "P5923", "v": "Q34698", "type": "item" },
			{ "label": "adverb", "p": "P5923", "v": "Q380057", "type": "item" },
		],
	};

	function add_statement(v, eid, p, type) {
		var mw_api = new mw.Api();
		mw_api.postWithEditToken({
			action: "wbcreateclaim",
			entity: eid,
			property: p,
			snaktype: "value",
			value: JSON.stringify({ "entity-type": type, "id": v })
		}).done(function (data) {
			location.reload();
		}).fail(function (err) {
			alert("Failed: " + err);
		});
	}

	function add_menu_item(p31, label, eid, p) {
		$(mw.util.addPortletLink("p-lex-p31", "#", label, "ca-p31-" + p31)).click(function (ev) {
			ev.preventDefault();
			add_statement(p31, eid, p);
		});
	}

	function create_menu_container() {
		if (document.getElementById("p-lex-p31"))
			return;

		$("#right-navigation").prepend("\
			<nav id=\"p-lex-p31\" class=\"mw-portlet vector-menu-dropdown vector-menu-dropdown-noicon\">\
			<span class=\"vector-menu-heading\">P31</span>\
			<div class=\"vector-menu-content\">\
			<ul></ul>\
			</div>\
			</nav>\
		");
	}

	mw.hook("wikibase.entityPage.entityLoaded").add(function(e){
		if (!e.language)
			return; // only supported on lexemes

		if (p31.hasOwnProperty(e.lexicalCategory)) {
			create_menu_container();

			var list = p31[e.lexicalCategory];
			for (var qid of Object.keys(list)) {
				add_menu_item(qid, list[qid], e.id, "P31", "item");
			}
		}

		if (map.hasOwnProperty(e.lexicalCategory)) {
			create_menu_container();

			var list = map[e.lexicalCategory];
			for (var st of list) {
				add_menu_item(st.v, st.label, e.id, st.p, st.type);
			}
		}

	});

})();