1 define(["dojo/_base/declare"], function(declare){ 2 var mapEvent = null; 3 return declare(null,{ 4 "id": '', 5 "layer": null, 6 "treeList": [], 7 "map":null, 8 constructor: function(id,layer,map){ 9 this.id = id || this.id; 10 this.layer = layer || this.layer; 11 this.map = map || this.map; 12 }, 13 getLayerList: function(){ 14 //获取图层信息 15 var visibleLayer = []; 16 var node = {}; 17 var layerinfos = this.layer.layerInfos; 18 var parentnodes = []; //保存所有父节点 19 var root = { 20 "id": "root", 21 "text": "所有图层", 22 "children": [] 23 };//根节点 24 25 if(layerinfos != null && layerinfos.length > 0){ 26 var len = layerinfos.length; 27 for(var i=0; i<len; i++){ 28 //debugger; 29 var info = layerinfos[i]; 30 if(info.defaultVisibility){ //获取未隐藏图层的ID 31 visibleLayer.push(info.id); 32 } 33 node = { 34 "id": info.id, 35 "text": info.name, 36 "pid": info.parentLayerId, 37 "checked": info.defaultVisibility ? true:false, 38 "children":[] 39 }; 40 if(info.parentLayerId == -1){ 41 root.children.push(node); 42 parentnodes.push(node); 43 }else{ 44 getChildrenNodes(parentnodes, node); 45 parentnodes.push(node); 46 } 47 } 48 } 49 //console.log(parentnodes) 50 this.treeList.push(root); 51 this.layer.setVisibleLayers(visibleLayer);//初始化显示图层 52 }, 53 setLayerVisibility: function(){ 54 //设置图层可见性 55 var _this = this; 56 $("#"+_this.id).tree({ 57 data: _this.treeList, 58 checkbox: true, 59 onCheck: function(node, checked){//更新显示选择的图层 60 var checkedNodes = $("#"+_this.id).tree("getChecked");//获取所有被选中的节点 61 var visible = []; 62 $.each(checkedNodes, function(i, cn){ 63 visible.push(cn.id); 64 }) 65 if(visible.length === 0){ 66 visible.push(-1); 67 } 68 //console.log($("#"+_this.id).tree("getSelected")); 69 _this.layer.setVisibleLayers(visible); 70 } 71 }); 72 if(mapEvent){ 73 mapEvent.remove(); 74 } 75 var lastNode = null; 76 mapEvent = _this.map.on('update-end', function(){ 77 var level = _this.map.getLevel(); 78 var nodeId = 80; 79 switch(level){ 80 case 14: nodeId = 80; break; 81 case 15: nodeId = 72; break; 82 case 16: nodeId = 59; break; 83 case 17: nodeId = 42; break; 84 case 18: nodeId = 21; break; 85 case 19: nodeId = 0; break; 86 default: nodeId = 80; break; 87 } 88 try{ 89 if(lastNode){ 90 $("#"+_this.id).tree("collapse",lastNode.target); 91 } 92 var node1 = $("#"+_this.id).tree('find', nodeId); 93 $("#"+_this.id).tree('select', node1.target); 94 $("#"+_this.id).tree("expand",node1.target); 95 lastNode = node1; 96 }catch(e){ 97 98 } 99 }); 100 }, 101 removeCls: function(){ 102 $("#"+this.id+" span").removeClass('tree-folder tree-file'); 103 } 104 }); 105 function getChildrenNodes(pnodes, node){ 106 107 for(var i=pnodes.length-1; i>=0; i--){ 108 var pnode = pnodes[i]; 109 //如果是父子关系,为父节点增加子节点,退出for循环 110 if(pnode.id == node.pid){ 111 pnode.state = "closed"; //关闭二级树 112 pnode.children.push(node); 113 return; 114 }else{ 115 //如果不是父子关系,删除父节点栈里的当前节点 116 pnodes.pop(); 117 } 118 } 119 } 120 })