1 | /** |
---|
2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
3 | * |
---|
4 | * Published under the BSD license. |
---|
5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
6 | * of the license. |
---|
7 | */ |
---|
8 | |
---|
9 | Ext.namespace("GeoExt.form"); |
---|
10 | |
---|
11 | /** private: function[toFilter] |
---|
12 | * :param form: ``Ext.form.BasicForm|Ext.form.FormPanel`` |
---|
13 | * :param logicalOp: ``String`` Either ``OpenLayers.Filter.Logical.AND`` or |
---|
14 | * ``OpenLayers.Filter.Logical.OR``, set to |
---|
15 | * ``OpenLayers.Filter.Logical.AND`` if null or undefined |
---|
16 | * :param wildcard: ``Integer`` Determines the wildcard behaviour of like |
---|
17 | * queries. This behaviour can either be: none, prepend, append or both. |
---|
18 | * |
---|
19 | * :return: ``OpenLayers.Filter`` |
---|
20 | * |
---|
21 | * Create an {OpenLayers.Filter} object from a {Ext.form.BasicForm} |
---|
22 | * or a {Ext.form.FormPanel} instance. |
---|
23 | */ |
---|
24 | GeoExt.form.toFilter = function(form, logicalOp, wildcard) { |
---|
25 | if(form instanceof Ext.form.FormPanel) { |
---|
26 | form = form.getForm(); |
---|
27 | } |
---|
28 | var filters = [], values = form.getValues(false); |
---|
29 | for(var prop in values) { |
---|
30 | var s = prop.split("__"); |
---|
31 | |
---|
32 | var value = values[prop], type; |
---|
33 | |
---|
34 | if(s.length > 1 && |
---|
35 | (type = GeoExt.form.toFilter.FILTER_MAP[s[1]]) !== undefined) { |
---|
36 | prop = s[0]; |
---|
37 | } else { |
---|
38 | type = OpenLayers.Filter.Comparison.EQUAL_TO; |
---|
39 | } |
---|
40 | |
---|
41 | if (type === OpenLayers.Filter.Comparison.LIKE) { |
---|
42 | switch(wildcard) { |
---|
43 | case GeoExt.form.ENDS_WITH: |
---|
44 | value = '.*' + value; |
---|
45 | break; |
---|
46 | case GeoExt.form.STARTS_WITH: |
---|
47 | value += '.*'; |
---|
48 | break; |
---|
49 | case GeoExt.form.CONTAINS: |
---|
50 | value = '.*' + value + '.*'; |
---|
51 | break; |
---|
52 | default: |
---|
53 | // do nothing, just take the value |
---|
54 | break; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | filters.push( |
---|
59 | new OpenLayers.Filter.Comparison({ |
---|
60 | type: type, |
---|
61 | value: value, |
---|
62 | property: prop |
---|
63 | }) |
---|
64 | ); |
---|
65 | } |
---|
66 | |
---|
67 | return new OpenLayers.Filter.Logical({ |
---|
68 | type: logicalOp || OpenLayers.Filter.Logical.AND, |
---|
69 | filters: filters |
---|
70 | }); |
---|
71 | }; |
---|
72 | |
---|
73 | /** private: constant[FILTER_MAP] |
---|
74 | * An object mapping operator strings as found in field names to |
---|
75 | * ``OpenLayers.Filter.Comparison`` types. |
---|
76 | */ |
---|
77 | GeoExt.form.toFilter.FILTER_MAP = { |
---|
78 | "eq": OpenLayers.Filter.Comparison.EQUAL_TO, |
---|
79 | "ne": OpenLayers.Filter.Comparison.NOT_EQUAL_TO, |
---|
80 | "lt": OpenLayers.Filter.Comparison.LESS_THAN, |
---|
81 | "le": OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO, |
---|
82 | "gt": OpenLayers.Filter.Comparison.GREATER_THAN, |
---|
83 | "ge": OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO, |
---|
84 | "like": OpenLayers.Filter.Comparison.LIKE |
---|
85 | }; |
---|
86 | |
---|
87 | GeoExt.form.ENDS_WITH = 1; |
---|
88 | GeoExt.form.STARTS_WITH = 2; |
---|
89 | GeoExt.form.CONTAINS = 3; |
---|
90 | |
---|
91 | /** private: function[recordToField] |
---|
92 | * :param record: ``Ext.data.Record``, typically from an attributeStore |
---|
93 | * |
---|
94 | * :return: ``Object`` An object literal with a xtype property, use |
---|
95 | * ``Ext.ComponentMgr.create`` (or ``Ext.create`` in Ext 3) to create |
---|
96 | * an ``Ext.form.Field`` from this object. |
---|
97 | * |
---|
98 | * This function can be used to create an ``Ext.form.Field`` from |
---|
99 | * an ``Ext.data.Record`` containing name, type, restriction and |
---|
100 | * label fields. |
---|
101 | */ |
---|
102 | GeoExt.form.recordToField = function(record) { |
---|
103 | var type = record.get("type"); |
---|
104 | |
---|
105 | if(typeof type === "object" && type.xtype) { |
---|
106 | // we have an xtype'd object literal in the type |
---|
107 | // field, just return it |
---|
108 | return type; |
---|
109 | } |
---|
110 | |
---|
111 | var field; |
---|
112 | |
---|
113 | var name = record.get("name"); |
---|
114 | var label = record.get("label"); |
---|
115 | var restriction = record.get("restriction") || {}; |
---|
116 | |
---|
117 | // use name for label if label isn't defined in the record |
---|
118 | if(label == null) { |
---|
119 | label = name; |
---|
120 | } |
---|
121 | |
---|
122 | type = type.split(":").pop(); // remove ns prefix |
---|
123 | |
---|
124 | var r = GeoExt.form.recordToField.REGEXES; |
---|
125 | |
---|
126 | if(type.match(r["text"])) { |
---|
127 | var maxLength = restriction["maxLength"] !== undefined ? |
---|
128 | parseFloat(restriction["maxLength"]) : undefined; |
---|
129 | var minLength = restriction["minLength"] !== undefined ? |
---|
130 | parseFloat(restriction["minLength"]) : undefined; |
---|
131 | field = { |
---|
132 | xtype: "textfield", |
---|
133 | name: name, |
---|
134 | fieldLabel: label, |
---|
135 | maxLength: maxLength, |
---|
136 | minLength: minLength |
---|
137 | }; |
---|
138 | } else if(type.match(r["number"])) { |
---|
139 | var maxValue = restriction["maxInclusive"] !== undefined ? |
---|
140 | parseFloat(restriction["maxInclusive"]) : undefined; |
---|
141 | var minValue = restriction["minInclusive"] !== undefined ? |
---|
142 | parseFloat(restriction["minInclusive"]) : undefined; |
---|
143 | field = { |
---|
144 | xtype: "numberfield", |
---|
145 | name: name, |
---|
146 | fieldLabel: label, |
---|
147 | maxValue: maxValue, |
---|
148 | minValue: minValue |
---|
149 | }; |
---|
150 | } else if(type.match(r["boolean"])) { |
---|
151 | field = { |
---|
152 | xtype: "checkbox", |
---|
153 | name: name, |
---|
154 | boxLabel: label |
---|
155 | }; |
---|
156 | } else if(type.match(r["date"])) { |
---|
157 | field = { |
---|
158 | xtype: "datefield", |
---|
159 | fieldLabel: label, |
---|
160 | name: name |
---|
161 | }; |
---|
162 | } |
---|
163 | |
---|
164 | return field; |
---|
165 | } |
---|
166 | |
---|
167 | /** private: constant[REGEXES] |
---|
168 | * ``Object`` Regular expressions for determining what type |
---|
169 | * of field to create from an attribute record. |
---|
170 | */ |
---|
171 | GeoExt.form.recordToField.REGEXES = { |
---|
172 | "text": new RegExp( |
---|
173 | "^(text|string)$", "i" |
---|
174 | ), |
---|
175 | "number": new RegExp( |
---|
176 | "^(number|float|decimal|double|int|long|integer|short)$", "i" |
---|
177 | ), |
---|
178 | "boolean": new RegExp( |
---|
179 | "^(boolean)$", "i" |
---|
180 | ), |
---|
181 | "date": new RegExp( |
---|
182 | "^(dateTime)$", "i" |
---|
183 | ) |
---|
184 | }; |
---|