Localisation updates from https://translatewiki.net.
[mediawiki.git] / resources / src / mediawiki.special.search.commonsInterwikiWidget.js
blobc607110dba23c857e2373ca613cb0da20162476e
1 ( function () {
3         function itemTemplate( results ) {
5                 return results.map( ( result ) => {
6                         const imageThumbnailSrc = result.thumbnail ? result.thumbnail.source : '';
8                         return $( '<div>' ).addClass( 'iw-result__mini-gallery' ).append(
9                                 $( '<a>' )
10                                         .addClass( 'iw-result__mini-gallery__image' )
11                                         .attr( {
12                                                 style: 'background-image: url(' + imageThumbnailSrc + ')',
13                                                 href: new mw.Title( result.title ).getUrl()
14                                         } ).append(
15                                                 $( '<span>' ).addClass( 'iw-result__mini-gallery__caption' )
16                                                         .text( result.title )
17                                         )
18                         );
19                 } );
21         }
23         function itemWrapperTemplate( pageQuery, itemTemplateOutput ) {
25                 return $( '<li>' ).addClass( 'iw-resultset iw-resultset--image' ).attr( 'data-iw-resultset-pos', 0 ).append(
26                         $( '<div>' ).addClass( 'iw-result__header' ).append(
27                                 $( '<strong>' ).text( mw.msg( 'searchprofile-images' ) )
28                         ),
29                         $( '<div>' ).addClass( 'iw-result__content' ).append(
30                                 itemTemplateOutput
31                         ),
32                         $( '<div>' ).addClass( 'iw-result__footer' ).append(
33                                 $( '<a>' )
34                                         .attr( 'href', new mw.Title( 'Special:Search' ).getUrl( {
35                                                 search: pageQuery,
36                                                 fulltext: 1,
37                                                 profile: 'images'
38                                         } ) )
39                                         .text( mw.msg( 'search-interwiki-more-results' ) )
40                         )
41                 );
43         }
45         const api = new mw.Api();
46         const pageUrl = new URL( location.href );
48         api.get( {
49                 action: 'query',
50                 generator: 'search',
51                 gsrsearch: pageUrl.searchParams.get( 'search' ),
52                 gsrnamespace: mw.config.get( 'wgNamespaceIds' ).file,
53                 gsrlimit: 3,
54                 prop: 'pageimages',
55                 pilimit: 3,
56                 piprop: 'thumbnail',
57                 pithumbsize: 300,
58                 formatversion: 2
59         } ).done( ( resp ) => {
60                 const results = resp.query && resp.query.pages || false;
62                 if ( !results ) {
63                         return;
64                 }
66                 results.sort( ( a, b ) => a.index - b.index );
68                 const multimediaWidgetTemplate = itemWrapperTemplate(
69                         pageUrl.searchParams.get( 'search' ),
70                         itemTemplate( results )
71                 );
72                 /* we really only need to wait for document ready for DOM manipulation */
73                 $( () => {
74                         $( '.iw-results' ).append( multimediaWidgetTemplate );
75                 } );
76         } );
78 }() );