MediaWiki:Gadget-edit-form-util.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.
EUtil = {
	getParameter: function ( name ) {
		return mw.util.getParamValue( name );
	},

	getSection: function () {
		return document.location.hash.replace( '#', '' );
	},

	isEditingSection: function () {
		return ( $( 'input[name="wpSection"]' ).val() !== '' );
	},

	getActiveLangCode: function () {
		return EUi.activeLangCode;
	},

	getActiveLangId: function () {
		return EUi.activeLangId;
	},

	focusArea: function ( subs, atEnd ) {
		var elem = $( '#ed_' + EUtil.getActiveLangId() + '_' + subs ),
		len;

		elem.focus();
		
		if ( atEnd && elem[ 0 ] ) {
			if ( elem[ 0 ].setSelectionRange ) {
				len = elem.val().length * 2;
				elem[ 0 ].setSelectionRange( len, len );
			} else {
				elem.val( elem.val() );
			}
			
			elem[ 0 ].scrollTop = 999999;
		}
	},

	isEmpty: function ( obj ) {
		var prop;

		for ( prop in obj ) {
			if ( obj.hasOwnProperty( prop ) ) {
				return false;
			}
		}
		
		return true;
	},

	escapeHTML: function ( html ) {
		return html
			.replace( /&/g, '&' )
			.replace( /</g, '&lt;' )
			.replace( />/g, '&gt;' );
	},

	escapeJS: function ( js ) {
		return js.replace( /\'/g, "\\'" );
	},

	getUrl: function ( langcode, page ) {
		return 'https://' + langcode + '.wiktionary.org/wiki/' + mw.util.wikiUrlencode( page );
	},
	
	getUrlApi: function ( lang, project ) {
		if ( project === undefined ) {
			project = EConstants.WIKTIONARY;
		} else if ( project === EConstants.WIKIMEDIA ) {
			lang = 'commons';
		}
		
		return 'https://' + lang + '.' + project + '.org' + mw.util.wikiScript( 'api' );
	}
};

EKeyboard = {
	opened:   0,
	keyboard: $( '<div>' ).attr( 'id', 'keyboard' ),
	keys:     $( '<div>' ).attr( 'id', 'keyboard_keys' ),

	init: function () {
		EKeyboard.keys.append( EStr.KEYBOARD_ALWAYS );

		$( 'body' ).append(
			EKeyboard.keyboard.hide(),
			EKeyboard.keys.hide()
		);

		if ( EUi.usingNew ) {
			ESpecialChars.detach();
		}
		
		EKeyboard.opened = false;

		EKeyboard.keyboard.on( 'click', function () {
			EKeyboard.keys.toggle();
			EKeyboard.opened = !EKeyboard.opened;
			return false;
		} );
		
		if ( EKeyboard.opened ) {
			EKeyboard.keys.show();
		}

		$( window ).on( 'resize', function () {
			if ( document.activeElement ) {
				$( document.activeElement ).trigger( 'focus' );
			}
		} );
		
		EUi.form.on( 'focus', '.keyboardable', $( this ).keyboard );
	},

	hide: function () {
		EKeyboard.keyboard.hide();
		EKeyboard.keys.hide();
	},

	updatePosition: function ( origin ) {
		var nPos;
		
		if ( !origin.is( ':visible' ) ) {
			EKeyboard.hide();
			return;
		}
		
		nPos = origin.offset();

		nPos.top += origin.height() + 7;
		nPos.left += 20;
		
		EKeyboard.keyboard.show().css( nPos );
		
		EKeyboard.keys.css( {
			top:  nPos.top,
			left: nPos.left + 34
		} ).data( 'active_area', origin.attr( 'id' ) );
	},

	insertTags: function ( tagOpen, tagClose, sampleText ) {
		var txtarea;

		if ( document.editform && !EUi.usingNew ) {
			txtarea = document.editform.wpTextbox1;
		} else if ( EUi.usingNew ) {
			var aname = EKeyboard.keys.data( 'active_area' );
			
			txtarea = aname
				? document.getElementById( aname )
				: undefined;
		}
		
		if ( !txtarea ) {
			txtarea = document.getElementsByTagName( 'textarea' )[ 0 ];
		}
		
		$( txtarea )
			.textSelection( 'encapsulateSelection', {
				pre:  tagOpen,
				peri: sampleText,
				post: tagClose
			} )
			.autoresize();
		
		EUi.relocateResult();
	}
};

ESpecialChars = {
	obj:          undefined,
	formerParent: undefined,
	detached:     0,

	detach: function () {
		var container;

		if ( ESpecialChars.detached ) {
			return;
		}
		
		container = EKeyboard.keys;
		ESpecialChars.obj = $( '#editpage-specialchars' );
		ESpecialChars.formerParent = ESpecialChars.obj.parent();
		ESpecialChars.obj.detach();

		container.append( ESpecialChars.obj );
		ESpecialChars.detached = 1;
	},

	attach: function () {
		if ( !ESpecialChars.detached ) {
			return;
		}
		
		EKeyboard.hide();
		ESpecialChars.obj.detach();
		ESpecialChars.formerParent.append( ESpecialChars.obj );
		ESpecialChars.detached = 0;
	},

	toggle: function () {
		if ( ESpecialChars.detached ) {
			ESpecialChars.attach();
		} else {
			ESpecialChars.detach();
		}
	}
};

EApi = {
	localApi: new mw.Api(),
	foreignApis: {},
	waiting: false,
	
	handleRequest: function ( mode, handler ) {
		if ( EApi.waiting ) {
			$.alerts.alert( EStr.WAITING_FOR_API );
		} else {
			EApi.prepare( mode );
			EApi.waiting = true;
			
			handler().done( function () {
				EApi.waiting = false;
			} );
		}
	},
	
	process: function ( options, callback ) {
		options = Array.isArray( options ) ? options : [ options ];
		
		var requests = $.map( options, function ( opts ) {
			var query = $.extend( true, {
				meta: 'siteinfo',
				formatversion: 2
			}, opts.data || opts );
			
			if ( opts.external ) {
				var url = EUtil.getUrlApi( opts.external.lang, opts.external.project );
				
				if ( !EApi.foreignApis[ url ] ) {
					EApi.foreignApis[ url ] = new mw.ForeignApi( url, {
						anonymous: true
					} );
				}
				
				return EApi.foreignApis[ url ].get( query );
			} else {
				return EApi.localApi.get( query );
			}
		} );
		
		return $.when.apply( null, requests ).done( callback );
	},

	prepare: function ( mode ) {
		var idpart = EConstants.API_ID[ mode ],
		elem = $( '#ed_' + EUtil.getActiveLangId() + '_extra_' + idpart );

		elem
			.removeClass( 'apidone apierror' )
			.addClass( 'apistarted' );
		
		if ( elem.data( 'orig_html' ) ) {
			elem.html( elem.data( 'orig_html' ) );
		}
	},

	display: function ( mode, res, subs, error ) {
		var idpart = EConstants.API_ID[ mode ],
		elem = $( '#ed_' + EUtil.getActiveLangId() + '_extra_' + idpart );

		if ( error === undefined ) {
			elem.addClass( 'apidone' ).removeClass( 'apistarted apierror' );
			
			if ( res !== undefined ) {
				EUi.showResult( EPrinter.resultToHTML( mode, res ), idpart );
			}
		} else {
			elem
				.addClass( 'apierror' )
				.removeClass( 'apistarted apidone' )
				.data( 'orig_html', elem.html() )
				.html( error );
		}
		
		if ( subs !== undefined ) {
			EUtil.focusArea(
				subs,
				EUi.isDefaultText( EUtil.getActiveLangId(), subs, 1 )
			);
		}
	}
};