Retour à la documentation
/**
* @class spw.widgets.SpwEditor
*/
define(["dojo/_base/declare",
"spw/api/SpwBaseTemplatedWidget",
"dojo/text!./templates/SpwEditor.html",
"dojo/i18n!./nls/SpwEditor",
"esri/toolbars/draw",
"esri/toolbars/edit",
"esri/graphic",
"esri/SnappingManager",
"esri/layers/FeatureLayer",
"esri/dijit/AttributeInspector",
"dojo/_base/Color",
"esri/symbols/SimpleMarkerSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/SimpleFillSymbol",
"esri/dijit/InfoWindow",
"esri/dijit/editing/TemplatePicker",
"dojo/_base/array",
"dojo/keys",
"dojo/on",
"dojo/dom-style",
"esri/sniff",
"dojo/aspect",
"dojo/_base/event",
"esri/tasks/query",
"esri/tasks/QueryTask",
"spw/api/ConfirmDialog",
"esri/geometry/Polygon",
"esri/geometry/Point",
"esri/geometry/Polyline",
"spw/api/MessageManager",
"dojo/dom-construct",
"dojo/_base/lang",
"dijit/form/Button",
"dijit/Tooltip",
"spw/widgets/SpwEditAttributes",
"spw/widgets/SpwEditRelatedAttribute",
"spw/api/GeometryUtils",
"dijit/layout/BorderContainer",
"dijit/layout/ContentPane",
"dijit/Toolbar",
"dijit/form/ToggleButton",
"dijit/layout/TabContainer"
],
function(declare, SpwBaseTemplatedWidget, tmpl, labels,
Draw, Edit, Graphic,
SnapManager, FeatureLayer, AttributeInspector,
Color, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, InfoWindow,
TemplatePicker,
array, keys, on, domStyle, has, aspect,
event, Query, QueryTask, ConfirmDialog,
Polygon, Point, Polyline,
MessageManager, domConstruct, lang, Button, Tooltip, SpwEditAttributes, SpwEditRelatedAttribute, GeometryUtils) {
var editorWidget = null;
editorWidget = declare("spw.widgets.SpwEditor", [SpwBaseTemplatedWidget], {
templateString: tmpl,
labels: labels,
definitionExpression: null,
_esriMap: null,
_currentLayer: null,
_defaultPasteLayer: null,
_currentFeature: null,
_editToolbar: null,
_drawToolbar: null,
_templatePicker: null,
_attributeInspector: null,
readOnlyAttributes: null,
hiddenAttributes: null,
_editingEnabled: null,
_drawActivated: false,
layersURL: null,
layers: null,
attributeEditorId: null,
layersAddedHandler: null,
editorActivated: null,
deactivateDrawToolbarAfterUsed: false,
clearSelection: null,
_loadingImgSrc: "data:image/gif;base64,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",
_emptyImgSrc: "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D",
_cptForResize: 0,
editMode: null,
_copiedGraphic: null,
_copiedGraphicLayer: null,
actions: ['editVertices', 'move', 'rotate', 'copy', 'editAttributes', 'delete'],
helpContents: {
editVertices: 'Sélectionnez le segment que vous souhaitez modifier',
move: 'Sélectionnez le segment que vous souhaitez déplacer',
rotate: 'Sélectionnez le segment que vous souhaitez tourner',
copy: 'Sélectionnez un segment pour le copier puis cliquez sur la carte pour le coller',
editAttributes: 'Sélectionnez un segment pour modifier ses attributs',
'delete': 'Sélectionnez le segment que vous souhaitez supprimer'
},
_editorWidget: null,
fieldsOrder: [],
infoWindowWidth: 350,
infoWindowHeight: 300,
infoWindowPosition: 'auto',
orderFields: function(fields) {
if (this.fieldsOrder == null) {
return fields;
}
var fieldsSorted = fields.slice(0);
fieldsSorted.sort(lang.hitch(this, function(f1, f2) {
var idx1 = this.fieldsOrder.indexOf(f1.name);
var idx2 = this.fieldsOrder.indexOf(f2.name);
if (idx2 < 0) {
return -1;
}
else if (idx1 < 0) {
return 1;
}
return idx1 - idx2;
}));
return fieldsSorted;
},
postCreate: function() {
if (this.enableCopy) {
domStyle.set(this.copyButton, 'display', '');
}
this._editingEnabled = false;
this.editMode = "editVertices";
this.editorActivated = false;
this.layers = [];
this.copyLayers = [];
this._spwMap = this.spwViewer.get('spwMap');
this._esriMap = this._spwMap.get('esriMap');
this._esriMap.infoWindow._contentPane.parentElement.style.padding = 0;
this._spwMap.on('MapCreated', lang.hitch(this, function(map) {
this._esriMap = this._spwMap.get('esriMap');
this._esriMap.infoWindow._contentPane.parentElement.style.padding = 0;
if (this._attributeInspector) {
this._esriMap.infoWindow.on("hide", lang.hitch(this, function() {
if (this._currentLayer !== null) {
this._currentLayer.clearSelection();
}
}));
this._esriMap.infoWindow.setContent(this._attributeInspector.domNode);
this._esriMap.infoWindow.set('anchor', this.infoWindowPosition);
this._esriMap.infoWindow.resize(this.infoWindowWidth, this.infoWindowHeight);
}
}));
this.editVerticesButton.on("click", this.clickToggleButton("editVertices"));
this.moveButton.on("click", this.clickToggleButton("move"));
this.rotateButton.on("click", this.clickToggleButton("rotate"));
this.editAttributesButton.on("click", this.clickToggleButton("editAttributes"));
this.copyButton.on("click", this.clickToggleButton("copy"));
this.deleteButton.on("click", this.clickToggleButton("delete"));
this.splitJoinButton.on("click", this.clickToggleButton("splitJoin"));
this.own(
on(this.splitJoinToggleButton, 'change', lang.hitch(this, this.onSplitJoinToggleChange)),
on(this.cancelSplitJoinButton, 'click', lang.hitch(this, this.onSplitJoinCancel)),
on(this.confirmSplitJoinButton, 'click', lang.hitch(this, this.onSplitJoinConfirm))
);
array.forEach(this.actions, lang.hitch(this, function(a, idx) {
if (this[a + 'Button'] == null) {
return;
}
domStyle.set(this[a + 'Button'].domNode, 'display', '');
if (idx === 0) {
this[a + 'Button'].set('checked', true);
if (this.helpContents[a]) {
this.helpTextNode.innerHTML = this.helpContents[a];
}
}
}));
new Tooltip({
connectId: [this.infoImg],
label: this.labels.helpMessage,
position: ["after"]
});
this.inherited(arguments);
this._cptForResize += 1;
this.tabContainerNode.watch('selectedChildWidget', lang.hitch(this, function() {
if (this.tabContainerNode.selectedChildWidget === this.creator_borderContainer) {
if (this._templatePicker) {
this._templatePicker.update();
}
} else {
this.deactivateTemplatePicker();
}
}));
},
_getEditorWidgetAttr: function() {
if (this._editorWidget == null) {
this._editorWidget = new SpwEditAttributes(lang.mixin({
widgetTitle: "Édition des attributs",
activated: false,
position: "panel",
width: "360px",
height: "auto",
top: "30%",
left: "40%",
spwViewer: this.spwViewer,
readOnlyAttributes: this.readOnlyAttributes,
ignoreFields: this.hiddenAttributes
}, this.editWidgetOptions));
this._editorWidget.startup();
}
return this._editorWidget;
},
onActivate: function() {
this.inherited(arguments);
if (this.layers.length === 0 && this.layersURL !== null) {
array.forEach(this.layersURL, lang.hitch(this, function(layerUrl) {
var tmp = new FeatureLayer(layerUrl, {
mode: FeatureLayer.MODE_AUTO,
outFields: ['*']
});
// we can't override this attribute, we have to set it
if (this.definitionExpression) {
tmp.setDefinitionExpression(this.definitionExpression);
}
this.layers.push(tmp);
}));
array.forEach(this.copyLayersURL, lang.hitch(this, function(layerUrl) {
var tmp = new FeatureLayer(layerUrl, {
mode: FeatureLayer.MODE_AUTO,
outFields: ['*']
});
this.copyLayers.push(tmp);
}));
}
if (this.layers !== null && this.layers.length > 0) {
this.layersAddedHandler = this._esriMap.on("layers-add-result", lang.hitch(this, this.activateEditor));
this._esriMap.addLayers(this.layers.concat(this.copyLayers));
}
this._cptForResize += 1;
},
onDeactivate: function() {
this.inherited(arguments);
if (this.layers !== null && this.layers.length > 0) {
array.forEach(this.layers, lang.hitch(this, function(layer) {
this._esriMap.removeLayer(layer);
}));
}
if (this.copyLayers !== null && this.copyLayers.length > 0) {
array.forEach(this.copyLayers, lang.hitch(this, function(layer) {
this._esriMap.removeLayer(layer);
}));
}
this.onSplitJoinCancel();
if (this.editorActivated) {
this.deactivateEditor();
}
this._copiedGraphic = null;
},
deactivate: function() {
if (this._drawActivated) {
this.deactivateTemplatePicker();
}
},
_esriMapClickHandler: null,
activateEditor: function(evt) {
if (this.layersAddedHandler !== null) {
this.layersAddedHandler.remove();
this.layersAddedHandler = null;
}
this.editorActivated = true;
this.enableSnapping();
var layers = array.map(evt.layers, function(result) {
return result.layer;
});
if (this._editToolbar === null) {
this._editToolbar = new Edit(this._esriMap);
this._editToolbar.on("deactivate", lang.hitch(this, function(evt) {
if (this._currentLayer !== null) {
this.startLoading();
this._currentLayer.applyEdits(null, [evt.graphic], null, lang.hitch(this, this.stopLoading), lang.hitch(this, this.displayErrorMsg));
}
}));
if (this._esriMapClickHandler) {
this._esriMapClickHandler.remove();
this._esriMapClickHandler = null;
}
this._esriMapClickHandler = this._esriMap.on("click", lang.hitch(this, function(evt) {
this.splitProject(evt);
this.copyGeometry(evt);
this.stopEdition();
}));
var polygonSelectionSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.5]));
var pointSelectionSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.5]));
var lineSelectionSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 3);
array.forEach(layers, lang.hitch(this, function(layer) {
switch (layer.geometryType) {
case "esriGeometryPoint":
layer.setSelectionSymbol(pointSelectionSymbol);
break;
case "esriGeometryPolygon":
layer.setSelectionSymbol(polygonSelectionSymbol);
break;
default:
layer.setSelectionSymbol(lineSelectionSymbol);
}
if (this.copyLayers.indexOf(layer) > -1) {
this._setEventsForCopyLayer(evt, layer);
} else {
this._setEventsForLayer(evt, layer);
}
}));
}
var isEditableLayer = [];
array.forEach(layers, lang.hitch(this, function(layer, idx) {
if (this.copyLayers.indexOf(layer) < 0) {
isEditableLayer.push(layer);
if (idx === 0) {
this._defaultPasteLayer = layer;
}
}
}));
this.addTemplatePicker(isEditableLayer);
this.addAttributeInspector(isEditableLayer);
},
_setEventsForCopyLayer: function(evt, layer) {
array.forEach(this._copyHandlers, function(h) {
h.remove();
});
this._copyHandlers = [];
this._copyHandlers.push(layer.on("mouse-over", lang.hitch(this, function() {
if (this.editMode === 'copy' && (this._templatePicker === null || this._templatePicker.getSelected() === null)) {
switch (layer.geometryType) {
case "esriGeometryPoint":
if (this.editMode !== "editVertices" && this.editMode !== "rotate") {
this._esriMap.setMapCursor("pointer");
}
break;
default:
this._esriMap.setMapCursor("pointer");
}
}
})));
this._copyHandlers.push(layer.on("mouse-out", lang.hitch(this, function() {
if (this.editMode === 'copy' && (this._templatePicker === null || this._templatePicker.getSelected() === null)) {
this._esriMap.setMapCursor("default");
}
})));
this._copyHandlers.push(layer.on("click", lang.hitch(this, function(evt) {
if (this.editMode === 'copy' && (this._templatePicker === null || this._templatePicker.getSelected() === null)) {
event.stop(evt);
this._editingEnabled = true;
var selectQuery = new Query();
selectQuery.where = "OBJECTID = " + evt.graphic.attributes.OBJECTID;
layer.selectFeatures(selectQuery);
this.clearSelection = function() {
layer.clearSelection();
};
this._copiedGraphic = new Graphic();
this._copiedGraphic.setGeometry(lang.clone(evt.graphic.geometry));
this._copiedGraphic.setAttributes(this.cloneAttributes(null));
this._copiedGraphicLayer = layer;
this.copyGeometry();
}
})));
},
_setEventsForLayer: function(evt, layer) {
array.forEach(this._layersHandlers, function(h) {
h.remove();
});
this._layersHandlers = [];
this._layersHandlers.push(layer.on("mouse-over", lang.hitch(this, function() {
if (this._templatePicker === null || this._templatePicker.getSelected() === null) {
switch (layer.geometryType) {
case "esriGeometryPoint":
if (this.editMode !== "editVertices" && this.editMode !== "rotate") {
this._esriMap.setMapCursor("pointer");
}
break;
default:
if (!this.splitJoinActivated) {
this._esriMap.setMapCursor("pointer");
}
}
}
})));
this._layersHandlers.push(layer.on("mouse-out", lang.hitch(this, function() {
if (this._templatePicker === null || this._templatePicker.getSelected() === null) {
this._esriMap.setMapCursor("default");
}
})));
this._layersHandlers.push(layer.on("click", lang.hitch(this, function(evt) {
if (this._templatePicker === null || this._templatePicker.getSelected() === null) {
if (this.splitJoinActivated) {
return;
}
event.stop(evt);
this._currentLayer = layer;
if (this._editingEnabled === true) {
this.stopEdition();
}
if (layer.geometryType !== "esriGeometryPoint" || (this.editMode !== "editVertices" && this.editMode !== "rotate")) {
this._editingEnabled = true;
if (['editAttributes', 'splitJoin'].indexOf(this.editMode) === -1) {
var selectQuery = new Query();
selectQuery.where = "OBJECTID = " + evt.graphic.attributes.OBJECTID;
layer.selectFeatures(selectQuery);
}
this.clearSelection = function() {
layer.clearSelection();
};
switch (this.editMode) {
case "editVertices":
this._editToolbar.activate(Edit.EDIT_VERTICES, evt.graphic);
break;
case "move":
this._editToolbar.activate(Edit.MOVE, evt.graphic);
break;
case "rotate":
this._editToolbar.activate(Edit.ROTATE, evt.graphic);
break;
case "editAttributes":
var selectQuery = new Query();
selectQuery.where = "OBJECTID = " + evt.graphic.attributes.OBJECTID;
layer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, lang.hitch(this, function(features) {
if (features.length > 0) {
this._currentFeature = features[0];
this._esriMap.infoWindow.setTitle(this._currentFeature.getLayer().name);
this._esriMap.infoWindow.show(evt.screenPoint, this._esriMap.getInfoWindowAnchor(evt.screenPoint));
}
}));
break;
case "copy":
this._copiedGraphic = new Graphic();
this._copiedGraphic.setGeometry(lang.clone(evt.graphic.geometry));
this._copiedGraphic.setAttributes(this.cloneAttributes(evt.graphic.attributes));
this._copiedGraphicLayer = layer;
break;
case "delete":
var selectQuery = new Query();
selectQuery.where = "OBJECTID = " + evt.graphic.attributes.OBJECTID;
layer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, lang.hitch(this, function(features) {
if (features.length > 0) {
evt.feature = features[0];
this.deleteElement(evt);
}
}));
break;
case "splitJoin":
var remove = array.some(layer.getSelectedFeatures(), function(f) {
return (f.attributes.OBJECTID === evt.graphic.attributes.OBJECTID);
});
var selectQuery = new Query();
selectQuery.where = "OBJECTID = " + evt.graphic.attributes.OBJECTID;
layer.selectFeatures(selectQuery, (remove ? FeatureLayer.SELECTION_SUBTRACT : FeatureLayer.SELECTION_ADD),
lang.hitch(this, this.updateSplitJoin, layer));
break;
}
}
}
})));
},
refreshAllServices: function() {
var mss = this.spwViewer.get('spwMap').getMapServices({
toLoad: true,
visible: true,
isBaseMap: false
});
array.forEach(mss, lang.hitch(this, function(ms) {
ms.layer && ms.layer.refresh && ms.layer.refresh();
}));
},
updateSplitJoin: function(layer) {
// TODO : SELECTION UNIQUEMENT SUR LE LAYER SUR LEQUEL ON A DEJA SELECTIONNE QUELQUE CHOSE SI C'EST LE CAS
if (this.splitJoinLayer && this.splitJoinLayer !== layer) {
MessageManager.getInstance().notifyError('Vous ne pouvez sélectionner que des segments du même type...');
layer.clearSelection();
return;
}
var selectedFeatures = layer.getSelectedFeatures();
this.splitJoinLayer = layer;
if (selectedFeatures.length > 1) {
this.splitJoinToggleButton.set('disabled', false);
this.cancelSplitJoinButton.set('disabled', false);
this.splitJoinToggleButton.set('label', 'Fusionner');
this.helpSplitJoinContent.innerHTML = 'Pour fusionner, veuillez cliquer sur fusionner puis sur confirmer une fois tous les segments sélectionnés.';
this.splitJoinMode = 'join';
this.splitJoinLayer = layer;
} else if (selectedFeatures.length === 1) {
this.splitJoinToggleButton.set('disabled', false);
this.cancelSplitJoinButton.set('disabled', false);
this.splitJoinToggleButton.set('label', 'Segmenter');
this.helpSplitJoinContent.innerHTML = 'Pour segmenter, veuillez cliquer sur segmenter puis placez le(s) point(s) sur le segment et cliquez sur confirmer.';
this.splitJoinMode = 'split';
this.splitJoinLayer = layer;
} else {
this.onSplitJoinCancel();
}
},
splitJoinActivated: false,
splitJoinMode: null,
splitPoints: null,
splitTolerance: 5,
onSplitJoinToggleChange: function() {
this.confirmSplitJoinButton.set('disabled', !this.splitJoinToggleButton.get('checked'));
this.splitJoinActivated = this.splitJoinToggleButton.get('checked');
if (!this.splitJoinToggleButton.get('checked')) {
this.onSplitJoinCancel();
}
},
splitProject: function(evt) {
if (!this.splitJoinActivated || this.splitJoinMode !== 'split') {
return;
}
var selectedLayers = this.splitJoinLayer.getSelectedFeatures();
if (selectedLayers.length === 0) {
return;
}
var pt = new Point(evt.mapPoint.x, evt.mapPoint.y, this._esriMap.spatialReference);
var projected = GeometryUtils.projectOnLine(selectedLayers[0].geometry, pt);
var gph = new Graphic(projected, new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 15, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 0, 255]), 1), new Color([0, 0, 255, 0.5])));
this.splitPoints = this.splitPoints || [];
this.splitPoints.push(gph);
this._esriMap.graphics.add(gph);
},
onSplitJoinCancel: function() {
this.splitJoinMode = null;
this.splitJoinToggleButton.set('disabled', true);
this.cancelSplitJoinButton.set('disabled', true);
this.splitJoinToggleButton.set('checked', false);
this.splitJoinToggleButton.set('label', 'Fusionner/segmenter');
this.helpSplitJoinContent.innerHTML = 'Veuillez sélectionner un ou plusieurs segments pour continuer.';
array.forEach(this.splitPoints, lang.hitch(this, function(pt) {
this._esriMap.graphics.remove(pt);
}));
this.splitPoints = [];
if (this.splitJoinLayer) {
this.splitJoinLayer.clearSelection();
this.splitJoinLayer = null;
}
},
onSplitJoinConfirm: function() {
if (this.splitJoinMode === 'split') {
this.doSplit();
} else if (this.splitJoinMode === 'join') {
this.doJoin();
}
},
doSplit: function() {
var features = this.splitJoinLayer.getSelectedFeatures();
if (this.splitPoints == null || this.splitPoints.length === 0) {
MessageManager.getInstance().notifyError('Vous devez ajouter au moins un point de rupture...');
return;
}
if (features.length === 0) {
console.error('no selected features...');
return;
}
var feature = features[0].geometry;
var lines = [];
var firstLoop = true;
var points = [];
array.forEach(this.splitPoints, lang.hitch(this, function(splitPoint) {
splitPoint = splitPoint.geometry;
var minDis = Infinity;
var minToSave = Infinity;
var index = 0;
var pathIndex = 0;
for (var i = 0; i < feature.paths.length; ++i) {
if (firstLoop) {
points[i] = [];
points[i].push({
point: new Point(feature.getPoint(i, 0), feature.spatialReference),
idx: -1,
fromLine: true,
dis: 0
});
}
for (var j = 0; j < feature.paths[i].length - 1; ++j) {
var tmpLine = new Polyline(feature.spatialReference);
tmpLine.addPath([feature.getPoint(i, j), feature.getPoint(i, j + 1)]);
var min = GeometryUtils.distance(tmpLine, splitPoint);
if (min < minDis) {
minDis = min;
minToSave = GeometryUtils.distance(splitPoint, new Point(feature.getPoint(i, j), feature.spatialReference));
index = j;
pathIndex = i;
}
}
if (firstLoop) {
points[i].push({
point: new Point(feature.getPoint(i, j), feature.spatialReference),
idx: Infinity,
fromLine: true,
dis: 0
});
}
}
firstLoop = false;
points[pathIndex].push({
point: splitPoint,
idx: index,
dis: minToSave,
fromLine: false
});
}));
array.forEach(points, function(pt) {
pt.sort(function(p1, p2) {
if (p1.idx > p2.idx) {
return 1;
} else if (p1.idx < p2.idx) {
return -1;
} else if (p1.dis < p2.dis) {
return -1;
} else if (p1.dis > p2.dis) {
return 1;
}
return 0;
});
});
var lines = [];
array.forEach(points, lang.hitch(this, function(pts, path) {
var tmpLine = new Polyline(feature.spatialReference);
tmpLine.addPath(feature.paths[path]);
array.forEach(pts, lang.hitch(this, function(endPoint, idx) {
if (idx === 0) {
return;
}
var startPoint = points[path][idx - 1];
var segment = GeometryUtils.slice(startPoint.point, endPoint.point, tmpLine);
var cloned = new Graphic();
cloned.setGeometry(segment);
cloned.setAttributes(this.cloneAttributes(features[0].attributes));
lines.push(cloned);
}));
}));
this.startLoading();
this.splitJoinLayer.applyEdits(lines, null, null, lang.hitch(this, function(adds) {
var ok = adds.length === lines.length && array.every(adds, function(add) {
return add.success;
});
if (ok) {
this.splitJoinLayer.applyEdits(null, null, [features[0]], lang.hitch(this, this.stopLoading), lang.hitch(this, function(err) {
MessageManager.getInstance().notifyError('Impossible de supprimer le segment segmenté.');
console.error(err);
this.stopLoading();
}));
this.refreshFeature(adds[0].objectId, lines[0], this.splitJoinLayer);
this.refreshFeature(adds[1].objectId, lines[1], this.splitJoinLayer);
this.onSplitJoinCancel();
} else {
MessageManager.getInstance().notifyError('Impossible de créer les différents segments (erreur service)');
}
}), lang.hitch(this, function(err) {
MessageManager.getInstance().notifyError('Impossible de créér les nouveaux segments.');
console.error(err);
this.stopLoading();
}));
},
doJoin: function() {
var features = this.splitJoinLayer.getSelectedFeatures();
if (features.length < 2) {
console.error('no enough selected features...');
return;
}
var merged = new Graphic(GeometryUtils.merge(array.map(features, function(feature) {
return feature.geometry;
})));
var updateMerged = lang.hitch(this, function() {
this.startLoading();
this.splitJoinLayer.applyEdits([merged], null, null, lang.hitch(this, function(adds) {
if (adds.length === 1 && adds[0].success) {
this.refreshFeature(adds[0].objectId, merged, this.splitJoinLayer);
this.splitJoinLayer.applyEdits(null, null, features, lang.hitch(this, this.stopLoading), lang.hitch(this, function(err) {
MessageManager.getInstance().notifyError('Impossible de supprimer les segments fusionnés');
this.stopLoading();
console.error(err);
}));
this.onSplitJoinCancel();
} else {
MessageManager.getInstance().notifyError('Impossible de créer l\'élément fusionné')
}
}));
});
var editor = null;
if (this.attributeEditorId) {
editor = this.spwViewer.spwWidgetsManager.getWidgets({
widgetId: this.attributeEditorId
})[0];
} else {
editor = this.get('editorWidget');
}
if (editor == null) {
MessageManager.getInstance().notifyError('Impossible d\'éditer les attributs (widget introuvable)');
return;
}
editor.edit(this.splitJoinLayer.url, this.cloneAttributes(features[0].attributes), lang.hitch(this, function(values) {
merged.setAttributes(values);
updateMerged();
}), lang.hitch(this, function() {
// TODO: annulation, faire quelque chose ?
}));
},
cloneAttributes: function(attributes) {
// TODO
if (attributes == null) {
return {
OBJECTID: null
};
}
return lang.mixin(lang.clone(attributes), {
OBJECTID: null
});
},
copyGeometry: function(event) {
if (this._copiedGraphic !== null && this._copiedGraphicLayer !== null && this.editMode === "copy") {
if (event) {
var mapPoint = event.mapPoint;
var diffX, diffY;
switch (this._copiedGraphic.geometry.type) {
case "point":
diffX = mapPoint.x - this._copiedGraphic.geometry.x;
diffY = mapPoint.y - this._copiedGraphic.geometry.y;
this._copiedGraphic.geometry.x = this._copiedGraphic.geometry.x + diffX;
this._copiedGraphic.geometry.y = this._copiedGraphic.geometry.y + diffY;
break;
case "polygon":
diffX = mapPoint.x - this._copiedGraphic.geometry.getExtent().getCenter().x;
diffY = mapPoint.y - this._copiedGraphic.geometry.getExtent().getCenter().y;
for (var j = 0; j < this._copiedGraphic.geometry.rings.length; ++j) {
for (var i = 0; i < this._copiedGraphic.geometry.rings[j].length; i++) {
this._copiedGraphic.geometry.rings[j][i][0] = this._copiedGraphic.geometry.rings[j][i][0] + diffX;
this._copiedGraphic.geometry.rings[j][i][1] = this._copiedGraphic.geometry.rings[j][i][1] + diffY;
}
}
break;
case "polyline":
diffX = mapPoint.x - this._copiedGraphic.geometry.getExtent().getCenter().x;
diffY = mapPoint.y - this._copiedGraphic.geometry.getExtent().getCenter().y;
for (var j = 0; j < this._copiedGraphic.geometry.paths.length; ++j) {
for (var i = 0; i < this._copiedGraphic.geometry.paths[j].length; i++) {
this._copiedGraphic.geometry.paths[j][i][0] = this._copiedGraphic.geometry.paths[j][i][0] + diffX;
this._copiedGraphic.geometry.paths[j][i][1] = this._copiedGraphic.geometry.paths[j][i][1] + diffY;
}
}
break;
}
}
if (this._currentLayer !== this._copiedGraphicLayer) {
var doEdit = lang.hitch(this, function(attributes) {
var saveGeometry = lang.hitch(this, function() {
this.startLoading();
this._defaultPasteLayer.applyEdits([this._copiedGraphic], null, null, lang.hitch(this, function(adds) {
this.stopLoading();
if (adds && adds.length === 1 && adds[0].success) {
this.refreshFeature(adds[0].objectId, this._copiedGraphic, this._defaultPasteLayer);
} else {
MessageManager.getInstance().notifyError('Impossible de copier le segment');
}
this._copiedGraphic = null;
this._copiedGraphicLayer = null;
}), lang.hitch(this, function(err) {
this._copiedGraphic = null;
this._copiedGraphicLayer = null;
this.displayErrorMsg();
}));
});
var editor = null;
if (this.attributeEditorId) {
editor = this.spwViewer.spwWidgetsManager.getWidgets({
widgetId: this.attributeEditorId
})[0];
} else {
editor = this.get('editorWidget');
}
if (editor == null) {
// show error
MessageManager.getInstance().notifyError('Impossible d\'éditer les attributs (widget introuvable)');
return;
}
editor.edit(this._defaultPasteLayer.url, attributes, lang.hitch(this, function(attr) {
lang.mixin(this._copiedGraphic.attributes, attr);
saveGeometry();
}), lang.hitch(this, function() {
// cancel
this._copiedGraphic = null;
this._copiedGraphicLayer = null;
this.stopLoading();
}));
});
doEdit(this._copiedGraphic.attributes);
} else {
this.startLoading();
this._copiedGraphicLayer.applyEdits([this._copiedGraphic], null, null, lang.hitch(this, function(adds) {
this.stopLoading();
if (adds && adds.length === 1 && adds[0].success) {
this.refreshFeature(adds[0].objectId, this._copiedGraphic, this._copiedGraphicLayer);
} else {
MessageManager.getInstance().notifyError('Impossible de copier le segment');
}
this._copiedGraphic = null;
this._copiedGraphicLayer = null;
}), lang.hitch(this, function() {
// cancel
this._copiedGraphic = null;
this._copiedGraphicLayer = null;
this.stopLoading();
}));
}
if (this.clearSelection) {
this.clearSelection();
}
}
},
stopEdition: function(newEditMode) {
if (this._editingEnabled) {
this._esriMap.infoWindow.hide();
this._editingEnabled = false;
if (newEditMode) {
this._copiedGraphic = null;
}
if (newEditMode || this.editMode !== 'splitJoin') {
if (this.clearSelection !== null) {
this.clearSelection();
}
}
this._editToolbar.deactivate();
}
this._esriMap.infoWindow.hide();
},
deactivateEditor: function() {
this.disableSnapping();
if (this._editToolbar) {
this._editToolbar.deactivate();
}
this.deactivateTemplatePicker();
this.deactivateAttributeInspector();
this.editorActivated = false;
},
enableSnapping: function() {
var symbol = new SimpleMarkerSymbol(
SimpleMarkerSymbol.STYLE_CROSS,
15,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([255, 0, 0, 0.5]),
5
),
null
);
this._esriMap.enableSnapping({
snapPointSymbol: symbol,
tolerance: 20,
snapKey: keys.ALT
});
},
disableSnapping: function() {
this._esriMap.disableSnapping();
},
addTemplatePicker: function(layers) {
var selectedTemplate = null;
if (this._drawToolbar === null) {
this._drawToolbar = new Draw(this._esriMap);
this._drawToolbar.on("draw-end", lang.hitch(this, function(evt) {
if (this.deactivateDrawToolbarAfterUsed) {
this.deactivateTemplatePicker();
}
if (selectedTemplate !== null) {
var createNewSegment = lang.hitch(this, function(newAttributes) {
var newGraphic = new Graphic(evt.geometry, null, newAttributes);
var createTemplate = selectedTemplate;
var saveGeometry = lang.hitch(this, function() {
this.startLoading();
createTemplate.featureLayer.applyEdits([newGraphic], null, null, lang.hitch(this, function(adds) {
this.stopLoading();
if (adds && adds.length === 1 && adds[0].success) {
this.refreshFeature(adds[0].objectId, newGraphic, createTemplate.featureLayer);
} else {
MessageManager.getInstance().notifyError('Impossible de créer le segment');
console.error('erreur esri');
}
}), lang.hitch(this, this.displayErrorMsg));
});
var editor = null;
if (this.attributeEditorId) {
editor = this.spwViewer.spwWidgetsManager.getWidgets({
widgetId: this.attributeEditorId
})[0];
} else {
editor = this.get('editorWidget');
}
if (editor == null) {
// show error
console.error('Impossible d\'éditer les attributs');
this.displayErrorMsg();
this.stopLoading();
return;
}
this.deactivateTemplatePicker();
editor.edit(createTemplate.featureLayer.url, newGraphic.attributes, lang.hitch(this, function(attr) {
lang.mixin(newGraphic.attributes, attr);
saveGeometry();
}), lang.hitch(this, this.stopLoading));
});
var newAttributes = lang.mixin(lang.clone(selectedTemplate.template.prototype.attributes));
createNewSegment(newAttributes);
}
}));
}
if (this._templatePicker === null) {
this.templateDiv = domConstruct.create('div', {
style: 'width: 100%; height: 100%;'
}, this.centerContentPane.domNode);
this._templatePicker = new TemplatePicker({
featureLayers: layers,
rows: "auto",
columns: 2,
grouping: true,
style: "width: 100%; height: auto; overflow: auto;"
}, this.templateDiv);
this._templatePicker.startup();
this._templatePicker.on("selection-change", lang.hitch(this, function() {
this.stopEdition();
if (this._templatePicker.getSelected()) {
this.enableButtons(false);
selectedTemplate = this._templatePicker.getSelected();
switch (selectedTemplate.featureLayer.geometryType) {
case "esriGeometryPoint":
this._drawToolbar.activate(Draw.POINT);
break;
case "esriGeometryPolyline":
this._drawToolbar.activate(Draw.POLYLINE);
break;
case "esriGeometryPolygon":
this._drawToolbar.activate(Draw.POLYGON);
break;
}
} else {
this.enableButtons(true);
this._drawToolbar.deactivate();
}
}));
}
},
refreshFeature: function(objectid, feature, layer) {
var query = new Query();
query.where = "OBJECTID = " + objectid;
query.returnGeometry = false;
query.outFields = ['*'];
new QueryTask(layer.url).execute(query, lang.hitch(this, function(info) {
if (info.features && info.features.length === 1) {
lang.mixin(feature.attributes, info.features[0].attributes);
} else {
layer.refresh();
}
}), lang.hitch(this, function(err) {
layer.refresh();
}));
},
enableButtons: function(enabled) {
this._drawActivated = !enabled;
this.editVerticesButton.set('disabled', !enabled);
this.moveButton.set('disabled', !enabled);
this.rotateButton.set('disabled', !enabled);
this.editAttributesButton.set('disabled', !enabled);
this.copyButton.set('disabled', !enabled);
this.deleteButton.set('disabled', !enabled);
this.splitJoinButton.set('disabled', !enabled);
},
deactivateTemplatePicker: function() {
if (this._drawToolbar !== null) {
this._drawToolbar.deactivate();
}
if (this._templatePicker !== null) {
this._templatePicker.clearSelection();
}
},
addAttributeInspector: function(layers) {
this._esriMap.infoWindow.on("hide", lang.hitch(this, function() {
if (this._currentLayer !== null) {
this._currentLayer.clearSelection();
}
}));
var featureLayersArray = array.map(layers, lang.hitch(this, function(layer) {
var fieldInfos = null;
if (layer.fields) {
var ordered = this.orderFields(layer.fields);
array.forEach(ordered, lang.hitch(this, function(field) {
if (this.hiddenAttributes && this.hiddenAttributes.indexOf(field.name) > -1) {
return;
}
fieldInfos = fieldInfos || [];
fieldInfos.push({
fieldName: field.name,
isEditable: (this.readOnlyAttributes == null || this.readOnlyAttributes.indexOf(field.name) === -1)
});
}));
}
return {
featureLayer: layer,
fieldInfos: fieldInfos
};
}));
this.attributeInspectorHandler && this.attributeInspectorHandler.remove();
this._attributeInspector = new AttributeInspector({
layerInfos: featureLayersArray
}, domConstruct.create("div"));
this.attributeInspectorHandler = aspect.after(this._attributeInspector, '_showFeature', lang.hitch(this, function() {
if (this._attributeInspector._attributes == null || this._attributeInspector._currentFeature == null) {
return;
}
if (this._attributeInspector._attributes._relatedAttributeAdded) {
return;
}
this._attributeInspector._attributes._relatedAttributeAdded = true;
if (this.relatedLayers && this.relatedLayers.length > 0) {
var tbody = this._attributeInspector._attributes.childNodes[0];
array.forEach(this.relatedLayers, lang.hitch(this, function(relatedLayer) {
var tr = domConstruct.create('tr', null, tbody, 'last');
domConstruct.create('td', {
innerHTML: '<div style="margin-top: 10px;">' + relatedLayer.name + '</div>',
style: 'text-align: left; vertical-align: top;'
}, tr);
var td = domConstruct.create('td', {
style: 'white-space: nowrap;'
}, tr);
var wgt = new SpwEditRelatedAttribute(lang.mixin(relatedLayer, {
spwViewer: this.spwViewer,
name: relatedLayer.name,
value: this._attributeInspector._currentFeature.attributes[relatedLayer.name],
required: relatedLayer.required === true,
disabled: relatedLayer.disabled === true
}), domConstruct.create('div', null, td));
on(wgt, 'change', lang.hitch(this, function() {
if (wgt.validate()) {
this._attributeInspector._currentFeature.attributes[relatedLayer.name] = wgt.get('value');
}
}));
}));
}
}));
var saveButton = new Button({
label: "Sauver",
"style": "float:right;"
}, domConstruct.create("div"));
domConstruct.place(saveButton.domNode, this._attributeInspector.deleteBtn.domNode, "after");
saveButton.on("click", lang.hitch(this, function() {
this.startLoading();
this._currentFeature.getLayer().applyEdits(null, [this._currentFeature], null, lang.hitch(this, this.stopLoading), lang.hitch(this, this.displayErrorMsg));
this._esriMap.infoWindow.hide();
}));
this._attributeInspector.on("attribute-change", lang.hitch(this, function(evt) {
this._currentFeature.attributes[evt.fieldName] = evt.fieldValue;
}));
this._attributeInspector.on("next", lang.hitch(this, function(evt) {
this._currentFeature = evt.feature;
console.log("Next " + this._currentFeature.attributes.objectid);
}));
this._attributeInspector.on("delete", lang.hitch(this, this.deleteElement));
this._esriMap.infoWindow.setContent(this._attributeInspector.domNode);
this._esriMap.infoWindow.set('anchor', this.infoWindowPosition);
this._esriMap.infoWindow.resize(this.infoWindowWidth, this.infoWindowHeight);
},
deleteElement: function(evt) {
var confirmDeleteFeature = new ConfirmDialog({
message: this.labels.deleteFeature_confirmMessage,
onConfirm: lang.hitch(this, function() {
this.startLoading();
evt.feature.getLayer().applyEdits(null, null, [evt.feature], lang.hitch(this, this.stopLoading), lang.hitch(this, this.displayErrorMsg));
this._esriMap.infoWindow.hide();
this._esriMap.setMapCursor("default");
}),
onCancel: lang.hitch(this, function() {
this._esriMap.setMapCursor("default");
})
});
confirmDeleteFeature.show();
},
deactivateAttributeInspector: function() {
if (this._esriMap && this._esriMap.infoWindow) {
this._esriMap.infoWindow.hide();
}
},
startLoading: function() {
this.loadingImg.src = this._loadingImgSrc;
},
stopLoading: function() {
this.loadingImg.src = this._emptyImgSrc;
this.refreshAllServices();
},
displayErrorMsg: function() {
MessageManager.getInstance().notifyError(this.labels.errorMessage_applyEdits);
console.error(this.labels.errorMessage_applyEdits);
this.stopLoading();
},
resize: function() {
this.inherited(arguments);
if (this._cptForResize > 1) {
this.tabContainerNode.resize();
}
},
clickToggleButton: function(buttonType) {
return lang.hitch(this, function(evt) {
this.stackContainerNode.selectChild(buttonType === 'splitJoin' ? this.helpJoinMergeNode : this.helpContentNode);
if (this.editMode !== buttonType) {
if (this.helpContents[buttonType]) {
this.helpTextNode.innerHTML = this.helpContents[buttonType];
}
if (this.editMode === 'splitJoin') {
this.onSplitJoinCancel();
}
this[this.editMode + "Button"].set("checked", false);
this.editMode = buttonType;
if (this.editMode === 'splitJoin') {
this.splitJoinToggleButton.set('disabled', true);
this.cancelSplitJoinButton.set('disabled', true);
this.confirmSplitJoinButton.set('disabled', true);
}
this.stopEdition(true);
} else {
event.stop(evt);
}
});
}
});
return editorWidget;
});