User:Dixtosa/NewerEntryCreator.js

//

window.NewerEntryCreatorData = {	generate: function(langcode, data) {		var partOfSpeechCollection = data.partOfSpeechCollection; var pronunciation = data.pronunciation; var sectionIndentation = 3; var hasMultipleEtymologies = partOfSpeechCollection.filter(item => item.etymologyNumber > 1).length > 0; if (hasMultipleEtymologies) indentation++; var wikitext = ""; wikitext += "==getByCode=="; if (pronunciation && pronunciation.wikitext) {			wikitext += "\n"; wikitext += "\n"; wikitext += "=".repeat(sectionIndentation) + "Pronunciation" + "=".repeat(sectionIndentation); wikitext += "\n"; wikitext += pronunciation.wikitext; }		for(var i = 0; i < partOfSpeechCollection.length; i++) {			var section = partOfSpeechCollection[i]; wikitext += "\n"; wikitext += "\n"; wikitext += "=".repeat(sectionIndentation) + section.sectionName + "=".repeat(sectionIndentation); wikitext += "\n"; wikitext += section.headword; wikitext += "\n"; for(var definition_index = 0; definition_index < section.definitions.length; definition_index++) {				wikitext += "\n"; wikitext += section.definitions[definition_index].wikitext; if (section.definitions[definition_index].exampleWikitext) {									wikitext += "\n"; wikitext += section.definitions[definition_index].exampleWikitext; }			}			for(var index = 0; index < section.subsections.length; index++) {				var subsection = section.subsections[index]; wikitext += "\n"; wikitext += "\n"; wikitext += "=".repeat(sectionIndentation+1) + subsection.title + "=".repeat(sectionIndentation+1); wikitext += "\n"; wikitext += subsection.wikitext; }		}		return wikitext; } };

