[Commits] r707 - in core/trunk/geoext: examples lib lib/GeoExt/widgets lib/GeoExt/widgets/form tests tests/lib/GeoExt/widgets tests/lib/GeoExt/widgets/form

commits at geoext.org commits at geoext.org
Wed May 13 09:39:05 CEST 2009


Author: elemoine
Date: 2009-05-13 09:39:05 +0200 (Wed, 13 May 2009)
New Revision: 707

Added:
   core/trunk/geoext/examples/search-form.html
   core/trunk/geoext/examples/search-form.js
   core/trunk/geoext/lib/GeoExt/widgets/form/
   core/trunk/geoext/lib/GeoExt/widgets/form/BasicForm.js
   core/trunk/geoext/lib/GeoExt/widgets/form/FormPanel.js
   core/trunk/geoext/lib/GeoExt/widgets/form/SearchAction.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/form/
   core/trunk/geoext/tests/lib/GeoExt/widgets/form/BasicForm.html
   core/trunk/geoext/tests/lib/GeoExt/widgets/form/FormPanel.html
   core/trunk/geoext/tests/lib/GeoExt/widgets/form/SearchAction.html
Modified:
   core/trunk/geoext/lib/GeoExt.js
   core/trunk/geoext/tests/list-tests.html
Log:
specific form Action, BasicForm and FormPanel, r=pgiraud (closes #49)


Added: core/trunk/geoext/examples/search-form.html
===================================================================
--- core/trunk/geoext/examples/search-form.html	                        (rev 0)
+++ core/trunk/geoext/examples/search-form.html	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,32 @@
+<html>
+    <head>
+        <script type="text/javascript" src="http://extjs.cachefly.net/builds/ext-cdn-771.js"></script>
+        <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-2.2.1/resources/css/ext-all.css" />
+        <link rel="stylesheet" type="text/css" href="http://extjs.com/deploy/dev/examples/shared/examples.css"></link>
+        <script src="http://openlayers.org/api/2.8-rc2/OpenLayers.js"></script>
+        <script type="text/javascript" src="../lib/GeoExt.js"></script>
+
+        <script type="text/javascript" src="search-form.js"></script>
+    </head>
+    <body>
+        <h1>Example of a search form using GeoExt.form.FormPanel</h1>
+
+        <p>This example shows how to create a search form based on
+        GeoExt.form.FormPanel.</p>
+        
+        <p>A GeoExt.form.FormPanel instance uses an OpenLayers.Protocol object
+        for sending search requests. In this example a fake protocol is used,
+        in a real-life scenario a specific protocol like
+        OpenLayers.Protocol.WFS would be used.</p>
+
+        <p>If you click on the "search" button below the form, the filter
+        resulting from the form values is represented to the right of the
+        form.</p>
+
+        <p>The js is not minified so it is readable. See <a
+        href="search-form.js">search-form.js</a></p>
+
+        <div id="formpanel" style="float:left;"></div>
+        <div id="filter"></div>
+    </body>
+</html>

Added: core/trunk/geoext/examples/search-form.js
===================================================================
--- core/trunk/geoext/examples/search-form.js	                        (rev 0)
+++ core/trunk/geoext/examples/search-form.js	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,73 @@
+
+var formPanel;
+
+Ext.onReady(function() {
+
+    // create a protocol, this protocol is used by the form
+    // to send the search request, this protocol's read
+    // method received an OpenLayers.Filter instance,
+    // which is derived from the content of the form
+    var protocol = new OpenLayers.Protocol({
+        read: function(options) {
+            var f; html = [];
+
+            f = options.filter;
+            html.push([f.CLASS_NAME, ",", f.type, "<br />"].join(" "));
+
+            f = options.filter.filters[0];
+            html.push([f.CLASS_NAME, ",", f.type, ",",
+                       f.property, ":", f.value, "<br />"].join(" "));
+
+            f = options.filter.filters[1];
+            html.push([f.CLASS_NAME, ",", f.type, ", ",
+                       f.property, ": ", f.value].join(" "));
+
+            Ext.get("filter").update(html.join(""));
+
+        }
+    });
+
+    // create a GeoExt form panel (configured with an OpenLayers.Protocol
+    // instance)
+    formPanel = new GeoExt.form.FormPanel({
+        width: 300,
+        height: 200,
+        protocol: protocol,
+        items: [{
+            xtype: "textfield",
+            name: "name__like",
+            value: "foo",
+            fieldLabel: "name"
+        }, {
+            xtype: "textfield",
+            name: "elevation__ge",
+            value: "1200",
+            fieldLabel: "maximum elevation"
+        }],
+        listeners: {
+            actioncomplete: function(form, action) {
+                // this listener triggers when the search request
+                // is complete, the OpenLayers.Protocol.Response
+                // resulting from the request is available
+                // through "action.response"
+            }
+        }
+    });
+
+    formPanel.addButton({
+        text: "search",
+        handler: function() {
+            // trigger search request, the options passed to doAction
+            // are passed to the protocol's read method, so one
+            // can register a read callback here
+            var o = {
+                callback: function(response) {
+                }
+            };
+            this.search(o);
+        },
+        scope: formPanel
+    });
+
+    formPanel.render("formpanel");
+});

Added: core/trunk/geoext/lib/GeoExt/widgets/form/BasicForm.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/form/BasicForm.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/widgets/form/BasicForm.js	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,67 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ * 
+ * pending approval */
+
+/**
+ * @include GeoExt/widgets/form/SearchAction.js
+ */
+
+/** api: (define)
+ *  module = GeoExt.form
+ *  class = BasicForm
+ *  base_link = `Ext.form.BasicForm <http://extjs.com/deploy/dev/docs/?class=Ext.form.BasicForm>`_
+ */
+
+Ext.namespace("GeoExt.form");
+
+/** api: constructor
+ *  .. class:: BasicForm(config)
+ *
+ *  A specific ``Ext.form.BasicForm`` whose doAction method creates
+ *  a :class:`GeoExt.form.SearchAction` if it is passed the string
+ *  "search" as its first argument.
+ *
+ *  In most cases one would not use this class directly, but
+ *  :class:`GeoExt.form.FormPanel` instead.
+ */
+GeoExt.form.BasicForm = Ext.extend(Ext.form.BasicForm, {
+    /** private: property[protocol]
+     *  ``OpenLayers.Protocol`` The protocol configured in this
+     *  instance.
+     */
+    protocol: null,
+
+    /** api: method[doAction]
+     *  :param action: ``String or Ext.form.Action`` Either the name
+     *      of the action or a ``Ext.form.Action`` instance.
+     *  :param options: ``Object`` The options passed to the Action
+     *      constructor.
+     *  :return: :class:`GeoExt.form.BasicForm` This form.
+     *
+     *  Performs the action, if the string "search" is passed as the
+     *  first argument then a :class:`GeoExt.form.SearchAction` is created.
+     */
+    doAction: function(action, options) {
+        if(action == "search") {
+            options = Ext.applyIf(options || {}, {protocol: this.protocol});
+            action = new GeoExt.form.SearchAction(this, options);
+        }
+        return GeoExt.form.BasicForm.superclass.doAction.call(
+            this, action, options
+        );
+    },
+
+    /** api: method[search]
+     *  :param options: ``Object`` The options passed to the Action
+     *      constructor.
+     *  :return: :class:`GeoExt.form.BasicForm` This form.
+     *  
+     *  Shortcut to do a search action.
+     */
+    search: function(options) {
+        return this.doAction("search", options);
+    }
+});

Added: core/trunk/geoext/lib/GeoExt/widgets/form/FormPanel.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/form/FormPanel.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/widgets/form/FormPanel.js	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,101 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ * 
+ * pending approval */
+
+/** api: (define)
+ *  module = GeoExt.form
+ *  class = FormPanel
+ *  base_link = `Ext.form.Action <http://extjs.com/deploy/dev/docs/?class=Ext.form.FormPanel>`_
+ */
+
+/**
+ * @include GeoExt/widgets/form/BasicForm.js
+ */
+
+Ext.namespace("GeoExt.form");
+
+/** api: example
+ *  Sample code showing how to use a GeoExt form panel.
+ *
+ *  .. code-block:: javascript
+ *
+ *      var formPanel = new GeoExt.form.Panel({
+ *          renderTo: "formpanel",
+ *          protocol: new OpenLayers.Protocol.WFS({
+ *              url: "http://publicus.opengeo.org/geoserver/wfs",
+ *              featureType: "tasmania_roads",
+ *              featureNS: "http://www.openplans.org/topp"
+ *          })
+ *          items: [{
+ *              xtype: "textfield",
+ *              name: "name__ilike",
+ *              value: "mont"
+ *          }, {
+ *              xtype: "textfield",
+ *              name: "elevation__ge",
+ *              value: "2000"
+ *          }],
+ *          listeners: {
+ *              actioncomplete: function(form, action) {
+ *                  // this listener triggers when the search request
+ *                  // is complete, the OpenLayers.Protocol.Response
+ *                  // resulting from the request is available
+ *                  // in "action.response"
+ *              }
+ *          }
+ *      });
+ *
+ *      formPanel.addButton({
+ *          text: "search",
+ *          handler: function() {
+ *              this.search();
+ *          },
+ *          scope: formPanel
+ *      });
+ */
+
+/** api: constructor
+ *  .. class:: FormPanel(config)
+ *
+ *  A specific ``Ext.form.FormPanel`` whose internal form is a
+ *  :class:`GeoExt.form.BasicForm` instead of ``Ext.form.BasicForm``.
+ *  One would use this form to do search requests through
+ *  an ``OpenLayers.Protocol`` object (``OpenLayers.Protocol.WFS``
+ *  for example).
+ *
+ *  Look at :class:`GeoExt.form.SearchAction` to understand how
+ *  form fields must be named for appropriate filters to be
+ *  passed to the protocol.
+ */
+GeoExt.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
+    /** api: config[protocol]
+     *  ``OpenLayers.Protocol`` The protocol instance this form panel
+     *  is configured with, actions resulting from this form
+     *  will be performed through the protocol.
+     */
+    protocol: null,
+
+    /** private: method[createForm]
+     *  Create the internal :class:`GeoExt.form.BasicForm` instance.
+     */
+    createForm: function() {
+        delete this.initialConfig.listeners;
+        return new GeoExt.form.BasicForm(null, this.initialConfig);
+    },
+
+    /** api: method[search]
+     *  :param options: ``Object`` The options passed to the
+     *      :class:`GeoExt.form.SearchAction` constructor.
+     *
+     *  Shortcut to the internal form's search method.
+     */
+    search: function(options) {
+        this.getForm().search(options);
+    }
+});
+
+/** api: xtype = gx_formpanel */
+Ext.reg("gx_formpanel", GeoExt.form.FormPanel);

