Skip to content

Commit

Permalink
GeoExt.form.recordToField: label template, r=bbinet (closes #445)
Browse files Browse the repository at this point in the history
  • Loading branch information
fvanderbiest committed Oct 26, 2011
1 parent c0da0f1 commit 142ac6f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
3 changes: 3 additions & 0 deletions examples/attribute-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ <h1>Attribute Form</h1>
information read from the DescribeFeatureType document. For example the
"STATE_NAME", "STATE_FIPS", "SUB_REGION", and "STATE_ABBR" text fields
indicate errors if more than 5 characters are entered.</p>

<p>Field labels have been formatted with templates, so that a star is
displayed when the field is required.</p>

<p>Note that the js is not minified so it is readable.
See <a href="attribute-form.js">attribute-form.js</a>.</p>
Expand Down
13 changes: 12 additions & 1 deletion examples/attribute-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ Ext.onReady(function() {
},
plugins: [
new GeoExt.plugins.AttributeForm({
attributeStore: attributeStore
attributeStore: attributeStore,
recordToFieldOptions: {
labelTpl: new Ext.XTemplate(
'{name}{[this.getStar(values)]}', {
compiled: true,
disableFormats: true,
getStar: function(v) {
return v.nillable ? '' : ' *';
}
}
)
}
})
]
});
Expand Down
2 changes: 1 addition & 1 deletion examples/data/describe_feature_type.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><xs:schema targetNamespace="http://www.openplans.org/topp" xmlns:topp="http://www.openplans.org/topp" xmlns:gml="http://www.opengis.net/gml" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0"><xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://localhost:8080/geoserver/schemas/gml/2.1.2.1/feature.xsd"/><xs:complexType xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/2001/XMLSchema" name="states_Type"><xs:complexContent><xs:extension base="gml:AbstractFeatureType"><xs:sequence><xs:element name="the_geom" minOccurs="0" nillable="true" type="gml:MultiPolygonPropertyType"/><xs:element name="STATE_NAME" minOccurs="0" nillable="true"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="STATE_FIPS" minOccurs="0" nillable="true"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="SUB_REGION" minOccurs="0" nillable="true"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="STATE_ABBR" minOccurs="0" nillable="true"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="LAND_KM" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="WATER_KM" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="PERSONS" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="FAMILIES" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="HOUSHOLD" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="MALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="FEMALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="WORKERS" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="DRVALONE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="CARPOOL" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="PUBTRANS" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="EMPLOYED" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="UNEMPLOY" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="SERVICE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="MANUAL" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="P_MALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="P_FEMALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="SAMP_POP" minOccurs="0" nillable="true" type="xs:double"/></xs:sequence></xs:extension></xs:complexContent></xs:complexType><xs:element name="states" type="topp:states_Type" substitutionGroup="gml:_Feature"/></xs:schema>
<?xml version="1.0" encoding="UTF-8"?><xs:schema targetNamespace="http://www.openplans.org/topp" xmlns:topp="http://www.openplans.org/topp" xmlns:gml="http://www.opengis.net/gml" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0"><xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://localhost:8080/geoserver/schemas/gml/2.1.2.1/feature.xsd"/><xs:complexType xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/2001/XMLSchema" name="states_Type"><xs:complexContent><xs:extension base="gml:AbstractFeatureType"><xs:sequence><xs:element name="the_geom" minOccurs="0" nillable="true" type="gml:MultiPolygonPropertyType"/><xs:element name="STATE_NAME" minOccurs="0" nillable="false"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="STATE_FIPS" minOccurs="0" nillable="true"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="SUB_REGION" minOccurs="0" nillable="true"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="STATE_ABBR" minOccurs="0" nillable="false"><xs:simpleType><xs:restriction base="xs:string"><xs:maxLength value="5"/></xs:restriction></xs:simpleType></xs:element><xs:element name="LAND_KM" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="WATER_KM" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="PERSONS" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="FAMILIES" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="HOUSHOLD" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="MALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="FEMALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="WORKERS" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="DRVALONE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="CARPOOL" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="PUBTRANS" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="EMPLOYED" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="UNEMPLOY" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="SERVICE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="MANUAL" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="P_MALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="P_FEMALE" minOccurs="0" nillable="true" type="xs:double"/><xs:element name="SAMP_POP" minOccurs="0" nillable="true" type="xs:double"/></xs:sequence></xs:extension></xs:complexContent></xs:complexType><xs:element name="states" type="topp:states_Type" substitutionGroup="gml:_Feature"/></xs:schema>
49 changes: 43 additions & 6 deletions lib/GeoExt/widgets/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ GeoExt.form.CONTAINS = 3;
* to apply to the field label if the field is not nillable (that is,
* the corresponding record has the "nillable" attribute set to ``false``).
* Default is ``"font-weigth: bold;"``.
* * labelTpl - ``Ext.Template`` or ``String`` or ``Array`` If set,
* the field label is obtained by applying the record's data hash to this
* template. This allows for very customizable field labels.
* See for instance :
*
* .. code-block:: javascript
*
* var formPanel = new Ext.form.FormPanel({
* autoScroll: true,
* plugins: [
* new GeoExt.plugins.AttributeForm({
* attributeStore: store,
* recordToFieldOptions: {
* mandatoryFieldLabelStyle: 'font-style:italic;',
* labelTpl: new Ext.XTemplate(
* '<span ext:qtip="{[this.getTip(values)]}">{name}</span>', {
* compiled: true,
* disableFormats: true,
* getTip: function(v) {
* if (!v.type) {
* return '';
* }
* var type = v.type.split(":").pop();
* return OpenLayers.i18n(type) +
* (v.nillable ? '' : ' (required)');
* }
* }
* )
* }
* })
* ]
* });
*
* :return: ``Object`` An object literal with a xtype property, use
* ``Ext.ComponentMgr.create`` (or ``Ext.create`` in Ext 3) to create
Expand All @@ -120,19 +152,24 @@ GeoExt.form.recordToField = function(record, options) {
// field, just return it
return type;
}

