Source: searchers/dawaSearcher.js

 /** 
 *  
 * @class Searches Dawa (http://dawa.aws.dk/)
 * @extends Septima.Search.Searcher
 * @constructs Septima.Search.DawaSearcher
 * @param {Object} options DawaSearcher expects these properties:
 * @param options.kommunekode {string} "*" Search all municipalities (Default), Search specific municipalities eg. "101" or "101|256"
 * @param options.srid {string} eg. "25832" (Default is 4326)
 *  
 * @example 
 * var dawaSearchOptions = {
 *     onSelect : function(result) {
 *          jQuery('#log').prepend('<b>Adresse valgt:</b> '+ result.title);},
 *     kommunekode: "151",
 *     matchesPhrase: "Begins with ",
 *     searchDelay: 200
 * };
 * searchers.push({title : 'Adresse', searcher : new Septima.Search.DawaSearcher(dawaSearchOptions)});
 *
 */
Septima.Search.DawaSearcher = Septima.Class (Septima.Search.Searcher, /**  @lends Septima.Search.DawaSearcher# */{
	
    roadIcon: Septima.Search.icons.searchers.dawa.road,
	
	initialize: function (options) {
		this.iconURI = Septima.Search.icons.searchers.dawa.result;
		this.minimumShowCount = 3;
		this.showOneStreet = false;
		if (options === undefined){
			this.Searcher({usesGeoFunctions: true});
		}else{
			if (options.kommunekode) {
				this.kommunekode = options.kommunekode;
			}
			if (options.minimumShowCount){
				this.minimumShowCount = options.minimumShowCount;
			}
			options.usesGeoFunctions = true;
			
	        this.allowDetails = false;
	        if (options.allowDetails){
	            this.allowDetails = options.allowDetails;
	        }
	        
			this.Searcher(options);
		}
	},
	
	fetchData: function (query, caller){
		var queryOptions = {
			limit: query.limit + this.minimumShowCount + 1,
			query: query.queryString,
			showOneStreet: this.showOneStreet
		};
		
		if (this.kommunekode) {
			queryOptions.kommunekode = this.kommunekode;
		}
		
		new Septima.Search.Dawa.Query(queryOptions).done(Septima.bind(function(query, dawaResult){
			caller.fetchSuccess(this.QueryResultFromDawaResult(query, dawaResult));
		}, this, query));

	},
	
	addHitToQueryResult: function(thisHit, queryResult){
		var presentation;
		var newQueryString;
		var newquery;
    	if (thisHit.type === "adgangsadresseinfo"){
            presentation = thisHit.tekst;
        	var result = queryResult.addResult(presentation, null, null, thisHit);
    		result.isComplete = false;
        	result.image = Septima.Search.icons.searchers.dawa.result;
    	}else if (thisHit.type === "vej"){
    		newQueryString = thisHit.vejnavn + " <select></select>"  + ' ' + thisHit.postnummer + ' ' + thisHit.postdistrikt;
        	presentation = thisHit.vejnavn  + ' ' + thisHit.postnummer + ' ' + thisHit.postdistrikt;
        	newQuery = queryResult.addNewQuery(presentation, null, newQueryString, null, thisHit);
        	newQuery.image = Septima.Search.icons.searchers.dawa.road;
    	}else if (thisHit.type === "vejnavn"){
    		newQueryString = thisHit.vejnavn + " <select></select>";
        	presentation = thisHit.vejnavn;
        	newQuery = queryResult.addNewQuery(presentation, null, newQueryString, null, thisHit);
        	newQuery.image = Septima.Search.icons.searchers.dawa.road;
    	}
	},
	
	QueryResultFromDawaResult: function (query, dawaResult){
		var queryResult = this.createQueryResult();
		var limit = query.limit;
		var count = dawaResult.length;
		var newquery;

		if (count > 0) {
	        var listStrategy = this.getListStrategy(query, count);
	        var thisHit;
			if (listStrategy.show){
			    for (var i = 0; i < dawaResult.length; i++){
			    	thisHit = dawaResult[i];
			    	this.addHitToQueryResult(thisHit, queryResult);
			    }
			}else{
			    if (this.minimumShowCount > 0){
				    for (var j = 0; (j < dawaResult.length) && (j < this.minimumShowCount); j++){
				    	thisHit = dawaResult[j];
				    	this.addHitToQueryResult(thisHit, queryResult);
				    }
				    if (count > this.minimumShowCount){
				    	newQuery = queryResult.addNewQuery("Flere adresser", "", query.queryString, null, null);
				    	newQuery.image = Septima.Search.icons.searchers.dawa.result;
				    }
			    }else{
			    	newQuery = queryResult.addNewQuery("Adresser", "", query.queryString, null, null);
			    	newQuery.image = Septima.Search.icons.searchers.dawa.result;
			    }
			}
		}
        return queryResult;
	},
	
	error: function(caller, jqXHR, textStatus, errorThrown){
		caller.fetchError(this, errorThrown);
	},
	
	completeResult:function(result){
		var completeDeferred = jQuery.Deferred();
		if (result.isComplete){
			completeDeferred.resolve(result);			
		}else{
			new Septima.Search.Dawa.AdgangsAdresseQuery(result.data.id).done(Septima.bind(function(deferred, result, data){
				result.data = data;
		        result.geometry = data.geometry;
				result.isComplete = true;
		        deferred.resolve(result);
			}, this, completeDeferred, result));
		}

		return completeDeferred.promise();
	},
	
	onSelect: function(result){
        if (typeof result.newquery !== 'undefined'){
        	if (result.data && result.data.type && result.data.type == 'vej'){
        		//new Septima.Search.Dawa.VejStykkeQuery(result.data.vejnavn, result.data.postnummer).ready(Septima.bind(function(result, data){
           		new Septima.Search.Dawa.VejStykkeQuery({postnr: result.data.postnummer, navn: result.data.vejnavn}).done(Septima.bind(function(result, data){
    				result.data.vejkode = data.kode;
    				this._onSelectCallback(result);
    			}, this, result));
        	}
        }else{
    		this._onSelectCallback(result);
        }
	},
	
    getDetailHandlersForResult: function(result){
        var detailHandlerDefs = [];
        if (this.allowDetails){
            var dagiDetailsHandler = Septima.bind(function(result, deferred, detailsContent){

//              "id": "3a1a910d-3d1c-4bd6-b599-abb1870fe30d",
//              "status": 1,
//              "oprettet": "2014-08-29T16:08:41.283",
//              "ændret": "2015-09-21T13:12:11.243",
//              "vejkode": "0006",
//              "vejnavn": "Aberdeengade",
//              "adresseringsvejnavn": "Aberdeengade",
//              "husnr": "4",
//              "supplerendebynavn": null,
//              "postnr": "2150",
//              "postnrnavn": "Nordhavn",
//              "stormodtagerpostnr": null,
//              "stormodtagerpostnrnavn": null,
//              "kommunekode": "0101",
//              "ejerlavkode": 2000157,
//              "etrs89koordinat_øst": 725702.18,
//              "etrs89koordinat_nord": 6179259.13,
//              "wgs84koordinat_bredde": 55.7064590329691,
//              "wgs84koordinat_længde": 12.5925721113134,
//              "nøjagtighed": "A",
//              "kilde": 1,
//              "tekniskstandard": "TD",
//              "tekstretning": 284.29,
//              "adressepunktændringsdato": "2014-08-29T00:00:00.000",
//              "ddkn_m100": "100m_61792_7257",
//              "ddkn_km1": "1km_6179_725",
//              "ddkn_km10": "10km_617_72",
//              "regionskode": 1084,
//              "jordstykke_ejerlavnavn": "Frihavnskvarteret, København",
//              "kvh": "01010006___4",
//              "sognekode": "7046",
//              "sognenavn": "Hans Egedes",
//              "politikredskode": "1470",
//              "retskredskode": "1101",
//              "opstillingskredskode": "0001",
              
               var  items = [];
              items.push({type: "labelvalue", label: "Sogn", value: result.data.properties.sognenavn});
              items.push({type: "labelvalue", label: "ejerlavnavn", value: result.data.properties.ejerlavnavn});
              items.push({type: "labelvalue", label: "matrikelnr", value: result.data.properties.matrikelnr});
              items.push({type: "labelvalue", label: "esrejendomsnr", value: result.data.properties.esrejendomsnr});
              items.push({type: "labelvalue", label: "regionsnavn", value: result.data.properties.regionsnavn});
              items.push({type: "labelvalue", label: "politikredsnavn", value: result.data.properties.politikredsnavn});
              items.push({type: "labelvalue", label: "retskredsnavn", value: result.data.properties.retskredsnavn});
              items.push({type: "labelvalue", label: "opstillingskredsnavn", value: result.data.properties.opstillingskredsnavn});
              items.push({type: "labelvalue", label: "zone", value: result.data.properties.zone});
              deferred.resolve(detailsContent.formatItems(items));
          }, this);
          
          detailHandlerDefs.push({"buttonText": "DAGI-opslag for " + result.title, "buttonImage": Septima.Search.icons.folderIcon, "handler": dagiDetailsHandler});            
        }

        return detailHandlerDefs;
    },
    

	CLASS_NAME: 'Septima.Search.DawaSearcher'

});