import { NavigationItem } from "./navigation-item" ; import { Observable } from "tns-core-modules/data/observable" ; import { RadSideDrawer } from "nativescript-ui-sidedrawer" ; import { Frame , View } from "tns-core-modules/ui/frame" ; export class PageViewModel extends Observable { public sideDrawer : RadSideDrawer ; constructor ( parent ? , subitems ? , hasBack ? , sideDrawer ? ) { super ( ) ; this . _currentParent = parent ; this . _currentSubItems = subitems ; this . _hasBackNavigation = hasBack ; this . sideDrawer = sideDrawer ; } get _selectedIndex ( ) : number { return this . get ( "selectedIndex" ) ; } set _selectedIndex ( value ) { this . set ( "selectedIndex" , value ) ; } get _currentSubItems ( ) : Array < NavigationItem > { return this . get ( "currentSubItems" ) ; } set _currentSubItems ( value ) { this . set ( "currentSubItems" , value ) ; } get _currentParent ( ) { return this . get ( "currentParent" ) ; } set _currentParent ( value ) { this . set ( "currentParent" , value ) ; } get _actionBarTitle ( ) { return this . get ( "actionBarTitle" ) ; } set _actionBarTitle ( value ) { this . set ( "actionBarTitle" , value ) ; } get _hasBackNavigation ( ) { return this . get ( "hasBackNavigation" ) ; } set _hasBackNavigation ( value ) { this . set ( "hasBackNavigation" , value ) ; } public onBackTap ( args ) { Frame . topmost ( ) . goBack ( ) ; } public onNavigationItemTap ( args ) { let itemIndex = args . index ; let tappedItem = this . _currentSubItems [ itemIndex ] ; if ( args . object . id === "sidedrawer-list" ) { args . object . eachChildView ( childView => { this . _toggleItemSelected ( childView . getViewById ( "item-container" ) , false ) ; } ) ; this . _toggleItemSelected ( args . view . getViewById ( "item-container" ) , true ) ; } if ( tappedItem . module === undefined ) { this . _hasBackNavigation = tappedItem . parent !== undefined ; } let hasBack = tappedItem . parent !== undefined ; let nextPageContext = new PageViewModel ( tappedItem , tappedItem . subItems , hasBack , this . sideDrawer ) ; this . sideDrawer . closeDrawer ( ) ; if ( tappedItem . subItems . length > 0 ) { Frame . topmost ( ) . navigate ( { moduleName : "navigation/category-list-nested-page" , context : nextPageContext } ) ; } else { if ( tappedItem . module ) { Frame . topmost ( ) . navigate ( { moduleName : tappedItem . module , context : tappedItem } ) ; } } } public onCloseDrawerTap ( ) { this . sideDrawer . closeDrawer ( ) ; } _toggleItemSelected ( view , isSelected ) { view . className = isSelected ? "sidedrawer-list-item active" : "sidedrawer-list-item" ; } }