Skip to content

Commit

Permalink
TopMenu: Add bead TopMenuSortingBead to give user ability to sort if …
Browse files Browse the repository at this point in the history
…needed

(reference #40)
  • Loading branch information
piotrzarzycki21 committed Nov 13, 2023
1 parent d20154e commit 467731d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
xmlns:js="library://ns.apache.org/royale/basic"
xmlns:html="library://ns.apache.org/royale/html" xmlns:model="classes.topMenu.model.*" xmlns:helpers="classes.topMenu.helpers.*"
initComplete="onTopMenuInitComplete(event)">
<j:beads>
<js:ContainerDataBinding />
</j:beads>
<j:model>
<model:TopMenuModel localId="menuModel"/>
</j:model>
Expand All @@ -17,11 +14,13 @@
<fx:Script>
<![CDATA[
import classes.topMenu.classes.ChangeSourceConst;
import classes.topMenu.components.beads.TopMenuSortingBead;
import classes.topMenu.events.TopMenuEvent;
import classes.topMenu.helpers.SelectedIndexManager;
import classes.topMenu.helpers.TopMenuUtils;
import classes.topMenu.model.TopMenuVO;
import org.apache.royale.binding.ContainerDataBinding;
import org.apache.royale.collections.ArrayList;
import org.apache.royale.icons.MaterialIcon;
import org.apache.royale.net.HTTPConstants;
Expand Down Expand Up @@ -69,6 +68,12 @@
}
}
override public function addedToParent():void
{
this.addBead(new ContainerDataBinding());
super.addedToParent();
}
public function navigateToItem(item:Object):void
{
if (item.parent == null)
Expand Down Expand Up @@ -100,24 +105,7 @@
this.selectedIndexManager.refreshSelectedIndex(this.rootMenu.selectedIndex, this.subMenu.selectedIndex);
}
}
public function sortTree(nodes:Array):void
{
// Sorting function
nodes.sort(function(a:TopMenuVO, b:TopMenuVO):int
{
// Nodes with children come first
if (a.children.length > 0 && b.children.length == 0) return -1;
if (a.children.length == 0 && b.children.length > 0) return 1;
// Then sort by label
if (a.label < b.label) return -1;
if (a.label > b.label) return 1;
return 0;
});
}
public function initializeMenuModel(data:Object):void
{
if (!data && this.model.data)
Expand Down Expand Up @@ -379,7 +367,12 @@
{
if (!this.rootMenu) return;
this.sortTree(menu);
var sortMenuBead:TopMenuSortingBead = this.getBeadByType(TopMenuSortingBead) as TopMenuSortingBead;
if (sortMenuBead)
{
sortMenuBead.sortMenu(menu);
}
var dp:ArrayList = new ArrayList(menu);
this.rootMenu.dataProvider = dp;
this.rootMenu.selectedIndex = 0;
Expand All @@ -388,13 +381,19 @@
{
this.selectedIndexManager.updateSource(dp.source);
}
}
private function setSubMenuItems(menu:Array):void
{
if (!this.subMenu) return;
this.sortTree(menu);
var sortMenuBead:TopMenuSortingBead = this.getBeadByType(TopMenuSortingBead) as TopMenuSortingBead;
if (sortMenuBead)
{
sortMenuBead.sortMenu(menu);
}
this.subMenu.dataProvider = new ArrayList(menu);
this.subMenu.visible = menu.length > 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package classes.topMenu.components.beads
{
import org.apache.royale.core.IBead;
import org.apache.royale.core.IStrand;
import classes.topMenu.model.TopMenuVO;
import org.apache.royale.core.Bead;

public class TopMenuSortingBead extends Bead
{
public function TopMenuSortingBead()
{
super();
}

override public function set strand(value:IStrand):void
{
super.strand = value;
}

public function eq(a:TopMenuVO, b:TopMenuVO):int
{
// Nodes with children come first
if (a.children.length > 0 && b.children.length == 0) return -1;
if (a.children.length == 0 && b.children.length > 0) return 1;

// Then sort by label
if (a.label < b.label) return -1;
if (a.label > b.label) return 1;
return 0;
}

public function sortMenu(menu:Array):void
{
if (menu && menu.length > 0)
{
menu.sort(eq);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package classes.topMenu.helpers
import org.apache.royale.jewel.Navigation;
import org.apache.royale.jewel.supportClasses.drawer.DrawerContent;

public class CloseMenuOnNavigationLinkClick implements IBead
public class CloseMenuOnNavigationLinkClick implements IBead
{
private var host:Drawer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<j:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:j="library://ns.apache.org/royale/jewel"
xmlns:js="library://ns.apache.org/royale/basic" xmlns:breadcrump="classes.breadcrump.*" xmlns:topMenu="classes.topMenu.*" xmlns:html="library://ns.apache.org/royale/html" xmlns:controls="view.controls.*"
xmlns:js="library://ns.apache.org/royale/basic" xmlns:breadcrump="classes.breadcrump.*" xmlns:topMenu="classes.topMenu.*" xmlns:html="library://ns.apache.org/royale/html" xmlns:controls="view.controls.*" xmlns:beads="classes.topMenu.components.beads.*"
implements="interfaces.IBrowseMyServerView" gap="1" currentState="selectedDatabaseState">
<j:beads>
<js:ContainerDataBinding />
Expand Down Expand Up @@ -84,6 +84,9 @@
<topMenu:style>
<js:SimpleCSSStyles marginLeft="-10" paddingBottom="64"/>
</topMenu:style>
<topMenu:beads>
<beads:TopMenuSortingBead />
</topMenu:beads>
</topMenu:TopMenu>
<j:Card>
<j:style>
Expand Down

0 comments on commit 467731d

Please sign in to comment.