/* * This file is part of the MediaWiki extension MultimediaViewer. * * MultimediaViewer is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * MultimediaViewer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MultimediaViewer. If not, see . */ ( function ( mw, $ ) { var MultiLightbox, LightboxImage, lightboxHooks, validExtensions = { 'jpg': true, 'jpeg': true, 'gif': true, 'svg': true, 'png': true, 'tiff': true, 'tif': true }, iiprops = [ 'timestamp', 'user', 'userid', 'comment', 'url', 'size', 'sha1', 'mime', 'mediatype', 'metadata', 'extmetadata' ], mmvLogActions = { 'thumbnail-link-click': 'User clicked on thumbnail to open lightbox.', 'enlarge-link-click': 'User clicked on enlarge link to open lightbox.', 'fullscreen-link-click': 'User clicked on fullscreen button in lightbox.', 'defullscreen-link-click': 'User clicked on button to return to normal lightbox view.', 'close-link-click': 'User clicked on the lightbox close button.' }; function MultimediaViewer() { var $thumbs = $( '.thumbimage' ), urls = [], viewer = this; this.api = new mw.Api(); this.imageInfo = {}; $thumbs.each( function ( i, thumb ) { var fileLink, $thumb = $( thumb ), $link = $thumb.closest( 'a.image' ), $thumbContain = $link.closest( '.thumb' ), $enlarge = $thumbContain.find( '.magnify a' ), $links = $link.add( $enlarge ), filePageLink = $link.prop( 'href' ), fileTitle = mw.Title.newFromImg( $thumb ), index = urls.length; if ( !validExtensions[fileTitle.getExtension().toLowerCase()] ) { // Not a valid extension, skip this one return; } $links.data( 'filePageLink', filePageLink ); urls.push( new LightboxImage( fileLink ) ); urls[index].filePageLink = filePageLink; $links.click( function ( e ) { var $this = $( this ); if ( $this.is( 'a.image' ) ) { viewer.log( 'thumbnail-link-click' ); } else if ( $this.is( '.magnify a' ) ) { viewer.log( 'enlarge-link-click' ); } e.preventDefault(); viewer.lightbox.currentIndex = index; // Open with a basic thumbnail and no information - fill in async viewer.lightbox.images[index].src = this.src; viewer.lightbox.open(); viewer.fetchImageInfo( fileTitle, function ( imageInfo ) { function whitelistHtml( $ele ) { function test( $ele ) { return $ele.jquery && ( $ele.is( 'a' ) || false ); } var $children, whitelisted = ''; if ( $ele && $ele.jquery && $ele.contents ) { $children = $ele.contents(); } else if ( $ele && $ele.textContent ) { return $ele.textContent; } else if ( $ele ) { return $ele; } if ( !$children || $children.length === 0 ) { return $ele.text(); } $children.each( function ( i, ele ) { var $ele = $( ele ); if ( test( $ele ) === true ) { whitelisted += $ele.html( whitelistHtml( $ele ) ).get( 0 ).outerHTML; } else { whitelisted += '' + whitelistHtml( $ele ) + ''; } } ); return whitelisted; } function setUserpageLink( username, gender ) { var userpage = 'User:' + username, userTitle = mw.Title.newFromText( userpage ); ui.$username .text( mw.message( 'multimediaviewer-userpage-link', username, gender ).text() ) .prop( 'href', userTitle.getUrl() ); if ( articlePath ) { ui.$username .prop( 'href', articlePath.replace( '$1', userTitle.getPrefixedText() ) ); } ui.$usernameLi.toggleClass( 'empty', !Boolean( username ) ); } var extmeta, repoInfo, articlePath, desc, datetime, dtmsg, username, ui = viewer.lightbox.iface, innerInfo = imageInfo.imageinfo[0] || {}; viewer.lightbox.images[index].src = innerInfo.url; viewer.lightbox.open(); ui.$title.text( fileTitle.getNameText() ); if ( viewer.repoInfo ) { repoInfo = viewer.repoInfo[imageInfo.imagerepository]; } if ( repoInfo ) { if ( repoInfo.displayname ) { ui.$repo.text( mw.message( 'multimediaviewer-repository', repoInfo.displayname ).text() ); } else { ui.$repo.text( mw.message( 'multimediaviewer-repository', mw.config.get( 'wgSiteName' ) ).text() ); } if ( repoInfo.server && repoInfo.articlepath ) { articlePath = repoInfo.server + repoInfo.articlepath; } else { articlePath = mw.config.get( 'wgArticlePath' ); } ui.$repo .prop( 'href', articlePath.replace( '$1', fileTitle.getPrefixedText() ) ); } ui.$repoLi.toggleClass( 'empty', !Boolean( repoInfo ) ); username = innerInfo.user; if ( username ) { // Fetch the gender from the uploader's home wiki // TODO this is ugly as hell, let's fix this in core. new mw.Api( { ajax: { url: repoInfo.apiurl || mw.util.wikiScript( 'api' ) } } ).get( { action: 'query', list: 'users', ususers: username, usprop: 'gender' } ).done( function ( data ) { var gender = data.query.users[0].gender; setUserpageLink( username, gender ); } ).fail( function () { setUserpageLink( username, 'unknown' ); } ); } extmeta = innerInfo.extmetadata; if ( extmeta ) { desc = extmeta.ImageDescription; if ( desc ) { desc = desc.value; ui.$imageDesc.html( whitelistHtml( $( desc ) ) ); } datetime = extmeta.DateTimeOriginal || extmeta.DateTime; if ( datetime ) { dtmsg = ( 'multimediaviewer-datetime-' + ( extmeta.DateTimeOriginal ? 'created' : 'uploaded' ) ); datetime = datetime.value; ui.$datetime.text( mw.message( dtmsg, datetime ).text() ); } ui.$datetimeLi.toggleClass( 'empty', !Boolean( datetime ) ); } } ); return false; } ); } ); if ( $thumbs.length > 0 ) { this.lightbox = new MultiLightbox( urls ); } lightboxHooks.register( 'imageLoaded', function () { // Add link wrapper to the image div, put image inside it this.$imageLink = $( '' ) .addClass( 'mw-mlb-image-link' ) .html( this.$image.detach() ); this.$imageDiv.append( this.$imageLink ); } ); lightboxHooks.register( 'modifyInterface', function () { var ui = this; this.$imageDesc = $( '

' ) .addClass( 'mw-mlb-image-desc' ); this.$imageDescDiv = $( '

' ) .addClass( 'mw-mlb-image-desc-div' ) .html( this.$imageDesc ); this.$imageLinks = $( '