← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7456: support online fetching of OUWT tree when partial loading is enabled (only in browser memory for ...

 

------------------------------------------------------------
revno: 7456
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2012-06-29 18:11:05 +0300
message:
  support online fetching of OUWT tree when partial loading is enabled (only in browser memory for now, sessionStorage fix is coming)
added:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonOrganisationUnitTreePartial.vm
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java
  dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonOrganisationUnitTreePartial.vm'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonOrganisationUnitTreePartial.vm	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ajax/jsonOrganisationUnitTreePartial.vm	2012-06-29 15:11:05 +0000
@@ -0,0 +1,5 @@
+{"organisationUnits": {
+#foreach( $unit in $organisationUnits )
+"${unit.id}":{"id":${unit.id},"n":"$!encoder.jsonEncode( ${unit.name} )",#if( $unit.parent )"pid":${unit.parent.id},#end"c":[#foreach( $child in $unit.sortedChildren )${child.id}#if( $velocityCount < $unit.children.size() ),#end#end]}
+#if( $velocityCount < $organisationUnits.size() ),#end#end
+}}
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js	2012-06-21 05:55:38 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js	2012-06-29 15:11:05 +0000
@@ -279,11 +279,10 @@
     this.select = function ( unitId )
     {
         var $linkTag = $( "#" + getTagId( unitId ) ).find( "a" ).eq( 0 );
+        var selected = JSON.parse( sessionStorage[getTagId( "Selected" )] );
 
         if ( $linkTag.hasClass( "selected" ) && ( unselectAllowed || rootUnselectAllowed ) )
         {
-            var selected = JSON.parse( sessionStorage[getTagId( "Selected" )] );
-
             if ( rootUnselectAllowed && !unselectAllowed && !multipleSelectionAllowed )
             {
                 var roots = JSON.parse( localStorage[getTagId( "Roots" )] );
@@ -328,8 +327,6 @@
         {
             if ( multipleSelectionAllowed )
             {
-                var selected = sessionStorage[getTagId( "Selected" )];
-
                 if ( selected )
                 {
                     selected = JSON.parse( selected );
@@ -419,8 +416,6 @@
         return 'orgUnit' + unitId;
     }
 
-    ;
-
     this.findByCode = function ()
     {
         var name = $( '#searchField' ).val();
@@ -503,20 +498,19 @@
     this.toggle = function ( unitId )
     {
         var children = $( "#" + getTagId( unitId ) ).find( "ul" );
+        var ou = organisationUnits[unitId];
 
         if ( children.length < 1 || !isVisible( children[0] ) )
         {
-            var ou = organisationUnits[unitId];
             processExpand( ou );
         }
         else
         {
-            var ou = organisationUnits[unitId];
             processCollapse( ou );
         }
     };
 
-    selectOrgUnits = function ( ous )
+    var selectOrgUnits = function ( ous )
     {
         $.each( ous, function ( i, item )
         {
@@ -524,12 +518,12 @@
         } );
     };
 
-    selectOrgUnit = function ( ou )
+    var selectOrgUnit = function ( ou )
     {
         $( "#" + getTagId( ou ) + " > a" ).addClass( "selected" );
     };
 
-    expandTreeAtOrgUnits = function ( ous )
+    var expandTreeAtOrgUnits = function ( ous )
     {
         $.each( ous, function ( i, item )
         {
@@ -537,7 +531,7 @@
         } );
     };
 
-    expandTreeAtOrgUnit = function ( ou )
+    var expandTreeAtOrgUnit = function ( ou )
     {
         if ( organisationUnits[ou] == null )
         {
@@ -624,8 +618,6 @@
         setToggle( $parentTag, false );
     }
 
-    ;
-
     function processExpand( parent )
     {
         var $parentTag = $( "#" + getTagId( parent.id ) );
@@ -633,7 +625,7 @@
 
         if ( $children.length < 1 )
         {
-            createChildren( $parentTag, parent );
+            getAndCreateChildren( $parentTag, parent );
         }
         else
         {
@@ -642,7 +634,28 @@
         }
     }
 
-    ;
+    function getAndCreateChildren(parentTag, parent)
+    {
+        if(parent.c !== undefined)
+        {
+            if(organisationUnits[parent.c[0]] !== undefined)
+            {
+                createChildren( parentTag, parent );
+            }
+            else
+            {
+                $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?parentId=' + parent.id,
+                function ( data, textStatus, jqXHR )
+                    {
+                        // load additional organisationUnits into sessionStorage
+                        $.extend(organisationUnits, data.organisationUnits);
+                        createChildren( parentTag, parent );
+                    }
+                );
+            }
+        }
+
+    }
 
     function createChildren( parentTag, parent )
     {
@@ -651,7 +664,11 @@
         $.each( parent.c, function ( i, item )
         {
             var ou = organisationUnits[item];
-            $childrenTag.append( createTreeElementTag( ou ) );
+
+            if(ou !== undefined)
+            {
+                $childrenTag.append( createTreeElementTag( ou ) );
+            }
         } );
 
         setVisible( $childrenTag, true );
@@ -660,8 +677,6 @@
         $( parentTag ).append( $childrenTag );
     }
 
-    ;
-
     function createTreeElementTag( ou )
     {
         var $toggleTag = $( "<span/>" );
@@ -692,8 +707,6 @@
         return $childTag;
     }
 
-    ;
-
     function setToggle( unitTag, expanded )
     {
         var $toggleTag = $( unitTag ).find( "span" );
@@ -709,8 +722,6 @@
         }
     }
 
-    ;
-
     function setVisible( tag, visible )
     {
         if ( visible )
@@ -723,47 +734,33 @@
         }
     }
 
-    ;
-
     function isVisible( tag )
     {
         return $( tag ).is( ":visible" );
     }
 
-    ;
-
     function getTagId( unitId )
     {
         return 'orgUnit' + unitId;
     }
 
-    ;
-
     function getToggleExpand()
     {
         return getToggleImage().attr( "src", "../images/colapse.png" ).attr( "alt", "[+]" );
     }
 
-    ;
-
     function getToggleCollapse()
     {
         return getToggleImage().attr( "src", "../images/expand.png" ).attr( "alt", "[-]" );
     }
 
-    ;
-
     function getToggleBlank()
     {
         return getToggleImage().attr( "src", "../images/transparent.gif" ).removeAttr( "alt" );
     }
 
-    ;
-
     function getToggleImage()
     {
         return $( "<img/>" ).attr( "width", 9 ).attr( "height", 9 );
     }
-
-    ;
 }

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java	2012-06-29 13:45:31 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/commons/action/GetOrganisationUnitTreeAction.java	2012-06-29 15:11:05 +0000
@@ -117,6 +117,13 @@
         return versionOnly;
     }
 
+    private Integer parentId;
+
+    public void setParentId( Integer parentId )
+    {
+        this.parentId = parentId;
+    }
+
     // -------------------------------------------------------------------------
     // Action implementation
     // -------------------------------------------------------------------------
@@ -124,6 +131,18 @@
     public String execute()
         throws Exception
     {
+        if ( parentId != null )
+        {
+            OrganisationUnit parent = organisationUnitService.getOrganisationUnit( parentId );
+
+            if ( parent != null )
+            {
+                organisationUnits.addAll( organisationUnitService.getOrganisationUnitWithChildren( parent.getId() ) );
+            }
+
+            return "partial";
+        }
+
         Collection<OrganisationUnit> userOrganisationUnits;
 
         User user = currentUserService.getCurrentUser();
@@ -160,11 +179,11 @@
 
             if ( offlineOrganisationUnitLevel == null )
             {
-                offlineOrganisationUnitLevel = organisationUnitService.getOrganisationUnitLevel( size );
+                offlineOrganisationUnitLevel = organisationUnitService.getOrganisationUnitLevelByLevel( size );
             }
 
             int minLevel = rootOrganisationUnits.get( 0 ).getLevel();
-            int maxLevel = organisationUnitService.getOrganisationUnitLevel( size ).getLevel();
+            int maxLevel = organisationUnitService.getOrganisationUnitLevelByLevel( size ).getLevel();
             int total = minLevel + offlineOrganisationUnitLevel.getLevel() - 1;
 
             if ( total > offlineOrganisationUnitLevel.getLevel() )

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml'
--- dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml	2012-06-28 09:21:53 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/resources/dhis-web-commons.xml	2012-06-29 15:11:05 +0000
@@ -755,6 +755,7 @@
 
     <action name="getOrganisationUnitTree" class="org.hisp.dhis.commons.action.GetOrganisationUnitTreeAction">
       <result name="success" type="velocity-json">/dhis-web-commons/ajax/jsonOrganisationUnitTree.vm</result>
+      <result name="partial" type="velocity-json">/dhis-web-commons/ajax/jsonOrganisationUnitTreePartial.vm</result>
       <param name="onExceptionReturn">plainTextError</param>
     </action>