MediaWiki:CompleteMap.js

From FreewarWiki
Revision as of 13:02, 8 February 2017 by Tiramon (talk | contribs) (addOnLoadHook was deprecated and is removed in 1.27)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Note:
After saving, you may have to bypass your browser's cache to see the changes.
  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.


 // Code for the [[CompleteMap]]
 // Include in [[MediaWiki:CompleteMap.js]]<nowiki>
 var routermode = false, routerloaded = false;
 var offsetX, offsetY, prefix = '', oldprefix;
 // Metadata-"Cache"
 var prefixes = Object(), crossmods = Array(), oldcrossmod = Object();
 var TopLeftXs = Object(), TopLeftYs = Object(), OffsetXs = Object(), OffsetYs = Object();
 
 function map_click(event)
 {
   if (this.id == 'map_horz' || this.id == 'map_vert') prefix = oldprefix;
   else if (this.id.substr(0, 8) == 'map_img_') prefix = this.id.substr(8);
   else prefix = '';
   offsetX = TopLeftXs[prefix + 'TopLeftX'] - 1;
   offsetY = TopLeftYs[prefix + 'TopLeftY'] - 1;
   var crossprefix = (routermode && document.map_form.map_radio[1].checked ? 'map_dest_' : 'map_');
 
   var offX;
   var offY;
   if (window.event)			// Internet Explorer, Opera, Konqueror, Safari
   {
     offX = window.event.offsetX;
     offY = window.event.offsetY;
   } else {
     offX = event.layerX;
     offY = event.layerY;
   }

   document.map_form.elements[crossprefix + 'x'].value = Number(this.id == 'map_vert' || this.id == 'map_dest_vert' ? document.map_form.elements[this.id.substr(0, this.id.length - 4) + 'x'].value : Math.floor((offX - 2) / 15) + offsetX + Number(!oldcrossmod[this.id.substr(0, this.id.length - 4)] ? 0 : oldcrossmod[this.id.substr(0, this.id.length - 4)][4]));
   document.map_form.elements[crossprefix + 'y'].value = Number(this.id == 'map_horz' || this.id == 'map_dest_horz' ? document.map_form.elements[this.id.substr(0, this.id.length - 4) + 'y'].value : Math.floor((offY - 2) / 15) + offsetY + Number(!oldcrossmod[this.id.substr(0, this.id.length - 4)] ? 0 : oldcrossmod[this.id.substr(0, this.id.length - 4)][6]));

   show_map_position(crossprefix);
   if (routermode) find_way();
   oldprefix = prefix;
 }
 
 function get_coords_position(str, x, y)
 {
   // Auxiliary function: determining position of "x,y" in str, so that before and after the result no digit stands.
   if (str.indexOf(' ' + x + ',' + y + ';') > -1) return str.indexOf(' ' + x + ',' + y + ';') + 1;
   else if (str.indexOf(' ' + x + ',' + y + '<') > -1) return str.indexOf(' ' + x + ',' + y + '<') + 1;
   else if (str.indexOf('}' + x + ',' + y + ';') > -1) return str.indexOf('}' + x + ',' + y + ';') + 1;
   else return str.indexOf('}' + x + ',' + y + '<') + 1;
 }
 
 function show_map_position(crossprefix)
 {
   var map_horz = document.getElementById(crossprefix + 'horz');
   var map_vert = document.getElementById(crossprefix + 'vert');
 
   var area = area_map_position(document.map_form.elements[crossprefix + 'x'].value, document.map_form.elements[crossprefix + 'y'].value);
   if (prefix == '?') {
     if (document.getElementById('prefix' + area.replace(/\s/g, '_')))
       prefix = document.getElementById('prefix' + area.replace(/\s/g, '_')).firstChild.nodeValue;
     else prefix = '';
     offsetX = TopLeftXs[prefix + 'TopLeftX'] - 1;
     offsetY = TopLeftYs[prefix + 'TopLeftY'] - 1;
   }

   var map_out_area = document.getElementById('map_out_area');
   var map_out_place = document.getElementById('map_out_place');
   if (area != '' || routermode)
   {
     map_out_area.firstChild.href = '/index.php/' + area;
     map_out_area.firstChild.firstChild.nodeValue = area;
     map_out_area.firstChild.nextSibling.nextSibling.href = '/index.php/Map:' + area;
     place = place_map_position(document.map_form.elements[crossprefix + 'x'].value, document.map_form.elements[crossprefix + 'y'].value);
     if (place != '')
     {
       map_out_place.firstChild.nextSibling.firstChild.nodeValue = place;
       map_out_place.firstChild.nextSibling.href = '/index.php/' + place;
       map_out_place.style.display = 'inline';
     } else
       map_out_place.style.display = 'none';
     map_out_area.style.visibility = 'visible';
   } else
     map_out_area.style.visibility = 'hidden';
   if (area == '' || document.map_form.elements[crossprefix + 'x'].value < 1 || document.map_form.elements[crossprefix + 'y'].value < 1)
   {
     map_horz.style.display = 'none';
     map_vert.style.display = 'none';
     return false;
   }
   var imgOffsetX = Number(OffsetXs[prefix + 'OffsetX']);
   var imgOffsetY = Number(OffsetYs[prefix + 'OffsetY']);
   var mod_left = 0, mod_top = 0, mod_width = 0, mod_height = 0;
   oldcrossmod[crossprefix] = null;
   for (var i in crossmods) {
     var mod = crossmods[i].split('|');
     if (Number(document.map_form.elements[crossprefix + 'x'].value) >= mod[0]
         && Number(document.map_form.elements[crossprefix + 'x'].value) <= mod[2]
         && Number(document.map_form.elements[crossprefix + 'y'].value) >= mod[1]
         && Number(document.map_form.elements[crossprefix + 'y'].value) <= mod[3]) {
       mod_left   = Number(mod[4]);
       mod_width  = Number(mod[5]);
       mod_top    = Number(mod[6]);
       mod_height = Number(mod[7]);
       oldcrossmod[crossprefix] = mod;
       break;
     }
   }
   map_horz.style.left = String((imgOffsetX + mod_left) * 15) + 'px';
   map_vert.style.top = String((imgOffsetY + mod_top) * 15) + 'px';
   map_horz.style.width = String((mod_width ? mod_width : (document.getElementById(prefix + 'BottomRightX').firstChild.nodeValue - TopLeftXs[prefix + 'TopLeftX'] + 3)) * 15) + 'px';
   map_vert.style.height = String((mod_height ? mod_height : (document.getElementById(prefix + 'BottomRightY').firstChild.nodeValue - TopLeftYs[prefix + 'TopLeftY'] + 3)) * 15) + 'px';
   map_horz.style.top = String((Number(document.map_form.elements[crossprefix + 'y'].value) + imgOffsetY - Number(offsetY)) * 15) + 'px';
   map_vert.style.left = String((Number(document.map_form.elements[crossprefix + 'x'].value) + imgOffsetX - Number(offsetX)) * 15) + 'px';
 
   map_horz.style.display = 'block';
   map_vert.style.display = 'block';
 
   return false;
 }
 
 function press_map_button() {
   prefix = '?';
   show_map_position('map_');
   if (routermode) {
     prefix = '?';
     show_map_position('map_dest_');
     find_way();
   }
   return false;
 }
 
 function init_map()
 {
   var map_nav = document.getElementById('map_nav');
   if (map_nav)
   {
     map_nav.getElementsByTagName('big')[0].style.display = 'none';
     var map_form, map_x, map_y, map_submit, map_maplink, map_link_img;
     map_form = document.createElement('form');
     map_form.name = 'map_form';
     map_form.onsubmit = press_map_button;
     map_nav.insertBefore(map_form, map_nav.firstChild);
     map_form.appendChild(map_nav.getElementsByTagName('table')[0]);
 
     map_x = document.createElement('input');	map_y = document.createElement('input');
     map_x.type = 'text';			map_y.type = 'text';
     map_x.id = 'map_x'; 			map_y.id = 'map_y';
     map_x.size = '3';				map_y.size = '3';
     map_x.style.textAlign = 'right';		map_y.style.textAlign = 'right';
 
     with (document.getElementById('map_start')) {
       appendChild(document.createTextNode('X: '));
       appendChild(map_x);
       appendChild(document.createTextNode(' Y: '));
       appendChild(map_y);
     }
 
     map_submit = document.createElement('input');
     map_submit.type = 'submit';
     map_submit.value = 'OK';
     document.getElementById('map_button').appendChild(map_submit);
     
     map_maplink = document.createElement('a');
     map_link_img = document.createElement('img');
     map_link_img.src = 'http://www.fwwiki.org/images/0/03/12px-Earth.png';
     map_link_img.border = '0';
     map_maplink.appendChild(map_link_img);
     document.getElementById('map_out_area').appendChild(map_maplink);
 
     n = document.getElementById('map').firstChild;
     do {
       if (n.nodeName == 'DIV') n.onclick = map_click;
     } while (n = n.nextSibling);
 
     init_map_metadata(document.getElementById('metadata'));
 
     document.getElementById('map_link_normal').firstChild.href = 'javascript:switch_mode(0);';
     document.getElementById('map_link_normal').style.display = 'inline';
     document.getElementById('map_link_router').firstChild.href = 'javascript:switch_mode(1);';
     document.getElementById('map_link_router').style.display = 'inline';
 
     document.getElementById('map_dest').firstChild.style.display = 'none';
   }
 }
 
 function init_map_metadata(n) {
   n = n.firstChild;
   do {
     if (n.id) {
       if (n.id.substr(0, 6) == 'prefix') prefixes[n.id] = n.firstChild.nodeValue;
       if (n.id.substr(n.id.length - 8) == 'TopLeftX') TopLeftXs[n.id] = n.firstChild.nodeValue;
       if (n.id.substr(n.id.length - 8) == 'TopLeftY') TopLeftYs[n.id] = n.firstChild.nodeValue;
       if (n.id.substr(n.id.length - 7) == 'OffsetX') OffsetXs[n.id] = n.firstChild.nodeValue;
       if (n.id.substr(n.id.length - 7) == 'OffsetY') OffsetYs[n.id] = n.firstChild.nodeValue;
     }
     switch (n.className) {
       case 'crossmod':
         crossmods[crossmods.length] = n.firstChild.nodeValue;
         break;
     }
     // Recursion, because it can give wrapping <p> and <pre> tags.
     if (n.tagName) init_map_metadata(n);
   } while (n = n.nextSibling);
 }
 
 function switch_mode(mode) {
   if (mode == routermode) return;
   routermode = mode;
   var tmp = document.getElementById('map_link_normal').parentNode.getAttribute('style');
   document.getElementById('map_link_normal').parentNode.setAttribute('style', document.getElementById('map_link_router').parentNode.getAttribute('style'));
   document.getElementById('map_link_router').parentNode.setAttribute('style', tmp);
   if (mode == 1 && !routerloaded) init_router();
   document.getElementById('map_dest').style.display = (mode == 1 ? 'block' : 'none');
   document.getElementById('map_tools').style.display = (mode == 1 ? 'block' : 'none');
   document.getElementById('map_out_route').style.display = (mode == 1 ? 'inline' : 'none');
   document.getElementById('map_out_area').style.display = (mode == 1 ? 'none' : 'inline');
   document.getElementById('map_label_start').style.display = (mode == 1 ? 'inline' : 'none');
   document.getElementById('map_label_dest').style.display = (mode == 1 ? 'inline' : 'none');
   document.getElementById('map_label_mouse').style.display = (mode == 1 ? 'inline' : 'none');
   document.getElementById('map_path').style.display = (mode == 1 ? 'inline' : 'none');
   document.getElementById('map_dest_horz').style.display = document.getElementById('map_dest_vert').style.display = (mode == 1 && document.map_form.elements['map_dest_x'].value > 0 && document.map_form.elements['map_dest_y'].value > 0 && area_map_position(document.map_form.elements['map_dest_x'].value, document.map_form.elements['map_dest_y'].value) ? 'block' : 'none');
   if (!mode) press_map_button();
 }
 
 function area_map_position(x, y)
 {
   // Zumindest FF teilt Textknoten nach 2^12 Zeichen, also alle Kindknoten aneinanderreihen
   var ret = '', n = document.getElementById('coordlist').firstChild;
   do {
     ret = ret + n.nodeValue;
   } while (n = n.nextSibling);
   ret = ret.substring(0, get_coords_position(ret, x, y));
   ret = ret.substring(ret.lastIndexOf('{') + 13);			// 13 is the length of "arealink|" + 1
   ret = ret.substring(0, ret.indexOf('}'));
   return ret;
 }
 
 function place_map_position(x, y)
 {
   // Zumindest FF teilt Textknoten nach 2^12 Zeichen, also alle Kindknoten aneinanderreihen
   var ret = '', n = document.getElementById('sitelist').firstChild;
   do {
     ret = ret + n.nodeValue;
   } while (n = n.nextSibling);
   ret = ret.substring(0, get_coords_position(ret, x, y));
   ret = ret.substring(ret.lastIndexOf('[') + 1);
   ret = ret.substring(0, ret.indexOf(']'));
   return ret;
 }
 
 jQuery( document ).ready( function( $ ) {
	init_map();
} );
//addOnloadHook(init_map);
 
 // </nowiki>