MediaWiki:DataMaps.js

From Official Temtem Wiki
Jump to navigation Jump to search

In other languages: EspañolFrançais


CSS and Javascript changes must comply with the wiki design rules.


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.
mw.loader.using( [ 'ext.datamaps.core' ], function () {

var DataMaps = require( 'ext.datamaps.core' );

/**
* For postal service map. Hide other markers when a popup is opened (by hand or by search).
*
* - We create a visibility requirement on our layer "_temtem_popupOpen".
* - And we inject the layer. The attached layers list may be shared, so a copy is needed.
*
*   Properties could be used (although their container can be uninitialised or shared (& frozen) within a streaming batch
*   - so initialising and cloning it is our job), but v0.15 does not use the container for visibility checks and only
*   different, initialised values are filtered (it does not act as a requirement).
*
* Caveat: this does result in an inconsistent state internally, as the marker will be apparently attached to our layer, but
*         the visibility manager does not have it registered. This shouldn't really matter though.
*
* @author User:alex4401
*/

var POPUP_VISIBILITY_LAYER_ID = '_temtem_popupOpen';


function _installPopupVisibilityMod( map ) {
	// Monkey patch to close the popup
	var _openMarkerPopup__orig = map.openMarkerPopup;
	map.openMarkerPopup = function ( leafletMarker ) {
		leafletMarker.attachedLayers = Array.from( leafletMarker.attachedLayers );
		leafletMarker.attachedLayers.push( POPUP_VISIBILITY_LAYER_ID );
		map.layerManager.updateMembers();
		_openMarkerPopup__orig.call( this, leafletMarker );
	};
	map.layerManager.setRequirement( POPUP_VISIBILITY_LAYER_ID, true );
	// Popup logic
	map.viewport.getLeafletMap().on( 'popupclose', function ( ctx ) {
		var source = ctx.popup._source;
		if ( source && source.apiInstance ) {
			const index = source.attachedLayers.indexOf( POPUP_VISIBILITY_LAYER_ID );
			if ( index > 0 ) {
				source.attachedLayers.splice( index, 1 );
				map.layerManager.updateMembers();
			}
		}
	} );
}


/**
 * Custom background display for the postal service map
 *
 * @author User:alex4401
 */

function PSBackgroundDisplay( map ) {
	return Reflect.construct( DataMaps.Controls.MapControl, [ map, 'backgrounds-temtem' ], PSBackgroundDisplay );
}
Object.setPrototypeOf( PSBackgroundDisplay.prototype, DataMaps.Controls.MapControl.prototype );
PSBackgroundDisplay.prototype._update = function () {
	this.element.innerText = this.map.backgrounds[ this.map.getCurrentBackgroundIndex() ].displayName;
};
PSBackgroundDisplay.prototype._build = function () {
	this._update();
	this.map.on( 'backgroundChange', this._update, this );
};



/**
 *
 * Install segment
 *
 */


mw.dataMaps.onMapInitialised( function ( map ) {
	if ( map.config.custom && map.config.custom.postalCustomizations ) {
		map.on( 'leafletLoaded', function () {
			_installPopupVisibilityMod( map );
			map.viewport.addControl( DataMaps.Viewport.anchors.legend, new PSBackgroundDisplay( map ), true );
		} );
	}
}, null, mw.dataMaps.IS_COMPATIBLE_WITH_NORMAL );

// Attempt to alleviate marker search desync caused by a race condition. This may duplicate a result.
mw.dataMaps.onMapInitialised( function ( map ) {
	if ( map.checkFeatureFlag( 1 << 3 ) ) {
		mw.loader.using( [ 'ext.datamaps.search' ], function () {
			function __runSearchHack() {
				var search = map.search;
				var mvm__markers = map.layerManager.markers;
				for ( var index = search.ownedIndex.items.length - 1; index < mvm__markers.length; index++ ) {
					search.addMarker( mvm__markers[ index ] );
				}
				search.ownedIndex.commit();
			}
			setTimeout( __runSearchHack, 500 );
			setTimeout( __runSearchHack, 1000 );
		} );
	}
} );

} );