Ext.namespace("GeoExt.ux");GeoExt.ux.FeatureEditingControler=Ext.extend(Ext.util.Observable,{map:null,drawControls:null,lastDrawControl:null,deleteAllAction:null,actions:null,featureControl:null,layers:null,activeLayer:null,featurePanel:null,popup:null,useIcons:true,downloadService:null,useDefaultAttributes:true,defaultAttributes:['name','description'],autoSave:true,style:null,defaultStyle:{fillColor:"red",strokeColor:"red"},layerOptions:{},cosmetic:false,fadeRatio:0.4,opacityProperties:["fillOpacity","hoverFillOpacity","strokeOpacity","hoverStrokeOpacity"],defaultOpacity:1,'import':true,'export':true,toggleGroup:null,popupOptions:{},defaultPopupOptions:{title:OpenLayers.i18n('Edit Feature'),layout:'fit',width:280},selectControlOptions:{},constructor:function(config){Ext.apply(this,config);this.addEvents(["activelayerchanged"]);this.drawControls=[];this.actions=[];this.layers=[];this.initMap();if(config['layers']!=null){this.addLayers(config['layers']);delete config['layers'];}
if(this.cosmetic===true){var style=this.style||OpenLayers.Util.applyDefaults(this.defaultStyle,OpenLayers.Feature.Vector.style["default"]);var styleMap=new OpenLayers.StyleMap(style);var layerOptions=OpenLayers.Util.applyDefaults(this.layerOptions,{styleMap:styleMap,displayInLayerSwitcher:false});layer=new OpenLayers.Layer.Vector("Cosmetic",layerOptions);this.addLayers([layer]);}
if(this.layers.length>0){this.setActiveLayer(this.layers[0]);}
GeoExt.ux.FeatureEditingControler.superclass.constructor.apply(this,arguments);},addLayers:function(layers){for(var i=0;i<layers.length;i++){this.addLayer(layers[i]);}},addLayer:function(layer){if(!layer.map){this.map.addLayer(layer);}
this.layers.push(layer);layer.events.on({"beforefeatureselected":this.onBeforeFeatureSelect,"featureunselected":this.onFeatureUnselect,"featureselected":this.onFeatureSelect,"beforefeaturemodified":this.onModificationStart,"featuremodified":this.onModification,"afterfeaturemodified":this.onModificationEnd,"beforefeatureadded":this.onBeforeFeatureAdded,scope:this});},setActiveLayer:function(layer){this.activeLayer=layer;this.fireEvent("activelayerchanged",this,layer);this.initDrawControls(layer);this.initFeatureControl(layer);this.initDeleteAllAction();this.initImport();this.initExport();},initMap:function(){if(this.map instanceof GeoExt.MapPanel){this.map=this.map.map;}
if(!this.map){this.map=GeoExt.MapPanel.guess().map;}
if(!this.toggleGroup){this.toggleGroup=this.map.id;}},initFeatureControl:function(layer){var control,actionOptions;control=new OpenLayers.Control.ModifyFeature(layer,this.selectControlOptions);this.featureControl=control;actionOptions={control:control,map:this.map,toggleGroup:this.toggleGroup,allowDepress:false,pressed:false,tooltip:OpenLayers.i18n("Edit Feature"),group:this.toggleGroup,checked:false};if(this.useIcons===true){actionOptions.iconCls="gx-featureediting-editfeature";}else{actionOptions.text=OpenLayers.i18n("Edit Feature");}
var action=new GeoExt.Action(actionOptions);this.actions.push(action);},destroyFeatureControl:function(){},initDrawControls:function(layer){var control,handler,geometryTypes,geometryType,options,action,iconCls,actionOptions,tooltip;geometryTypes=[];options={};if(OpenLayers.i18n(layer.geometryType)){geometryTypes.push(OpenLayers.i18n(layer.geometryType));}else{geometryTypes.push(OpenLayers.i18n("Point"));geometryTypes.push(OpenLayers.i18n("LineString"));geometryTypes.push(OpenLayers.i18n("Polygon"));geometryTypes.push(OpenLayers.i18n("Label"));}
for(var i=0;i<geometryTypes.length;i++){geometryType=geometryTypes[i];switch(geometryType){case OpenLayers.i18n("LineString"):case OpenLayers.i18n("MultiLineString"):handler=OpenLayers.Handler.Path;iconCls="gx-featureediting-draw-line";tooltip=OpenLayers.i18n("Create line");break;case OpenLayers.i18n("Point"):case OpenLayers.i18n("MultiPoint"):handler=OpenLayers.Handler.Point;iconCls="gx-featureediting-draw-point";tooltip=OpenLayers.i18n("Create point");break;case OpenLayers.i18n("Polygon"):case OpenLayers.i18n("MultiPolygon"):handler=OpenLayers.Handler.Polygon;iconCls="gx-featureediting-draw-polygon";tooltip=OpenLayers.i18n("Create polygon");break;case OpenLayers.i18n("Label"):handler=OpenLayers.Handler.Point;iconCls="gx-featureediting-draw-label";tooltip=OpenLayers.i18n("Create label");break;}
control=new OpenLayers.Control.DrawFeature(layer,handler,options);this.drawControls.push(control);if(geometryType==OpenLayers.i18n("Label")){control.events.on({"featureadded":this.onLabelAdded,scope:this});}
control.events.on({"featureadded":this.onFeatureAdded,scope:this});actionOptions={control:control,map:this.map,toggleGroup:this.toggleGroup,allowDepress:false,pressed:false,tooltip:tooltip,group:this.toggleGroup,checked:false};if(this.useIcons===true){actionOptions.iconCls=iconCls;}else{actionOptions.text=geometryType;}
action=new GeoExt.Action(actionOptions);this.actions.push(action);}},destroyDrawControls:function(){for(var i=0;i<this.drawControls.length;i++){this.drawControls[i].destroy();}
this.drawControls=[];},initDeleteAllAction:function(){var actionOptions={handler:this.deleteAllFeatures,scope:this,tooltip:OpenLayers.i18n('Delete all features')};if(this.useIcons===true){actionOptions.iconCls="gx-featureediting-delete";}else{actionOptions.text=OpenLayers.i18n('DeleteAll');}
var action=new Ext.Action(actionOptions);this.deleteAllAction=action;this.actions.push(action);},deleteAllFeatures:function(){Ext.MessageBox.confirm(OpenLayers.i18n('Delete All Features'),OpenLayers.i18n('Do you really want to delete all features ?'),function(btn){if(btn=='yes'){if(this.popup){this.popup.close();this.popup=null;}
for(var i=0;i<this.layers.length;i++){this.layers[i].destroyFeatures();}}},this);},initImport:function(layer){if(this['import']===true){var actionOptions={handler:this.importFeatures,scope:this,tooltip:OpenLayers.i18n('Import KML')};if(this.useIcons===true){actionOptions.iconCls="gx-featureediting-import";}else{actionOptions.text=OpenLayers.i18n("Import");}
var action=new Ext.Action(actionOptions);this.actions.push(action);}},importFeatures:function(){GeoExt.ux.data.Import.KMLImport(this.map,this.activeLayer);},initExport:function(){if(this['export']===true){var actionOptions={handler:this.exportFeatures,scope:this,tooltip:OpenLayers.i18n('Export KML')};if(this.useIcons===true){actionOptions.iconCls="gx-featureediting-export";}else{actionOptions.text=OpenLayers.i18n("Export");}
var action=new Ext.Action(actionOptions);this.actions.push(action);}},exportFeatures:function(){GeoExt.ux.data.Export.KMLExport(this.map,this.layers,null,this.downloadService);},getSelectControl:function(){var control=false;switch(this.featureControl.CLASS_NAME){case"OpenLayers.Control.SelectFeature":control=this.featureControl;break;case"OpenLayers.Control.ModifyFeature":case"OpenLayers.Control.DeleteFeature":control=this.featureControl.selectControl;break;}
return control;},getActiveDrawControl:function(){var control=false;for(var i=0;i<this.drawControls.length;i++){if(this.drawControls[i].active){control=this.drawControls[i];break;}}
return control;},onLabelAdded:function(event){var feature=event.feature;feature.isLabel=true;},onFeatureAdded:function(event){var feature,drawControl;feature=event.feature;feature.state=OpenLayers.State.INSERT;drawControl=this.getActiveDrawControl();if(drawControl){drawControl.deactivate();this.lastDrawControl=drawControl;}
this.featureControl.activate();this.getSelectControl().select(feature);},onModificationStart:function(event){var feature=(event.geometry)?event:event.feature;var drawControl=this.getActiveDrawControl();if(drawControl){drawControl.deactivate();this.featureControl.activate();}
var options={autoSave:this.autoSave,features:[feature],controler:this,useIcons:this.useIcons};if(this['export']===true){options['plugins']=[new GeoExt.ux.ExportFeature(),new GeoExt.ux.CloseFeatureDialog()];}
this.featurePanel=new GeoExt.ux.form.FeaturePanel(options);popupOptions={feature:feature,controler:this,items:[this.featurePanel]};popupOptions=OpenLayers.Util.applyDefaults(popupOptions,this.popupOptions);popupOptions=OpenLayers.Util.applyDefaults(popupOptions,this.defaultPopupOptions);var popup=new GeoExt.Popup(popupOptions);feature.popup=popup;this.popup=popup;popup.on({close:function(){if(OpenLayers.Util.indexOf(this.controler.activeLayer.selectedFeatures,this.feature)>-1){this.controler.getSelectControl().unselect(this.feature);}}});popup.show();},onModification:function(event){var feature=(event.geometry)?event:event.feature;},onModificationEnd:function(event){var feature=(event.geometry)?event:event.feature;if(!feature){return;}
this.triggerAutoSave();if(feature.popup){feature.popup.close();feature.popup=null;}
this.reactivateDrawControl();},onBeforeFeatureAdded:function(event){var feature=event.feature;this.parseFeatureStyle(feature);this.parseFeatureDefaultAttributes(feature);},parseFeatureStyle:function(feature){var symbolizer=this.activeLayer.styleMap.createSymbolizer(feature);feature.style=symbolizer;},parseFeatureDefaultAttributes:function(feature){var hasAttributes;if(this.useDefaultAttributes===true){hasAttributes=false;for(var key in feature.attributes){hasAttributes=true;break;}
if(!hasAttributes){for(var i=0;i<this.defaultAttributes.length;i++){feature.attributes[this.defaultAttributes[i]]='';}}}},reactivateDrawControl:function(){if(this.lastDrawControl&&this.activeLayer.selectedFeatures.length===0){this.featureControl.deactivate();this.lastDrawControl.activate();this.lastDrawControl=null;}},triggerAutoSave:function(){if(this.autoSave){this.featurePanel.triggerAutoSave();}},onBeforeFeatureSelect:function(event){var feature=(event.geometry)?event:event.feature;if(feature.layer.selectedFeatures.length===0){this.applyStyles('faded',{'redraw':true});}},onFeatureUnselect:function(event){var feature=(event.geometry)?event:event.feature;this.applyStyle(feature,'faded',{'redraw':true});if(feature.layer.selectedFeatures.length===0){this.applyStyles('normal',{'redraw':true});}},onFeatureSelect:function(event){var feature=(event.geometry)?event:event.feature;this.applyStyle(feature,'normal',{'redraw':true});},applyStyles:function(style,options){style=style||"normal";options=options||{};for(var i=0;i<this.layers.length;i++){layer=this.layers[i];for(var j=0;j<layer.features.length;j++){feature=layer.features[j];if(!feature._sketch){this.applyStyle(feature,style);}}
if(options['redraw']===true){layer.redraw();}}},applyStyle:function(feature,style,options){var fRatio;options=options||{};switch(style){case"faded":fRatio=this.fadeRatio;break;default:fRatio=1/this.fadeRatio;}
for(var i=0;i<this.opacityProperties.length;i++){property=this.opacityProperties[i];if(feature.style[property]){feature.style[property]*=fRatio;}}
if(options['redraw']===true){feature.layer.drawFeature(feature);}},CLASS_NAME:"GeoExt.ux.FeatureEditingControler"});;Ext.namespace("GeoExt.ux.form");GeoExt.ux.form.FeatureEditingPanel=Ext.extend(Ext.form.FormPanel,{labelWidth:100,border:false,bodyStyle:'padding:5px 5px 5px 5px',width:300,defaults:{width:120},defaultType:'textfield',controler:null,downloadService:null,initComponent:function(){if(this.controler){this.initToolbar();this.initForm();}
GeoExt.ux.form.FeatureEditingPanel.superclass.initComponent.call(this);},initToolbar:function(){Ext.apply(this,{tbar:new Ext.Toolbar(this.controler.actions)});},initForm:function(){oItems=[];oItems.push({id:"testid",fieldLabel:OpenLayers.i18n("myOption"),maxLength:50,xtype:"checkbox"});Ext.apply(this,{items:oItems});},beforeDestroy:function(){delete this.controler;}});Ext.reg("gx_featureeditingpanel",GeoExt.ux.form.FeatureEditingPanel);;Ext.namespace("GeoExt.ux.form");GeoExt.ux.form.RedLiningPanel=Ext.extend(GeoExt.ux.form.FeatureEditingPanel,{map:null,'import':true,'export':true,toggleGroup:null,popupOptions:{},selectControlOptions:{},initComponent:function(){this.initMap();this.initControler();GeoExt.ux.form.RedLiningPanel.superclass.initComponent.call(this);},initMap:function(){if(this.map instanceof GeoExt.MapPanel){this.map=this.map.map;}
if(!this.map){this.map=GeoExt.MapPanel.guess().map;}},initControler:function(){this.controler=new GeoExt.ux.FeatureEditingControler({'cosmetic':true,'map':this.map,'import':this['import'],'export':this['export'],'toggleGroup':this.toggleGroup,'popupOptions':this.popupOptions,'selectControlOptions':this.selectControlOptions});}});Ext.reg("gx_redliningpanel",GeoExt.ux.form.RedLiningPanel);;Ext.namespace("GeoExt.ux.form");GeoExt.ux.form.FeaturePanel=Ext.extend(Ext.form.FormPanel,{labelWidth:100,border:false,bodyStyle:'padding:5px 5px 5px 5px',width:'auto',autoWidth:true,height:'auto',autoHeight:true,defaults:{width:120},defaultType:'textfield',features:null,layer:null,controler:null,autoSave:true,deleteAction:null,attributeFieldSetId:"gx_featurepanel_attributefieldset_id",labelAttribute:"name",useIcons:true,initComponent:function(){this.initFeatures(this.features);this.initToolbar();this.initForm();GeoExt.ux.form.FeaturePanel.superclass.initComponent.call(this);},initFeatures:function(features){if(features instanceof Array){this.features=features;}else{this.features=[features];}},initToolbar:function(){this.initDeleteAction();Ext.apply(this,{bbar:new Ext.Toolbar(this.getActions())});},initForm:function(){var oItems,oGroup,feature,field,oGroupItems;if(this.features.length!=1){return;}else{feature=this.features[0];}
oItems=[];oGroupItems=[];oGroup={id:this.attributeFieldSetId,xtype:'fieldset',title:OpenLayers.i18n('Attributes'),layout:'form',collapsible:true,autoHeight:this.autoHeight,autoWidth:this.autoWidth,defaults:this.defaults,defaultType:this.defaultType};for(var attribute in feature.attributes){field={'name':attribute,'fieldLabel':attribute,'id':attribute,'value':feature.attributes[attribute]};oGroupItems.push(field);}
oGroup.items=oGroupItems;oItems.push(oGroup);Ext.apply(this,{items:oItems});},initDeleteAction:function(){var actionOptions={handler:this.deleteFeatures,scope:this,tooltip:OpenLayers.i18n('Delete feature')};if(this.useIcons===true){actionOptions.iconCls="gx-featureediting-delete";}else{actionOptions.text=OpenLayers.i18n('Delete');}
this.deleteAction=new Ext.Action(actionOptions);},deleteFeatures:function(){Ext.MessageBox.confirm(OpenLayers.i18n('Delete Feature'),OpenLayers.i18n('Do you really want to delete this feature ?'),function(btn){if(btn=='yes'){for(var i=0;i<this.features.length;i++){var feature=this.features[i];if(feature.popup){feature.popup.close();feature.popup=null;}
feature.layer.destroyFeatures([feature]);}
this.controler.reactivateDrawControl();}},this);},getActions:function(){return[this.deleteAction];},triggerAutoSave:function(){if(this.autoSave){this.save();}},save:function(){var feature;if(this.features&&this.features.length===0){return;}
if(this.features.length!=1){return;}else{feature=this.features[0];}
this.parseFormFieldsToFeatureAttributes(feature);if(feature.isLabel===true){if(feature.attributes[this.labelAttribute]!=""){feature.style.label=feature.attributes[this.labelAttribute];feature.style.graphic=false;feature.style.labelSelect=true;feature.layer.drawFeature(feature);}else{feature.layer.destroyFeatures([feature]);if(this.controler.popup){this.controler.popup.close();this.controler.popup=null;}}}},parseFeatureAttributesToFormFields:function(feature){var aoElements,nElements,fieldSet;fieldSet=this.findById(this.attributeFieldSetId);aoElements=fieldSet.items.items;nElements=aoElements.length;for(var i=0;i<nElements;i++){var oElement=aoElements[i];var szAttribute=oElement.getName();var szValue=null;if(oElement.initialConfig.isfid)
{szValue=feature.fid;}
else
{szValue=feature.attributes[szAttribute];}
oElement.setValue(szValue);}},parseFormFieldsToFeatureAttributes:function(feature){var field,id,value,fieldSet;fieldSet=this.findById(this.attributeFieldSetId);for(var i=0;i<fieldSet.items.length;i++){field=fieldSet.items.get(i);id=field.getName();value=field.getValue();feature.attributes[id]=value;}},onAfterRender:function(){var feature;if(this.features.length!=1){return;}else{feature=this.features[0];}
this.parseFeatureAttributesToFormFields(feature);},beforeDestroy:function(){delete this.feature;}});Ext.reg("gx_featurepanel",GeoExt.ux.form.FeaturePanel);;Ext.namespace("GeoExt.ux");GeoExt.ux.ImportFeatures=Ext.extend(Ext.util.Observable,{layer:null,controler:null,init:function(form){this.layer=form.layer;this.controler=form.controler;var actionOptions={handler:this.importFeatures,scope:this,tooltip:OpenLayers.i18n('Import KML')};if(this.controler.useIcons===true){actionOptions.iconCls="gx-featureediting-import";}else{actionOptions.text=OpenLayers.i18n("Import");}
var action=new Ext.Action(actionOptions);form.getTopToolbar().add(action);this.controler.actions.push(action);},importFeatures:function(){GeoExt.ux.data.Import.KMLImport(this.layer.map,this.layer);}});;Ext.namespace("GeoExt.ux");GeoExt.ux.ExportFeatures=Ext.extend(Ext.util.Observable,{controler:null,init:function(form){this.controler=form.controler;var actionOptions={handler:this.exportFeatures,scope:this,tooltip:OpenLayers.i18n('Export KML')};if(this.controler.useIcons===true){actionOptions.iconCls="gx-featureediting-export";}else{actionOptions.text=OpenLayers.i18n("Export");}
var action=new Ext.Action(actionOptions);form.getTopToolbar().add(action);this.controler.actions.push(action);},exportFeatures:function(){var downloadService=this.controler.downloadService;GeoExt.ux.data.Export.KMLExport(this.controler.map,this.controler.layers,null,downloadService);}});;Ext.namespace("GeoExt.ux");GeoExt.ux.ExportFeature=Ext.extend(Ext.util.Observable,{controler:null,editFeatureForm:null,init:function(form){this.controler=form.controler;var actionOptions={handler:this.exportFeatures,scope:this,tooltip:OpenLayers.i18n('Export KML')};if(this.controler.useIcons===true){actionOptions.iconCls="gx-featureediting-export";}else{actionOptions.text=OpenLayers.i18n("Export");}
var action=new Ext.Action(actionOptions);var bbar=form.getBottomToolbar();if(bbar.rendered||!bbar.buttons){bbar.add(action);}else{bbar.buttons.push(action);}
this.editFeatureForm=form;},exportFeatures:function(){var map=this.controler.map;var downloadService=this.controler.downloadService;var features=this.editFeatureForm.features;this.controler.triggerAutoSave();GeoExt.ux.data.Export.KMLExport(map,null,features,downloadService);}});;Ext.namespace("GeoExt.ux");GeoExt.ux.CloseFeatureDialog=Ext.extend(Ext.util.Observable,{controler:null,editFeatureForm:null,init:function(form){this.controler=form.controler;var actionOptions={handler:this.closeFeatureDialog,scope:this,tooltip:OpenLayers.i18n('Close')};actionOptions.text=OpenLayers.i18n("Close");var action=new Ext.Action(actionOptions);var bbar=form.getBottomToolbar();if(bbar.rendered||!bbar.buttons){bbar.add('->');bbar.add(action);}else{bbar.buttons.push('->');bbar.buttons.push(action);}
this.editFeatureForm=form;},closeFeatureDialog:function(){this.controler.triggerAutoSave();if(this.controler.popup){this.controler.popup.close();}
this.controler.reactivateDrawControl();}});
