Changeset 1190

Show
Ignore:
Timestamp:
10/14/08 09:39:42 (3 months ago)
Author:
fredj
Message:

replace search mediator with protocol decorators (see #261)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/camptocamp/MapFishUnhcr/client/apidoc_config/Menu.txt

    r1124 r1190  
    6969         File: Protocol  (no auto-title, core/Protocol.js) 
    7070         File: MapFish  (no auto-title, core/Protocol/MapFish.js) 
     71         File: TriggerEventDecorator (no auto-title, core/Protocol/TriggerEventDecorator.js) 
     72         File: MergeFilterDecorator (no auto-title, core/Protocol/MergeFilterDecorator.js) 
    7173         } # Group: Protocol 
    7274 
    73       File: SearchMediator  (no auto-title, core/SearchMediator.js) 
    7475      File: PrintProtocol  (no auto-title, core/PrintProtocol.js) 
    7576      File: Offline  (no auto-title, core/Offline.js) 
  • sandbox/camptocamp/MapFishUnhcr/client/examples/search/c2corg.html

    r1124 r1190  
    9393            map.addControl(new OpenLayers.Control.PanZoomBar()); 
    9494 
    95             var mediator = new mapfish.SearchMediator({ 
     95            // we need to wrap the MapFish protocol in a trigger event 
     96            // decorator protocol so that the default popup can be  
     97            // displayed by the searcher 
     98            var protocol = mapfish.Protocol.decorateProtocol({ 
    9699                protocol: new mapfish.Protocol.MapFish({ 
    97100                    url: mapfish.SERVER_BASE_URL + 'summits', 
     
    99102                        maxfeatures: 10 
    100103                    } 
    101                 }) 
     104                }), 
     105                TriggerEventDecorator: null 
    102106            }); 
    103107 
    104108            searcher = new mapfish.Searcher.Map({ 
    105109                mode: mapfish.Searcher.Map.BOX, 
    106                 mediator: mediator
     110                protocol: protocol
    107111                displayDefaultPopup: true 
    108112            }); 
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/MapFish.js

    r1173 r1190  
    9999            "core/Routing.js", 
    100100            "core/Util.js", 
    101             "core/SearchMediator.js", 
    102101            "core/Searcher.js", 
    103102            "core/Searcher/Map.js", 
    104103            "core/Searcher/Form.js", 
     104            "core/PrintProtocol.js", 
     105            "core/Offline.js", 
    105106            "core/Protocol.js", 
    106107            "core/Protocol/MapFish.js", 
    107108            "core/Protocol/WFS.js", 
    108109            "core/Protocol/MergeFilterDecorator.js", 
    109             "core/PrintProtocol.js", 
    110             "core/Offline.js", 
    111             "core/Protocol.js", 
    112             "core/Protocol/MapFish.js", 
     110            "core/Protocol/TriggerEventDecorator.js", 
    113111            "widgets/MapComponent.js", 
    114112            "widgets/Shortcuts.js", 
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/core/Protocol.js

    r1124 r1190  
    1818 */ 
    1919 
    20 /* 
    21  * @requires OpenLayers/Protocol.js 
     20/** 
     21 * Namespace: mapfish.Protocol 
     22 * Contains convenience methods for protocol manipulation. 
    2223 */ 
     24mapfish.Protocol = { 
    2325 
    24 /** 
    25  * Class: mapfish.Protocol 
    26  * Abstract protocol class.  Not to be instantiated directly.  Use 
    27  *     one of the protocol subclasses instead. 
    28  *  
    29  * mapfish.Protocol actually references <OpenLayers.Protocol> 
    30  */ 
    31 mapfish.Protocol = OpenLayers.Protocol; 
     26    /** 
     27     * APIFunction: decorateProtocol 
     28     * Decorate a protocol. 
     29     * 
     30     * Example of use: 
     31     * (start code) 
     32     * var protocol = mapfish.Protocol.decorateProtocol({ 
     33     *     protocol: protocol, 
     34     *     TriggerEventDecorator: { 
     35     *         eventListeners: { 
     36     *             crudfinished: function() { 
     37     *                 alert("CRUD operation completed"); 
     38     *             } 
     39     *         } 
     40     *     }, 
     41     *     MergeFilterDecorator: null 
     42     * }); 
     43     * (end) 
     44     * 
     45     * Parameters: 
     46     * config - {Object} Config object specifying how protocol must be 
     47     *     decorated, see the above the example. 
     48     * 
     49     * Returns: 
     50     * {<OpenLayers.Protocol>} The resulting protocol. 
     51     * */ 
     52    decorateProtocol: function(config) { 
     53        var protocol = config.protocol; 
     54        for (var key in config) { 
     55            if (key != "protocol") { 
     56                if (!mapfish.Protocol[key]) { 
     57                    OpenLayers.Console.error( 
     58                        "mapfish.Protocol." + key + " does not exist"); 
     59                } else { 
     60                    protocol = new mapfish.Protocol[key]( 
     61                        OpenLayers.Util.extend( 
     62                            {protocol: protocol}, config[key]) 
     63                    ); 
     64                } 
     65            } 
     66       } 
     67       return protocol; 
     68    } 
     69}; 
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/core/Protocol/MapFish.js

    r1124 r1190  
    4545    initialize: function(options) { 
    4646        options = options || {}; 
    47         options.format = new OpenLayers.Format.GeoJSON(); 
     47        if (!options.format) { 
     48            options.format = new OpenLayers.Format.GeoJSON(); 
     49        } 
    4850        OpenLayers.Protocol.HTTP.prototype.initialize.call(this, options); 
    4951    }, 
     
    104106     */ 
    105107    "read": function(options) { 
     108        // workaround a bug in OpenLayers 
     109        options.params = OpenLayers.Util.applyDefaults( 
     110            options.params, this.options.params); 
    106111        if (options) { 
    107112            this.filterAdapter(options); 
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/core/Protocol/MergeFilterDecorator.js

    r1176 r1190  
    2020/* 
    2121 * @requires OpenLayers/Util.js 
    22  * @requires core/Protocol.js 
     22 * @requires OpenLayers/Protocol.js 
    2323 * @requires core/Searcher.js 
    2424 */ 
     
    109109     */ 
    110110    "read": function(options) { 
    111         options.filter = this.mergeFilters(options.filter); 
     111        options.filter = this.mergeFilters( 
     112            options.filter || options.params, options.searcher); 
     113        delete options.searcher; 
    112114        return this.protocol.read(options); 
    113115    }, 
     
    118120     * 
    119121     * Parameters: 
    120      * filter - {<OpenLayers.Filter>} 
     122     * filter - {<OpenLayers.Filter>}|{Object} 
     123     * searcher - {<mapfish.Searcher>} 
    121124     * 
    122125     * Returns: 
    123126     * {<OpenLayers.Filter>} The resulting filter. 
    124127     */ 
    125     mergeFilters: function(filter) { 
    126         for (var i = 0, len = this.searchers.length; i < len; i++) { 
    127             filter = this.toFilter(this.searchers[i].getFilter(), filter); 
     128    mergeFilters: function(filter, searcher) { 
     129        var i, len, s; 
     130        for (i = 0, len = this.searchers.length; i < len; i++) { 
     131            s = this.searchers[i]; 
     132            if (s != searcher) { 
     133                filter = this.toFilter(s.getFilter(), filter); 
     134            } 
    128135        } 
    129136        return filter; 
     
    145152        } 
    146153        if (!filter) { 
    147              filter = new OpenLayers.Filter.Logical({ 
    148                  type: OpenLayers.Filter.Logical.AND 
    149              }); 
     154            filter = new OpenLayers.Filter.Logical({ 
     155                type: OpenLayers.Filter.Logical.AND 
     156            }); 
     157        } else if (!this.isFilter(filter)) { 
     158            filter = this.fromObjToFilter(filter); 
    150159        } else if (!this.isLogicalFilter(filter)) { 
    151             filter = new OpenLayers.Filter.Logical({ 
    152                 type: OpenLayers.Filter.Logical.AND, 
    153                 filters: [filter] 
    154             }); 
     160            filter = new OpenLayers.Filter.Logical({ 
     161                type: OpenLayers.Filter.Logical.AND, 
     162                filters: [filter] 
     163            }); 
    155164        } 
    156165        var filters = filter.filters; 
     
    158167            filters.push(obj); 
    159168        } else { 
    160             for (var key in obj) { 
    161                 filters.push( 
    162                     new OpenLayers.Filter.Comparison({ 
    163                         type: OpenLayers.Filter.Comparison.EQUAL_TO, 
    164                         property: key, 
    165                         value: obj[key] 
    166                     }) 
    167                 ); 
    168             } 
     169            filters.push(this.fromObjToFilter(obj)); 
    169170        } 
    170171        return filter; 
     172    }, 
     173 
     174    /** 
     175     * Method: fromObjToFilter 
     176     * 
     177     * Paremeters: 
     178     * obj - {Object} 
     179     * 
     180     * Returns: 
     181     * {<OpenLayers.Filter.Logical>} 
     182     */ 
     183    fromObjToFilter: function(obj) { 
     184        var filters = []; 
     185        for (var key in obj) { 
     186            filters.push( 
     187                new OpenLayers.Filter.Comparison({ 
     188                    type: OpenLayers.Filter.Comparison.EQUAL_TO, 
     189                    property: key, 
     190                    value: obj[key] 
     191                }) 
     192            ); 
     193        } 
     194        return new OpenLayers.Filter.Logical({ 
     195            type: OpenLayers.Filter.Logical.AND, 
     196            filters: filters 
     197        }); 
    171198    }, 
    172199 
     
    265292    CLASS_NAME: "mapfish.Protocol.MergeFilterDecorator" 
    266293}); 
    267 /* 
    268  * Copyright (C) 2007  Camptocamp 
    269  * 
    270  * This file is part of MapFish Client 
    271  * 
    272  * MapFish Client is free software: you can redistribute it and/or modify 
    273  * it under the terms of the GNU General Public License as published by 
    274  * the Free Software Foundation, either version 3 of the License, or 
    275  * (at your option) any later version. 
    276  * 
    277  * MapFish Client is distributed in the hope that it will be useful, 
    278  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
    279  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    280  * GNU General Public License for more details. 
    281  * 
    282  * You should have received a copy of the GNU General Public License 
    283  * along with MapFish Client.  If not, see <http://www.gnu.org/licenses/>. 
    284  */ 
    285  
    286 /** 
    287  * Class: mapfish.Protocol.MergeFilterDecorator 
    288  */ 
    289 mapfish.Protocol.MergeFilterDecorator = OpenLayers.Class({ 
    290    
    291     /** 
    292      * Property: searchers 
    293      * Array({<mapfish.Searcher>} Array of searchers 
    294      */   
    295     searchers: null, 
    296  
    297     /** 
    298      * APIProperty: protocol 
    299      * {<OpenLayers.Protocol>} The protocol to use for sending search 
    300      *     requests. 
    301      */ 
    302     protocol: null, 
    303  
    304     /** 
    305      * Constructor: mapfish.Protocol.MergeFilterDecorator 
    306      * 
    307      * Parameters: 
    308      * protocol - {<OpenLayers.Protocol>} The protocol to decorate 
    309      * options - {Object} 
    310      */ 
    311     initialize: function(protocol, options) { 
    312         this.searchers = []; 
    313         this.protocol = protocol; 
    314         OpenLayers.Util.extend(this, options); 
    315         OpenLayers.Util.applyDefaults(this, this.protocol); 
    316     }, 
    317  
    318     /** 
    319      * APIMethod: read 
    320      * Construct a request for reading new features. 
    321      * 
    322      * Parameters: 
    323      * options - {Object} Optional object for configuring the request. 
    324      *     This object is modified and should not be reused. 
    325      * 
    326      * Returns: 
    327      * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response> 
    328      *      object, whose "priv" property references the HTTP request, this 
    329      *      object is also passed to the callback function when the request 
    330      *      completes, its "features" property is then populated with the 
    331      *      the features received from the server. 
    332      */ 
    333     read: function(options) { 
    334         options = options || {}; 
    335  
    336         var filter = this.toFilter(options.filter); 
    337  
    338         for (var i = 0, len = this.searchers.length; i < len; i++) { 
    339             filter = this.toFilter(this.searchers[i].getFilter(), filter); 
    340         } 
    341  
    342         options.filter = filter; 
    343         return this.protocol.read(options); 
    344     }, 
    345  
    346     /** 
    347      * Method: toFilter 
    348      * 
    349      * Parameters: 
    350      * obj - {Object} 
    351      * filter - {<OpenLayers.Filter>} 
    352      * 
    353      * Returns: 
    354      * {<OpenLayers.Filter.Logical>} 
    355      */ 
    356     toFilter: function(obj, filter) { 
    357         filter = filter || new OpenLayers.Filter.Logical( 
    358             {type: OpenLayers.Filter.Logical.AND}); 
    359  
    360         var filters = filter.filters; 
    361  
    362         if (this.isFilter(obj)) { 
    363             filters.push(obj); 
    364         } else { 
    365             for (var key in obj) { 
    366                 filters.push( 
    367                     new OpenLayers.Filter.Comparison({ 
    368                         type: OpenLayers.Filter.Comparison.EQUAL_TO, 
    369                         property: key, 
    370                         value: obj[key] 
    371                     }) 
    372                 ); 
    373             } 
    374         } 
    375         return filter; 
    376     }, 
    377  
    378    /** 
    379      * Method: isFilter 
    380      * Check if the object passed is a <OpenLayers.Filter> object. 
    381      * 
    382      * Parameters: 
    383      * obj - {Object} 
    384      */ 
    385     isFilter: function(obj) { 
    386         return !!obj && !!obj.CLASS_NAME && 
    387                !!obj.CLASS_NAME.match(/^OpenLayers\.Filter/); 
    388     }, 
    389  
    390     CLASS_NAME: "mapfish.SearchMediator" 
    391 }); 
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/core/SearchMediator.js

    r1124 r1190  
    1 /* 
    2  * Copyright (C) 2007-2008  Camptocamp 
    3  * 
    4  * This file is part of MapFish Client 
    5  * 
    6  * MapFish Client is free software: you can redistribute it and/or modify 
    7  * it under the terms of the GNU General Public License as published by 
    8  * the Free Software Foundation, either version 3 of the License, or 
    9  * (at your option) any later version. 
    10  * 
    11  * MapFish Client is distributed in the hope that it will be useful, 
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    14  * GNU General Public License for more details. 
    15  * 
    16  * You should have received a copy of the GNU General Public License 
    17  * along with MapFish Client.  If not, see <http://www.gnu.org/licenses/>. 
    18  */ 
    19  
    20 /* 
    21  * @requires OpenLayers/Util.js 
    22  * @requires OpenLayers/Ajax.js 
    23  * @requires OpenLayers/Filter/Logical.js 
    24  */ 
    25  
    26 /** 
    27  * Class: mapfish.SearchMediator 
    28  * A SearchMediator object is to be used when multiple searchers must be coupled 
    29  * (e.g. coupling a search form with a BBOX searcher). 
    30  */ 
    31 mapfish.SearchMediator = OpenLayers.Class({ 
    32  
    33     /** 
    34      * Property: searchers 
    35      * Array({<mapfish.Searcher>} Array of searchers controlled by this 
    36      *      mediator. 
    37      */ 
    38     searchers: null, 
    39  
    40     /** 
    41      * APIProperty: protocol 
    42      * {<OpenLayers.Protocol>} The protocol to use for sending search 
    43      *     requests. 
    44      */ 
    45     protocol: null, 
    46  
    47     /** 
    48      * Property: response 
    49      * {<OpenLayers.Protocol.Response>} The response got from the protocol. 
    50      */ 
    51     response: null, 
    52  
    53     /** 
    54      * Property: events 
    55      * {<OpenLayers.Events>} 
    56      */ 
    57     events: null, 
    58  
    59     /** 
    60      * Constant: EVENT_TYPES 
    61      * {Array(String)} Supported event types. Register a listener 
    62      *     for a particular event with the following syntax: 
    63      * (start code) 
    64      * mediator.events.register(type, obj, listener); 
    65      * (end) 
    66      * 
    67      * Listeners will be called with a reference to an event object. 
    68      * 
    69      * Supported events: 
    70      * - *searchtriggered* Triggered when a search request was triggered, 
    71      *     listeners will receive an object with a *filter property 
    72      *     referencing the <OpenLayers.Filter> object associated to the 
    73      *     search request. 
    74      * - *searchfinished* Triggered when the search request completes, 
    75      *     listeners will receive an {<OpenLayers.Protocol.Response>} 
    76      *     object. 
    77      * - *clear* Triggered when the clear API method is called. 
    78      */ 
    79     EVENT_TYPES: ["searchtriggered", "searchfinished", "searchcanceled", "clear"], 
    80  
    81     /** 
    82      * Constructor: mapfish.SearchMediator 
    83      * 
    84      * Parameters: 
    85      * options - {Object} 
    86      */ 
    87     initialize: function(options) { 
    88         OpenLayers.Util.extend(this, options); 
    89         this.searchers = []; 
    90         this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES); 
    91         if(this.eventListeners instanceof Object) { 
    92             this.events.on(this.eventListeners); 
    93         } 
    94     }, 
    95  
    96     /** 
    97      * Method: destroy 
    98      */ 
    99     destroy: function () { 
    100         if (this.protocol.autoDestroy) { 
    101             this.protocol.destroy(); 
    102         } 
    103         this.protocol = null; 
    104  
    105         this.searchers = null; 
    106  
    107         if (this.events) { 
    108             this.events.destroy(); 
    109             this.events = null; 
    110         } 
    111     }, 
    112  
    113     /** 
    114      * Method: triggerSearch 
    115      *      Trigger search request to the search service. 
    116      */ 
    117     triggerSearch: function() { 
    118         var filter = null, f = null, params = null; 
    119         for (var i = 0, len = this.searchers.length; i < len; i++) { 
    120             if (f = this.searchers[i].getFilter()) 
    121                 filter = this.toFilter(f, filter); 
    122         } 
    123  
    124         this.events.triggerEvent("searchtriggered", {filter: filter}); 
    125  
    126         // workaround a bug in OpenLayers 
    127         params = OpenLayers.Util.extend( 
    128             {}, this.protocol.options.params); 
    129  
    130         this.response = this.protocol.read({ 
    131             filter: filter, 
    132             callback: this.finishSearch, 
    133             params: params, 
    134             scope: this 
    135         }); 
    136     }, 
    137  
    138     /** 
    139      * Method: finishSearch 
    140      *      Called with we get a response from the search service. 
    141      * 
    142      * Parameters: 
    143      * {<OpenLayers.Protocol.Response>} The protocol response. 
    144      */ 
    145     finishSearch: function(response) { 
    146         this.response = null; 
    147         this.events.triggerEvent("searchfinished", response); 
    148     }, 
    149  
    150     /** 
    151      * Method: cancelSearch 
    152      *     Cancel ongoing search. 
    153      */ 
    154     cancelSearch: function() { 
    155         if (this.response) { 
    156             var response = this.response; 
    157             if (response.priv && 
    158                 typeof response.priv.abort == "function") { 
    159                 response.priv.abort(); 
    160                 this.response = null; 
    161                 this.events.triggerEvent("searchcanceled", response); 
    162             } 
    163         } 
    164     }, 
    165  
    166     /** 
    167      * Method: toFilter 
    168      * 
    169      * Parameters: 
    170      * obj - {Object} 
    171      * filter - {<OpenLayers.Filter>} 
    172      * 
    173      * Returns: 
    174      * {<OpenLayers.Filter.Logical>} 
    175      */ 
    176     toFilter: function(obj, filter) { 
    177         filter = filter || new OpenLayers.Filter.Logical( 
    178             {type: OpenLayers.Filter.Logical.AND}); 
    179  
    180         var filters = filter.filters; 
    181  
    182         if (this.isFilter(obj)) { 
    183             filters.push(obj); 
    184         } else { 
    185             for (var key in obj) { 
    186                 filters.push( 
    187                     new OpenLayers.Filter.Comparison({ 
    188                         type: OpenLayers.Filter.Comparison.EQUAL_TO, 
    189                         property: key, 
    190                         value: obj[key] 
    191                     }) 
    192                 ); 
    193             } 
    194         } 
    195         return filter; 
    196     }, 
    197  
    198     /** 
    199      * Method: isFilter 
    200      * Check if the object passed is a <OpenLayers.Filter> object. 
    201      * 
    202      * Parameters: 
    203      * obj - {Object} 
    204      */ 
    205     isFilter: function(obj) { 
    206         return !!obj.CLASS_NAME && 
    207                !!obj.CLASS_NAME.match(/^OpenLayers\.Filter/); 
    208     }, 
    209  
    210     /** 
    211      * APIMethod: clear 
    212      *      Clear all the previous results. 
    213      */ 
    214     clear: function() { 
    215         this.events.triggerEvent("clear"); 
    216     }, 
    217  
    218     CLASS_NAME: "mapfish.SearchMediator" 
    219 }); 
    220  
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/core/Searcher.js

    r1124 r1190  
    1818 */ 
    1919 
    20 /* 
    21  * @requires core/SearchMediator.js 
    22  */ 
    23  
    2420/** 
    2521 * Class: mapfish.Searcher 
     
    3026         
    3127    /** 
    32      * Property: mediator 
    33      * {<mapfish.SearchMediator>} - The search mediator. 
    34      */ 
    35     mediator: null, 
    36  
    37     /** 
    3828     * Constructor: mapfish.Searcher 
    39      * 
    40      * Parameters: 
    41      * options {Object} Optional object whose properties will be set on the 
    42      *     instance. 
    4329     * 
    4430     * Returns: 
    4531     * {<mapfish.Searcher>} 
    4632     */ 
    47     initialize: function() { 
    48         if (!this.mediator) { 
    49             OpenLayers.Console.error("no mediator set"); 
    50             return; 
    51         } 
    52         this.mediator.searchers.push(this); 
    53     }, 
     33    initialize: function() {}, 
    5434 
    5535    /** 
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/core/Searcher/Form.js

    r1124 r1190  
    3636 
    3737    /** 
     38     * APIProperty: protocol 
     39     * {<OpenLayers.Protocol>} - The protocol. 
     40     */ 
     41    protocol: null, 
     42 
     43    /** 
    3844     * APIProperty: map 
    3945     * {DOMElement} The form node. 
    4046     */ 
    4147    form: null, 
     48 
     49    /** 
     50     * Property: response 
     51     * {<OpenLayers.Protocol.Response>} The response returned by the 
     52     *     read call to <OpenLayers.Protocol> object. 
     53     */ 
     54    response: null, 
    4255 
    4356    /** 
     
    5265     */ 
    5366    initialize: function(options) { 
     67        mapfish.Searcher.prototype.initialize.call(this, options); 
    5468        OpenLayers.Util.extend(this, options); 
    55         mapfish.Searcher.prototype.initialize.call(this, options); 
    5669        if (!this.form) { 
    5770            OpenLayers.Console.error("no form set"); 
     71            return; 
     72        } 
     73        if (!this.protocol) { 
     74            OpenLayers.Console.error("no protocol set"); 
    5875            return; 
    5976        } 
     
    6279    /** 
    6380     * APIMethod: triggerSearch 
    64      *      To be called to instruct the search mediator to trigger 
    65      *      search. 
     81     *      To be called to trigger search. 
    6682     */ 
    6783    triggerSearch: function() { 
    68         this.mediator.cancelSearch(); 
    69         this.mediator.triggerSearch(); 
     84        // FIXME really we should rely on the protocol itself to 
     85        // cancel the request, the Protocol class in OpenLayers 
     86        // 2.7 does not expose a cancel() method 
     87        if (this.response) { 
     88            var response = this.response; 
     89            if (response.priv && 
     90                typeof response.priv.abort == "function") { 
     91                response.priv.abort(); 
     92                this.response = null; 
     93            } 
     94        } 
     95        this.response = this.protocol.read( 
     96            {filter: this.getFilter(), searcher: this}); 
    7097    }, 
    7198 
  • sandbox/camptocamp/MapFishUnhcr/client/mfbase/mapfish/core/Searcher/Map.js

    r1124 r1190  
    2121/* 
    2222 * @requires core/Searcher.js 
    23  * @requires core/SearchMediator.js 
     23 * @requires core/Protocol.js 
    2424 * @requires OpenLayers/Map.js 
    2525 * @requires OpenLayers/Util.js 
     
    4242 */ 
    4343mapfish.Searcher.Map = OpenLayers.Class(mapfish.Searcher, OpenLayers.Control, { 
     44 
     45    /** 
     46     * APIProperty: protocol 
     47     * {<OpenLayers.Protocol>} - The protocol. 
     48     */ 
     49    protocol: null, 
    4450 
    4551    /** 
     
    110116     * {Boolean} Display a default popup with the search results, 
    111117     *      does not apply if mode is set to <mapfish.Searcher.Map.HOVER>, 
    112      *      defaults to false. 
     118     *      defaults to false, or if the protocol used isn't of type 
     119     *      <mapfish.Protocol.TriggerEventDecorator>. 
    113120     */ 
    114121    displayDefaultPopup: false, 
     
    132139 
    133140    /** 
     141     * Property: popupLonLat 
     142     * {<OpenLayers.LonLat>} The <OpenLayers.LonLat> object representing 
     143     *      where the popup must be displayed. 
     144     */ 
     145    popupLonLat: null, 
     146 
     147    /** 
     148     * Property: response 
     149     * {<OpenLayers.Protocol.Response>} The response returned by the 
     150     *     read call to <OpenLayers.Protocol> object. 
     151     */ 
     152    response: null, 
     153 
     154    /** 
    134155     * Constructor: mapfish.Searcher.Map 
    135156     * 
     
    144165        this.mode = mapfish.Searcher.Map.CLICK; 
    145166 
     167        mapfish.Searcher.prototype.initialize.call(this, options); 
    146168        OpenLayers.Control.prototype.initialize.call(this, options); 
    147         mapfish.Searcher.prototype.initialize.call(this, options); 
     169 
     170        if (!this.protocol) { 
     171            OpenLayers.Console.error("no protocol set"); 
     172            return; 
     173        } 
    148174 
    149175        switch(this.mode) { 
     
    191217                this.map.events.register( 
    192218                    "moveend", this, this.handleMoveend); 
    193             } else if (this.displayDefaultPopup) { 
    194                 this.mediator.events.on({ 
    195                     searchfinished: this.displayPopup, 
     219            } else if (this.displayDefaultPopup && 
     220                       this.protocol.CLASS_NAME == 
     221                           "mapfish.Protocol.TriggerEventDecorator") { 
     222                this.protocol.events.on({ 
     223                    crudfinished: this.displayPopup, 
    196224                    scope: this 
    197225                }); 
     
    215243                this.map.events.unregister( 
    216244                    "moveend", this, this.handleMoveend); 
    217             } else if (this.displayDefaultPopup) { 
    218                 this.mediator.events.un({ 
    219                     searchfinished: this.displayPopup, 
     245            } else if (this.displayDefaultPopup && 
     246                       this.protocol.CLASS_NAME == 
     247                           "mapfish.Protocol.TriggerEventDecorator") { 
     248                this.protocol.events.un({ 
     249                    crudfinished: this.displayPopup, 
    220250                    scope: this 
    221251                }); 
     
    234264    handlePoint: function(evt) { 
    235265        this.position = evt.xy; 
     266        this.popupLonLat = this.map.getLonLatFromViewPortPx(this.position); 
    236267        this.triggerSearch(); 
    237268    }, 
     
    252283                minXY.lon, minXY.lat, 
    253284                maxXY.lon, maxXY.lat); 
     285            this.popupLonLat = this.position.getCenterLonLat(); 
    254286        } else { 
    255287            this.position = position; 
     288            this.popupLonLat = this.map.getLonLatFromViewPortPx(this.position); 
    256289        } 
    257290        this.triggerSearch(); 
     
    264297    handleMoveend: function() { 
    265298        this.position = this.map.getExtent(); 
     299        this.popupLonLat = this.position.getCenterLonLat(); 
    266300        this.triggerSearch(); 
    267301    }, 
     
    272306    triggerSearch: function() { 
    273307        this.cancelSearch(); 
    274         this.mediator.triggerSearch(); 
     308 
     309        var filter = this.getFilter(); 
     310        filter = this.isFilter(filter) ? {filter: filter} : {params: filter}; 
     311        var options = OpenLayers.Util.extend({searcher: this}, filter); 
     312             
     313        this.response = this.protocol.read(options); 
    275314    }, 
    276315 
     
    284323     */ 
    285324    cancelSearch: function(evt) { 
    286         this.mediator.cancelSearch(); 
     325        // FIXME really we should rely on the protocol itself to 
     326        // cancel the request, the Protocol class in OpenLayers 
     327        // 2.7 does not expose a cancel() method 
     328        if (this.response) { 
     329            var response = this.response; 
     330            if (response.priv && 
     331                typeof response.priv.abort == "function") { 
     332                response.priv.abort(); 
     333                this.response = null; 
     334            } 
     335        } 
    287336        if (this.mode == mapfish.Searcher.Map.HOVER) { 
    288337            this.onMouseMove(); 
     
    300349 
    301350        if (features && features.length > 0) { 
    302             var lonlat; 
    303             if (this.position instanceof OpenLayers.Bounds) { 
    304                 lonlat = this.position.getCenterLonLat(); 
    305             } else { 
    306                 lonlat = this.map.getLonLatFromViewPortPx(this.position); 
    307             } 
    308  
    309351            var k; 
    310352 
     
    327369            var popup = new OpenLayers.Popup.FramedCloud( 
    328370                "mapfish_popup",    // popup id 
    329                 lonlat,             // OpenLayers.LonLat object 
     371                this.popupLonLat,   // OpenLayers.LonLat object 
    330372                null,               // popup is autosized 
    331373                html,               // html string 
     
    338380 
    339381    /** 
     382     * Method: isFilter 
     383     * Check if the object passed is a <OpenLayers.Filter> object. 
     384     * 
     385     * Parameters: 
     386     * obj - {Object} 
     387     * 
     388     * Returns: 
     389     * {Boolean} 
     390     */ 
     391    isFilter: function(obj) { 
     392        return !!obj.CLASS_NAME && 
     393               !!obj.CLASS_NAME.match(/^OpenLayers\.Filter/); 
     394    }, 
     395 
     396    /** 
    340397     * Method: getFilter 
    341398     *      Get the search filter. 
     
    345402     */ 
    346403    getFilter: function() { 
    347         var filter; 
    348         mapfish.Searcher.prototype.getFilter.call(this); 
    349         if (this.position instanceof OpenLayers.Bounds) { 
    350             filter = new OpenLayers.Filter.Spatial({ 
    351                 type: OpenLayers.Filter.Spatial.BBOX, 
    352                 value: this.position 
    353             }); 
    354         } else { 
    355             var tolerance = this.searchTolerance; 
    356             if (tolerance && this.searchToleranceUnits == "pixels") { 
    357                 tolerance *= this.map.getResolution(); 
    358             } 
    359             var lonlat = this.map.getLonLatFromViewPortPx(this.position); 
    360             filter = {lon: lonlat.lon, lat: lonlat.lat}; 
    361