Hi all, i just recently started playing around with the BaNano framework, I tried creating a custom view, basically just a simple Top Navigation Bar.
The question I have is simple, how can I handle the click events on any of the Menu items when clicked?
Below is my code for the custom View class:
The result view looks like this:
I tried the Method:
But i don't see the event being raised either inside the Custom class or the Main class where the view is being initialized.
I realize i may be missing something as I just started playing with this, but already spent pretty much the whole day trying to figure it out without much luck.
Any ideas, thoughts?
Thanks,
Walter
The question I have is simple, how can I handle the click events on any of the Menu items when clicked?
Below is my code for the custom View class:
topnav:
'Custom BANano View class
'Uncomment the events you want to show to the user and implement the HandleEvents in DesignerCreateView
'#Event: Focus (event As BANanoEvent)
'#Event: Blur (event As BANanoEvent)
'#Event: Resize (event As BANanoEvent)
'#Event: Scroll (event As BANanoEvent)
'#Event: Keydown (event As BANanoEvent)
'#Event: KeyPress (event As BANanoEvent)
'#Event: KeyUp (event As BANanoEvent)
#Event: Click (event As BANanoEvent)
'#Event: ContextMenu (event As BANanoEvent)
'#Event: Dblclick (event As BANanoEvent)
'#Event: MouseDown (event As BANanoEvent)
'#Event: MouseEnter (event As BANanoEvent)
'#Event: MouseLeave (event As BANanoEvent)
'#Event: MouseMove (event As BANanoEvent)
'#Event: MouseOver (event As BANanoEvent)
'#Event: MouseOut (event As BANanoEvent)
'#Event: MouseUp (event As BANanoEvent)
'#Event: Wheel (event As BANanoEvent)
'#Event: Drag (event As BANanoEvent)
'#Event: DragEnd (event As BANanoEvent)
'#Event: DragEnter (event As BANanoEvent)
'#Event: DragStart (event As BANanoEvent)
'#Event: DragLeave (event As BANanoEvent)
'#Event: DragOver (event As BANanoEvent)
'#Event: Drop (event As BANanoEvent)
'#Event: TouchCancel (event As BANanoEvent)
'#Event: TouchEnd (event As BANanoEvent)
'#Event: TouchEnter (event As BANanoEvent)
'#Event: TouchLeave (event As BANanoEvent)
'#Event: TouchMove (event As BANanoEvent)
'#Event: TouchStart (event As BANanoEvent)
'#Event: Change (event As BANanoEvent)
' Properties that will be show in the ABStract Designer. They will be passed in the props map in DesignerCreateView (Case Sensitive!)
#DesignerProperty: Key: Classes, DisplayName: Classes, FieldType: String, DefaultValue: , Description: Classes added to the HTML tag.
#DesignerProperty: Key: Style, DisplayName: Style, FieldType: String, DefaultValue: , Description: Styles added to the HTML tag. Must be a json String.
#DesignerProperty: Key: MarginLeft, DisplayName: Margin Left, FieldType: String, DefaultValue: , Description: Margin Left
#DesignerProperty: Key: MarginRight, DisplayName: Margin Right, FieldType: String, DefaultValue: , Description: Margin Right
#DesignerProperty: Key: MarginTop, DisplayName: Margin Top, FieldType: String, DefaultValue: , Description: Margin Top
#DesignerProperty: Key: MarginBottom, DisplayName: Margin Bottom, FieldType: String, DefaultValue: , Description: Margin Bottom
#DesignerProperty: Key: PaddingLeft, DisplayName: Padding Left, FieldType: String, DefaultValue: , Description: Padding Left
#DesignerProperty: Key: PaddingRight, DisplayName: Padding Right, FieldType: String, DefaultValue: , Description: Padding Right
#DesignerProperty: Key: PaddingTop, DisplayName: Padding Top, FieldType: String, DefaultValue: , Description: Padding Top
#DesignerProperty: Key: PaddingBottom, DisplayName: Padding Bottom, FieldType: String, DefaultValue: , Description: Padding Bottom
#DesignerProperty: Key: Menu, DisplayName: Menu, FieldType: String, DefaultValue: Home, List: Home|Report, Description: Adds Menus to Navigation Bar.
Sub Class_Globals
Private BANano As BANano 'ignore
Private mName As String 'ignore
Private mEventName As String 'ignore
Private mCallBack As Object 'ignore
Private mTarget As BANanoElement 'ignore
Private mElement As BANanoElement 'ignore
Private mClasses As String = ""
Private mStyle As String = ""
Public MarginLeft As String = ""
Public MarginRight As String = ""
Public MarginTop As String = ""
Public MarginBottom As String = ""
Public PaddingLeft As String = ""
Public PaddingRight As String = ""
Public PaddingTop As String = ""
Public PaddingBottom As String = ""
End Sub
Public Sub Initialize (CallBack As Object, Name As String, EventName As String)
mName = Name
mEventName = EventName.ToLowerCase
mCallBack = CallBack
Log("eventName: " & mEventName)
End Sub
' this is the place where you create the view in html and run initialize javascript
Public Sub DesignerCreateView (Target As BANanoElement, Props As Map)
mTarget = Target
If Props <> Null Then
mClasses = Props.Get("Classes")
mStyle = Props.Get("Style")
MarginLeft = Props.Get("MarginLeft")
MarginRight = Props.Get("MarginRight")
MarginTop = Props.Get("MarginTop")
MarginBottom = Props.Get("MarginBottom")
PaddingLeft = Props.Get("PaddingLeft")
PaddingRight = Props.Get("PaddingRight")
PaddingTop = Props.Get("PaddingTop")
PaddingBottom = Props.Get("PaddingBottom")
End If
Dim exStyle As String = BuildExStyle
mTarget.Append($"<div class="${mName}">"$)
Dim menu As String = Props.Get("Menu")
mTarget.Append($"<a class="active" href=$"#news">${menu}</a>"$)
mTarget.Append("</div>")
mElement = mTarget.Get("#" & mName)
'' defining events is very simple. Note that it has to be run AFTER adding it to the HTML DOM! eventName must be lowercase!
mElement.HandleEvents("click", Me, mEventName & "_click")
'mElement.HandleEvents("mouseenter", Me, "mouseenter")
End Sub
public Sub AddToParent(targetID As String)
mTarget = BANano.GetElement("#" & targetID.ToLowerCase)
DesignerCreateView(mTarget, Null)
End Sub
public Sub Remove()
mTarget.Empty
BANano.SetMeToNull
End Sub
public Sub Trigger(event As String, params() As String)
If mElement <> Null Then
mElement.Trigger(event, params)
End If
End Sub
public Sub BuildExStyle() As String
Dim sb As StringBuilder
sb.Initialize
If MarginLeft <> "" Then sb.Append("margin-left: " & MarginLeft & ";")
If MarginRight <> "" Then sb.Append("margin-right: " & MarginRight & ";")
If MarginTop <> "" Then sb.Append("margin-top: " & MarginTop & ";")
If MarginBottom <> "" Then sb.Append("margin-bottom: " & MarginBottom & ";")
If PaddingLeft <> "" Then sb.Append("padding-left: " & PaddingLeft & ";")
If PaddingRight <> "" Then sb.Append("padding-right: " & PaddingRight & ";")
If PaddingTop <> "" Then sb.Append("padding-top: " & PaddingTop & ";")
If PaddingBottom <> "" Then sb.Append("padding-bottom: " & PaddingBottom & ";")
Return sb.ToString
End Sub
#Region Property Getters and Setters
public Sub setClasses(Classes As String)
If mElement <> Null Then
mElement.AddClass(Classes)
End If
mClasses = Classes
End Sub
public Sub getClasses() As String
Return mClasses
End Sub
' must be a json string
' e.g. $"{ "width": "200px", "height": "200px", "background": "green", "border-radius": "5px" }"$
public Sub setStyle(Style As String)
If mElement <> Null Then
Log("setting style: " & Style)
mElement.SetStyle(Style)
End If
mStyle = Style
End Sub
public Sub getStyle() As String
Return mStyle
End Sub
Public Sub setMenu(menu As List)
''' Dim exStyle As String = BuildExStyle
mTarget.Empty
Dim body As BANanoElement = BANano.GetElement("#body")
Dim div As BANanoElement = body.Append($"<div id="div1"></div>"$).Get("#div1")
div.AddClass(mName)
Dim link As String = menu.Get(0)
link = link.ToLowerCase
div.Append($"<a class="active" href=$"#${link}">${menu.Get(0)}</a>"$)
For i = 1 To menu.Size - 1
Dim link As String = menu.Get(i)
link = link.ToLowerCase
div.Append($"<a href=$"#${link}">${menu.Get(i)}</a>"$)
Next
div.Append("</div>")
mTarget = div
mElement = div.Get("#" & mName)
mElement.HandleEvents("click", Me, mEventName & "_click")
End Sub
#End Region
#Region Internal Events
Sub topnav_click (event As BANanoEvent)
Log("clicked..." & " " & event.Value)
End Sub
#End Region
The result view looks like this:
I tried the Method:
B4X:
mElement.HandleEvents("click", Me, mEventName & "_click")
I realize i may be missing something as I just started playing with this, but already spent pretty much the whole day trying to figure it out without much luck.
Any ideas, thoughts?
Thanks,
Walter