Project

General

Profile

Download (6.81 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5

    
6
/**
7
 * @requires OpenLayers/Console.js
8
 * @requires OpenLayers/Format.js
9
 * @requires OpenLayers/Filter/Spatial.js
10
 * @requires OpenLayers/Filter/Comparison.js
11
 * @requires OpenLayers/Filter/Logical.js
12
 */
13

    
14
/**
15
 * Class: OpenLayers.Format.QueryStringFilter
16
 * Parser for reading a query string and creating a simple filter.
17
 *
18
 * Inherits from:
19
 *  - <OpenLayers.Format>
20
 */
21
OpenLayers.Format.QueryStringFilter = (function() {
22

    
23
    /** 
24
     * Map the OpenLayers.Filter.Comparison types to the operation strings of 
25
     * the protocol.
26
     */
27
    var cmpToStr = {};
28
    cmpToStr[OpenLayers.Filter.Comparison.EQUAL_TO] = "eq";
29
    cmpToStr[OpenLayers.Filter.Comparison.NOT_EQUAL_TO] = "ne";
30
    cmpToStr[OpenLayers.Filter.Comparison.LESS_THAN] = "lt";
31
    cmpToStr[OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO] = "lte";
32
    cmpToStr[OpenLayers.Filter.Comparison.GREATER_THAN] = "gt";
33
    cmpToStr[OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO] = "gte";
34
    cmpToStr[OpenLayers.Filter.Comparison.LIKE] = "ilike";
35

    
36
    /**
37
     * Function: regex2value
38
     * Convert the value from a regular expression string to a LIKE/ILIKE
39
     * string known to the web service.
40
     *
41
     * Parameters:
42
     * value - {String} The regex string.
43
     *
44
     * Returns:
45
     * {String} The converted string.
46
     */
47
    function regex2value(value) {
48

    
49
        // highly sensitive!! Do not change this without running the
50
        // Protocol/HTTP.html unit tests
51

    
52
        // convert % to \%
53
        value = value.replace(/%/g, "\\%");
54

    
55
        // convert \\. to \\_ (\\.* occurences converted later)
56
        value = value.replace(/\\\\\.(\*)?/g, function($0, $1) {
57
            return $1 ? $0 : "\\\\_";
58
        });
59

    
60
        // convert \\.* to \\%
61
        value = value.replace(/\\\\\.\*/g, "\\\\%");
62

    
63
        // convert . to _ (\. and .* occurences converted later)
64
        value = value.replace(/(\\)?\.(\*)?/g, function($0, $1, $2) {
65
            return $1 || $2 ? $0 : "_";
66
        });
67

    
68
        // convert .* to % (\.* occurnces converted later)
69
        value = value.replace(/(\\)?\.\*/g, function($0, $1) {
70
            return $1 ? $0 : "%";
71
        });
72

    
73
        // convert \. to .
74
        value = value.replace(/\\\./g, ".");
75

    
76
        // replace \* with * (watching out for \\*)
77
        value = value.replace(/(\\)?\\\*/g, function($0, $1) {
78
            return $1 ? $0 : "*";
79
        });
80

    
81
        return value;
82
    }
83
    
84
    return OpenLayers.Class(OpenLayers.Format, {
85
        
86
        /**
87
         * Property: wildcarded.
88
         * {Boolean} If true percent signs are added around values
89
         *     read from LIKE filters, for example if the protocol
90
         *     read method is passed a LIKE filter whose property
91
         *     is "foo" and whose value is "bar" the string
92
         *     "foo__ilike=%bar%" will be sent in the query string;
93
         *     defaults to false.
94
         */
95
        wildcarded: false,
96

    
97
        /**
98
         * APIProperty: srsInBBOX
99
         * {Boolean} Include the SRS identifier in BBOX query string parameter.  
100
         *     Default is false.  If true and the layer has a projection object set,
101
         *     any BBOX filter will be serialized with a fifth item identifying the
102
         *     projection.  E.g. bbox=-1000,-1000,1000,1000,EPSG:900913
103
         */
104
        srsInBBOX: false,
105

    
106
        /**
107
         * APIMethod: write
108
         * Serialize an <OpenLayers.Filter> objects using the "simple" filter syntax for 
109
         *     query string parameters.  This function must be called as a method of
110
         *     a protocol instance.
111
         *
112
         * Parameters:
113
         * filter - {<OpenLayers.Filter>} filter to convert.
114
         * params - {Object} The parameters object.
115
         *
116
         * Returns:
117
         * {Object} The resulting parameters object.
118
         */
119
        write: function(filter, params) {
120
            params = params || {};
121
            var className = filter.CLASS_NAME;
122
            var filterType = className.substring(className.lastIndexOf(".") + 1);
123
            switch (filterType) {
124
                case "Spatial":
125
                    switch (filter.type) {
126
                        case OpenLayers.Filter.Spatial.BBOX:
127
                            params.bbox = filter.value.toArray();
128
                            if (this.srsInBBOX && filter.projection) {
129
                                params.bbox.push(filter.projection.getCode());
130
                            }
131
                            break;
132
                        case OpenLayers.Filter.Spatial.DWITHIN:
133
                            params.tolerance = filter.distance;
134
                            // no break here
135
                        case OpenLayers.Filter.Spatial.WITHIN:
136
                            params.lon = filter.value.x;
137
                            params.lat = filter.value.y;
138
                            break;
139
                        default:
140
                            OpenLayers.Console.warn(
141
                                "Unknown spatial filter type " + filter.type);
142
                    }
143
                    break;
144
                case "Comparison":
145
                    var op = cmpToStr[filter.type];
146
                    if (op !== undefined) {
147
                        var value = filter.value;
148
                        if (filter.type == OpenLayers.Filter.Comparison.LIKE) {
149
                            value = regex2value(value);
150
                            if (this.wildcarded) {
151
                                value = "%" + value + "%";
152
                            }
153
                        }
154
                        params[filter.property + "__" + op] = value;
155
                        params.queryable = params.queryable || [];
156
                        params.queryable.push(filter.property);
157
                    } else {
158
                        OpenLayers.Console.warn(
159
                            "Unknown comparison filter type " + filter.type);
160
                    }
161
                    break;
162
                case "Logical":
163
                    if (filter.type === OpenLayers.Filter.Logical.AND) {
164
                        for (var i=0,len=filter.filters.length; i<len; i++) {
165
                            params = this.write(filter.filters[i], params);
166
                        }
167
                    } else {
168
                        OpenLayers.Console.warn(
169
                            "Unsupported logical filter type " + filter.type);
170
                    }
171
                    break;
172
                default:
173
                    OpenLayers.Console.warn("Unknown filter type " + filterType);
174
            }
175
            return params;
176
        },
177
        
178
        CLASS_NAME: "OpenLayers.Format.QueryStringFilter"
179
        
180
    });
181

    
182

    
183
})();
(19-19/41)