MediaWiki:Gadget-translation editor add section.js

Definition från Wiktionary, den fria ordlistan.
Hoppa till navigering Hoppa till sök

OBS: Efter du har publicerat sidan kan du behöva tömma din webbläsares cache för att se ändringarna.

  • Firefox / Safari: Håll ned Skift och klicka på Uppdatera sidan eller tryck Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
  • Google Chrome: Tryck Ctrl-Skift-R (⌘-Skift-R på Mac)
  • Internet Explorer / Edge: Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5
  • Opera: Tryck Ctrl-F5.
// This is an extension of [[MediaWiki:Gadget-translation_editor.js]]
// It adds the possibility to add new translation sections
'use strict';

// Not in sections with just inflections
if (editor.enabled) {
	$('#Svenska').parent('h2')
		.nextUntil('h2')
		.filter(function () {
			var $this = $(this);
			var next = $this.next();
			// Only act on the last element in the section
			if (next.length && !next.is('h2, h3')) {
				return false;
			}
			// There must be definitions
			var ol = $this.prevUntil('h3', 'ol');
			if (!ol.length && $this.is('ol')) {
				ol = $this;
			}
			if (!get_translatable_definitions(ol).length) {
				return false;
			}
			// There must not already be translations
			var trans = $this.prevUntil('h3', 'h4').filter(function () {
				return $(this).children('.mw-headline').text() === 'Översättningar';
			});
			return !trans.length;
		})
		.each(function () {
			$('<p>', { html:
					$('<a>', {
						href: '#',
						text: 'Lägg till översättningar...',
						click: add_translation_section,
						css: { fontStyle: 'italic' }
					})
			})
			.insertAfter(this);
		});
}


function get_translatable_definitions(ol) {
	return ol.children().map(function () {
		var children = this.childNodes;
		var text = [];
		var i, c, tag, tmp;
		for (i = 0; i < children.length; i++) {
			c = children[i];
			tag = c.tagName;
			if (tag) tag = tag.toLowerCase();
			if (tag === 'i') {
				tmp = $(c).text();
				if (tmp[0] === '(' && tmp[tmp.length - 1] === ')') {
					// remove
				} else if (/^böjningsform av$|\b(stavnings)?variant av$/.test(tmp)) {
					// skip this whole definition
					return;
				} else {
					text.push(c);
				}
			} else if (tag === 'dl') {
				// remove
			} else {
				text.push(c);
			}
		}

		return $.trim($(text).text())
			.replace(/;.+$/, '');
	});
}

function add_translation_section(e) {
	var $this = $(this);
	var $parent = $this.parent();
	e.preventDefault();
	
	if ($this.data('loading')) {
		return;
	}

	$this
		.data('loading', true)
		.text('Laddar...');

	var definitions = get_translatable_definitions($parent.prevUntil('h3', 'ol').first());

	var section_wikitext = '\n\n====Översättningar====\n' +
		definitions.map(function () {
			var gloss = this
				// For non-breaking spaces, use &nbsp;.
				.replace(/\xa0/g, '&nbsp;')
				// Avoid duplicate spaces.
				.replace(/ {2,}/g, ' ')
				// Avoid spaces in the beginning or end.
				.replace(/^ | $/g, '');

			// Use explicit param name 1= if the gloss contains a '='.
			if (/=/.test(gloss)) {
				gloss = "1=" + gloss;
			}

			return '{{ö-topp|' + gloss + '}}\n{{ö-botten}}';
		}).toArray().join('\n\n') + '\n\n';

	$.when(parse_wikitext(section_wikitext), editor.wikitext())
	.done(function (html, wikitext) {
		var div = $('<div>', {'class': 'ed-added', html: html, css: {clear: 'both'}});
		div.find('h4 > .mw-editsection').remove();

		create_show_hide_toggle(div, true);

		// append the new section wikitext to the right section
		var h2_pos = wikitext.indexOf('==Svenska==\n');
		var h3_pos = wikitext.indexOf(
			'===' + $parent.prevUntil('h3').last().prev().find('.mw-headline').text() + '===\n',
			h2_pos);
		// new h2 or h3 heading or {{STANDARDSORTERING:...}} or iw links
		var re = /\n(===?[^=]|\{\{STANDARDSORTERING:|\[\[[a-z-]+:[^\]]+\]\](\n|$))/g;
		re.lastIndex = h3_pos;
		var match = re.exec(wikitext);
		var section_end = match ? match.index : wikitext.length;

		// insert section into wikitext
		wikitext =
			$.trim( wikitext.substr(0, section_end) ) +
			section_wikitext +
			$.trim( wikitext.substr(section_end) );
		
		var added_edittools = false;

		editor.edit({
			wikitext: wikitext,
			summary: '+översättningar',
			summary_type: 'add_trans_section',
			save: "no", // actually have to add translation too
			redo: function () {
				$parent
					.hide()
					.after(div);
				if (!added_edittools) {
					div.find('.översättningar').each(function () {
						add_translation_form(this);
						add_heading_updater(this);
						div.find('.ed-lang-code').first().focus();
					});
					added_edittools = true;
				}
				div[0].scrollIntoView();
			},
			undo: function () {
				$parent.show();
				div.detach();
			}
		});

		$this
			.data('loading', false)
			.text('Lägg till översättningar...');
	});
}