Retour à la documentation
/**
* @class spw.widgets.SpwAddressSearch
*/
define([
"dojo/_base/declare",
"spw/api/SpwBaseTemplatedWidget",
"dojo/text!./templates/SpwAddressSearch.html",
"dojo/_base/lang",
"dojo/store/Memory",
"dojo/data/ObjectStore",
"spw/api/Utils",
"spw/api/MessageManager",
"dojo/_base/array",
"dojo/i18n!./nls/SpwAddressSearch",
"dojo/on",
"dojo/query",
"dijit/form/FilteringSelect",
"dijit/form/Form",
"dijit/form/TextBox",
"dijit/form/Button"
], function(declare, SpwBaseTemplatedWidget, template, lang, Memory,
ObjectStore, Utils, MessageManager, array, labels, on, query) {
if(!Array.prototype.contains){
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
};
}
return declare("spw.widgets.SpwAddressSearch", [SpwBaseTemplatedWidget], /** @lends spw.widgets.SpwAddressSearch.prototype */{
templateString: template,
labels: labels,
localiteStore: null,
rueOfLocaliteStore: null,
selectLocalite: null,
selectRue: null,
numeroTextbox: null,
goButton: null,
apiLoaded: false,
/* START widget config */
apiSRID: 31370,
useCors: true,
closeOnZoom: true,
showInfoMessage: true,
hasCloseButton: true,
widgetTitle: "Localiser une adresse",
// helpContent: "http://geoportail.wallonie.be/aideWalOnMap",
position: "none",
style: {
width: "400px"
},
geolocalisationApiUrl: "//geoservices.wallonie.be/geolocalisation/js/SpwGeolocalisationApi.js",
/* END widget config */
postMixInProperties: function() {
this.inherited(arguments);
//Load data from geolocation services
require([this.geolocalisationApiUrl], lang.hitch(this, function(){
if(typeof(spwGeolocalisation) != "undefined"){
this.set('apiLoaded', true);
}
}));
},
postCreate: function() {
this.inherited(arguments);
//Load data from geolocation services
if(this.get('apiLoaded')){
this.getListeLocalites();
} else {
this.watch("apiLoaded", lang.hitch(this, function(){
this.getListeLocalites();
}));
}
this.own(
on(this._formNode, 'submit', lang.hitch(this, this.goButtonClicked)),
on(this._formNode, 'reset', lang.hitch(this, this.onReset))
);
},
onReset: function(e) {
this.selectLocalite.reset();
this.proposeRueDeLocalite();
this.numeroTextbox.set('disabled', true);
},
onActivate: function() {
this.inherited(arguments);
if (this.selectLocalite.item == null) {
this.selectLocalite.reset();
this.proposeRueDeLocalite();
}
else if (this.selectRue.item == null) {
this.selectRue.reset();
this.numeroTextbox.reset();
this.numeroTextbox.set('disabled', true);
}
},
getListeLocalites: function(){
if(this.useCors !== true){
spwGeolocalisation.setProxyPage(this.spwViewer.get('proxyPageUrl'));
}
spwGeolocalisation.getListeLocalites(lang.hitch(this, this.onListLocaliteSuccess));
},
onListLocaliteSuccess: function(data){
if (typeof data.localites != "undefined"){
this.createLocaliteStore(data.localites);
this.selectLocalite.set('store', this.localiteStore);
this.selectLocalite.set('queryExpr', '*${0}*');
this.selectLocalite.on("KeyUp", lang.hitch(this, function(){
if(this.selectLocalite.get('displayedValue').length < 3){
this.selectLocalite.closeDropDown();
}
}));
this.selectLocalite.on("Focus", lang.hitch(this, function(){
this.selectLocalite.set("displayedValue", "");
this.selectRue.set("displayedValue", "");
this.selectRue.set("disabled", true);
this.numeroTextbox.set("value", "");
this.numeroTextbox.set("disabled", true);
}));
this.selectLocalite.watch("item", lang.hitch(this, function(){
this.proposeRueDeLocalite();
}));
this.selectRue.set('queryExpr', '*${0}*');
this.selectRue.on("Focus", lang.hitch(this, function(){
this.selectRue.set("displayedValue", "");
this.numeroTextbox.set("value", "");
this.numeroTextbox.set("disabled", true);
}));
this.selectRue.watch("item", lang.hitch(this, function(){
this.numeroTextbox.set("disabled", false);
}));
}else{
this.onListLocaliteError(null,null, data);
}
},
createLocaliteStore: function(localites){
for(var i = 0 ; i < localites.length ; i++)
{
var localite = localites[i];
if(localite.nom == localite.commune){
localite.cps = this._makeCodePostauxListOfCommune(localites, localite.commune);
var allCpToString = this._makeCodePostauxList(localite.cps);
localite.searchLabel = localite.nom + " (" + allCpToString + ")";
localite.displayLabel = "<b>" + localite.nom + "</b> (" + allCpToString +")";
} else {
var allCpToString = this._makeCodePostauxList(localite.cps);
localite.searchLabel = localite.nom + " (" + allCpToString + ")";
localite.displayLabel = localite.nom + " (" + allCpToString + ") - <b>" +
localite.commune + "</b>";
}
}
var memoryLocaliteStore = new Memory({data: localites});
memoryLocaliteStore.queryEngine = this.createLocaliteQueryEngine();
this.localiteStore = new ObjectStore({ objectStore:memoryLocaliteStore });
},
_makeCodePostauxListOfCommune: function(localiteList, commune){
var tab = [];
for(var i = 0 ; i < localiteList.length ; i++)
{
if(localiteList[i].commune == commune){
for(var j = 0 ; j < localiteList[i].cps.length ; j++){
if(tab.indexOf(localiteList[i].cps[j]) == -1){
tab.push(localiteList[i].cps[j]);
}
}
}
}
return tab;
},
_makeCodePostauxList: function(cpTab){
var list = "";
for(var i = 0 ; i < cpTab.length ; i++){
list += cpTab[i] + ",";
}
return list.substr(0, list.length-1);
},
createLocaliteQueryEngine: function(){
return function(query, options){
var filteringFunction = function(object){
if(options.query.searchLabel.length > 0 && Utils.removeAccent(object.searchLabel).indexOf(Utils.removeAccent(options.query.searchLabel)) > -1){
return true;
}
return false;
};
var execute = function(arr){
var results = array.filter(arr, filteringFunction);
return results.sort(function(a, b){
if(Utils.removeAccent(a.nom) == Utils.removeAccent(b.nom)) return 0;
else if(Utils.removeAccent(a.nom) > Utils.removeAccent(b.nom)) return 1;
else return -1;
});
};
execute.matches = filteringFunction;
return execute;
};
},
currentItem: null,
proposeRueDeLocalite: function(){
if(this.selectLocalite.item != null && this.selectLocalite.item != this.currentItem){
this.currentItem = this.selectLocalite.item;
if(this.currentItem.nom == this.currentItem.commune){
if(this.selectLocalite.item.cps != null && this.selectLocalite.item.cps.length > 0){
spwGeolocalisation.getListeRuesOfAllCpsOfCommuneByCp(this.selectLocalite.item.cps[0],
lang.hitch(this, function(data){this.onListRueOfLocaliteSuccess(data, this.selectLocalite.item);}),
this.onListLocaliteError);
} else {
spwGeolocalisation.getListeRuesByCommune(this.selectLocalite.item.nom,
lang.hitch(this, function(data){this.onListRueOfLocaliteSuccess(data, this.selectLocalite.item);}),
this.onListLocaliteError);
}
} else {
if(this.selectLocalite.item.cps != null && this.selectLocalite.item.cps.length > 1){
spwGeolocalisation.getListeRuesOfAllCpsOfCommuneByCp(this.selectLocalite.item.cps[0],
lang.hitch(this, function(data){this.onListRueOfLocaliteSuccess(data, this.selectLocalite.item);}),
this.onListLocaliteError);
} else if (this.selectLocalite.item.cps != null && this.selectLocalite.item.cps.length == 1){
spwGeolocalisation.getListeRuesByCp(this.selectLocalite.item.cps[0],
lang.hitch(this, function(data){this.onListRueOfLocaliteSuccess(data, this.selectLocalite.item);}),
this.onListLocaliteError);
} else {
spwGeolocalisation.getListeRuesByLocalite(this.selectLocalite.item.nom,
lang.hitch(this, function(data){this.onListRueOfLocaliteSuccess(data, this.selectLocalite.item);}),
this.onListLocaliteError);
}
}
} else if (this.selectLocalite.item == null){
this.currentItem = null;
this.rueOfLocaliteStore = new ObjectStore({ objectStore:new Memory({data: []})});
this.selectRue.set('store', this.rueOfLocaliteStore);
this.selectRue.set('disabled', true);
}
},
onListRueOfLocaliteSuccess: function(data, localiteOrigine){
var datas = [];
if(localiteOrigine.cps.length > 1){
for (var i=0; i<data.rues.length; i++){
if(localiteOrigine.nom == localiteOrigine.commune || data.rues[i].localites.contains(localiteOrigine.nom)) {
data.rues[i].displayName = data.rues[i].nom + " - " + data.rues[i].cps[0];
datas.push(data.rues[i]);
}
}
} else {
for (var i=0; i<data.rues.length; i++){
if(localiteOrigine.nom == localiteOrigine.commune || data.rues[i].localites.contains(localiteOrigine.nom)) {
data.rues[i].displayName = data.rues[i].nom;
datas.push(data.rues[i]);
}
}
}
this.selectRue.set("disabled", false);
this.rueOfLocaliteStore = new ObjectStore({ objectStore:new Memory({data: datas, queryEngine:this.createRueQueryEngine()})});
this.selectRue.set('store', this.rueOfLocaliteStore);
},
createRueQueryEngine: function(){
return function(query, options){
var filteringFunction = function(object){
if(options.query.displayName.length > 0 && Utils.removeAccent(object.displayName).indexOf(Utils.removeAccent(options.query.displayName)) > -1){
return true;
}
return false;
};
var execute = function(arr){
var results = array.filter(arr, filteringFunction);
return results.sort(function(a, b){
if(Utils.removeAccent(a.displayName) == Utils.removeAccent(b.displayName)) return 0;
else if(Utils.removeAccent(a.displayName) > Utils.removeAccent(b.displayName)) return 1;
else return -1;
});
};
execute.matches = filteringFunction;
return execute;
};
},
onListLocaliteError: function(request, status, error){
MessageManager.getInstance().notifyError(this.labels.geolocalisationError + error);
},
goButtonClicked: function(){
if(this.selectLocalite.displayedValue == null || this.selectLocalite.displayedValue.length < 3) return false;
if(this.selectLocalite.item != null){
if(this.selectRue.item != null){
var rue=this.selectRue.item.nom;
var cpDeRue=this.selectRue.item.cps[0];//Peut etre différent de la localité sélectionnée si celle-ci est une commune
if(this.numeroTextbox.get("value") != null && this.numeroTextbox.get("value") != ""){
var numero = this.numeroTextbox.get("value");
spwGeolocalisation.getPositionByCpRueAndNumero(cpDeRue, rue, numero,
lang.hitch(this, function(data){
if(data.errorCode != 0){
MessageManager.getInstance().notifyWarning(data.infoMsg || data.errorMsg);
} else {
this.spwViewer.get('spwMap').zoomToPoint(data.x, data.y, 20, this.apiSRID);
this.closeOnZoom && this.closeWidget();
if (data.infoMsg && this.showInfoMessage) {
MessageManager.getInstance().notifyInfo('',data.infoMsg, 3000);
}
}
}),
lang.hitch(this, function(request, status, error){
MessageManager.getInstance().notifyError(this.labels.geolocalisationError + error);
})
);
} else {
this.spwViewer.get('spwMap').zoomToBbox(this.selectRue.item.xMin, this.selectRue.item.xMax,
this.selectRue.item.yMin, this.selectRue.item.yMax, this.apiSRID);
this.closeOnZoom && this.closeWidget();
}
} else {
this.spwViewer.get('spwMap').zoomToBbox(this.selectLocalite.item.xMin, this.selectLocalite.item.xMax,
this.selectLocalite.item.yMin, this.selectLocalite.item.yMax, this.apiSRID);
this.closeOnZoom && this.closeWidget();
}
} else {
MessageManager.getInstance().notifyError(this.labels.invalidForm);
}
return false;
},
closeWidget: function() {
var p = this.getParent();
if (p && p.spwParentMenu && (p = p.spwParentMenu.getParent())) {
if (p.spwParentMenu && (p = p.spwParentMenu.getParent())) {
p._button.closeDropDown();
}
}
}
});
});