Karber:Balyozxane/kopi-enWikt.js

Wikiqısebend ra

Note: Qeydi ra dıme, gani viriya cıgeyrayoği pak bo ke vurnayışi bıvêniyê.

  • Gışta şıma ke niya ro gocega Firefox / Safari: Shift ser, bıtıknê ra newe ra bar kerên ya zi Ctrl-F5 ya zi Ctrl-R bıtıknê (seba Mac ra ⌘-R).
  • Google Chrome: Ctrl-Shift-R ro nê. (seba Mac ra ⌘-Shift-R)
  • Internet Explorer / Edge: Ctrl ke niyo ro cı, Newe ke bıtıknê ya zi Ctrl-F5 bıkerê.
  • Opera: Ctrl-F5 bıtıknê.
// <nowiki>
$(document).ready(function() {

	//CSS loader
	mw.loader.addStyleTag('.help { font-size: 12px }');
	mw.loader.addStyleTag('.button { display: inline-block; text-align: center; margin-top: 10px; margin-bottom: 10px }');
	mw.loader.addStyleTag('.etiket-box { font: 12px sans-serif; margin-left: 1rem; margin: 1px; width: 100%; padding:0px; }');

	// It is important to make sure that OOUI is loaded before we can make use of it.
	mw.loader.using("oojs-ui", "mediawiki.api").done(function() {
		let rupelaEnInput, rupelaKuInput;

		// Initialize form input variables
		rupelaEnInput = new OO.ui.TextInputWidget({
			id: 'rupelaEn',
			placeholder: 'Rûpela en.wikt',
			classes: ['etiket-box']
		});
		rupelaKuInput = new OO.ui.TextInputWidget({
			id: 'rupelaKu',
			placeholder: 'Rûpela nû ya ku.wikt',
			classes: ['etiket-box']
		});

		fieldset = new OO.ui.FieldsetLayout({
			label: 'Rûpelê Kopî Bike',
			id: 'new-kopi-fieldset',
			classes: ["help"],
			items: [
				new OO.ui.FieldLayout(rupelaEnInput, {
					label: 'Rûpela en.wikt',
					align: 'left',
				}),
				new OO.ui.FieldLayout(rupelaKuInput, {
					label: 'Rûpela nû ya ku.wikt',
					align: 'left',
				})
			]
		});

		// Create the submit button
		const submitButton = new OO.ui.ButtonInputWidget({
			label: 'Bişîne',
			classes: ["button"],
			flags: ['primary', 'progressive'],
		});
		// Create the "Bigre" button
		const cancelButton = new OO.ui.ButtonInputWidget({
			label: 'Bigre',
			classes: ["button"],
			flags: ['destructive']
		});

		// Add click event to the "Bigre" button
		cancelButton.on('click', function() {
			popUp.toggle();

			rupelaEnInput.getValue() === "";
			rupelaKuInput.getValue() === "";
		});

		// Add form fields and submit button to the form
		fieldset.addItems([submitButton, cancelButton]);


		// Add click event to the submit button
		submitButton.on('click', function() {
			console.log("Submit button clicked.");
			submitForm();
			popUp.toggle();
		});

		// Function to handle form submission
		function submitForm() {
			console.log("Submitting form.");
			const rupelaEn = rupelaEnInput.getValue();
			const rupelaKu = rupelaKuInput.getValue();

			fetchEnWiktContentAndCreate(rupelaEn, rupelaKu);
		}

		function fetchEnWiktContent(pageTitle, callback) {
			var apiUrl = `https://en.wiktionary.org/w/api.php?action=query&titles=${encodeURIComponent(pageTitle)}&prop=revisions&rvprop=content&rvlimit=1&format=json&callback=?`;

			$.getJSON(apiUrl, function(data) {
				var pages = data.query.pages;
				var pageId = Object.keys(pages)[0];
				var content = pages[pageId].revisions[0]['*'];

				callback(content); // Call the callback function with the content value
			}).fail(function(error) {
				console.error("Error fetching content:", error);
				callback(null); // Call the callback function with null in case of an error
			});
		}



		function fetchEnWiktContentAndCreate(rupelaEn, rupelaKu) {
		    fetchEnWiktContent(rupelaEn, function(content) {
		        const api = new mw.Api();
		        const summary = `Imported from [[:en:${rupelaEn}]] using [[Karber:Balyozxane/kopi-enWikt.js|kopi-enWikt.js]]`;
		
		        api.postWithToken('csrf', {
		            action: 'edit',
		            title: rupelaKu,
		            text: content,
		            summary: summary,
		            createonly: true
		        }).then(data => {
		            mw.notify('Rûpela ' + rupelaKu + ' hat çêkirin.');
		            
		            // Open the created page in a new tab
		            const kuWiktionaryUrl = `https://diq.wiktionary.org/wiki/${encodeURIComponent(rupelaKu)}`;
		            window.open(kuWiktionaryUrl, '_blank');
		        }).catch(err => {
		            mw.notify('Xeletî di çêkirina rûpelê de: ' + err);
		        });
		    });
		}




		// To add the edit wizard tab on the top navigation bar
		var node = mw.util.addPortletLink('p-tb', '#', 'Kopî enWiktê', 't-kopi', 'Rûpela inglîzî kopî bike');

		// A popup widget is instantiated
		var popUp = new OO.ui.PopupWidget({
			align: "forwards",
			$floatableContainer: $(node),
			$content: fieldset.$element,
			padded: true,
			popup: false,
			width: 400,
			height: 250,
			head: true,
			// hideWhenOutOfView: false,
			// autoClose: false,
			hideCloseButton: true,
		});


		$(node).on('click', function(e) {
			console.log("Link clicked.");
			popUp.toggle();
			e.preventDefault();
		});

		$(document.body).append(popUp.$element);
	});
});

// </nowiki>