Added: core/trunk/geoext/lib/GeoExt/widgets/form/SearchAction.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/form/SearchAction.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/widgets/form/SearchAction.js	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,202 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ * 
+ * pending approval */
+
+/** api: (define)
+ *  module = GeoExt.form
+ *  class = SearchAction
+ *  base_link = `Ext.form.Action <http://extjs.com/deploy/dev/docs/?class=Ext.form.Action>`_
+ */
+
+Ext.namespace("GeoExt.form");
+ 
+/** api: example
+ *  Sample code showing how to use a GeoExt SearchAction with an Ext form panel:
+ *  
+ *  .. code-block:: javascript
+ *
+ *      var formPanel = new Ext.form.Panel({
+ *          renderTo: "formpanel",
+ *          items: [{
+ *              xtype: "textfield",
+ *              name: "name__like",
+ *              value: "mont"
+ *          }, {
+ *              xtype: "textfield",
+ *              name: "elevation__ge",
+ *              value: "2000"
+ *          }]
+ *      });
+ *
+ *      var searchAction = new GeoExt.form.SearchAction(formPanel.getForm(), {
+ *          protocol: new OpenLayers.Protocol.WFS({
+ *              url: "http://publicus.opengeo.org/geoserver/wfs",
+ *              featureType: "tasmania_roads",
+ *              featureNS: "http://www.openplans.org/topp"
+ *          })
+ *      });
+ *
+ *      formPanel.getForm().doAction(searchAction, {
+ *          callback: function(response) {
+ *              // response.features includes the features read
+ *              // from the server through the protocol
+ *          }
+ *      });
+ */
+
+/**
+ * Function: GeoExt.form.filterFromForm
+ * Create an {OpenLayers.Filter} object from a {Ext.form.BasicForm}
+ *     instance or a {Ext.form.FormPanel}.
+ *
+ * Parameters:
+ * form - {Ext.form.BasicForm|Ext.form.FormPanel}
+ * logicalOp - {String} Either {OpenLayers.Filter.Logical.AND}
+ *     or {OpenLayers.Filter.Logical.OR}, set to
+ *     {OpenLayers.Filter.Logical.AND} if null or
+ *     undefined.
+ *
+ * Returns:
+ * {OpenLayers.Filter}
+ */
+GeoExt.form.filterFromForm = function(form, logicalOp) {
+    if(form instanceof Ext.form.FormPanel) {
+        form = form.getForm();
+    }
+    var filters = [], values = form.getValues(false);
+    for(var prop in values) {
+        var s = prop.split("__");
+
+        var value = values[prop], type;
+
+        if(s.length > 1 && 
+           (type = GeoExt.form.filterFromForm.FILTER_MAP[s[1]]) !== undefined) {
+            prop = s[0];
+        } else {
+            type = OpenLayers.Filter.Comparison.EQUAL_TO;
+        }
+
+        filters.push(
+            new OpenLayers.Filter.Comparison({
+                type: type,
+                value: value,
+                property: prop
+            })
+        );
+    }
+
+    return new OpenLayers.Filter.Logical({
+        type: logicalOp || OpenLayers.Filter.Logical.AND,
+        filters: filters
+    });
+};
+
+/**
+ * Constant: GeoExt.form.filterFromForm.FILTER_MAP
+ * An object mapping operator strings as found in field names to
+ *     {OpenLayers.Filter.Comparison} types.
+ */
+GeoExt.form.filterFromForm.FILTER_MAP = {
+    "eq": OpenLayers.Filter.Comparison.EQUAL_TO,
+    "ne": OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
+    "lt": OpenLayers.Filter.Comparison.LESS_THAN,
+    "le": OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO,
+    "gt": OpenLayers.Filter.Comparison.GREATER_THAN,
+    "ge": OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
+    "like": OpenLayers.Filter.Comparison.LIKE
+};
+
+/** api: constructor
+ *  .. class:: SearchAction(form, options)
+ *
+ *      A specific ``Ext.form.Action`` to be used when using a form to do
+ *      trigger search requests througn an ``OpenLayers.Protocol``.
+ *
+ *      Arguments:
+ *
+ *      * form ``Ext.form.BasicForm`` A basic form instance.
+ *      * options ``Object`` Options passed to the protocol'read method
+ *
+ *      When run this action builds an ``OpenLayers.Filter`` from the form
+ *      and passes this filter to its protocol's read method. The form fields
+ *      must be named after a specific convention, so that an appropriate 
+ *      ``OpenLayers.Filter.Comparison`` filter is created for each
+ *      field.
+ *
+ *      For example a field with the name ``foo__like`` would result in an
+ *      ``OpenLayers.Filter.Comparison`` of type
+ *      ``OpenLayers.Filter.Comparison.LIKE`` being created.
+ *
+ *      Here is the convention:
+ *
+ *      * ``<name>__eq: OpenLayers.Filter.Comparison.EQUAL_TO``
+ *      * ``<name>__ne: OpenLayers.Filter.Comparison.NOT_EQUAL_TO``
+ *      * ``<name>__lt: OpenLayers.Filter.Comparison.LESS_THAN``
+ *      * ``<name>__le: OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO``
+ *      * ``<name>__gt: OpenLayers.Filter.Comparison.GREATER_THAN``
+ *      * ``<name>__ge: OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO``
+ *      * ``<name>__like: OpenLayers.Filter.Comparison.LIKE``
+ *
+ *      In most cases your would not directly create ``GeoExt.form.SearchAction``
+ *      objects, but use :class:`GeoExt.form.FormPanel` instead.
+ */
+GeoExt.form.SearchAction = Ext.extend(Ext.form.Action, {
+    /** private: property[type]
+     *  ``String`` The action type string.
+     */
+    type: "search",
+
+    /** api: property[response]
+     *  ``OpenLayers.Protocol.Response`` A reference to the response
+     *  resulting from the search request. Read-only.
+     */
+    response: null,
+
+    /** private */
+    constructor: function(form, options) {
+        GeoExt.form.SearchAction.superclass.constructor.call(this, form, options);
+    },
+
+    /** private: method[run]
+     *  Run the action.
+     */
+    run: function() {
+        var o = this.options;
+        var f = GeoExt.form.filterFromForm(this.form);
+        if(o.clientValidation === false || this.form.isValid()){
+            this.response = o.protocol.read(
+                Ext.applyIf({
+                    filter: f,
+                    callback: this.handleResponse,
+                    scope: this
+                }, o)
+            );
+        } else if(o.clientValidation !== false){
+            // client validation failed
+            this.failureType = Ext.form.Action.CLIENT_INVALID;
+            this.form.afterAction(this, false);
+        }
+    },
+
+    /** private: method[handleResponse]
+     *  :param response: ``OpenLayers.Protocol.Response`` The response
+     *  object.
+     *
+     *  Handle the response to the search query.
+     */
+    handleResponse: function(response) {
+        this.response = response;
+        if(response.success()) {
+            this.form.afterAction(this, true);
+        } else {
+            this.form.afterAction(this, false);
+        }
+        var o = this.options;
+        if(o.callback) {
+            o.callback.call(o.scope, response);
+        }
+    }
+});

