MediaWiki:Gadget-morfeusz-lookup.js

Z Wikisłownika – wolnego słownika wielojęzycznego

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
/**
 * http://morfeusz.sgjp.pl/
 * https://github.com/PeterBowman/morfeusz-lookup
 */

var SITE = 'https://morfeusz.toolforge.org/api';

function performRequest( query ) {
	var xhr,
		deferred = $.Deferred();
	
	$.extend( this.options, {
		data: $.extend( query, this.parameters )
	} );
	
	xhr = $.ajax( this.options )
		.fail( function ( jqXHR, textStatus, errorThrown ) {
			deferred.reject( 'http', {
				textStatus: textStatus,
				errorThrown: errorThrown
			}, jqXHR );
		} )
		.done( function ( data, textStatus, jqXHR ) {
			if ( data.hasOwnProperty( 'errors' ) ) {
				deferred.reject( 'error', data, jqXHR );
			} else if ( data === undefined || data === null || data === '' ) {
				deferred.reject( 'empty', null, jqXHR );
			} else if ( !data.hasOwnProperty( 'results' ) || !Array.isArray( data.results ) ) {
				deferred.reject( 'malformed', data, jqXHR );
			} else {
				deferred.resolve( data, jqXHR );
			}
		} );
	
	return deferred.promise( {
		abort: xhr.abort
	} );
}

function MorfeuszLookup( options ) {
	options = options || {};
	$.extend( options, { url: MorfeuszLookup.getSite() } );
	this.options = options;
	this.parameters = {};
}

MorfeuszLookup.prototype = {
	analyze: function ( text ) {
		return performRequest.call( this, { action: 'analyze', text: text } );
	},
	
	generate: function ( titles ) {
		if ( Array.isArray( titles ) ) {
			titles = titles.join( '|' );
		}
		
		return performRequest.call( this, { action: 'generate', titles: titles } );
	},
	
	/**
	 * Ignored in generator mode. Accepted values:
	 * - 'separate' (default)
	 * - 'continuous'
	 */
	setTokenNumbering: function ( tokenNumbering ) {
		$.extend( this.parameters, { tokenNumbering: tokenNumbering } );
		return this;
	},
	
	/**
	 * Ignored in generator mode. Accepted values:
	 * - 'conditional' (default)
	 * - 'strict'
	 * - 'ignore'
	 */
	setCaseHandling: function ( caseHandling ) {
		$.extend( this.parameters, { caseHandling: caseHandling } );
		return this;
	},
	
	/**
	 * Ignored in generator mode. Accepted values:
	 * - 'skip' (default)
	 * - 'append'
	 * - 'keep'
	 */
	setWhitespaceHandling: function ( whitespaceHandling ) {
		$.extend( this.parameters, { whitespaceHandling: whitespaceHandling } );
		return this;
	},
	
	/**
	 * Accepted values:
	 * - 'strict' (default)
	 * - 'isolated'
	 * - 'permissive'
	 */
	setAgglutinationRules: function ( agglutinationRules ) {
		$.extend( this.parameters, { agglutinationRules: agglutinationRules } );
		return this;
	},
	
	/**
	 * Accepted values:
	 * - 'split' (default)
	 * - 'composite'
	 */
	setPastTenseSegmentation: function ( pastTenseSegmentation ) {
		$.extend( this.parameters, { pastTenseSegmentation: pastTenseSegmentation } );
		return this;
	},
	
	/**
	 * Default: false.
	 */
	 expandDag: function ( enable ) {
	 	$.extend( this.parameters, { expandDag: Boolean( enable ) } );
		return this;
	 },
	 
	 /**
	 * Default: false.
	 */
	 expandTags: function ( enable ) {
	 	$.extend( this.parameters, { expandTags: Boolean( enable ) } );
		return this;
	 },
	 
	 /**
	 * Default: true.
	 */
	 expandDot: function ( enable ) {
	 	$.extend( this.parameters, { expandDot: Boolean( enable ) } );
		return this;
	 },
	 
	 /**
	 * Default: true.
	 */
	 expandUnderscore: function ( enable ) {
	 	$.extend( this.parameters, { expandUnderscore: Boolean( enable ) } );
		return this;
	 }
};

MorfeuszLookup.getSite = function () {
	return SITE;
};

module.exports = { MorfeuszLookup: MorfeuszLookup };