Користувач:RLuts/autocomplete.js

Матеріал з Вікіпедії — вільної енциклопедії.
Перейти до навігації Перейти до пошуку

Увага: Після публікування слід очистити кеш браузера, щоб побачити зміни.

  • Firefox / Safari: тримайте Shift, коли натискаєте Оновити, або натисніть Ctrl-F5 чи Ctrl-Shift-R (⌘-R на Apple Mac)
  • Google Chrome: натисніть Ctrl-Shift-R (⌘-Shift-R на Apple Mac)
  • Internet Explorer / Edge: тримайте Ctrl, коли натискаєте Оновити, або натисніть Ctrl-F5
  • Opera: натисніть Ctrl-F5
mw.loader.using( 'jquery.suggestions', function() {
	$( '#AjaxQuestion0' )
		.suggestions( {
			fetch: function( query ) {
				var $this = $(this);
				var request = $.ajax( {
					url: '//commons.wikimedia.org/w/api.php',
					data: {
						'action': 'opensearch',
						'search': query,
						'namespace': 14,
						'suggest': ''
					},
					dataType: 'jsonp',
					success: function( data ) {
						if ( data && 1 in data ) {
							console.log(data);
							for (i in data[1]) {
								data[1][i] = data[1][i].replace("Category:","");
							}
							$this.suggestions( 'suggestions', data[1] );
						}
					}
				});
				$(this).data( 'request', request );
			},
			cancel: function () {
				var request = $(this).data( 'request' );
				// If the delay setting has caused the fetch to have not even happend yet, the request object will
				// have never been set
				if ( request && typeof request.abort == 'function' ) {
					request.abort();
					$(this).removeData( 'request' );
				}
			},
			result: {
				select: function( $input ) {
					$input.closest( 'form' ).submit();
				}
			},
			delay: 120,
			positionFromLeft: $( 'body' ).is( '.rtl' ),
			highlightInput: true
		} )
		.bind( 'paste cut', function( e ) {
			// make sure paste and cut events from the mouse trigger the keypress handler and cause the suggestions to update
			$( this ).trigger( 'keypress' );
		} );
});