Modified: core/trunk/geoext/lib/GeoExt.js
===================================================================
--- core/trunk/geoext/lib/GeoExt.js	2009-05-13 00:14:56 UTC (rev 706)
+++ core/trunk/geoext/lib/GeoExt.js	2009-05-13 07:39:05 UTC (rev 707)
@@ -69,7 +69,10 @@
             "GeoExt/data/WMSCapabilitiesStore.js",
             "GeoExt/data/ProtocolProxy.js",
             "GeoExt/widgets/MapPanel.js",
-            "GeoExt/widgets/Popup.js"
+            "GeoExt/widgets/Popup.js",
+            "GeoExt/widgets/form/SearchAction.js",
+            "GeoExt/widgets/form/BasicForm.js",
+            "GeoExt/widgets/form/FormPanel.js"
         );
 
         var agent = navigator.userAgent;

Added: core/trunk/geoext/tests/lib/GeoExt/widgets/form/BasicForm.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/form/BasicForm.html	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/form/BasicForm.html	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,82 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../../../openlayers/lib/OpenLayers.js"></script>
+    <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">
+    function test_constructor(t) {
+        t.plan(1);
+
+        /*
+         * Set up
+         */
+        
+        var protocol, form;
+
+        protocol = new OpenLayers.Protocol();
+
+        form = new GeoExt.form.BasicForm(Ext.get("form"), {
+            protocol: protocol
+        });
+
+        /*
+         * Test
+         */
+
+        t.ok(form.protocol == protocol,
+             "BasicForm constructor properly sets protocol in the instance");
+    }
+
+    function test_doAction(t) {
+        t.plan(3);
+
+        /*
+         * Set up
+         */
+        
+        var protocol, form;
+
+        protocol = new OpenLayers.Protocol({
+            read: function(options) {
+                t.ok(options.filter instanceof OpenLayers.Filter.Logical,
+                     ["doAction calls read on the form's protocol, read",
+                      "is passed a logical filter"].join(" "));
+            }
+        });
+
+        form = new GeoExt.form.BasicForm(Ext.get("form"), {
+            protocol: protocol,
+            getValues: function() {
+                return {"foo0__eq": "bar0", "foo1__like": "bar1"};
+            }
+        });
+
+        /*
+         * Test
+         */
+
+        // 1 test
+        var tmp = form.doAction("search");
+        t.ok(tmp == form,
+             "doAction returns the form instance");
+        t.wait_result(1);
+
+        // 1 test
+        protocol = new OpenLayers.Protocol({
+            read: function(options) {
+                t.ok(options.filter instanceof OpenLayers.Filter.Logical,
+                     ["doAction calls read on the protocol it is given,",
+                      "read is passed a logical filter"].join(" "));
+            }
+        });
+        form.doAction("search", {protocol: protocol});
+        t.wait_result(1);
+    }
+    </script>
+  <body>
+    <div id="form"></div>
+  </body>
+</html>

