"use strict"; //if (!console) console = { log: function() {} }; var componentconstructors = {}; var maptypes = {}; var map = null; // Leaflet assumes top-level 'map'... componentconstructors['testcomponent'] = function(dynmap, configuration) { /* console.log('initialize'); $(dynmap).bind('worldchanged', function() { console.log('worldchanged'); }); $(dynmap).bind('mapchanging', function() { console.log('mapchanging'); }); $(dynmap).bind('mapchanged', function() { console.log('mapchanged'); }); $(dynmap).bind('zoomchanged', function() { console.log('zoomchanged'); }); $(dynmap).bind('worldupdating', function() { console.log('worldupdating'); }); $(dynmap).bind('worldupdate', function() { console.log('worldupdate'); }); $(dynmap).bind('worldupdated', function() { console.log('worldupdated'); }); $(dynmap).bind('worldupdatefailed', function() { console.log('worldupdatefailed'); }); $(dynmap).bind('playeradded', function() { console.log('playeradded'); }); $(dynmap).bind('playerremoved', function() { console.log('playerremoved'); }); $(dynmap).bind('playerupdated', function() { console.log('playerupdated'); }); */ }; function DynMap(options) { var me = this; if(me.checkForSavedURL()) return; me.options = options; $.getJSON(me.options.url.configuration, function(configuration) { if(configuration.error == 'login-required') { me.saveURL(); window.location = 'login.html'; } else if(configuration.error) { alert(configuration.error); } else { me.configure(configuration); me.initialize(); } }, function(status, statusMessage) { //alert('Could not retrieve configuration: ' + statusMessage); }); } DynMap.prototype = { components: [], worlds: {}, registeredTiles: [], players: {}, lasttimestamp: new Date().getUTCMilliseconds(), /* Pseudorandom - prevent cached '?0' */ reqid: 0, servertime: 0, serverday: false, inittime: new Date().getTime(), followingPlayer: '', initfollow: null, missedupdates: 0, maxcount: -1, currentcount: 0, playerfield: null, layercontrol: undefined, nogui: true, formatUrl: function(name, options) { var url = this.options.url[name]; $.each(options, function(n,v) { url = url.replace("{" + n + "}", v); }); return url; }, configure: function(configuration) { var me = this; $.extend(me.options, configuration); $.each(me.options.worlds, function(index, worldentry) { var world = me.worlds[worldentry.name] = $.extend({}, worldentry, { maps: {} }); $.each(worldentry.maps, function(index, mapentry) { var map = $.extend({}, mapentry, { world: world, dynmap: me }); map = world.maps[mapentry.name] = maptypes[mapentry.type](map); if(me.options.defaultmap && me.options.defaultmap == mapentry.name) world.defaultmap = map; world.defaultmap = world.defaultmap || map; }); me.defaultworld = me.defaultworld || world; }); var urlarg = me.getParameterByName('worldname'); if(urlarg == "") urlarg = me.options.defaultworld || ""; if(urlarg != "") { me.defaultworld = me.worlds[urlarg] || me.defaultworld; } urlarg = me.getParameterByName('mapname'); if(urlarg != "") { me.defaultworld.defaultmap = me.defaultworld.maps[urlarg] || me.defaultworld.defaultmap; } urlarg = me.getIntParameterByName('x'); if(urlarg != null) me.defaultworld.center.x = urlarg; urlarg = me.getIntParameterByName('y'); if(urlarg != null) me.defaultworld.center.y = urlarg; urlarg = me.getIntParameterByName('z'); if(urlarg != null) me.defaultworld.center.z = urlarg; urlarg = me.getParameterByName('nogui'); if(urlarg != "") { me.nogui = (urlarg == 'true'); } }, initialize: function() { var me = this; // Get a handle to the DOM element which acts as the overall container and apply a class of // "dynmap" to it. var container = $(me.options.container); container.addClass('dynmap'); // Create a new container within the main container which actually holds the map. It needs a // class of "map". var mapContainer; (mapContainer = $('
')) .addClass('map') .appendTo(container); // Set the title if the options specify one. if(me.options.title) document.title = me.options.title; // Try to set the default zoom level based on the URL parameter. var urlzoom = me.getIntParameterByName('zoom'); if(urlzoom != null) me.options.defaultzoom = urlzoom; // Decide whether or not the layer control will be visible based on the URL parameter or // or fallback to the options var showlayerctl = me.getParameterByName('showlayercontrol'); if(showlayerctl != "") me.options.showlayercontrol = showlayerctl; // If we still don't have a default zoom level, force it to be 1 if(typeof me.options.defaultzoom == 'undefined') me.options.defaultzoom = 7; // Decide whether we should be following a given player or not based solely on URL parameter. var initfollowplayer = me.getParameterByName('playername'); if(initfollowplayer != "") me.initfollow = initfollowplayer; // Derive the state of the sidebar based on the URL parameter. var sidebaropen = false;//me.getParameterByName('sidebaropened'); if(sidebaropen == 'false' || sidebaropen == 'true' || sidebaropen == 'pinned') me.options.sidebaropened = sidebaropen; var map = this.map = new L.Map(mapContainer.get(0), { zoom: me.options.defaultzoom, center: new L.LatLng(0, 0), zoomAnimation: false,//true, zoomControl: false,//!me.nogui, attributionControl: false, crs: L.extend({}, L.CRS, { code: 'simple', projection: { project: function(latlng) { // Direct translation of lat -> x, lng -> y. return new L.Point(latlng.lat, latlng.lng); }, unproject: function(point) { // Direct translation of x -> lat, y -> lng. return new L.LatLng(point.x, point.y); } }, // a = 1; b = 2; c = 1; d = 0 // x = a * x + b; y = c * y + d // End result is 1:1 values during transformation. transformation: new L.Transformation(1, 0, 1, 0), scale: function(zoom) { // Equivalent to 2 raised to the power of zoom, but faster. return (1 << zoom); } }), continuousWorld: true, worldCopyJump: false, scrollwheel: false, disableDoubleClickZoom: true, draggable: false }); window.map = map; // Placate Leaflet need for top-level 'map'.... map.on('zoomend', function() { //me.maptype.updateTileSize(me.map.getZoom()); //$(me).trigger('zoomchanged'); }); /*google.maps.event.addListener(map, 'dragstart', function(mEvent) { me.followPlayer(null); });*/ // Sidebar var panel; var sidebar; var pinbutton; var nopanel = (me.getParameterByName('nopanel') == 'true') || me.nogui; if(me.options.sidebaropened != 'true') { // false or pinned var pincls = 'pinned' if(me.options.sidebaropened == 'false') pincls = ''; sidebar = me.sidebar = $('
') .addClass('sidebar ' + pincls); panel = $('
') .addClass('panel') .appendTo(sidebar); // Pin button. pinbutton = $('
') .addClass('pin') .click(function() { sidebar.toggleClass('pinned'); }) .appendTo(panel); } else { sidebar = me.sidebar = $('
') .addClass('sidebar pinned'); panel = $('
') .addClass('panel') .appendTo(sidebar); } if(!nopanel) sidebar.appendTo(container); // World scrollbuttons var upbtn_world = $('
') .addClass('scrollup'); /*.bind('mousedown mouseup touchstart touchend', function(event){ if(event.type == 'mousedown' || event.type == 'touchstart'){ worldlist.animate({"scrollTop": "-=300px"}, 3000, 'linear'); }else{ worldlist.stop(); } });*/ var downbtn_world = $('
') .addClass('scrolldown'); /*.bind('mousedown mouseup touchstart touchend', function(event){ if(event.type == 'mousedown' || event.type == 'touchstart'){ worldlist.animate({"scrollTop": "+=300px"}, 3000, 'linear'); }else{ worldlist.stop(); } });*/ // Worlds var worldlist; $('
') .append($('').text(me.options['msg-maptypes'])) .append(upbtn_world) .append(me.worldlist = worldlist = $('