function isTransliterationQuestionApplicable(p) {	if (p.languageConfiguration === undefined) return true; //by default all langauges need manual transliteration if (p.languageConfiguration.transliteration === undefined) return true; return p.languageConfiguration.transliteration.needsTransliteration && !p.languageConfiguration.transliteration.isAutomaticallyTransliterated; }

NewerEntryCreatorData.all = {	'all':{ questions: { definitions: [ {question: "definition #1", parameterName : "definition1", value: '', sortkey: 100, group: "Definition 1"}, {question: "add example #1", parameterName : "addExample1", choices: ['yes', 'no'], value: 'no', sortkey: 101, group: "Definition 1"}, {question: "example #1", parameterName : "example1", value: '', shouldShow: p => p.addExample1 == "yes", sortkey: 102, group: "Definition 1"}, {question: "example transliteration #1", parameterName : "example_transliteration1", shouldShow: p => p.addExample1 == "yes" && isTransliterationQuestionApplicable(p), sortkey: 103, group: "Definition 1"}, {question: "example translation #1", parameterName : "example_translation1", value: '', shouldShow: p => p.addExample1 == "yes", sortkey: 104, group: "Definition 1"}, {question: "definition #2", parameterName : "definition2", value: '', shouldShow: (p) => p.definition1 != "" && p.definition1 !== undefined, sortkey: 110, group: "Definition 2"}, {question: "add example #2", parameterName : "addExample2", choices: ['yes', 'no'], value: 'no', shouldShow: (p) => p.definition1 != "" && p.definition1 !== undefined, sortkey: 111, group: "Definition 2"}, {question: "example #2", parameterName : "example2", value: '', shouldShow: p => p.addExample2 == "yes", sortkey: 112, group: "Definition 2"}, {question: "example transliteration #2", parameterName : "example_transliteration2", shouldShow: p => p.addExample2 == "yes" && isTransliterationQuestionApplicable(p), sortkey: 113, group: "Definition 2"}, {question: "example translation #2", parameterName : "example_translation2", value: '', shouldShow: p => p.addExample2 == "yes", sortkey: 114, group: "Definition 2"}, {question: "definition #3", parameterName : "definition3", value: '', shouldShow: (p) => p.definition2 != "" && p.definition2 !== undefined, sortkey: 120, group: "Definition 3"}, {question: "add example #3", parameterName : "addExample3", choices: ['yes', 'no'], value: 'no', shouldShow: (p) => p.definition2 != "" && p.definition2 !== undefined, sortkey: 121, group: "Definition 3"}, {question: "example #3", parameterName : "example3", value: '', shouldShow: p => p.addExample3 == "yes", sortkey: 122, group: "Definition 3"}, {question: "example transliteration #3", parameterName : "example_transliteration3", shouldShow: p => p.addExample3 == "yes" && isTransliterationQuestionApplicable(p), sortkey: 123, group: "Definition 3"}, {question: "example translation #3", parameterName : "example_translation3", value: '', shouldShow: p => p.addExample3 == "yes", sortkey: 124, group: "Definition 3"}, ]		}	} };

NewerEntryCreatorData.ka = {	transliteration: { needsTransliteration: true, isAutomaticallyTransliterated: true },	pluralizeNoun: function(word) { if (word.slice(word.length - 1) == "ე") return word + "ები"; else return word.substr(0, word.length - 1) + "ები"; },	"Pronunciation": function { return {wikitext: "* \n* "}; },	"Adjective": { questions : [ {question: "comparable?", choices: ["yes", "no"], value: "yes", parameterName : "comparable"}, ],		generateHeadword: function(parameters) {			if (parameters.comparable == "yes") return ""; else return ""; }	},	"Verbal noun": { enableDefinitionQuestions: false, questions : [ {question: "perfectness", choices: ["pf", "impf"], parameterName : "perfectness"}, {question: "comma separated lemmas", parameterName : "verbalNounOf"}, ],		generateHeadword: function(parameters) {			if (parameters.perfectness) return ""; else return ""; },		generateDefinitions: function(parameters) {			if (parameters.verbalNounOf && parameters.verbalNounOf != "") {				var lemmas = parameters.verbalNounOf.split(","); return lemmas.map(lemma => {return {wikitext: "# "}}); }			return []; },		generateSubsections: function(parameters) {			var inflection = { title: "Inflection", wikitext: "", };			return [inflection]; }	},	"Proper noun": { generateHeadword: function(parameters) {			return ""; }	},	"Adverb": { generateHeadword: function(parameters) {			return ""; }	},	"Noun": { questions : [ {question: "countable?", value: 'yes', choices: ['yes', 'sometimes', 'no'], parameterName : "countable"}, {question: "Is this correct?", value: (p) => NewerEntryCreatorData.ka.pluralizeNoun(p.word), parameterName : "plural", shouldShow: (p) => p.countable !== 'no'}, ],		generateHeadword: function(parameters) {			if (parameters.countable == 'yes') return ""; else if (parameters.countable == 'sometimes') return ""; else return ""; },		generateSubsections: function(parameters) {			var inflection = { title: "Inflection", wikitext: "", };			if (parameters.countable == 'yes' || (parameters.countable == 'sometimes')) inflection.wikitext = ""; else inflection.wikitext = ""; return [inflection]; }	},	"Verb": { questions : [ {question: "Is it present or future?", choices : ["present", "future"], parameterName : "tense"}, ],		generateHeadword: function(parameters) {			if (parameters.tense) return ""; else return ""; }	},	"Verb form": { enableDefinitionQuestions: false, questions : [ {question: "lemma", parameterName : "lemma"}, {question: "person?", choices : [ {value:"1sg", text:"I"}, {value:"2sg", text:"you (sg)"}, {value:"3sg", text:"it"}, {value:"1pl", text:"we"}, {value:"2pl", text:"you (pl)"}, {value:"3pl", text:"they"} ], parameterName : "verb_form_person"}, {question: "which form?", type: "dropdown", choices : [ {value:"1", text:"present. რას შვრება?"}, {value:"2", text:"imperfect. რას შვრებოდა?"}, {value:"3", text:"present subjunctive. რას შვრებოდეს?"}, {value:"4", text:"future. რას იზამს?"}, {value:"5", text:"conditional. რას იზამდა?"}, {value:"6", text:"future subjunctive. რას იზამდეს?"}, {value:"7", text:"aorist. რა ქნა?"}, {value:"8", text:"optative. რა ქნას?"}, {value:"9", text:"perfect. რა უქნია?"}, {value:"10", text:"pluperfect. რა ექნა?"}, {value:"11", text:"perfect subjunctive. რა ექნას?"}, ], parameterName : "verb_form_id"}, ],		generateDefinitions: function(parameters) {			if (				parameters.verb_form_id && parameters.verb_form_id != "" 				&& parameters.verb_form_person && parameters.verb_form_person != ""				&& parameters.lemma && parameters.lemma != "") {				var person = parameters.verb_form_person.substr(0, 1); var plurality = parameters.verb_form_person.substr(1, 2); var definition = "# "; return [{wikitext:definition}]; }			return []; },	} };

NewerEntryCreatorProcess = { acceptablePartsOfSpeech: { 'Adjective': {headword: 'adjective', sectionName: 'Adjective'}, 'Verb': {headword: 'verb', sectionName: 'Verb'}, 'Noun': {headword: 'noun', sectionName: 'Noun'}, 'Pronoun': {headword: 'pronoun', sectionName: 'Pronoun'}, 'Determiner': {headword: 'determiner', sectionName: 'Determiner'}, 'Adverb': {headword: 'adverb', sectionName: 'Adverb'}, 'Proper noun': {headword:'proper noun', sectionName: 'Proper noun'}, 'Verbal noun': {headword:'verbal noun', sectionName: 'Verbal noun'}, 'Conjunction': {headword: 'conjunction', sectionName: 'Conjunction'}, 'Interjection': {headword: 'interjection', sectionName: 'Interjection'}, 'Particle': {headword: 'particle', sectionName: 'Particle'}, 'Noun form': {headword: 'noun form', sectionName: 'Noun'}, 'Verb form': {headword: 'verb form', sectionName: 'Verb'}, },	generateDefinitions: function(parameters) {		var defs = []; def1 = {wikitext: parameters.definition1 != '' && parameters.definition1 !== undefined ? '# ' + parameters.definition1 : ""}; def2 = {wikitext: parameters.definition2 != '' && parameters.definition2 !== undefined ? '# ' + parameters.definition2 : ""}; def3 = {wikitext: parameters.definition3 != '' && parameters.definition3 !== undefined ? '# ' + parameters.definition3 : ""}; if (parameters.addExample1 == "yes") {			def1.exampleWikitext = "#: "; }		if (parameters.addExample2 == "yes") {			def2.exampleWikitext = "#: "; }		if (parameters.addExample3 == "yes") {			def3.exampleWikitext = "#: "; }		if (def1.wikitext) defs.push(def1); if (def2.wikitext) defs.push(def2); if (def3.wikitext) defs.push(def3); return defs; },	generate: function(langcode, partOfSpeechCollectionParameters) {		var partOfSpeechCollection = []; for(var i = 0; i < partOfSpeechCollectionParameters.length; i++) {			var parameters = partOfSpeechCollectionParameters[i]; if (this.acceptablePartsOfSpeech[parameters.partOfSpeech] === undefined) continue; var langPosSpecificFuncs = NewerEntryCreatorData[parameters.langcode][parameters.partOfSpeech]; var headword = ''; if (langPosSpecificFuncs !== undefined && langPosSpecificFuncs.generateHeadword) headword = langPosSpecificFuncs.generateHeadword(parameters); var definitions = this.generateDefinitions(parameters); if (langPosSpecificFuncs !== undefined && langPosSpecificFuncs.generateDefinitions) definitions = langPosSpecificFuncs.generateDefinitions(parameters); var subsections = []; if (langPosSpecificFuncs !== undefined && langPosSpecificFuncs.generateSubsections) subsections = langPosSpecificFuncs.generateSubsections(parameters); partOfSpeechCollection.push({				sectionName: this.acceptablePartsOfSpeech[parameters.partOfSpeech].sectionName,				headword: headword,				definitions: definitions,				subsections: subsections			}); }		var langSpecificFuncs = NewerEntryCreatorData[langcode]; var pronunciation = {}; if (langSpecificFuncs !== undefined && langSpecificFuncs.Pronunciation) pronunciation = langSpecificFuncs.Pronunciation; return {partOfSpeechCollection: partOfSpeechCollection, pronunciation: pronunciation}; } }; function showQuestions(questions, parameters) {	questions = questions.sort((a, b) => (a.sortkey ?? 0) - (b.sortkey ?? 0));	var currentGroup = "adumbhack_random_string"; var $currentGroupDiv = undefined; for(var question of questions) {		if (question.shouldShow && question.shouldShow(parameters) == false) continue; if (question.group != currentGroup) {			if ($currentGroupDiv !== undefined) $("#newer-entry-creator").append($currentGroupDiv); $currentGroupDiv = $(" ") .attr("class", "newer-entry-creator-question-group") .attr("id", "newer-entry-creator-question-group-" + (question.group ?? "defaultGroup").replace(" ", "-")) .append($(" ").text((question.group ?? "Questions"))); }		currentGroup = question.group; var $questionDiv = $(" "); $questionDiv.append($(" ").text(question.question)); if (question.choices) {			if (question.type == "dropdown") {				var $select = $(' ').prop({	      					id: "newer-entry-creator-" + question.parameterName,	       					name: "newer-entry-creator-" + question.parameterName,			        		className: 'newer-entry-creator-input'	   			}); for(var choice of question.choices) {					var optionText = choice.text || choice; var optionValue = choice.value || choice; $select.append(						$(" ").prop({ id: "newer-entry-creator-" + question.parameterName + "-" + optionValue, selected: optionValue == question.value, value: optionValue, text: optionText })					);				}				$questionDiv.append($select); }			else {				for(var choice of question.choices) {					var optionText = choice.text || choice; var optionValue = choice.value || choice; $questionDiv .append(						$(' ').prop({ type: 'radio', id: "newer-entry-creator-" + question.parameterName + "-" + optionValue, name: "newer-entry-creator-" + question.parameterName, value: optionValue, className: 'newer-entry-creator-input', checked: optionValue == question.value })					)					.append(						$(" ")							.attr("for", "newer-entry-creator-" + question.parameterName + "-" + optionValue)							.text(optionText)					); }			}		}		else {			var initialValue = (typeof question.value === "function") ? question.value(parameters) : question.value; console.log(typeof question.value === "function"); $questionDiv .append(				$(' ').prop({ type: 'text', id: "newer-entry-creator-" + question.parameterName, name: "newer-entry-creator-" + question.parameterName, value: initialValue, className: 'newer-entry-creator-input' })  				.val(initialValue)			); }		$currentGroupDiv.append($questionDiv); }	$("#newer-entry-creator").append($currentGroupDiv); } function setParametersFromInputs(parameters) {	$(".newer-entry-creator-input").each(function{			if ($(this).attr("type") != "radio" || $(this).is(':checked'))	 		parameters[$(this).attr("name").substr(20)] = $(this).val;	}); } function isPrintableKeyCode(keycode) {   var valid = (keycode > 47 && keycode < 58)  || // number keys keycode == 32 || keycode == 13  || // spacebar & return key(s) (if you want to allow carriage returns) (keycode > 64 && keycode < 91)  || // letter keys (keycode > 95 && keycode < 112) || // numpad keys (keycode > 185 && keycode < 193) || // ;=,-./` (in order) (keycode > 218 && keycode < 223);  // [\]' (in order)

return valid; } function refreshUi(e){ console.log("In RefreshUi. " + e.keyCode); if (e.ctrlKey || (e.keyCode !== undefined && !(isPrintableKeyCode(e.keyCode) || e.keyCode == 8 /*backspace*/ || e.keyCode == 46 /*del*/))) return; var focusedName = $(this).attr("id"); var langcode = $("[name='newer-entry-creator-langcode']").val; var partOfSpeech = $("[name='newer-entry-creator-partOfSpeech']").val; var parameters = {word: mw.config.values.wgPageName, languageConfiguration: NewerEntryCreatorData[langcode]}; setParametersFromInputs(parameters); console.log(parameters); if (NewerEntryCreatorData[langcode] === undefined) return; $("#newer-entry-creator").remove; createNewerEntryCreator; var definitionQuestions = NewerEntryCreatorData.all.all.questions.definitions || []; var questions = []; if (NewerEntryCreatorData[langcode][partOfSpeech] !== undefined) {		if (NewerEntryCreatorData[langcode][partOfSpeech].enableDefinitionQuestions !== false) questions = questions.concat(definitionQuestions || []); questions = questions.concat(NewerEntryCreatorData[langcode][partOfSpeech].questions || []); }	else {		questions = questions.concat(definitionQuestions || []); }	showQuestions(questions, parameters); console.log("setting parameters back"); $(".newer-entry-creator-input").each(function{		var oldValue = parameters[$(this).attr("name").substr(20)];		if (oldValue === undefined) return;		if ($(this).attr("type") == "radio")			$(this).prop('checked', $(this).attr("value") == oldValue);		else			$(this).val(oldValue);	}); setParametersFromInputs(parameters); //an event could make a new input with default value appear $(".newer-entry-creator-input").bind('change keyup', refreshUi); //$("[name='" + focusedName + "']").focus; $("#" + focusedName).focus; var generatedWikitext = window.NewerEntryCreatorData.generate(langcode, NewerEntryCreatorProcess.generate(langcode, [parameters])); $("#wpTextbox1").val(generatedWikitext); addCss; }

function addCss {	$(".newer-entry-creator-question-group") //.css("border", "1px solid black") .css("padding", "10px"); }

function createNewerEntryCreator {	$(" ").insertBefore($("#mw-content-text")); $("#newer-entry-creator").append($(" ")); for (var pos in NewerEntryCreatorProcess.acceptablePartsOfSpeech) $("#parts-of-speech").append($(""+pos+" ")); $("#newer-entry-creator").append($(" langcode: ")); $("#newer-entry-creator").append($("")); $("#newer-entry-creator").append($(" part of speech: ")); $("#newer-entry-creator").append($("")); } $(document).ready(function{	if (mw.config.values.wgRelevantArticleId != 0) return;	createNewerEntryCreator;	$(".newer-entry-creator-input").change(refreshUi); });

//