Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Gadget-responsiveimagemap.js: Difference between revisions

MediaWiki interface page
Fix 3
Tag: Reverted
Go back to original version
Tag: Manual revert
Line 1: Line 1:
;(function(window, $, mw) {
/*
    if (window.responsiveImageMapLoaded) return;
* rwdImageMaps jQuery plugin v1.6
    window.responsiveImageMapLoaded = true;
*
* Allows image maps to be used in a responsive design by recalculating the area coordinates to match the actual image size on load and window.resize
*
* Copyright (c) 2016 Matt Stow
* https://github.com/stowball/jQuery-rwdImageMaps
* http://mattstow.com
* Licensed under the MIT license
*
*
* Modified by HumansCanWinElves to fit better for Fandom wikis
*
*/


    function rwdImageMap($img) {
/*
        $img.each(function() {
The MIT License (MIT)
            var that = this;
            var $that = $(that);
Copyright (c) 2016 Matt Stow
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


            // Only proceed if the image has a usemap attribute
;(function(window, $, mw) {
            if (typeof($(this).attr('usemap')) === 'undefined') return;
if (window.responsiveImageMapLoaded) return;
 
window.responsiveImageMapLoaded = true;
            // Handle WebKit image load issues by using onload
            var $realImage = $('<img />');
function rwdImageMap($img) {
            $realImage.on('load', function() {
$img.each(function() {
                var attrW = 'width', attrH = 'height';
if (typeof($(this).attr('usemap')) == 'undefined')
                var w = $that.attr(attrW), h = $that.attr(attrH);
return;


                // Fallback if width/height attributes are missing
var that = this,
                if (!w || !h) {
$that = $(that);
                    var temp = new Image();
                    temp.src = $that.attr('src');
                    if (!w) w = temp.width;
                    if (!h) h = temp.height;
                }


                var wPercent = $that.width() / 100,
// Since WebKit doesn't know the height until after the image has loaded, perform everything in an onload copy
                    hPercent = $that.height() / 100,
$('<img />').on('load', function() {
                    map = $that.attr('usemap').replace('#', ''),
var attrW = 'width',
                    c = 'coords';
attrH = 'height',
w = $that.attr(attrW),
h = $that.attr(attrH);


                // Update each area element's coordinates within the map
if (!w || !h) {
                $that.siblings('map[name="' + map + '"]').find('area').each(function() {
var temp = new Image();
                    var $this = $(this);
temp.src = $that.attr('src');
                    if (!$this.data(c))
if (!w)
                        $this.data(c, $this.attr(c));
w = temp.width;
if (!h)
h = temp.height;
}


                    var coords = $this.data(c).split(','),
var wPercent = $that.width()/100,
                        coordsPercent = [];
hPercent = $that.height()/100,
map = $that.attr('usemap').replace('#', ''),
c = 'coords';


                    for (var i = 0; i < coords.length; ++i) {
// The ImageMap MediaWiki extension uses the same map name for
                        // Recalculate each coordinate based on image scaling
// identical maps on different images (probably the result of
                        if (i % 2 === 0) {
// a hash function). As such, the manipulation must be limited
                            // Adjust the x coordinate
// not only by the map name but also to only sibling map
                            coordsPercent[i] = parseInt(((coords[i] / w) * 100) * wPercent);
$that.siblings('map[name="' + map + '"]').find('area').each(function() {
                        } else {
var $this = $(this);
                            // Adjust the y coordinate
if (!$this.data(c))
                            coordsPercent[i] = parseInt(((coords[i] / h) * 100) * hPercent);
$this.data(c, $this.attr(c));
                        }
                    }


                    // Log out the adjusted coordinates for debugging
var coords = $this.data(c).split(','),
                    console.log("Updated coordinates for area:", coordsPercent);
coordsPercent = new Array(coords.length);
                   
                    // Update the area element's coords with recalculated coordinates
                    $this.attr(c, coordsPercent.join(','));
                });
            }).attr('src', $that.attr('src')); // Trigger image load
        });
    }


    // Trigger on page load
for (var i = 0; i < coordsPercent.length; ++i) {
    mw.hook('wikipage.content').add(function($e) {
if (i % 2 === 0)
        var $img = $e.find('.responsive-imagemap .noresize:not(.made-responsive)')
coordsPercent[i] = parseInt(((coords[i]/w)*100)*wPercent);
            .css({'width': '', 'height': ''})
else
            .addClass('made-responsive')
coordsPercent[i] = parseInt(((coords[i]/h)*100)*hPercent);
            .find('img[usemap]');
}
       
$this.attr(c, coordsPercent.toString());
        rwdImageMap($img);
});
    });
}).attr('src', $that.attr('src'));
});
}


    // Trigger on window resize
mw.hook('wikipage.content').add(function($e) {
    $(window).resize(function() {
    var $img =
        rwdImageMap($('.responsive-imagemap .made-responsive img[usemap]'));
    $e.find('.responsive-imagemap .noresize:not(.made-responsive)').css({
    });
        'width': '',
        'height': ''
    })
    .addClass('made-responsive')
    .find('img[usemap]');
rwdImageMap($img);
});
$(window).resize(function() {
rwdImageMap($('.responsive-imagemap .made-responsive img[usemap]'));
});


})(this, jQuery, mediaWiki);
})(this, jQuery, mediaWiki);

Revision as of 19:39, 27 May 2025

/*
* rwdImageMaps jQuery plugin v1.6
*
* Allows image maps to be used in a responsive design by recalculating the area coordinates to match the actual image size on load and window.resize
*
* Copyright (c) 2016 Matt Stow
* https://github.com/stowball/jQuery-rwdImageMaps
* http://mattstow.com
* Licensed under the MIT license
*
*
* Modified by HumansCanWinElves to fit better for Fandom wikis
*
*/

/*
	The MIT License (MIT)
	
	Copyright (c) 2016 Matt Stow
	
	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
	the Software, and to permit persons to whom the Software is furnished to do so,
	subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
	FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
	COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
	IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
	CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

;(function(window, $, mw) {
	if (window.responsiveImageMapLoaded) return;
	window.responsiveImageMapLoaded = true;
	
	function rwdImageMap($img) {
		$img.each(function() {
			if (typeof($(this).attr('usemap')) == 'undefined')
				return;

			var that = this,
				$that = $(that);

			// Since WebKit doesn't know the height until after the image has loaded, perform everything in an onload copy
			$('<img />').on('load', function() {
				var attrW = 'width',
					attrH = 'height',
					w = $that.attr(attrW),
					h = $that.attr(attrH);

				if (!w || !h) {
					var temp = new Image();
					temp.src = $that.attr('src');
					if (!w)
						w = temp.width;
					if (!h)
						h = temp.height;
				}

				var wPercent = $that.width()/100,
					hPercent = $that.height()/100,
					map = $that.attr('usemap').replace('#', ''),
					c = 'coords';

				// The ImageMap MediaWiki extension uses the same map name for
				// identical maps on different images (probably the result of
				// a hash function). As such, the manipulation must be limited
				// not only by the map name but also to only sibling map
				$that.siblings('map[name="' + map + '"]').find('area').each(function() {
					var $this = $(this);
					if (!$this.data(c))
						$this.data(c, $this.attr(c));

					var coords = $this.data(c).split(','),
						coordsPercent = new Array(coords.length);

					for (var i = 0; i < coordsPercent.length; ++i) {
						if (i % 2 === 0)
							coordsPercent[i] = parseInt(((coords[i]/w)*100)*wPercent);
						else
							coordsPercent[i] = parseInt(((coords[i]/h)*100)*hPercent);
					}
					$this.attr(c, coordsPercent.toString());
				});
			}).attr('src', $that.attr('src'));
		});
	}

	mw.hook('wikipage.content').add(function($e) {
	    var $img =
		    $e.find('.responsive-imagemap .noresize:not(.made-responsive)').css({
		        'width': '',
		        'height': ''
		    })
		    .addClass('made-responsive')
		    .find('img[usemap]');
		rwdImageMap($img);
	});
	
	$(window).resize(function() {
		rwdImageMap($('.responsive-imagemap .made-responsive img[usemap]'));
	});

})(this, jQuery, mediaWiki);
Cookies help us deliver our services. By using our services, you agree to our use of cookies.