1 /**
  2  * @fileOverview  
  3  * Copyright (c) 2013 Regione Autonoma della Sardegna
  4  * Published under the GPL license.<br>
  5  * See <a href="https://sardegnageoportale.it/webgis/license.txt">https://sardegnageoportale.it/webgis/license.txt</a>
  6  * for the full text of the license.
  7  * @version 0.1
  8  */
  9 
 10 /**
 11  * @requires OpenLayers/Control.js
 12  * @requires OpenLayers/Handler/Box.js
 13  */
 14 
 15 /**
 16  * @namespace framework.widgets.control
 17  */
 18 Ext.namespace("framework.widgets.control");
 19 
 20 /** 
 21  *  Provides a search by drawing a box
 22  *  @name_ SearchInBox
 23  *  @class Provides a search by drawing a box
 24  *  @constructor
 25  *  @extends <a target="_blank" href="http://dev.openlayers.org/docs/files/OpenLayers/Control-js.html">OpenLayers.Control</a>
 26  */
 27 
 28 framework.widgets.control.SearchInBox = OpenLayers.Class(OpenLayers.Control,
 29         /** 
 30          * @lends framework.widgets.control.SearchInBox.prototype 
 31          */
 32                 {
 33                     /**
 34                      * @private
 35                      */
 36                     draw: function() {
 37                         this.handler = new OpenLayers.Handler.Box(this,
 38                                 {done: this.searchBox}, {keyMask: this.keyMask});
 39                         this.events.on({'deactivate': function() {
 40                                 //this.layer.destroyFeatures();
 41                                 //delete this.map.boundRicerca;
 42                             },
 43                             scope: this})
 44                     },
 45                     /**
 46                      * @private
 47                      * @param {Object} OpenLayers.Bounds or OpenLayers.Pixel
 48                      */
 49                     searchBox: function(position) {
 50                         if (position instanceof OpenLayers.Bounds) {
 51                             var bounds;
 52                             if (!this.out) {
 53                                 var minXY = this.map.getLonLatFromPixel({
 54                                     x: position.left,
 55                                     y: position.bottom
 56                                 });
 57                                 var maxXY = this.map.getLonLatFromPixel({
 58                                     x: position.right,
 59                                     y: position.top
 60                                 });
 61                                 bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat,
 62                                         maxXY.lon, maxXY.lat);
 63 
 64                                 this.drawFeatureExtent(bounds);
 65                             }
 66                             else {
 67                                 var pixWidth = Math.abs(position.right - position.left);
 68                                 var pixHeight = Math.abs(position.top - position.bottom);
 69                                 var zoomFactor = Math.min((this.map.size.h / pixHeight),
 70                                         (this.map.size.w / pixWidth));
 71                                 var extent = this.map.getExtent();
 72                                 var center = this.map.getLonLatFromPixel(
 73                                         position.getCenterPixel());
 74                                 var xmin = center.lon - (extent.getWidth() / 2) * zoomFactor;
 75                                 var xmax = center.lon + (extent.getWidth() / 2) * zoomFactor;
 76                                 var ymin = center.lat - (extent.getHeight() / 2) * zoomFactor;
 77                                 var ymax = center.lat + (extent.getHeight() / 2) * zoomFactor;
 78                                 bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax);
 79                             }
 80                             // always zoom in/out 
 81                             var lastZoom = this.map.getZoom();
 82                             this.map.zoomToExtent(bounds);
 83                             if (lastZoom == this.map.getZoom() && this.alwaysZoom == true) {
 84                                 this.map.zoomTo(lastZoom + (this.out ? -1 : 1));
 85                             }
 86                         } else { // it's a pixel
 87                             if (!this.out) {
 88                                 this.map.setCenter(this.map.getLonLatFromPixel(position),
 89                                         this.map.getZoom() + 1);
 90                             } else {
 91                                 this.map.setCenter(this.map.getLonLatFromPixel(position),
 92                                         this.map.getZoom() - 1);
 93                             }
 94                         }
 95                         this.deactivate();
 96                     },
 97                     /** draw a poligon feature on map
 98                      * @public
 99                      * @param {Object} OpenLayers.Bounds
100                      */
101                     drawFeatureExtent: function(bounds) {
102                         var polygon = bounds.toGeometry();
103 
104                         var feature = new OpenLayers.Feature.Vector(polygon, null, OpenLayers.Feature.Vector.style['default']);
105                         
106                         //Se utilizzato in ambiente di ricerca, agganciare a questo evento la pulizia della mappa
107                         this.map.events.triggerEvent('cleanSearchObjects');
108                         this.layer.events.remove('featureselected');
109 	
110                         
111                         //pulizia vecchie features
112                         this.layer.destroyFeatures();
113                         if(window.popupAddr) {
114                             window.popupAddr.close();
115                             window.popupAddr.destroy();
116                         }
117                         this.layer.addFeatures(feature);
118                         this.map.setLayerIndex(this.layer, this.map.layers.length - 1);
119 
120                         this.map.boundRicerca = polygon;
121                     },
122                     CLASS_NAME: "framework.widgets.control.SearchInBox"
123                 });
124