| 1 | {{{ |
| 2 | #!html |
| 3 | <script type="text/javascript" src="js/jquery.qtip-1.0.0-rc3.min.js"></script> |
| 4 | |
| 5 | <link rel="stylesheet" href="/openlayers/theme/default/style.css" type="text/css" /> |
| 6 | |
| 7 | <script type="text/javascript"> |
| 8 | <![CDATA[ |
| 9 | var previousMode='NONE'; |
| 10 | var editMode='NONE'; |
| 11 | var map, layerLS,tselect, editable,efeature,modify,mapControls; |
| 12 | var MAPFILE='/var/www/localhost/htdocs/ZOODEMOREP/wfs.map'; |
| 13 | var layerNames=['test001']; |
| 14 | |
| 15 | OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3; |
| 16 | |
| 17 | function init(){ |
| 18 | map = new OpenLayers.Map('map', { controls:[new OpenLayers.Control.Navigation()]}); |
| 19 | |
| 20 | |
| 21 | var saveStrategy = new OpenLayers.Strategy.Save(); |
| 22 | wfsPolygon = new OpenLayers.Layer.WFS( |
| 23 | "Editable Features", '/cgi-bin/mapserv?map='+MAPFILE, |
| 24 | {typename: 'test001'}, |
| 25 | { extractAttributes: true,isBaseLayer: true} |
| 26 | ); |
| 27 | |
| 28 | editable = new OpenLayers.Layer.Vector("Selection"); |
| 29 | |
| 30 | map.addLayers([editable,wfsPolygon]); |
| 31 | map.zoomToExtent(new OpenLayers.Bounds(2.1822340,41.3932440,2.1822342,41.3932442)); |
| 32 | |
| 33 | tselect=new OpenLayers.Control.SelectFeature(wfsPolygon, {callbacks: {'click':feature_info}}) |
| 34 | mapControls = { |
| 35 | pan: new OpenLayers.Control.Pan({title:"Pan"}), |
| 36 | zoomin: new OpenLayers.Control.ZoomBox({title:"Zoom in box", out: false}), |
| 37 | zoomout: new OpenLayers.Control.ZoomBox({title:"Zoom out box", out: true}), |
| 38 | createpolygon: new OpenLayers.Control.DrawFeature(editable, OpenLayers.Handler.Polygon), |
| 39 | select: tselect//new OpenLayers.Control.SelectFeature(wfsPolygon, {callbacks: {'click':feature_info}}) |
| 40 | }; |
| 41 | |
| 42 | for(var key in mapControls) { |
| 43 | control = mapControls[key]; |
| 44 | map.addControl(control); |
| 45 | } |
| 46 | |
| 47 | var history = new OpenLayers.Control.NavigationHistory(); |
| 48 | map.addControl(history); |
| 49 | |
| 50 | var btnPrev = new OpenLayers.Control.Panel({ |
| 51 | div: document.getElementById("btnPrev"), displayClass:"prev" |
| 52 | }); |
| 53 | var btnNext = new OpenLayers.Control.Panel({ |
| 54 | div: document.getElementById("btnNext"), displayClass:"next" |
| 55 | }); |
| 56 | |
| 57 | map.addControl(btnPrev); |
| 58 | btnPrev.addControls([history.previous]); |
| 59 | map.addControl(btnNext); |
| 60 | |
| 61 | btnNext.addControls([history.next]); |
| 62 | }; |
| 63 | |
| 64 | function toggleControl(element) { |
| 65 | for(key in mapControls) { |
| 66 | var control = mapControls[key]; |
| 67 | if(element.name == key && $(element).is('.ui-state-active') && |
| 68 | !$(element).is('.zoo-action')) { |
| 69 | control.activate(); |
| 70 | } else { |
| 71 | control.deactivate(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | function cancelWFSTEdit(){ |
| 77 | try{modify.unselectFeature(editable.features[0]);}catch(e){} |
| 78 | editable.removeFeatures(editable.features); |
| 79 | try{ |
| 80 | for(var i in efeature.attributes){ |
| 81 | try{ |
| 82 | document.getElementById("l_"+i).value=""; |
| 83 | }catch(e){/*alert(i+" "+e);*/} |
| 84 | } |
| 85 | }catch(e){} |
| 86 | modify.deactivate(); |
| 87 | mapControls.select=tselect;//new OpenLayers.Control.SelectFeature(wfsPolygon, {callbacks: {'click':feature_info}}) |
| 88 | map.addControl(mapControls.select); |
| 89 | } |
| 90 | |
| 91 | var local_fields=["osm_id","name","type"]; |
| 92 | function getInsertRequest(){ |
| 93 | query='<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" version="1.0.0" service="WFS"><wfs:Insert><test001>'; |
| 94 | for(var i=0;i< local_fields.length;i++){ |
| 95 | try{ |
| 96 | if(document.getElementById("l_"+local_fields[i]).value!="") |
| 97 | query+='<'+local_fields[i]+'>'+document.getElementById("l_"+local_fields[i]).value+'</'+local_fields[i]+'>'; |
| 98 | document.getElementById("l_"+local_fields[i]).value=""; |
| 99 | }catch(e){/*alert(i+" "+e);*/} |
| 100 | } |
| 101 | try{ |
| 102 | var toto=new OpenLayers.Format.GML(); |
| 103 | var toto1=toto.buildGeometryNode(editable.features[0].geometry); |
| 104 | var str=(new XMLSerializer()).serializeToString(toto1); |
| 105 | query+='<msGeometry>'+str+'</msGeometry>'; |
| 106 | }catch(e){alert(e);} |
| 107 | query+='</test001></wfs:Insert></wfs:Transaction>'; |
| 108 | editMode='NONE'; |
| 109 | mapControls.select=tselect;//new OpenLayers.Control.SelectFeature(wfsPolygon, {callbacks: {'click':feature_info}}); |
| 110 | map.addControl(mapControls.select); |
| 111 | mapControls.select.activate(); |
| 112 | return query; |
| 113 | } |
| 114 | |
| 115 | function getUpdateRequest(){ |
| 116 | query='<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" version="1.0.0" service="WFS"><wfs:Update typeName="test001">'; |
| 117 | for(var i in efeature.attributes){ |
| 118 | try{ |
| 119 | query+='<wfs:Property><wfs:Name>'+i+'</wfs:Name><wfs:Value>'+document.getElementById("l_"+i).value+'</wfs:Value></wfs:Property>'; |
| 120 | }catch(e){/*alert(i+" "+e);*/} |
| 121 | } |
| 122 | try{ |
| 123 | var toto=new OpenLayers.Format.GML(); |
| 124 | var toto1=toto.buildGeometryNode(editable.features[0].geometry); |
| 125 | var str=(new XMLSerializer()).serializeToString(toto1); |
| 126 | query+='<wfs:Property><wfs:Name>msGeometry</wfs:Name><wfs:Value>'+str+'</wfs:Value></wfs:Property>'; |
| 127 | }catch(e){alert(e);} |
| 128 | query+='<ogc:Filter><ogc:FeatureId fid="test001.'+efeature.attributes[local_fields[0]]+'" /></ogc:Filter>'; |
| 129 | query+='</wfs:Update></wfs:Transaction>'; |
| 130 | return query; |
| 131 | } |
| 132 | |
| 133 | function getDeleteRequest(){ |
| 134 | query='<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" version="1.0.0" service="WFS"><wfs:Delete typeName="test001">'; |
| 135 | query+='<ogc:Filter><ogc:FeatureId fid="test001.'+efeature.attributes[local_fields[0]]+'" /></ogc:Filter>'; |
| 136 | query+='</wfs:Delete></wfs:Transaction>'; |
| 137 | return query; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | function saveWFSTEdit(){ |
| 142 | query=editMode=='Insert'?getInsertRequest():editMode=='Delete'?getDeleteRequest():getUpdateRequest(); |
| 143 | var request = new OpenLayers.Request.XMLHttpRequest(); |
| 144 | request.open('POST','/cgi-bin/proxy-wfst.cgi?map='+MAPFILE+'&url=http://localhost/cgi-bin-new/zoo_loader.cgi',true); |
| 145 | request.setRequestHeader('Content-Type','text/xml'); |
| 146 | request.onreadystatechange = function() { |
| 147 | if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) { |
| 148 | wfsPolygon.refresh(); |
| 149 | } |
| 150 | } |
| 151 | request.send(query); |
| 152 | try{ |
| 153 | for(var i in efeature.attributes){ |
| 154 | try{ |
| 155 | document.getElementById("l_"+i).value=""; |
| 156 | }catch(e){/*alert(i+" "+e);*/} |
| 157 | } |
| 158 | }catch(e){} |
| 159 | modify.unselectFeature(editable.features[0]); |
| 160 | editable.removeFeatures(editable.features); |
| 161 | if(previousMode!='NONE'){ |
| 162 | editMode=previousMode; |
| 163 | previousMode='NONE'; |
| 164 | } |
| 165 | modify.deactivate(); |
| 166 | } |
| 167 | |
| 168 | function feature_info(feature) { |
| 169 | try{editable.removeFeatures(editable.features);}catch(e){} |
| 170 | efeature=feature; |
| 171 | editable.addFeatures(feature.clone()); |
| 172 | modify=new OpenLayers.Control.ModifyFeature(editable); |
| 173 | map.addControl(modify); |
| 174 | modify.activate(); |
| 175 | modify.beforeSelectFeature(editable.features[0]); |
| 176 | modify.selectFeature(editable.features[0]); |
| 177 | for(var i in feature.attributes){ |
| 178 | try{ |
| 179 | document.getElementById("l_"+i).value=feature.attributes[i]; |
| 180 | }catch(e){/*alert(i+" "+e);*/} |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | $(function(){ |
| 185 | $(".fg-button:not(.ui-state-disabled)") |
| 186 | .hover( |
| 187 | function(){ |
| 188 | $(this).addClass("ui-state-hover"); |
| 189 | }, |
| 190 | function(){ |
| 191 | $(this).removeClass("ui-state-hover"); |
| 192 | } |
| 193 | ) |
| 194 | .mousedown(function(){ |
| 195 | if(!$(this).is('.zoo-action')){ |
| 196 | $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active"); |
| 197 | if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); } |
| 198 | else { $(this).addClass("ui-state-active"); } |
| 199 | } |
| 200 | }) |
| 201 | .mouseup(function(){ |
| 202 | if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button, .fg-buttonset-multi .fg-button') ){ |
| 203 | $(this).removeClass("ui-state-active"); |
| 204 | } |
| 205 | }); |
| 206 | }); |
| 207 | |
| 208 | $(document).ready(function(){ |
| 209 | |
| 210 | $('#btnPrev[title], #btnNext[title], a[title]').qtip({ |
| 211 | style: { |
| 212 | name: 'dark', |
| 213 | tip: true, |
| 214 | border:{width:2,color:'#fe8701',radius:3}, |
| 215 | width:{min:120} |
| 216 | } |
| 217 | }); |
| 218 | }); |
| 219 | |
| 220 | ]]> |
| 221 | </script> |
| 222 | |
| 223 | }}} |