/** * 底图切换 * @author Wenyb * @date 2015/6/29 */ define([ "dojo/_base/declare", "base/BaseControl", "dojo/text!controls/mapswitch/MapSwitchControl.html", "dojo/dom-style", "dojo/on", "base/AppEvent", "base/ConfigData", "dojo/_base/array", "dojo/dom-class", "dojo/dom-construct", "dojo/mouse", "dojo/dom-attr", "dojo/_base/lang" ], function( declare, BaseControl, template, domStyle, on, AppEvent, ConfigData, arrayUtil, domClass, domConstruct, mouse, domAttr, lang) { var Widget = declare("MapSwitchControl", [BaseControl], { templateString: template, visibleGroupLayerId:null, visibleLayerSetId:null, constructor: function(options) { this.inherited(arguments); declare.safeMixin(this, options); this._grouplayers = []; this._currentVisibleLayerlabel = null; this.base_class = "mapswitch-container"; this._galleryDomTable = {}; this._maps2galleryTable = {}; this._galleryLeaveHandlerTable = {}; var baseMaps = ConfigData.basemap.grouplayers; for (var key in baseMaps) { var gl = baseMaps[key]; if (gl.visible == true) { this._currentVisibleLayerlabel = gl.label; } this._grouplayers.push(gl.label); } this.gallery = ConfigData.basemap.gallerys[0]; this.groups = ConfigData.basemap.grouplayers; this.index = []; this.galleryDom = null; AppEvent.addAppEventListener(AppEvent.SWITCH_BASEMAP, lang.hitch(this, this.onSwitchBaseMapHandler)); }, startup: function() { this.inherited(arguments); var tableDom = domConstruct.create("table", {}, this.container); domStyle.set(tableDom, "border-spacing", "0px 5px"); domStyle.set(tableDom, "margin", "0px"); arrayUtil.forEach(ConfigData.basemap.gallerys, lang.hitch(this, function(item, index) { var trDom = domConstruct.create("tr", { "galleryid": item.id }, tableDom); var tdDom = domConstruct.create("td", { "galleryid": item.id }, trDom); domClass.add(tdDom, "mapswitch-td"); this._galleryDomTable[item.id] = tdDom; this._maps2galleryTable[item.id] = []; this._galleryLeaveHandlerTable[item.id] = null; on(tdDom, mouse.enter, lang.hitch(this,this._onGalleryDomMouseEnter)); on(tdDom, mouse.leave, lang.hitch(this, this._onGalleryDomMouseLeave)); })); //构建每个地图集里面的子项 arrayUtil.forEach(ConfigData.basemap.grouplayers, lang.hitch(this, function(item, index) { var styleValue = "background:url('" + item.icon + "') center 2px no-repeat #ffffff;margin-right:5px"; var mapDom = domConstruct.create("div", { "mapid": item.id, "class":"mapswitch-div", "style":styleValue, "title":"切换到"+item.label }); on(mapDom, "click", lang.hitch(this, this._onMapDomClick)); var mapNameDom = domConstruct.create("span", { "innerHTML": item.label, "class":"mapswitch-mapname" }, mapDom); var gallerytdDom = this._galleryDomTable[item.gallery]; this._maps2galleryTable[item.gallery].push(mapDom); domConstruct.place(mapDom, gallerytdDom); if(this.visibleGroupLayerId){ domAttr.set(gallerytdDom,"visibleGroupLayerId",this.visibleGroupLayerId); }else if(item.visible){ domAttr.set(gallerytdDom,"visibleGroupLayerId",item.id); } })); arrayUtil.forEach(ConfigData.basemap.gallerys, lang.hitch(this, function(item, index){ this._hideGalleryDomByVisibleMap(item.id); })); }, /** * 隐藏地图类型的按钮,只显示一种可选按钮 */ _hideGalleryDomByVisibleMap:function(galleryId){ var gallerytdDom = this._galleryDomTable[galleryId]; var visibleLayer = domAttr.get(gallerytdDom,"visibleGroupLayerId"); var mapDoms=this._maps2galleryTable[galleryId]; var hasShowLayer=false;//是否已经显示了一个备选的图层按钮,如果已显示,则其他的都不再显示。 arrayUtil.forEach(mapDoms,function(item,index){ if(domAttr.get(item,"mapid")==visibleLayer){ domStyle.set(item,"display","none"); return; } if(hasShowLayer){ domStyle.set(item,"display","none"); }else{ domStyle.set(item,"display","block"); hasShowLayer=true; } }); }, _showGalleryDomByVisibleMap:function(galleryId){ var gallerytdDom = this._galleryDomTable[galleryId]; var visibleLayer = domAttr.get(gallerytdDom,"visibleGroupLayerId"); var mapDoms=this._maps2galleryTable[galleryId]; var hasShowLayer=false;//是否已经显示了一个备选的图层按钮,如果已显示,则其他的都不再显示。 arrayUtil.forEach(mapDoms,function(item,index){ // if(domAttr.get(item,"mapid")==visibleLayer){ // domStyle.set(item,"display","none"); // return; // } domStyle.set(item,"display","block"); hasShowLayer=true; }); }, _onMapDomClick:function(evt) { var mapid = domAttr.get(evt.currentTarget, "mapid"); AppEvent.dispatchAppEvent(AppEvent.SWITCH_BASEMAP, { hockId: this.hockId, grouplayer: mapid, sender:this.id }); var galleryId=domAttr.get(evt.currentTarget.parentNode,"galleryid"); domAttr.set(evt.currentTarget.parentNode,"visibleGroupLayerId",mapid); if (mapid == 'imglry') { this.map._layers.cia.opacity = 1 } else { this.map._layers.cia.opacity = 0 } this._hideGalleryDomByVisibleMap(galleryId); }, _onGalleryDomMouseEnter:function(evt){ var galleryName = domAttr.get(evt.currentTarget, "galleryid"); this._showGalleryDomByVisibleMap(galleryName); }, _onGalleryDomMouseLeave:function(evt){ var galleryName = domAttr.get(evt.currentTarget, "galleryid"); this._hideGalleryDomByVisibleMap(galleryName); }, onSwitchBaseMapHandler:function(evt){ if(this.id == evt.sender){ return; } if (evt.hockId != this.hockId) return; for(var key in this._galleryDomTable){ domAttr.set(this._galleryDomTable[key],"visibleGroupLayerId",evt.grouplayer); this._hideGalleryDomByVisibleMap(key); } } }); return Widget; });