Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / resources / src / mediawiki.special / mediawiki.special.search.commonsInterwikiWidget.js
blob3810ac1b0a9b16c0dfa60ec115ff7ff256589ef3
1 ( function ( mw, $ ) {
3         var api = new mw.Api(),
4                 pageUrl = new mw.Uri(),
5                 imagesText = new mw.Message( mw.messages, 'searchprofile-images' ),
6                 moreResultsText = new mw.Message( mw.messages, 'search-interwiki-more' );
8         function itemTemplate( results ) {
10                 var resultOutput = '', i, result, imageCaption, imageThumbnailSrc;
12                 for ( i = 0; i < results.length; i++ ) {
13                         result = results[ i ],
14                         imageCaption = mw.html.element( 'span', { 'class': 'iw-result__mini-gallery__caption' }, result.title );
15                         imageThumbnailSrc = ( result.thumbnail ) ? result.thumbnail.source : '';
16                         resultOutput += '<div class="iw-result__mini-gallery">' +
17                                                 /* escaping response content */
18                                                 mw.html.element( 'a', {
19                                                         href: '/wiki/' + result.title,
20                                                         'class': 'iw-result__mini-gallery__image',
21                                                         style: 'background-image: url(' + imageThumbnailSrc + ');'
22                                                 }, new mw.html.Raw( imageCaption ) ) +
23                                         '</div>';
24                 }
26                 return resultOutput;
27         }
29         function itemWrapperTemplate( pageQuery, itemTemplateOutput ) {
31                 return '<li class="iw-resultset iw-resultset--image" data-iw-resultsset-pos="0">' +
32                                 '<div class="iw-result__header">' +
33                                         '<span class="iw-result__icon iw-result__icon--image"></span>' +
34                                         '<strong>' + imagesText.escaped() + '</strong>' +
35                                 '</div>' +
36                                 '<div class="iw-result__content">' +
37                                 /* template output has been sanitized by mw.html.element */
38                                 itemTemplateOutput +
39                                 '</div>' +
40                                 '<div class="iw-result__footer">' +
41                                         '<a href="/w/index.php?title=Special:Search&search=' + encodeURIComponent( pageQuery ) + '&fulltext=1&profile=images">' +
42                                                 moreResultsText.escaped() +
43                                         '</a>' +
44                                 '</div>' +
45                         '</li>';
47         }
49         api.get( {
50                 action: 'query',
51                 generator: 'search',
52                 gsrsearch: pageUrl.query.search,
53                 gsrnamespace: mw.config.get( 'wgNamespaceIds' ).file,
54                 gsrlimit: 3,
55                 prop: 'pageimages',
56                 pilimit: 3,
57                 piprop: 'thumbnail',
58                 pithumbsize: 300,
59                 formatversion: 2
60         } )
61         .done( function ( resp ) {
62                 var results = ( resp.query && resp.query.pages ) ? resp.query.pages : false,
63                         multimediaWidgetTemplate;
65                 if ( !results ) {
66                         return;
67                 }
69                 results.sort( function( a, b ) {
70                         return b.index - a.index;
71                 } );
73                 multimediaWidgetTemplate = itemWrapperTemplate( pageUrl.query.search, itemTemplate( results ) );
74                 /* we really only need to wait for document ready for DOM manipulation */
75                 $( function () {
76                         $( '.iw-results' ).prepend( multimediaWidgetTemplate );
77                 } );
78         } );
80 }( mediaWiki, jQuery ) );