[fusion-commits] r1418 - in sandbox/aboudreault: MapServer/php widgets

svn_fusion at osgeo.org svn_fusion at osgeo.org
Tue Jun 10 14:13:13 EDT 2008


Author: aboudreault
Date: 2008-06-10 14:13:13 -0400 (Tue, 10 Jun 2008)
New Revision: 1418

Modified:
   sandbox/aboudreault/MapServer/php/Search.php
   sandbox/aboudreault/widgets/SelectionPanel.js
Log:
SelectionPanel with some changes...

Modified: sandbox/aboudreault/MapServer/php/Search.php
===================================================================
--- sandbox/aboudreault/MapServer/php/Search.php	2008-06-04 14:02:38 UTC (rev 1417)
+++ sandbox/aboudreault/MapServer/php/Search.php	2008-06-10 18:13:13 UTC (rev 1418)
@@ -112,57 +112,49 @@
     $oResultSet = $oLayer->getResult(0);
     $oShape = $oLayer->getShape($oResultSet->tileindex,$oResultSet->shapeindex);
     $selFields = array();
-    
-    if (isset($_SESSION[$mapName][$layerName]['query_items']))
-    {
-        $aQueryItems = $_SESSION[$mapName][$layerName]['query_items'];
-    }
-    else
-    {
-        //token separator (for parsing displayed attributes on a query)
-        $tokenSeparator = ","; 
-        // checking if metadata "query_include_items" is set
-        $metadataItems = $oLayer->getMetaData('query_include_items');
-        if ( ($metadataItems == "") || ($metadataItems == "all") )
-        {
-            while ( list($key,$val) = each($oShape->values) )
-            {
-                $aQueryItems[$key]  = NULL;
+   
+     if (isset($_SESSION[$mapName][$layerName]['query_items'])) {
+            $aQueryItems = $_SESSION[$mapName][$layerName]['query_items'];
+        } else {
+            //token separator (for parsing displayed attributes on a query)
+            $tokenSeparator = ","; 
+            // checking if metadata "query_include_items" is set
+            $metadataItems = $oLayer->getMetaData('query_include_items');
+            if ( ($metadataItems == "") || ($metadataItems == "all") ) {
+                while ( list($key,$val) = each($oShape->values) ) {
+                    $aQueryItems[$key]  = NULL;
+                }
+            } else {
+              $token = strtok($metadataItems, $tokenSeparator);
+              while ($token !== false) {
+                  $aQueryItems[trim($token)] = NULL;
+                  $token = strtok($tokenSeparator);
+              }
             }
-        }
-        else
-        {
-            $token = strtok($metadataItems, $tokenSeparator);
-            while ($token !== false) 
-            {
-                $aQueryItems[$token] = NULL;
-                $token = strtok($tokenSeparator);
+          
+            // checking if metadata "query_exclude_items" is set
+            $metadataItems = $oLayer->getMetaData('query_exclude_items');
+            if ($metadataItems != "") {
+                $token = strtok($metadataItems, $tokenSeparator);
+                while ($token !== false) {
+                    if (array_key_exists($token, $aQueryItems)) {
+                        unset($aQueryItems[$token]);
+                    }
+                    $token = strtok($tokenSeparator);
+                }
             }
-        }
-        
-        // checking if metadata "query_exclude_items" is set
-        $metadataItems = $oLayer->getMetaData('query_exclude_items');
-        if ($metadataItems != "")
-        {
-            $token = strtok($metadataItems, $tokenSeparator);
-            while ($token !== false) 
-            {
-                if (array_key_exists($token, $aQueryItems))
-                    unset($aQueryItems[$token]);
-                $token = strtok($tokenSeparator);
+            
+            // get all alias
+            while ( list($key,$val) = each($aQueryItems) ) {
+                $keyAlias = $oLayer->getMetaData("query_".$key."_alias");
+                trim($keyAlias);
+                if ($keyAlias != "") {
+                    $aQueryItems[$key] = $keyAlias;
+                }
             }
-        }
-        
-        // get all alias
-        while ( list($key,$val) = each($aQueryItems) )
-        {
-            $keyAlias = $oLayer->getMetaData("query_".$key."_alias");
-            trim($keyAlias);
-            if ($keyAlias != "")
-                $aQueryItems[$key] = $keyAlias;
-        }
-        $_SESSION[$mapName][$layerName]['query_items'] = $aQueryItems;
-    }
+            $_SESSION[$mapName][$layerName]['query_items'] = $aQueryItems;
+     }
+   
     $oShape = $oLayer->getShape($oResultSet->tileindex,$oResultSet->shapeindex);
 
     while ( list($key,$val) = each($oShape->values) )

Modified: sandbox/aboudreault/widgets/SelectionPanel.js
===================================================================
--- sandbox/aboudreault/widgets/SelectionPanel.js	2008-06-04 14:02:38 UTC (rev 1417)
+++ sandbox/aboudreault/widgets/SelectionPanel.js	2008-06-10 18:13:13 UTC (rev 1418)
@@ -33,47 +33,57 @@
 
 Fusion.Widget.SelectionPanel = OpenLayers.Class(Fusion.Widget,
 {
+    /**
+     * Property: previousIcon
+     * {String} The default image for Previous page button.
+     */
+    previousIcon: 'images/icon_back.gif',
+
+    /**
+     * Property: nextIcon
+     * {String} The default image for Previous page button.
+     */    
+    nextIcon: 'images/icon_forward.gif',
+    
     initialize : function(widgetTag) {
         //console.log('SelectionPanel.initialize');
-
         Fusion.Widget.prototype.initialize.apply(this, [widgetTag, false]);
 
-        this.defPrevTaskIcon = 'images/icon_back.gif';
-        this.defNextTaskIcon = 'images/icon_forward.gif';
 
         var json = widgetTag.extension;
+        if (json.PreviousImageUrl) this.previousIcon = json.PreviousImageUrl;
+        if (json.NextImageUrl) this.nextIcon = json.NextImageUrl;
         this.iResultsPerPage = json.ResultsPerPage ? json.ResultsPerPage[0] : 0;
         this.iResultsPerPage = parseInt(this.iResultsPerPage);
-        if (isNaN(this.iResultsPerPage))
+        if (isNaN(this.iResultsPerPage) || (this.iResultsPerPage < 0))
             this.iResultsPerPage = 0;
 
         if (json.SelectionRenderer)
-        {   // todo: use OpenLayers.Util.Try ?
-            try {
-                var renderer = eval(json.SelectionRenderer[0]);
-                if (renderer.prototype.CLASS_NAME && renderer.prototype.CLASS_NAME == "Fusion.Widget.SelectionPanel.SelectionRenderer") {
-                    this.renderer = new renderer(this);
-                } else if (typeof renderer == "function") {
-                    var renderFunction = renderer;
-                    this.renderer = new Fusion.Widget.SelectionPanel.SelectionRenderer(this);
-                    this.renderer.updateSelection = function() {
-                        this.getMap().getSelection(
-                            OpenLayers.Function.bind(renderFunction));
-                    };
-                    this.renderer.clearSelection = false;
-                }
+        {
+            var renderer = eval(json.SelectionRenderer[0]);
+            if (renderer && renderer.prototype.CLASS_NAME && renderer.prototype.CLASS_NAME == "Fusion.Widget.SelectionPanel.SelectionRenderer") {
+                this.renderer = new renderer(this);
+            } else if (typeof renderer == "function") {
+                var renderFunction = renderer;
+                this.renderer = new Fusion.Widget.SelectionPanel.SelectionRenderer(this);
+                this.renderer.updateSelection = function() {
+                    this.getMap().getSelection(
+                        OpenLayers.Function.bind(renderFunction));
+                };
+                this.renderer.clearSelection = false;
+            } else {
+                this.renderer = new Fusion.Widget.SelectionPanel.SelectionRendererDefault(this);
             }
-            catch (e) {}// this will occur if the renderer name provided is undefined.
         } else {
             this.renderer = new Fusion.Widget.SelectionPanel.SelectionRendererDefault(this);
         }
         this.iResultsPerPage = null; // handled by the renderer
-
+        
         if (this.renderer.updateSelection) {
             this.getMap().registerForEvent(Fusion.Event.MAP_SELECTION_ON,
                                            OpenLayers.Function.bind(this.renderer.updateSelection, this.renderer));
         }
-
+        
         if (this.renderer.clearSelection) {
             this.getMap().registerForEvent(Fusion.Event.MAP_SELECTION_OFF,
                                            OpenLayers.Function.bind(this.renderer.clearSelection, this.renderer));
@@ -83,7 +93,7 @@
 
 /* Class: Fusion.Widget.SelectionPanel.SelectionRenderer
  * This is a class designed to help users to create their own renderer
- *     for customize display results. 
+ * for customize display results. 
  */
 Fusion.Widget.SelectionPanel.SelectionRenderer = OpenLayers.Class(
 {
@@ -95,11 +105,11 @@
     oSelection: null,
 
     /**
-     * Property: iCurrentIndex
-     * {int} The index of the current position for pagination
+     * Property: aiCurrentIndex
+     * {Array(int)} The index of the current position for pagination.
      *
      */
-    iCurrentIndex: 0,
+    aiCurrentIndex: null,
     
     /**
      * Property: iResultsPerPage
@@ -117,11 +127,25 @@
     initialize: function(selectionPanel) {
         this.oSelectionPanel = selectionPanel;
         this.iResultsPerPage = selectionPanel.iResultsPerPage;
+        this.aiCurrentIndex = new Array();
     },
+    
+    /**
+     * Method: updateSelectionObject
+     * Helper method to update the aiCurrentIndex array for pagination.
+     *
+     */
+    updatePageIndexes: function() {
+        var nLayers = this.oSelection.getNumLayers();
+        for (var i=0; i<nLayers; i++) {
+            this.aiCurrentIndex[this.oSelection.getLayer(i).getName()] = 0;
+        }
+    },
 
     /**
      * Method: getNextPage
      * Get the next batches of features. Wrapper of the getPage() method.
+     * This method calcul the startIndex/endIndex of the next batch.
      *
      * Parameters:
      * selectionLayer - {<Fusion.SelectionObject.Layer>} The layer that contains 
@@ -131,27 +155,38 @@
      * {Array(Array)} An array of all features with their properties.
      */
     getNextPage: function(selectionLayer) {
-        if (selectionLayer && this.iCurrentIndex != selectionLayer.getNumElements()) {
-            var startIndex = 0;
-            var endIndex = selectionLayer.getNumElements();
-
-            if (this.iResultsPerPage != 0) {
-                var iTotalElement = endIndex;
-                startIndex = this.iCurrentIndex;
-                endIndex = startIndex + this.iResultsPerPage;
-                if (endIndex >= iTotalElement) {
-                    endIndex = iTotalElement;
-                }
-                this.iCurrentIndex = endIndex;
+        if (selectionLayer && (this.iResultsPerPage != 0)) {
+            var layerName = selectionLayer.getName();
+            if (this.aiCurrentIndex[layerName] >= selectionLayer.getNumElements()) {
+                this.aiCurrentIndex[layerName] = this.aiCurrentIndex[layerName] - this.iResultsPerPage;
             }
+            var iTotalElement = selectionLayer.getNumElements();
+            var startIndex = this.aiCurrentIndex[layerName];
+            var endIndex = startIndex + this.iResultsPerPage;
+            if (endIndex >= iTotalElement) {
+                endIndex = iTotalElement;
+            }
+      
+            if (startIndex < 0) {
+                startIndex = 0;
+            }
+            this.aiCurrentIndex[layerName] = endIndex;
+            
+            // if the last page doesn't contains "iResultsPerPage" elements. Fix the current index for the next getPreviousPage() call.
+            var diff = (endIndex - startIndex); 
+            if ( diff != this.iResultsPerPage) {
+                this.aiCurrentIndex[layerName] = this.aiCurrentIndex[layerName] + (this.iResultsPerPage - diff);
+            }
+            
             return this.getPage(selectionLayer, startIndex, endIndex);
         }
-        return false;
+        return this.getPage(selectionLayer, false, false);
     },
 
     /**
      * Method: getPreviousPage
      * Get the previous batches of features. Wrapper of the getPage() method.
+     * This method calcul the startIndex/endIndex of the previous batch.
      *
      * Parameters:
      * selectionLayer - {<Fusion.SelectionObject.Layer>} The layer that contains 
@@ -161,24 +196,21 @@
      * {Array(Array)} An array of all features with their properties.
      */
     getPreviousPage: function(selectionLayer) {
-        var page = false;
-        if (selectionLayer && this.iCurrentIndex != 0) {
-            var page = new Array();
-            var startIndex = 0;
-            var endIndex = selectionLayer.getNumElements();
-
-            if (this.iResultsPerPage != 0) {
-                var iTotalElement = endIndex;
-                startIndex = this.iCurrentIndex - (this.iResultsPerPage * 2);
-                endIndex = this.iCurrentIndex  - this.iResultsPerPage;
-                if (startIndex < 0) {
-                    startIndex = 0;
-                }
-                this.iCurrentIndex = endIndex;
+        var layerName = selectionLayer.getName();
+        if (selectionLayer && (this.aiCurrentIndex[layerName] != 0) && (this.iResultsPerPage != 0)) {
+            
+            var iTotalElement = selectionLayer.getNumElements();
+            var startIndex = this.aiCurrentIndex[layerName] - (this.iResultsPerPage * 2);
+            var endIndex = this.aiCurrentIndex[layerName]  - this.iResultsPerPage;
+            if (startIndex < 0) {
+                startIndex = 0;
+                endIndex = (iTotalElement < this.iResultsPerPage) ? iTotalElement : this.iResultsPerPage;
             }
+            
+            this.aiCurrentIndex[layerName] = endIndex;
             return this.getPage(selectionLayer, startIndex, endIndex);
         }
-        return page;
+        return this.getPage(selectionLayer, false, false);
     },
 
     /**
@@ -208,12 +240,14 @@
     getPage: function(selectionLayer, startIndex, endIndex) {
         var page = false;
         if (selectionLayer) {
-            var page = new Array();
+            page = new Array();
+            startIndex = startIndex ? startIndex : 0;
+            endIndex = endIndex ? endIndex : selectionLayer.getNumElements();
             var propNames = selectionLayer.getPropertyNames();
-            var index = 0;
+            var index =0;
             for (var i=startIndex; i<endIndex; i++, index++) {
+                page[index] = new Array();
                 for (var j=0; j<propNames.length; j++) {
-                    page[index] = new Array();
                     page[index][j] = selectionLayer.getElementValue(i, j);
                 }
             }
@@ -270,7 +304,7 @@
 
         d.appendChild(this.toolbar);
         d.appendChild(this.featureDiv);
-
+        
         Fusion.addWidgetStyleSheet(this.oSelectionPanel.getLocation() + 'SelectionPanel/SelectionPanel.css');
         this.oSelectionPanel.domObj.appendChild(d);
 },
@@ -295,10 +329,11 @@
             this.oSelection = oSelection[mapName];
             break;
         }
+        
         if (!this.oSelection) {
             return;
         }
-        
+ 
         //clear the layer list select box of any previous selections
         Element.removeClassName(this.featureDiv, 'noSelection');
         while (this.layerList.length>0) {
@@ -325,11 +360,13 @@
 
     renderSelectionFeatures: function() {
         var layerIdx = this.layerList.selectedIndex;
+        var layerObj = this.oSelection.getLayer(layerIdx);
+
         //clear the feature list select box of any previous selections
         while (this.featureList.length>0) {
           this.featureList.remove(this.featureList.options[0]);
         }
-        var layerObj = this.oSelection.getLayer(layerIdx);
+
         var nElements = layerObj.getNumElements();
         for (var i=0; i<nElements; i++) {
             var opt = new Option(i+1, i);
@@ -377,4 +414,139 @@
         this.featureDiv.innerHTML = '';
         this.featureDiv.appendChild(table);
     }
-});
\ No newline at end of file
+});
+
+
+
+/* Class: Fusion.Widget.SelectionPanel.SelectionRendererHorizontal
+ * This class provide a alternate behavior for the selection panel.
+ * Generate a table which have one feature per row.
+ * 
+ */
+Fusion.Widget.SelectionPanel.SelectionRendererHorizontal = OpenLayers.Class(Fusion.Widget.SelectionPanel.SelectionRenderer,
+{
+    initialize : function(selectionPanel) {
+        Fusion.Widget.SelectionPanel.SelectionRenderer.prototype.initialize.apply(this, [selectionPanel]);
+        
+        var d = document.createElement('div');
+        this.featureDiv = document.createElement('div');
+        this.featureDiv.innerHTML = 'No Selection';
+        Element.addClassName(this.featureDiv, 'noSelection');
+        d.appendChild(this.featureDiv);
+
+        if (this.iResultsPerPage != 0) {
+            this.previousButton = document.createElement('img');
+            this.previousButton.src = this.oSelectionPanel.previousIcon;
+            this.previousButton.style.position = "absolute";
+            this.previousButton.style.left = "0px";
+            this.previousButton.style.padding = "3px";
+            Event.observe(this.previousButton, 'click',
+                          OpenLayers.Function.bind(this.renderLayers, this, 'prev'));
+            this.nextButton = document.createElement('img');
+            this.nextButton.src = this.oSelectionPanel.nextIcon;
+            this.nextButton.style.position = "absolute";
+            this.nextButton.style.right = "0px";
+            this.nextButton.style.padding = "3px";
+            Event.observe(this.nextButton, 'click',
+                          OpenLayers.Function.bind(this.renderLayers, this, 'next'));
+            
+            d.appendChild(this.previousButton);
+            d.appendChild(this.nextButton);
+        }
+
+        Element.addClassName(this.featureDiv, 'selectionPanelContent');
+        Fusion.addWidgetStyleSheet(this.oSelectionPanel.getLocation() + 'SelectionPanel/SelectionPanel.css');
+        this.oSelectionPanel.domObj.appendChild(d);
+    },
+
+    updateSelection: function() {
+        this.getMap().getSelection(
+            OpenLayers.Function.bind(this.renderSelection, this));
+    },
+
+    clearSelection: function() {
+        this.oSelection = null;
+        Element.addClassName(this.featureDiv, 'noSelection');
+        this.featureDiv.innerHTML = OpenLayers.i18n('noSelection');
+    },
+    
+    renderSelection: function(oSelection) {
+        //TODO: this just gets the first map, we need them all
+        this.oSelection = null;
+        for (var mapName in oSelection) {
+            this.oSelection = oSelection[mapName];
+            break;
+        }
+        this.updatePageIndexes();
+        this.renderLayers("next");
+    },
+    
+    renderLayers: function(renderingPage) {
+        if (!this.oSelection) {
+            return;
+        }
+        
+        Element.removeClassName(this.featureDiv, 'noSelection');
+        this.featureDiv.innerHTML = '';
+        
+        var nLayers = this.oSelection.getNumLayers();
+        for (var i=0; i<nLayers; i++) {
+            var table = document.createElement('table');
+            table.style.borderLeft = "1px solid #CCCCCC";
+            table.style.marginBottom = "10px";
+            var layerObj = this.oSelection.getLayer(i);
+            var aNames = layerObj.getPropertyNames();
+            //find the legend label from the Map layer objects
+            var mapLayers = this.getMap().aMaps[0].aLayers; //TODO: allow multiple maps
+            var labelName = layerObj.getName();
+            for (var j=0; j<mapLayers.length; ++j) {
+                if (mapLayers[j].layerName == labelName) {
+                    labelName = mapLayers[j].legendLabel;
+                    break;
+                }
+            }
+            
+            var thead = document.createElement('thead');
+            var tr = document.createElement('tr');
+            var th = document.createElement('th');
+            th.innerHTML = labelName;
+            th.colSpan=aNames.length;
+            th.style.textAlign = "center";
+            tr.appendChild(th);
+            thead.appendChild(tr);
+            tr = document.createElement('tr');
+            for (var j=0; j<aNames.length; j++) {
+                th = document.createElement('th');
+                th.innerHTML = aNames[j];
+                th.style.textAlign = "center";
+                tr.appendChild(th);
+            }
+            thead.appendChild(tr);
+            table.appendChild(thead);
+            var tbody = document.createElement('tbody');
+            var page = (renderingPage == 'next') ? this.getNextPage(layerObj): this.getPreviousPage(layerObj);
+            this.renderFeatures(page,tbody);
+            table.appendChild(tbody);
+            this.featureDiv.appendChild(table);
+        }
+
+    },
+
+    renderFeatures: function(page, dom) {
+        if (!page)
+            return;
+
+        for (var i=0; i<page.length; i++) {
+            var tr = document.createElement('tr');
+            if (i%2) {
+                Element.addClassName(tr, 'oddRow');
+            }
+            for (var j=0; j<page[i].length; j++) {
+                var td = document.createElement('td');
+                td.innerHTML = page[i][j];
+                tr.appendChild(td);
+            }
+            dom.appendChild(tr);            
+        }
+    }
+});



More information about the fusion-commits mailing list