Newer
Older
DH_Apicture / public / static / libs / mapbox / extend / XYZLayer1.js
@wudi wudi 23 days ago 2 KB 1
(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ?
		module.exports = factory() :
		typeof define === 'function' && define.amd ? define(factory) :
			(global = typeof globalThis !== 'undefined' ? globalThis :
				global || self, global.mapboxgl1.XYZLayer1 = factory());
}(window,(function(){

	function generateUUID() {
		let d = new Date().getTime();
		let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
			let r = (d + Math.random()*16)%16 | 0;
			d = Math.floor(d/16);
			return (c=='x' ? r : (r&0x3|0x8)).toString(16);
		});
		return uuid;
	}

	return class XYZLayer1 {
		id=null;
		map=null;
		type="raster";
		params=null;
		constructor(options={id:'',url:'',layers:'',subdomains:[]}) {
			this.params = options;
			this.id = options.id || generateUUID();
			this.newfiberId = this.id;
		}

		remove(){
			if(!this.map) return console.warn(`Please trigger "addTo" This method is called after the "map" instance is passed in;`);
			let layer = this.map.getLayer(this.id);
			if(!layer) return console.warn(`The current layer has not been added to the map;`);
			this.map.removeLayer(this.id);
			this.map.removeSource(this.id);
		}

		addTo(map){
			if(!map) return console.warn(`Configure the map instance;`);
			this.map = map;
			if(!this.params.url) return console.error(`The current object is missing important parameters. Please add them and trigger the method again; url:${this.params.url};url:${this.params.layers}`);
			let urls = this.params.subdomains.map(k => this.params.url.replaceAll('{s}',k));
			if(!(urls.length)) urls = [this.params.url];
			this.map.addSource(this.id, {'type':this.type, 'tiles': urls, 'tileSize': this.params.width || 256});
			this.map.addLayer({'id':this.id, 'type':this.type, 'source': this.id, 'paint': {},minzoom:this.params.minZoom || 0,maxzoom:this.params.maxZoom||22});
		}

		show(){
			this.visible = true;
			this.setLayerParams({visibility:'visible'});
		}

		hide(){
			this.visible = false;
			this.setLayerParams({visibility:'none'});
		}

		setOpacity(opacity){
			this.setLayerParams({},{'raster-opacity':opacity});
		}

		setInitializeParams(params={}){
			this.params = Object.assign(this.params,params);
		}

		setLayerParams(layout={},paint={}){
			let layoutKeys = Object.keys(layout);
			let paintKeys = Object.keys(paint);
			layoutKeys.forEach(key => this.map.setLayoutProperty(this.id,key,layout[key]));
			paintKeys.forEach(key => this.map.setPaintProperty(this.id,key,paint[key]));
		}
	}
})));