B4J Question [BANano] [SOLVED] Using it to learn JavaScript with a 'ficticious' BANano.GetJavaScript(?) and code B4J Web

Mashiane

Expert
Licensed User
Hi there...

I'm thinking... with people not knowing BANano (yet) and usually preferring to use javascript / jquery to create their B4J Web apps using jetty running on 55056? What it BANano could help? One in also learning javascript and also helping those who would rather write javascript when creating the B4JWeb apps using the approach above. A simple example is the beautiful projects created "outside" of banano that are awesome to use from forum members.

Assuming that you can pass BANano the B4XCode via, lets say a method

B4X:
BANano.GetJavaScript("Sub Anele
DoThis
DoThat
End Sub")
and it spills out

B4X:
function anele {
dothis();
dothat();
}

Now this has the potential of 1, you get to get the needed source code to help you create B4JWeb apps using jetty just like how everyone is doing and 2. helps you also learn javascript and also use the code outside with other frameworks that you might want to upskill yourself with and 3. It pushes people to wanna learn banano also.

#JustAThought.

PS: Thoughts anyone?
 
Solution
just create stand alone JavaScript
That is something you can already do.

Main (keep BANano_Ready empty!) :
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #IgnoreWarnings: 16, 10, 14, 15       
#End Region

Sub Process_Globals
    Private BANano As BANano 'ignore
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    ' you can change some output params here
    BANano.Initialize("BANano", "BANanoTest",1)
    BANano.Header.Title="BANano"
    BANano.JAVASCRIPT_NAME = "myCode.js"
   
    ' set the Transpiler Switches to this:
    BANano.TranspilerOptions.MergeAllCSSFiles = False
    BANano.TranspilerOptions.MergeAllJavascriptFiles = False
    BANano.TranspilerOptions.RemoveDeadCode =...

tchart

Well-Known Member
Licensed User
Agree, I've just started playing with BAnano and I would love a stand alone transpiler that just create stand alone JavaScript.
 
Upvote 0

alwaysbusy

Expert
Licensed User
just create stand alone JavaScript
That is something you can already do.

Main (keep BANano_Ready empty!) :
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #IgnoreWarnings: 16, 10, 14, 15       
#End Region

Sub Process_Globals
    Private BANano As BANano 'ignore
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    ' you can change some output params here
    BANano.Initialize("BANano", "BANanoTest",1)
    BANano.Header.Title="BANano"
    BANano.JAVASCRIPT_NAME = "myCode.js"
   
    ' set the Transpiler Switches to this:
    BANano.TranspilerOptions.MergeAllCSSFiles = False
    BANano.TranspilerOptions.MergeAllJavascriptFiles = False
    BANano.TranspilerOptions.RemoveDeadCode = False
    BANano.TranspilerOptions.ShowWarningDeadCode = False
               
    ' start the build   
    BANano.Build(File.DirApp)
   
    #if release
        ExitApplication
    #end if   
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

' HERE STARTS YOUR APP ' KEEP IT EMPTY!
Sub BANano_Ready()

End Sub

Whatever code you want to be transpiled to javascript, e.g. here a class MyClass:
B4X:
Sub Class_Globals
    Private Counter As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    Counter = 0
End Sub

public Sub AddToCounter(value As Int)
    Counter = Counter + value
End Sub

public Sub GetCounter() As Int
    Return
End Sub

Now Build() to transpile. In the scripts folder you can find the transpiled code myCode.js.

Create some HTML file, and include bananocore.js and the transpiled code as <script> (+whatever javascript/css files you would need) and start using it.
B4X:
<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

<script type="application/javascript" src="bananocore.js"></script>
<script type="application/javascript" src="myCode.js"></script>
<script>
    var myClass = new banano_teststandalonebanano_myclass();
    myClass.initialize();
   
    myClass.addtocounter(1000);
   
    console.log(myClass.getcounter());
</script>
</body>
</html>

Note: all methods are lowercased, as B4J is not strict about the case.

You can do some cleanup in the myCode.js file if it bothers you (although the impact is so minimal I would not even spend time on it) e.g. by removing the following:
B4X:
//var _banano_teststandalonebanano = new banano_teststandalonebanano(); ' <---- REMOVE
/* App */
function banano_teststandalonebanano_myclass() {
    var _B = this;
    _B.__1 = 0;
    _B.initialize = function() {
        _B.__1 = 0;
    };
    _B.addtocounter = function(__2) {
        _B.__1 = _B.__1 + __2;
    };
    _B.getcounter = function() {
        return _B.__1;
    };
}

/*                                                                            ' <---- REMOVE
function banano_teststandalonebanano() {
    var _B;
    this.banano_ready = function() {
        if (_B == null) _B = this;
    };
}
window.addEventListener('online', function() {
    if (typeof _banano_teststandalonebanano['banano_online'] === "function") {
        _banano_teststandalonebanano.banano_online();
    }
});
window.addEventListener('offline', function() {
    if (typeof _banano_teststandalonebanano['banano_offline'] === "function") {
        _banano_teststandalonebanano.banano_offline();
    }
});
var BANversion = 1631864437171;
window.document.addEventListener("readystatechange", BANLoadChecker, true);

function BANLoadChecker() {
    if (window.document.readyState == "complete") {
        _banano_teststandalonebanano.banano_ready();
    }
}
*/

Result:
1631863946446.png



The next version of BANano will hava a new Transpiler Option:
B4X:
BANano.TranspilerOptions.DisableShortenVariableNames = True

which will allow e.g. global variables to retain their true name (counter) instead of a shortened one (__1) so one will be able to this:
B4X:
console.log(myClass.counter);

The cleaned up code would then look something like this:
B4X:
function banano_teststandalonebanano_myclass() {
    var _B = this;
    _B.counter = 0;
    _B.initialize = function() {
        _B.counter = 0;
    };
    _B.addtocounter = function(value) {
        _B.counter = _B.counter + value;
    };
    _B.getcounter = function() {
        return _B.counter;
    };
}

The suggested method BANano.GetJavaScript("...") is just not possible, as BANano does so much more than transpile line by line. Many things are only possible in the B4J syntax and equally other ones only in JavaScript which means N lines in B4J can result in a complete other set of N lines in Javascript. Temporary variables (and even methods) have to be injected, variables/methods have to be aware of the existence of other variables/methods, etc... to make stuff possible.

Using BANano to 'learn' JavaScript is not a good idea. One should follow a course or something if one wants to learn JS. The whole purpose of BANano is that one does not have to do this and can just use the familiar B4J syntax. The way you write B4X code may even be confusing when you look at how it is done in JavaScript.

But the above example shows that it IS possible to use BANano transpiled code "outside" with other e.g. templates.

Alwaysbusy
 
Last edited:
Upvote 0
Solution

Mashiane

Expert
Licensed User
Wow! Thanks a lot.

Can I please make a small request. For absolute position based HTML elements in the Abstract Designer, can you add on the layout code to read the TOP, BOTTOM, HEIGHT and WIDTH inbuilt properties please. If I add these now, I have to copy and paste values from there to my own defined DesignerProperties at the moment. Please.

Thanks again. This is awesome!
 
Upvote 0

alwaysbusy

Expert
Licensed User
inbuilt properties
Normally, the following properties should already be in the next version:

top
left
width
height
hanchor
vanchor
tag
enabled
visible

Bottom doesn't appear to be a thing in the layout, but I guess you can easily calculate that yourself as top + height if you need that.

But adding that wasn't such a small request ;-)

Alwaysbusy
 
Upvote 0

Mashiane

Expert
Licensed User
Normally, the following properties should already be in the next version:

top
left
width
height
hanchor
vanchor
tag
enabled
visible

Bottom doesn't appear to be a thing in the layout, but I guess you can easily calculate that yourself as top + height if you need that.

But adding that wasn't such a small request ;-)

Alwaysbusy
Just a silly question, I am assuming by this you mean, you are able to read them on this sub

B4X:
Sub DesignerCreateView (Target As BANanoElement, Props As Map)
        sWidth = Props.Get("Width")
        sHeight = Props.Get("Height")
...

Without these being custom design properties?

Am I making a right assumption?
 
Upvote 0

Mashiane

Expert
Licensed User
One of the advantages for this is debugging you know. Sometimes you compile your library and everything is fine no errors but..

Then when you run and use it in your app, you get something like this..

1632150278677.png


This can help on pre-updates scenarios. Before you inject new code to your library, you first do the test "outside" and see if everything works perfectly. Off course just pasting the code and seeing that it will be transpiled and "execute" perfectly would be "thee" dream approach.

Awesome stuff!
 
Upvote 0
Top