Added: core/trunk/geoext/tests/lib/GeoExt/widgets/form/FormPanel.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/form/FormPanel.html	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/form/FormPanel.html	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,142 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../../../openlayers/lib/OpenLayers.js"></script>
+    <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">
+    function test_constructor(t) {
+        t.plan(3);
+
+        /*
+         * Set up
+         */
+
+        var protocol, form;
+
+        protocol = new OpenLayers.Protocol();
+
+        form = new GeoExt.form.FormPanel({
+           renderTo: "form",
+           protocol: protocol
+        });
+
+        /*
+         * Test
+         */
+
+        t.ok(form.protocol == protocol,
+             "FormPanel constructor sets protocol in the instance");
+        t.ok(form.getForm() instanceof GeoExt.form.BasicForm,
+             ["FormPanel constructor creates a GeoExt.form.BasicForm as",
+              "its internal form"].join(" "));
+        t.ok(form.getForm().protocol == protocol,
+             "FormPanel constructor sets protocol in internal form");
+    }
+
+    function test_actioncomplete(t) {
+        t.plan(2);
+
+        /*
+         * Set up
+         */
+
+        var response, protocol, field, form;
+        var success = 0;
+
+        response = new OpenLayers.Protocol.Response({
+            code: OpenLayers.Protocol.Response.SUCCESS
+        });
+
+        protocol = new OpenLayers.Protocol({
+            read: function(o) {
+                o.callback.call(o.scope, response);
+                return response;
+            }
+        });
+
+        field = new Ext.form.TextField({
+            name: "foo0",
+            value: "bar0"
+        });
+
+        form = new GeoExt.form.FormPanel({
+           renderTo: "form",
+           protocol: protocol,
+           items: [field],
+           listeners: {
+               actioncomplete: function(form, action) {
+                   t.ok(action.response == response,
+                        "actioncomplete passed expected response");
+                   success++;
+               }
+           }
+        });
+
+        /*
+         * Test
+         */
+
+        form.search();
+        t.delay_call(1, function() {
+            t.eq(success, 1,
+                 "actioncomplete called when search request succeeds");
+        });
+    }
+
+    function test_actionfailed(t) {
+        t.plan(2);
+
+        /*
+         * Set up
+         */
+
+        var response, protocol, field, form;
+        var failure = 0;
+
+        response = new OpenLayers.Protocol.Response({
+            code: OpenLayers.Protocol.Response.FAILURE
+        });
+
+        protocol = new OpenLayers.Protocol({
+            read: function(o) {
+                o.callback.call(o.scope, response);
+                return response;
+            }
+        });
+
+        field = new Ext.form.TextField({
+            name: "foo0",
+            value: "bar0"
+        });
+
+        form = new GeoExt.form.FormPanel({
+           renderTo: "form",
+           protocol: protocol,
+           items: [field],
+           listeners: {
+               actionfailed: function(form, action) {
+                   t.ok(action.response == response,
+                        "actionfailed passed expected response");
+                   failure++;
+               }
+           }
+        });
+
+        /*
+         * Test
+         */
+
+        form.search();
+        t.delay_call(1, function() {
+            t.eq(failure, 1,
+                 "actionfailed called when search request fails");
+        });
+    }
+    </script>
+  <body>
+    <div id="form"></div>
+  </body>
+</html>

