[Commits] r2172 - in core/trunk/geoext: lib/GeoExt/plugins lib/GeoExt/widgets tests/lib/GeoExt/plugins tests/lib/GeoExt/widgets
commits at geoext.org
commits at geoext.org
Tue May 11 14:31:25 CEST 2010
Author: elemoine
Date: 2010-05-11 14:31:25 +0200 (Tue, 11 May 2010)
New Revision: 2172
Modified:
core/trunk/geoext/lib/GeoExt/plugins/AttributeForm.js
core/trunk/geoext/lib/GeoExt/widgets/form.js
core/trunk/geoext/tests/lib/GeoExt/plugins/AttributeForm.html
core/trunk/geoext/tests/lib/GeoExt/widgets/form.html
Log:
add a GeoExt.form.recordToField function that creates a form field from an attribute record, r=pgiraud (closes #261)
Modified: core/trunk/geoext/lib/GeoExt/plugins/AttributeForm.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/plugins/AttributeForm.js 2010-05-11 10:51:28 UTC (rev 2171)
+++ core/trunk/geoext/lib/GeoExt/plugins/AttributeForm.js 2010-05-11 12:31:25 UTC (rev 2172)
@@ -72,25 +72,6 @@
*/
formPanel: null,
- /** private: property[regExes]
- * ``Object`` Regular expressions for determining what type
- * of field to create from an attribute record.
- */
- regExes: {
- "text": new RegExp(
- "^(string)$", "i"
- ),
- "number": new RegExp(
- "^(float|decimal|double|int|long|integer|short)$", "i"
- ),
- "boolean": new RegExp(
- "^(boolean)$", "i"
- ),
- "date": new RegExp(
- "^(dateTime)$", "i"
- )
- },
-
/** private: method[init]
* :param formPanel: class:`Ext.form.FormPanel`
*
@@ -149,45 +130,9 @@
*/
fillForm: function() {
this.attributeStore.each(function(record) {
- var name = record.get("name");
- var type = record.get("type").split(":").pop(); // remove ns prefix
- var restriction = record.get("restriction") || {};
- if(type.match(this.regExes["text"])) {
- var maxLength = restriction["maxLength"] !== undefined ?
- parseFloat(restriction["maxLength"]) : undefined;
- var minLength = restriction["minLength"] !== undefined ?
- parseFloat(restriction["minLength"]) : undefined;
- this.formPanel.add({
- xtype: "textfield",
- name: name,
- fieldLabel: name,
- maxLength: maxLength,
- minLength: minLength
- });
- } else if(type.match(this.regExes["number"])) {
- var maxValue = restriction["maxInclusive"] !== undefined ?
- parseFloat(restriction["maxInclusive"]) : undefined;
- var minValue = restriction["minInclusive"] !== undefined ?
- parseFloat(restriction["minInclusive"]) : undefined;
- this.formPanel.add({
- xtype: "numberfield",
- name: name,
- fieldLabel: name,
- maxValue: maxValue,
- minValue: minValue
- });
- } else if(type.match(this.regExes["boolean"])) {
- this.formPanel.add({
- xtype: "checkbox",
- name: name,
- boxLabel: name
- });
- } else if(type.match(this.regExes["date"])) {
- this.formPanel.add({
- xtype: "datefield",
- fieldLabel: name,
- name: name
- });
+ var field = GeoExt.form.recordToField(record);
+ if(field) {
+ this.formPanel.add(field);
}
}, this);
this.formPanel.doLayout();
Modified: core/trunk/geoext/lib/GeoExt/widgets/form.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/form.js 2010-05-11 10:51:28 UTC (rev 2171)
+++ core/trunk/geoext/lib/GeoExt/widgets/form.js 2010-05-11 12:31:25 UTC (rev 2172)
@@ -15,9 +15,9 @@
* ``OpenLayers.Filter.Logical.AND`` if null or undefined
* :param wildcard: ``Integer`` Determines the wildcard behaviour of like
* queries. This behaviour can either be: none, prepend, append or both.
- *
+ *
* :return: ``OpenLayers.Filter``
- *
+ *
* Create an {OpenLayers.Filter} object from a {Ext.form.BasicForm}
* or a {Ext.form.FormPanel} instance.
*/
@@ -87,3 +87,98 @@
GeoExt.form.ENDS_WITH = 1;
GeoExt.form.STARTS_WITH = 2;
GeoExt.form.CONTAINS = 3;
+
+/** private: function[recordToField]
+ * :param record: ``Ext.data.Record``, typically from an attributeStore
+ *
+ * :return: ``Object`` An object literal with a xtype property, use
+ * ``Ext.ComponentMgr.create`` (or ``Ext.create`` in Ext 3) to create
+ * an ``Ext.form.Field`` from this object.
+ *
+ * This function can be used to create an ``Ext.form.Field`` from
+ * an ``Ext.data.Record`` containing name, type, restriction and
+ * label fields.
+ */
+GeoExt.form.recordToField = function(record) {
+ var type = record.get("type");
+
+ if(typeof type === "object" && type.xtype) {
+ // we have an xtype'd object literal in the type
+ // field, just return it
+ return type;
+ }
+
+ var field;
+
+ var name = record.get("name");
+ var label = record.get("label");
+ var restriction = record.get("restriction") || {};
+
+ // use name for label if label isn't defined in the record
+ if(label == null) {
+ label = name;
+ }
+
+ type = type.split(":").pop(); // remove ns prefix
+
+ var r = GeoExt.form.recordToField.REGEXES;
+
+ if(type.match(r["text"])) {
+ var maxLength = restriction["maxLength"] !== undefined ?
+ parseFloat(restriction["maxLength"]) : undefined;
+ var minLength = restriction["minLength"] !== undefined ?
+ parseFloat(restriction["minLength"]) : undefined;
+ field = {
+ xtype: "textfield",
+ name: name,
+ fieldLabel: label,
+ maxLength: maxLength,
+ minLength: minLength
+ };
+ } else if(type.match(r["number"])) {
+ var maxValue = restriction["maxInclusive"] !== undefined ?
+ parseFloat(restriction["maxInclusive"]) : undefined;
+ var minValue = restriction["minInclusive"] !== undefined ?
+ parseFloat(restriction["minInclusive"]) : undefined;
+ field = {
+ xtype: "numberfield",
+ name: name,
+ fieldLabel: label,
+ maxValue: maxValue,
+ minValue: minValue
+ };
+ } else if(type.match(r["boolean"])) {
+ field = {
+ xtype: "checkbox",
+ name: name,
+ boxLabel: label
+ };
+ } else if(type.match(r["date"])) {
+ field = {
+ xtype: "datefield",
+ fieldLabel: label,
+ name: name
+ };
+ }
+
+ return field;
+}
+
+/** private: constant[REGEXES]
+ * ``Object`` Regular expressions for determining what type
+ * of field to create from an attribute record.
+ */
+GeoExt.form.recordToField.REGEXES = {
+ "text": new RegExp(
+ "^(text|string)$", "i"
+ ),
+ "number": new RegExp(
+ "^(number|float|decimal|double|int|long|integer|short)$", "i"
+ ),
+ "boolean": new RegExp(
+ "^(boolean)$", "i"
+ ),
+ "date": new RegExp(
+ "^(dateTime)$", "i"
+ )
+};
Modified: core/trunk/geoext/tests/lib/GeoExt/plugins/AttributeForm.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/plugins/AttributeForm.html 2010-05-11 10:51:28 UTC (rev 2171)
+++ core/trunk/geoext/tests/lib/GeoExt/plugins/AttributeForm.html 2010-05-11 12:31:25 UTC (rev 2172)
@@ -123,59 +123,6 @@
t.eq(store.hasListener("load"), false,
"destroy unbinds plugin from store");
}
-
- function test_autofill(t) {
- t.plan(13);
-
- // Set up
-
- var store, plugin, form, field;
-
- store = new GeoExt.data.AttributeStore({
- fields: [
- {name: "name"},
- {name: "type"},
- {name: "nillable"},
- {name: "restriction"}
- ]
- });
- store.loadData(doc);
- plugin = new GeoExt.plugins.AttributeForm({
- attributeStore: store
- });
- form = new Ext.form.FormPanel({
- renderTo: "form",
- width: 200,
- plugins: [plugin]
- });
-
- // Test
-
- field = form.getComponent(0);
- t.ok(field instanceof Ext.form.TextField, "[txt] field is a text field");
- t.eq(field.name, "STATE_NAME", "[txt] field name is correct");
- t.eq(field.allowBlank, true, "[txt] field allowBlank is correct");
- t.eq(field.maxLength, 10, "[txt] field maxLength is correct");
- t.eq(field.minLength, 5, "[txt] field minLength is correct");
-
- field = form.getComponent(1);
- t.ok(field instanceof Ext.form.NumberField, "[num] field is a number field");
- t.eq(field.name, "SAMP_POP", "[num] field name is correct");
- t.eq(field.maxValue, 10, "[txt] field maxValue is correct");
- t.eq(field.minValue, 5, "[txt] field minValue is correct");
-
- field = form.getComponent(2);
- t.ok(field instanceof Ext.form.Checkbox, "[bool] field is a checkbox");
- t.eq(field.name, "BOOLEAN", "[bool] field name is correct");
-
- field = form.getComponent(3);
- t.ok(field instanceof Ext.form.DateField, "[date] field is a checkbox");
- t.eq(field.name, "DATE", "[date] field name is correct");
-
- // Tear down
-
- form.destroy();
- }
</script>
<body>
<div id="form"></div>
Modified: core/trunk/geoext/tests/lib/GeoExt/widgets/form.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/form.html 2010-05-11 10:51:28 UTC (rev 2171)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/form.html 2010-05-11 12:31:25 UTC (rev 2172)
@@ -5,6 +5,7 @@
<script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
<script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
+ <script type="text/javascript" src="form.js"></script>
<script type="text/javascript">
@@ -174,6 +175,84 @@
t.eq(filter.filters[0].value, "bar", "Default behaviour is to not add any wildcards");
}
+ function test_recordToField(t) {
+ t.plan(13);
+
+ // set up
+
+ var store, plugin, form, field;
+
+ store = new GeoExt.data.AttributeStore({
+ fields: [
+ {name: "name"},
+ {name: "type"},
+ {name: "nillable"},
+ {name: "restriction"}
+ ],
+ ignore: {
+ type: /^gml:(Multi)?(Point|LineString|Polygon|Curve|Surface|Geometry)PropertyType$/
+ }
+ });
+ store.loadData(doc);
+
+ // test
+
+ field = GeoExt.form.recordToField(store.getAt(0));
+ field = Ext.ComponentMgr.create(field);
+ t.ok(field instanceof Ext.form.TextField, "[txt] field is a text field");
+ t.eq(field.name, "STATE_NAME", "[txt] field name is correct");
+ t.eq(field.allowBlank, true, "[txt] field allowBlank is correct");
+ t.eq(field.maxLength, 10, "[txt] field maxLength is correct");
+ t.eq(field.minLength, 5, "[txt] field minLength is correct");
+
+ field = GeoExt.form.recordToField(store.getAt(1));
+ field = Ext.ComponentMgr.create(field);
+ t.ok(field instanceof Ext.form.NumberField, "[num] field is a number field");
+ t.eq(field.name, "SAMP_POP", "[num] field name is correct");
+ t.eq(field.maxValue, 10, "[num] field maxValue is correct");
+ t.eq(field.minValue, 5, "[num] field minValue is correct");
+
+ field = GeoExt.form.recordToField(store.getAt(2));
+ field = Ext.ComponentMgr.create(field);
+ t.ok(field instanceof Ext.form.Checkbox, "[bool] field is a checkbox");
+ t.eq(field.name, "BOOLEAN", "[bool] field name is correct");
+
+ field = GeoExt.form.recordToField(store.getAt(3));
+ field = Ext.ComponentMgr.create(field);
+ t.ok(field instanceof Ext.form.DateField, "[date] field is a checkbox");
+ t.eq(field.name, "DATE", "[date] field name is correct");
+
+ // tear down
+ }
+
+ function test_recordToField_objectliteral(t) {
+ t.plan(1)
+
+ // test cases where object litterals instead of strings
+ // are provided in "type" fields
+
+ // set up
+
+ var store, field;
+
+ store = new GeoExt.data.AttributeStore({
+ fields: [
+ {name: "name"},
+ {name: "type"}
+ ],
+ data: [
+ {name: "foo", type: {xtype: "combo"}}
+ ]
+ });
+
+ // test
+
+ field = GeoExt.form.recordToField(store.getAt(0));
+ field = Ext.ComponentMgr.create(field);
+ t.ok(field instanceof Ext.form.ComboBox,
+ "field is a combo");
+ }
+
</script>
<body>
<div id="form"></div>
More information about the Commits
mailing list