diff --git a/client/javascript/externs/formReader/component/map.js b/client/javascript/externs/formReader/component/map.js index 0482db946269fcaebace4fb649834a3e949861c0..84e8ba3cc709ec0c973afeee8a915f6be0de8a41 100755 --- a/client/javascript/externs/formReader/component/map.js +++ b/client/javascript/externs/formReader/component/map.js @@ -117,6 +117,10 @@ nsVitisComponent.Map = function (opt_option) { * @private */ this.$log_ = angular.element(vitisApp.appHtmlFormDrtv).injector().get(["$log"]); + /** + * @private + */ + this.$q_ = angular.element(vitisApp.appHtmlFormDrtv).injector().get(["$q"]); var this_ = this; @@ -355,7 +359,31 @@ nsVitisComponent.Map = function (opt_option) { } } - /****************************************************Map*******************************/ + var hiddenFeatures = angular.copy($("#" + this.hiddenFieldId).val()); + + // Initialise la carte + this.initMap(options).then(function(){ + // Initialise l'étendue de la carte + this_.initExtent(options); + // Initialise le CSS de la carte + this_.initCSS(options); + // Initialise les features de la carte + this_.initFeatureOverlay(options, hiddenFeatures); + }); + + return this; +}; + +/** + * Initialise la carte + * + * @param {object} options + */ +nsVitisComponent.Map.prototype.initMap = function (options) { + this.$log_.info('nsVitisComponent.Map.initMap'); + + var deferred = this.$q_.defer(); + var this_ = this; this.MapObject = new ol.Map({ target: this_.Target, @@ -372,16 +400,54 @@ nsVitisComponent.Map = function (opt_option) { this.setExtent(options["center"]["extent"]); } - if (options["type"] === "vmap" && options["tree"]) { - this['aTree'] = this.loadTree(options["tree"]); + if (options["type"] === "vmap") { + if (options["tree"]) { + this['aTree'] = this.loadTree(options["tree"]); + } else if (options["map_id"]) { + this.getAjaxVmapMap(options["map_id"]).then(function(oMap){ + + // Set map + this_['aTree'] = this_.loadTree(oMap); + + // Set proj_ + this_.proj_ = this_.MapObject.getView().getProjection().getCode(); + + deferred.resolve(this_['aTree']); + }, + // Cas où carte non trouvée + function(){ + this_.MapObject.addLayer(new ol.layer.Tile({ + source: new ol.source.OSM() + })); + this_['aTree'] = [{ + 'service': options["type"], + 'layers': this_.Layers + }]; + setTimeout(function () { + deferred.resolve(this_['aTree']); + }); + }); + } } else { this['aTree'] = [{ - 'service': options["type"], - 'layers': this.Layers - }]; + 'service': options["type"], + 'layers': this.Layers + }]; + setTimeout(function () { + deferred.resolve(this_['aTree']); + }); } - /****************************************Extent***************************************/ + return deferred.promise; +} + +/** + * Initialise l'étendue de la carte + * + * @param {object} options + */ +nsVitisComponent.Map.prototype.initExtent = function (options) { + this.$log_.info('nsVitisComponent.Map.initExtent'); // Vérifie que l'étendue soit valable var projExtent = ol.proj.get(options["proj"]).getExtent(); @@ -393,20 +459,34 @@ nsVitisComponent.Map = function (opt_option) { console.error('map extent oversized'); this.MapObject.getView().fit(projExtent); } +} +/** + * Initialise lle CSS de la carte + * + * @param {object} options + */ +nsVitisComponent.Map.prototype.initCSS = function (options) { + this.$log_.info('nsVitisComponent.Map.initCSS'); - //****************************************CSS******************************************/ $(".ol-mouse-position").css("bottom", "8px"); $(".ol-mouse-position").css("top", "auto"); $(".ol-mouse-position").css("background", this.appColor); $(".ol-mouse-position").css("color", "#ffffff"); $(".ol-mouse-position").css("border-radius", "4px"); - $(".ol-scale-line").css("background", this.appColor); - $(".ol-current-projection-studio").css("background", this.appColor); +} + +/** + * Initialise les features de la carte + * + * @param {object} options + */ +nsVitisComponent.Map.prototype.initFeatureOverlay = function (options, hiddenFeatures) { + this.$log_.info('nsVitisComponent.Map.initFeatureOverlay'); - /****************************************Feature Overlay********************************/ + var this_ = this; options["draw_color"] = goog.isDef(options["draw_color"]) ? options["draw_color"] : "rgba(54,184,255,0.6)"; options["contour_color"] = goog.isDef(options["contour_color"]) ? options["contour_color"] : "rgba(0,0,0,0.4)"; @@ -464,40 +544,72 @@ nsVitisComponent.Map = function (opt_option) { source: this_.FeatureOverlay.getSource() }) - var hiddenFeatures = $("#" + this.hiddenFieldId).val(); + // Met en place les Features + this.setFeatures(hiddenFeatures); + + // Écrit les features dans le champ hidden + this.Features.on("change", function () { + setTimeout(function () { + this_.saveFeatures(); + this_.MapObject.dispatchEvent('moveend'); + }); + }); +} + +/** + * Set the map features + * + * @param {string} hiddenFeatures GeoJSON features + */ +nsVitisComponent.Map.prototype.setFeatures = function (hiddenFeatures) { + this.$log_.info('nsVitisComponent.Map.setFeatures'); + + // Vide les précédentes features + this.FeatureOverlay.getSource().forEachFeature(function(oFeature){ + this.FeatureOverlay.getSource().removeFeature(oFeature); + }, this); // Lit les features écrites dans le champ hidden if (goog.isDef(hiddenFeatures)) { if (!goog.string.isEmpty(hiddenFeatures) && hiddenFeatures !== "[object Object]") { - var aFeatures = this_.getFeaturesByString(hiddenFeatures); + var aFeatures = this.getFeaturesByString(hiddenFeatures); // Ajoute les features for (var i = 0; i < aFeatures.length; i++) { this.FeatureOverlay.getSource().addFeature(aFeatures[i]); } - var featuresExtent = this.FeatureOverlay.getSource().getExtent(); - this.MapObject.getView().fit(featuresExtent); - // En cas de simple point - if (aFeatures.length === 1) { - if (aFeatures[0].getGeometry().getType() === 'Point') { - this.MapObject.getView().setZoom(12); - } - } + // Zoom sur les features + this.zoomOnFeatures(); + + // Sauvegarde les features + this.saveFeatures(); } } +} - // Écrit les features dans le champ hidden - this.Features.on("change", function () { - setTimeout(function () { - this_.saveFeatures(); - this_.MapObject.dispatchEvent('moveend'); - }); - }); +/** + * Zoom sur les features de this.FeatureOverlay + */ +nsVitisComponent.Map.prototype.zoomOnFeatures = function () { + this.$log_.info('nsVitisComponent.Map.zoomOnFeatures'); - return this; -}; + var aFeatures = this.FeatureOverlay.getSource().getFeatures(); + var featuresExtent = this.FeatureOverlay.getSource().getExtent(); + + if (aFeatures.length > 0) { + + this.MapObject.getView().fit(featuresExtent); + + // En cas de simple point + if (aFeatures.length === 1) { + if (aFeatures[0].getGeometry().getType() === 'Point') { + this.MapObject.getView().setZoom(15); + } + } + } +} /** * Save the current features @@ -526,14 +638,20 @@ nsVitisComponent.Map.prototype.saveFeatures = function () { /** * Parse the string (GeoJSON, WKT) and return the features * @param {string} sFeatures + * @param {string} sFormat * @returns {Array} + * @export */ -nsVitisComponent.Map.prototype.getFeaturesByString = function (sFeatures) { +nsVitisComponent.Map.prototype.getFeaturesByString = function (sFeatures, sFormat) { this.$log_.info('nsVitisComponent.Map.getFeaturesByString'); var aFeatures = []; - if (this.outputFormat_ === 'wkt' || this.outputFormat_ === 'ewkt') { + if (!goog.isDefAndNotNull(sFormat)) { + sFormat = this.outputFormat_; + } + + if (sFormat === 'wkt' || sFormat === 'ewkt') { if (ol.isEWKTGeom(sFeatures)) { aFeatures = ol.getFeaturesFromEWKT(sFeatures, this.proj_); } else { @@ -547,7 +665,7 @@ nsVitisComponent.Map.prototype.getFeaturesByString = function (sFeatures) { console.error('cannot read the geometry on format wkt'); } } - } else if (this.outputFormat_ === 'geojson') { + } else if (sFormat === 'geojson') { try { aFeatures = this.geoJSONFormat.readFeatures(sFeatures, { dataProjection: this.baseProj_, @@ -1318,10 +1436,19 @@ nsVitisComponent.Map.prototype.activeInteraction = function (sInteraction, bForc this_.MapObject.addInteraction(this.snapInteraction_); }; +/** + * Get the formReaderScope + * @return {object} scope + */ nsVitisComponent.Map.prototype.getFormReaderScope_ = function (){ return angular.element(this.Target).scope(); } +/** + * Show the divide segment form + * @param {string} sFeature1 + * @param {string} sFeature2 + */ nsVitisComponent.Map.prototype.showDividedSegmentForm_ = function (sFeature1, sFeature2){ var this_ = this; @@ -1674,6 +1801,62 @@ nsVitisComponent.Map.prototype.removeFeature = function (oFeature) { this.Features.changed(); }; +/** + * Requête Ajax pour répupérer la définition d'une carte + * @param {string} sMapId map_id + * @return {object} map json + */ +nsVitisComponent.Map.prototype.getAjaxVmapMap = function (sMapId) { + + var deferred = this.$q_.defer(); + + if (!angular.isDefined(sMapId) || sMapId == '') { + console.error('map_id non valide'); + deferred.reject(); + } else { + + // Récupère la liste des rapports disponibles + ajaxRequest({ + 'method': 'GET', + 'url': this.$propertiesSrvc_["web_server_name"] + "/" + this.$propertiesSrvc_["services_alias"] + '/vmap/mapjsons/' + sMapId, + 'headers': { + 'Accept': 'application/x-vm-json' + }, + 'success': function (response) { + if (!goog.isDefAndNotNull(response['data'])) { + console.error('response.data undefined: ', response); + deferred.reject(); + return 0; + } + if (!goog.isDefAndNotNull(response['data']['data'])) { + console.error('Aucune carte disponible pour ' + sMapId); + deferred.reject(); + return 0; + } + if (!goog.isDefAndNotNull(response['data']['data'][0])) { + console.error('Aucune carte disponible pour ' + sMapId); + deferred.reject(); + return 0; + } + if (!goog.isDefAndNotNull(response['data']['data'][0]['children'])) { + console.error('Aucune carte disponible pour ' + sMapId); + deferred.reject(); + return 0; + } + if (!goog.isDefAndNotNull(response['data']['data'][0]['children'][1])) { + console.error('Aucune carte disponible pour ' + sMapId); + deferred.reject(); + return 0; + } + var oMap = response['data']['data'][0]; + deferred.resolve(oMap); + } + }); + } + + return deferred.promise; +} + /** * Load a Map.json to set layers and view of the map * @param {Object} tree Json tree to set the map (he's generated by Vmap) diff --git a/client/javascript/externs/formReader/component/map_workbench/map_workbench.js b/client/javascript/externs/formReader/component/map_workbench/map_workbench.js index 32ed53d696a53063fe3dd4020ed207461e8f235a..76b09c686bef4b3411631f3042d7f1c92ef4929c 100755 --- a/client/javascript/externs/formReader/component/map_workbench/map_workbench.js +++ b/client/javascript/externs/formReader/component/map_workbench/map_workbench.js @@ -365,7 +365,7 @@ nsVitisComponent.MapWorkbenchDirective = function ($timeout) { break; } }); - }); + }, 500); // Intancie le subform setTimeout(function () { @@ -745,7 +745,7 @@ nsVitisComponent.MapWorkbenchController.prototype.addFeature = function () { }); }); - // Désactive la possibilité d'ajouter des features + // Désactive la possibilité d'ajouter des features this.unactiveDrawInteractions_(); // Ajoute la/les nouvelle interaction @@ -811,7 +811,7 @@ nsVitisComponent.MapWorkbenchController.prototype.setStyleFromType_ = function ( // La chaine commence et se finit par {{ et }} ? if (oStyle[key1][key2].substr(0, 2) === '{{' && oStyle[key1][key2].substr(-2, 2) === '}}') { - // Le contenu des balises est un des champs du formulaire ? + // Le contenu des balises est un des champs du formulaire ? var sTag = oStyle[key1][key2].substr(2, oStyle[key1][key2].length - 4); if (goog.isDefAndNotNull(oAttributes[sTag])) { oStyle[key1][key2] = oAttributes[sTag]; @@ -887,4 +887,4 @@ nsVitisComponent.MapWorkbenchController.prototype.unactiveDrawInteractions_ = fu // this.$scope_['oMap']['setInteractions'](this.$scope_['field']['map_options']['interactions']); // } // } -}; \ No newline at end of file +};