User:HastaLaVi2/EntryAdder.js

//by HastaLaVi2 //

// First, force the js file which has the data arrays to load, otherwise the form may not work functionally function loadArrays { // Create the request and the script var xhr = new XMLHttpRequest, s = document.createElement('script');

// Send the request to retrieve data.js   xhr.open('GET', '/w/index.php?title=User:HastaLaVi2/EntryAdder.js/Data.js&action=raw&ctype=text/javascript', false); xhr.send;

// Listen for onload, and remove the script after execution s.addEventListener("load", function(e) {       s.parentElement.removeChild(s);    });

// Load the code inside the script and run it in the head s.textContent = xhr.responseText; document.head.appendChild(s); }

// And then do the actual work, place the form where it should be $( document ).ready( function ( $, mw ) {	// This is the variable which creates the main table for entry adder	var mainTable = " ",		nl1 = '%0D%0A',					// Creates a new line		nl2 = '%0D%0A%0D%0A',			// Creates two new lines		sq = '%23%20',					// URL code for the character "#"		st = '%2A%20',					// URL code for the character "*"		trans = "" + nl1 + "" + nl1 + "",		// Translitions table templates		// Form widgets start from here...		formItems = [];		formItems[1] = new OO.ui.DropdownWidget( { label: 'Language', menu : { } } );		formItems[2] = new OO.ui.DropdownWidget( { label: 'Lemma', menu : { } } );		formItems[3] = new OO.ui.CheckboxMultiselectWidget,		selectedDatas = [];	// So, this deals with adding languages and lemmas to the dropdown menus	function addOptionsToDropdown( dropdown, optionTexts ) {		dropdown.getMenu.addItems( optionTexts.map( function ( optionText ) { if(optionText.constructor === Array){ return new OO.ui.MenuOptionWidget( { label: optionText[1], data: optionText[0] } ); } else { return new OO.ui.MenuOptionWidget( { label: optionText } ); }		} ) );	}	// This function is for appending headings to the selecting list function addOptionsToCheckbox( checkbox, optionTexts ) { checkbox.addItems( optionTexts.map( function ( optionText ) { return new OO.ui.CheckboxMultioptionWidget( { label: optionText, data: optionTexts.indexOf(optionText) } ); } ) );	}	var langSelected = function(option){ selectedDatas.lang = option.getLabel; selectedDatas.code = option.getData; };	var lemmSelected = function(option){ selectedDatas.lemma = option.getLabel; };	// Function for inserting lemmas and languages boxes to the form function insertLanguagesAndLemmas($insertBefore, formItems) { var namespace = mw.config.get( 'wgNamespaceNumber' ); formItems[1].$element.css({			'width': '48%',			'z-index': '1000',		}); formItems[2].$element.css({			'width': '48%',			'z-index': '1000',		}); addOptionsToDropdown( formItems[1], languages ); formItems[1].getMenu.on('select', langSelected); addOptionsToDropdown( formItems[2], lemmas ); formItems[2].getMenu.on('select', lemmSelected); $insertBefore.before( formItems[1].$element ); $insertBefore.before( formItems[2].$element ); }	// Function for inserting headings to the form function insertHeadings($insertBefore, formItems) { addOptionsToCheckbox(formItems[3], headings); $insertBefore.before( formItems[3].$element ); }	// This function creates title lines function makeTitle(number, title) { if(number==2){ return "%3D%3D" + title + "%3D%3D"; } else if(number==3){ return "%3D%3D%3D" + title + "%3D%3D%3D"; } else if(number==4){ return "%3D%3D%3D%3D" + title + "%3D%3D%3D%3D"; } else if(number==5){ return "%3D%3D%3D%3D%3D" + title + "%3D%3D%3D%3D%3D"; }	}	// Checks which headings before lemma title are selected and give them their proper title lines function handleBeforeLemma(table) { var sample = ""; for (i = 0; i < 6; ++i) { if (table[i]) { sample = sample + nl2 + makeTitle(3, table[i]) + nl1 + ((i==1||i==2||i==3) ? "" : st); }		}		return sample; }	function headword(lang, lemma) { // Check if a headword template for the spesific language exists, otherwise we have to use simple headword template lemma = lemma.toLowerCase; return nl1 + "" + nl1 + sq; // NOTE: This section in need of help, to check if a language has a headword template like "" }	function handleAfterLemma(table) { var sample = "", sayi; for (i = 6; i < 29; ++i) { if (i==24||i==25||i==26||i==27||i==28) { sayi = 3; } else { sayi = 4; }			if (table[i]) { sample = sample + nl2 + makeTitle(sayi, table[i]) + nl1 + (i==23 ? trans : ""); }		}		return sample; }	// This function creates the result page's wikicode function createResultPageCode(formItems) { var headTitle = mw.util.getParamValue( 'title' ),			// Picks the title value from URL headSearch = mw.util.getParamValue( 'search' ),			// Picks the search value from URL title, resultPage = "", tryThis; if($('#firstHeading').text === "Search results"){ title = headSearch; } else { title = headTitle; }		// We will need this var later on headword templates var editableLemma = selectedDatas.lemma; // Edit all the selected data before clicking the button selectedDatas.lang = makeTitle(2, selectedDatas.lang); selectedDatas.lemma = makeTitle(3, selectedDatas.lemma); // Seperate headings before the lemma title and after, because we are gonne run them seperately selectedDatas.headsBefore = []; selectedDatas.headsAfter = []; formItems[3].findSelectedItems.map( function(heading) {			// Those who has its index smaller than 6 are the hedings before lemma title, you can check this out from the "Data.js" file			if (heading.getData < 6) {				selectedDatas.headsBefore[heading.getData] = heading.getLabel;			} else {				selectedDatas.headsAfter[heading.getData] = heading.getLabel;			}		}); resultPage = resultPage + selectedDatas.lang + handleBeforeLemma(selectedDatas.headsBefore) + nl2 + selectedDatas.lemma + headword(selectedDatas.code, editableLemma) + handleAfterLemma(selectedDatas.headsAfter); window.location = 'https://en.wiktionary.org/w/index.php?title=' + title + '&action=edit&preload=User:HastaLaVi2/Example&preloadparams[]=' + resultPage; }	// Function for adding "create" button to the form function finalButton($insertBefore, formItems) { var button = new OO.ui.ButtonWidget( { 			label: 'Create entry',		} ); button.on( 'click', function {			createResultPageCode(formItems);		} ); $insertBefore.before( button.$element ); }	// This is where the magic actually happens $.when( mw.loader.using( 'oojs-ui-core' ), $.ready ).then( function {		var namespace = mw.config.get( 'wgNamespaceNumber' );		if (namespace === 0||namespace === -1) {			loadArrays;			$( '.mw-newarticletext' ).after(mainTable);			$( '.mw-newarticletextanon' ).after(mainTable);			$( '.mw-search-createlink' ).after(mainTable);			insertLanguagesAndLemmas( $( '.headingsNew' ), formItems);			insertHeadings( $( '.buttonNew' ), formItems);			finalButton( $( '.helloToTheMoon' ), formItems);		}	} ); }( jQuery, mediaWiki ) );

//