MediaWiki:Gadget-MapFrame.js

From Wikivoyage
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Usage: inserts an iframe into a div with id "mapdiv", in conjunction with Template:MapFrame
          for embeddable dynamic maps. Relies on HTML5 data parameters.  */                    

function insertIFrame() {
    var exhtml = '//tools.wmflabs.org/wikivoyage/w/poimap2.php?';
    // hide the map iframe by default - only show if gadget is enabled
    $('#mapwrap').show();
    var map = $('#mapdiv');
    if ( map.length ) {
        validateMap(map);
        var exsrc = exhtml+'lat='+map.data("lat")+'&lon='+map.data("long")
            +'&zoom='+map.data("zoom")+'&layer='+map.data("layer")+'&lang='+mw.config.get('wgContentLanguage')+'&name='+mw.config.get('wgPageName');
 
        var ifstr = '<iframe src="'+exsrc+'" width="'+map.data("width")+'" height="'+map.data("height")
            +'" style="border:0"></iframe>';
        map.html(ifstr);
        if ( map.data("layer").indexOf('M') < 0 ) {
            $('.vcard a').attr('href', function () { return this.href.replace('layer=M','layer='+map.data("layer")[0]) });
        }
    }
}

function validateMap(map) {
    if (!$.isNumeric(map.data("lat"))) map.data("lat", 51.47766);
    if (!$.isNumeric(map.data("long"))) map.data("long", -0.00115);
    if (!$.isNumeric(map.data("zoom")) && map.data("zoom") != 'auto') map.data("zoom", 14);
    if (!$.isNumeric(map.data("height"))) map.data("height", 420);
    if (!$.isNumeric(map.data("width"))) map.data("width", 420);
    if (map.data("layer").length > 8) map.data("layer", 'M');
}

jQuery(document).ready(insertIFrame);