type = type.split(":").pop(); // remove ns prefix

var field;
var name = record.get("name");
var label = record.get("label");
var restriction = record.get("restriction") || {};
var nillable = record.get("nillable") || false;

// use name for label if label isn't defined in the record
if(label == null) {
var label = record.get("label");
var labelTpl = options.labelTpl;
if (labelTpl) {
var tpl = (labelTpl instanceof Ext.Template) ?
labelTpl :
new Ext.XTemplate(labelTpl);
label = tpl.apply(record.data);
} else if (label == null) {
// use name for label if label isn't defined in the record
label = name;
}

type = type.split(":").pop(); // remove ns prefix

var baseOptions = {
name: name,
Expand Down
19 changes: 18 additions & 1 deletion tests/lib/GeoExt/widgets/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
}

function test_recordToField(t) {
t.plan(26);
t.plan(28);

// set up

Expand Down Expand Up @@ -270,6 +270,23 @@
field = Ext.ComponentMgr.create(field);
t.eq(field.labelStyle, 'font-style:italic;', "[label] non nillable field custom labelStyle is correct");

// label tip
field = GeoExt.form.recordToField(store.getAt(0), {
labelTpl: new Ext.XTemplate('{name}{[this.getStar(values)]}', {
getStar: function(v) {
return (v.nillable ? '':' *');
}
})
});
field = Ext.ComponentMgr.create(field);
t.eq(field.fieldLabel, 'STATE_NAME *', "[label template] template is applied successfully");

field = GeoExt.form.recordToField(store.getAt(0), {
labelTpl: '{name} foo'
});
field = Ext.ComponentMgr.create(field);
t.eq(field.fieldLabel, 'STATE_NAME foo', "[label template] labelTpl accepts strings too");

// txt
field = GeoExt.form.recordToField(store.getAt(0));
field = Ext.ComponentMgr.create(field);
Expand Down

0 comments on commit 142ac6f

Please sign in to comment.