Added: core/trunk/geoext/tests/lib/GeoExt/widgets/form/SearchAction.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/form/SearchAction.html	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/form/SearchAction.html	2009-05-13 07:39:05 UTC (rev 707)
@@ -0,0 +1,205 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../../../openlayers/lib/OpenLayers.js"></script>
+    <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">
+
+    function test_filterFromForm(t) {
+        t.plan(27);
+
+        /*
+         * Set up
+         */
+
+        var form, filter, fields = [];
+
+        fields.push(new Ext.form.TextField({
+            name: "foo0",
+            value: "bar0"
+        }));
+
+        fields.push(new Ext.form.TextField({
+            name: "foo1__eq",
+            value: "bar1"
+        }));
+
+        fields.push(new Ext.form.TextField({
+            name: "foo2__ne",
+            value: "bar2"
+        }));
+
+        fields.push(new Ext.form.TextField({
+            name: "foo3__lt",
+            value: "bar3"
+        }));
+
+        fields.push(new Ext.form.TextField({
+            name: "foo4__le",
+            value: "bar4"
+        }));
+
+        fields.push(new Ext.form.TextField({
+            name: "foo5__gt",
+            value: "bar5"
+        }));
+
+        fields.push(new Ext.form.TextField({
+            name: "foo6__ge",
+            value: "bar6"
+        }));
+
+        fields.push(new Ext.form.TextField({
+            name: "foo7__like",
+            value: "bar7"
+        }));
+
+        form = new Ext.form.FormPanel({
+            renderTo: "form",
+            items: fields
+        });
+
+        /*
+         * Test
+         */
+
+        // 26 tests
+        filter = GeoExt.form.filterFromForm(form);
+
+        t.ok(filter instanceof OpenLayers.Filter.Logical,
+             "GeoExt.form.filterFormForm returns a logical filter");
+
+        t.eq(filter.type, OpenLayers.Filter.Logical.AND,
+             ["GeoExt.form.filterFormForm returns a logical AND filter if",
+              "logicalOp is undefined"].join(" "));
+
+        t.eq(filter.filters[0].type, OpenLayers.Filter.Comparison.EQUAL_TO,
+             "GeoExt.form.filterFormForm creates correct filter type");
+        t.eq(filter.filters[0].property, "foo0",
+             "GeoExt.form.filterFormForm creates correct filter prop");
+        t.eq(filter.filters[0].value, "bar0",
+             "GeoExt.form.filterFormForm creates correct filter value");
+
+        t.eq(filter.filters[1].type, OpenLayers.Filter.Comparison.EQUAL_TO,
+             "GeoExt.form.filterFormForm creates correct filter type (__eq)");
+        t.eq(filter.filters[1].property, "foo1",
+             "GeoExt.form.filterFormForm creates correct filter prop (__eq)");
+        t.eq(filter.filters[1].value, "bar1",
+             "GeoExt.form.filterFormForm creates correct filter value (__eq)");
+
+        t.eq(filter.filters[2].type, OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
+             "GeoExt.form.filterFormForm creates correct filter type (__ne)");
+        t.eq(filter.filters[2].property, "foo2",
+             "GeoExt.form.filterFormForm creates correct filter prop (__ne)");
+        t.eq(filter.filters[2].value, "bar2",
+             "GeoExt.form.filterFormForm creates correct filter value (__ne)");
+
+        t.eq(filter.filters[3].type, OpenLayers.Filter.Comparison.LESS_THAN,
+             "GeoExt.form.filterFormForm creates correct filter type (__lt)");
+        t.eq(filter.filters[3].property, "foo3",
+             "GeoExt.form.filterFormForm creates correct filter prop (__lt)");
+        t.eq(filter.filters[3].value, "bar3",
+             "GeoExt.form.filterFormForm creates correct filter value (__lt)");
+
+        t.eq(filter.filters[4].type, OpenLayers.Filter.Comparison.LESS_THAN_OR_EQUAL_TO,
+             "GeoExt.form.filterFormForm creates correct filter type (__le)");
+        t.eq(filter.filters[4].property, "foo4",
+             "GeoExt.form.filterFormForm creates correct filter prop (__le)");
+        t.eq(filter.filters[4].value, "bar4",
+             "GeoExt.form.filterFormForm creates correct filter value (__le)");
+
+        t.eq(filter.filters[5].type, OpenLayers.Filter.Comparison.GREATER_THAN,
+             "GeoExt.form.filterFormForm creates correct filter type (__gt)");
+        t.eq(filter.filters[5].property, "foo5",
+             "GeoExt.form.filterFormForm creates correct filter prop (__gt)");
+        t.eq(filter.filters[5].value, "bar5",
+             "GeoExt.form.filterFormForm creates correct filter value (__gt)");
+
+        t.eq(filter.filters[6].type, OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
+             "GeoExt.form.filterFormForm creates correct filter type (__ge)");
+        t.eq(filter.filters[6].property, "foo6",
+             "GeoExt.form.filterFormForm creates correct filter prop (__ge)");
+        t.eq(filter.filters[6].value, "bar6",
+             "GeoExt.form.filterFormForm creates correct filter value (__ge)");
+
+        t.eq(filter.filters[7].type, OpenLayers.Filter.Comparison.LIKE,
+             "GeoExt.form.filterFormForm creates correct filter type (__like)");
+        t.eq(filter.filters[7].property, "foo7",
+             "GeoExt.form.filterFormForm creates correct filter prop (__like)");
+        t.eq(filter.filters[7].value, "bar7",
+             "GeoExt.form.filterFormForm creates correct filter value (__like)");
+
+        // 1 test
+        filter = GeoExt.form.filterFromForm(form,
+                                            OpenLayers.Filter.Logical.OR);
+
+        t.eq(filter.type, OpenLayers.Filter.Logical.OR,
+             ["GeoExt.form.filterFormForm returns a logical OR filter if",
+              "logicalOp is OpenLayers.Filter.Logical.OR"].join(" "));
+    }
+
+    function test_constructor(t) {
+        t.plan(1);
+
+        /*
+         * Set up
+         */
+
+        var form, action, protocol;
+
+        form = new Ext.form.BasicForm(Ext.get("form"));
+        protocol = new OpenLayers.Protocol();
+        action = new GeoExt.form.SearchAction(form, {protocol: protocol});
+
+        /*
+         * Test
+         */
+
+        t.ok(action.options.protocol == protocol,
+             "SearchAction constructor properly sets protocol in options");
+    }
+
+    function test_run(t) {
+        t.plan(1);
+
+        /*
+         * Set up
+         */
+
+        var field, form, action, protocol;
+
+        var field = new Ext.form.TextField({
+            name: "foo__eq",
+            value: "bar"
+        });
+
+        form = new Ext.form.FormPanel({
+            renderTo: "form",
+            items: [field]
+        });
+
+        protocol = new OpenLayers.Protocol({
+            read: function(options) {
+                t.ok(options.filter instanceof OpenLayers.Filter.Logical,
+                     "run calls protocol.read with a logical filter");
+            }
+        });
+
+        action = new GeoExt.form.SearchAction(form.getForm(), {
+            protocol: protocol,
+            clientValidation: false
+        });
+
+        /*
+         * Test
+         */
+       action.run();
+    }
+    </script>
+  <body>
+    <div id="form"></div>
+  </body>
+</html>

Modified: core/trunk/geoext/tests/list-tests.html
===================================================================
--- core/trunk/geoext/tests/list-tests.html	2009-05-13 00:14:56 UTC (rev 706)
+++ core/trunk/geoext/tests/list-tests.html	2009-05-13 07:39:05 UTC (rev 707)
@@ -10,4 +10,7 @@
   <li>lib/GeoExt/data/WMSCapabilitiesReader.html</li>
   <li>lib/GeoExt/widgets/MapPanel.html</li>
   <li>lib/GeoExt/widgets/Popup.html</li>
+  <li>lib/GeoExt/widgets/form/SearchAction.html</li>
+  <li>lib/GeoExt/widgets/form/BasicForm.html</li>
+  <li>lib/GeoExt/widgets/form/FormPanel.html</li>
 </ul>



More information about the Commits mailing list