Recently in one of our sencha touch project we have created Navigation Drawer for sencha touch. In this blog I will explain how to create it.
Let's first understand what is navigation drawer. We are all familiar with Facebook slide navigation. It has button on top left corner of toolbar, when you tap on it, the content in center slide to right and menu opens with left to right animation. When you again tap on that menu is closed with right to left navigation and center content slides to left. Navigation drawer is introduced in Android 4.0. It's slight variation of slide menu. Here the center content does not slide left or right but the menu comes on top of slide content. However the top toolbar is still accessible so user can still close the menu. Now lets see how to create this in sencha touch. Please note that this is the one way we used to create navigation drawer, there could be other options as well.
First lets create a list which will act as navigation menu.
Ext.define('SlideNav.view.Navigation', {
extend: 'Ext.List',
xtype: 'navigation',
modal: true,
hideOnMaskTap: false,
requires : ['Ext.data.Store'],
config: {
width: 250,
itemTpl : '{title}',
data : [
{
title : 'Item 1'
},
{
title : 'Item 2'
},
{
title : 'Item 3'
}
]
}
});
As you can see in above code we created a list and set it as modal element with modal config. List extends Ext.container so we can open it as modal element. This is our navigation menu. Now lets add a button on top left corner of our toolbar and create the main view.
Ext.define('SlideNav.view.Main', {
extend: 'Ext.Container',
xtype: 'main',
config: {
style: {
zIndex: -1,
position: 'absolute'
},
layout:{
type: 'card',
align: 'stretch'
},
items: [
{
xtype: 'toolbar',
docked: 'top',
title: 'Slide Navigation',
items: [
{
xtype: 'button',
iconCls: 'list',
ui: 'plain',
itemId: 'slideNavBtn',
id: 'slideNavBtn'
}
]
},
{
xtype: 'panel',
itemId: 'slideContainer',
layout: 'card',
id: 'slideContainer',
items: [
{
xtype: 'panel',
html: 'Hello Welcome to The Design Shop.Sencha Touch is very good framework.'
}
]
}
]
}
});
So this is our main view and it has toolbar with top navigation button. Now lets add tap event for it in controller and add logic to open and close the menu.
Ext.define('SlideNav.controller.App',{
extend: 'Ext.app.Controller',
config:{
refs:{
main : 'main',
navigation : 'navigation',
navBtn : '#slideNavBtn'
},
control : {
navBtn : {
tap : 'toggleNav'
}
}
},
init: function() {
this.toggle = 0;
},
toggleNav : function(){
var me = this;
if(!me.getNavigation()) {
Ext.create('SlideNav.view.Navigation');
Ext.Viewport.add(me.getNavigation());
me.getNavigation().show();
}
if(this.toggle == 0) {
Ext.Animator.run({
element: me.getNavigation().element,
duration: 500,
easing: 'ease-in',
preserveEndState: true,
from: {
left: -250
},
to: {
left: 0
}
});
this.toggle = 1;
}
else {
Ext.Animator.run({
element: me.getNavigation().element,
duration: 500,
easing: 'ease-in',
preserveEndState: true,
from: {
left: 0
},
to: {
left: -250
}
});
this.toggle = 0;
}
}
});
As you can see in above code we are using one controller variable toggle to know the state of menu and and on tap of it first we create the navigation and menu and add it to viewport and then we are using Ext.Animator to open it with animation. So basically we are setting left property from -250 to 0 as 250 is the width of the menu. You can change it according to your requirement or set dynamic width here.
Let's first understand what is navigation drawer. We are all familiar with Facebook slide navigation. It has button on top left corner of toolbar, when you tap on it, the content in center slide to right and menu opens with left to right animation. When you again tap on that menu is closed with right to left navigation and center content slides to left. Navigation drawer is introduced in Android 4.0. It's slight variation of slide menu. Here the center content does not slide left or right but the menu comes on top of slide content. However the top toolbar is still accessible so user can still close the menu. Now lets see how to create this in sencha touch. Please note that this is the one way we used to create navigation drawer, there could be other options as well.
First lets create a list which will act as navigation menu.
Ext.define('SlideNav.view.Navigation', {
extend: 'Ext.List',
xtype: 'navigation',
modal: true,
hideOnMaskTap: false,
requires : ['Ext.data.Store'],
config: {
width: 250,
itemTpl : '{title}',
data : [
{
title : 'Item 1'
},
{
title : 'Item 2'
},
{
title : 'Item 3'
}
]
}
});
As you can see in above code we created a list and set it as modal element with modal config. List extends Ext.container so we can open it as modal element. This is our navigation menu. Now lets add a button on top left corner of our toolbar and create the main view.
Ext.define('SlideNav.view.Main', {
extend: 'Ext.Container',
xtype: 'main',
config: {
style: {
zIndex: -1,
position: 'absolute'
},
layout:{
type: 'card',
align: 'stretch'
},
items: [
{
xtype: 'toolbar',
docked: 'top',
title: 'Slide Navigation',
items: [
{
xtype: 'button',
iconCls: 'list',
ui: 'plain',
itemId: 'slideNavBtn',
id: 'slideNavBtn'
}
]
},
{
xtype: 'panel',
itemId: 'slideContainer',
layout: 'card',
id: 'slideContainer',
items: [
{
xtype: 'panel',
html: 'Hello Welcome to The Design Shop.Sencha Touch is very good framework.'
}
]
}
]
}
});
So this is our main view and it has toolbar with top navigation button. Now lets add tap event for it in controller and add logic to open and close the menu.
Ext.define('SlideNav.controller.App',{
extend: 'Ext.app.Controller',
config:{
refs:{
main : 'main',
navigation : 'navigation',
navBtn : '#slideNavBtn'
},
control : {
navBtn : {
tap : 'toggleNav'
}
}
},
init: function() {
this.toggle = 0;
},
toggleNav : function(){
var me = this;
if(!me.getNavigation()) {
Ext.create('SlideNav.view.Navigation');
Ext.Viewport.add(me.getNavigation());
me.getNavigation().show();
}
if(this.toggle == 0) {
Ext.Animator.run({
element: me.getNavigation().element,
duration: 500,
easing: 'ease-in',
preserveEndState: true,
from: {
left: -250
},
to: {
left: 0
}
});
this.toggle = 1;
}
else {
Ext.Animator.run({
element: me.getNavigation().element,
duration: 500,
easing: 'ease-in',
preserveEndState: true,
from: {
left: 0
},
to: {
left: -250
}
});
this.toggle = 0;
}
}
});
As you can see in above code we are using one controller variable toggle to know the state of menu and and on tap of it first we create the navigation and menu and add it to viewport and then we are using Ext.Animator to open it with animation. So basically we are setting left property from -250 to 0 as 250 is the width of the menu. You can change it according to your requirement or set dynamic width here.
No comments:
Post a Comment