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
Undo revision 3871 by Bog (talk)
Tag: Undo
Different fix
Tag: Reverted
Line 1: Line 1:
/*
;(function(window, $, mw) {
* rwdImageMaps jQuery plugin v1.6
    if (window.responsiveImageMapLoaded) return;
*
    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) {
The MIT License (MIT)
        $img.each(function() {
            var that = this;
Copyright (c) 2016 Matt Stow
            var $that = $(that);
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) {
            // Only proceed if the image has a usemap attribute
if (window.responsiveImageMapLoaded) return;
            if (typeof($(this).attr('usemap')) === 'undefined') return;
window.responsiveImageMapLoaded = true;
 
            // Handle WebKit image load issues by using onload
function rwdImageMap($img) {
            $('<img />').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;


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


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


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


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


// The ImageMap MediaWiki extension uses the same map name for
                    for (var i = 0; i < coords.length; ++i) {
// identical maps on different images (probably the result of
                        // Recalculate each coordinate based on image scaling
// a hash function). As such, the manipulation must be limited
                        if (i % 2 === 0)
// 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);
                            coordsPercent[i] = parseInt(((coords[i] / h) * 100) * hPercent);
if (!$this.data(c))
                    }
$this.data(c, $this.attr(c));


var coords = $this.data(c).split(','),
                    // Update the area element's coords with recalculated coordinates
coordsPercent = new Array(coords.length);
                    $this.attr(c, coordsPercent.join(','));
                });
            }).attr('src', $that.attr('src')); // Trigger image load
        });
    }


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


mw.hook('wikipage.content').add(function($e) {
    // Trigger on window resize
    var $img =
    $(window).resize(function() {
    $e.find('.responsive-imagemap .noresize:not(.made-responsive)').css({
        rwdImageMap($('.responsive-imagemap .made-responsive img[usemap]'));
        '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 18:34, 27 May 2025

;(function(window, $, mw) {
    if (window.responsiveImageMapLoaded) return;
    window.responsiveImageMapLoaded = true;

    function rwdImageMap($img) {
        $img.each(function() {
            var that = this;
            var $that = $(that);

            // Only proceed if the image has a usemap attribute
            if (typeof($(this).attr('usemap')) === 'undefined') return;

            // Handle WebKit image load issues by using onload
            $('<img />').on('load', function() {
                var attrW = 'width', attrH = 'height';
                var w = $that.attr(attrW), h = $that.attr(attrH);

                // Fallback if width/height attributes are missing
                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';

                // Update each area element's coordinates within the 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 = [];

                    for (var i = 0; i < coords.length; ++i) {
                        // Recalculate each coordinate based on image scaling
                        if (i % 2 === 0)
                            coordsPercent[i] = parseInt(((coords[i] / w) * 100) * wPercent);
                        else
                            coordsPercent[i] = parseInt(((coords[i] / h) * 100) * hPercent);
                    }

                    // 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
    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);
    });

    // Trigger on window resize
    $(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.