FJS Core

In FrameworkJS, there is no need to write code separately for Android, iOS, and web platforms. The core of the FrameworkJS is built using JavaScript, HTML, and CSS languages, which can be easily applied to Android and iOS platforms. Additionally, by uploading the files to CloadIO, application will be able to run on the web.

index.html

FrameworkJS includes an index.html file where all the HTML code is placed. In this file, you simply need to add the HTML tags. The structure section of the index file is as follows:

  • head section
    • meta: At the top of the page, there are meta tags, title, and link tags.
      We have two meta tags, that we use the viewport meta tag to control layout and the next one is for declaring character encodings in HTML.
    • title: This is the title of the application.
    • link: The link tags are used to import CSS files, and it is important to organize them in a specific order. First, we include CSS libraries, followed by CSS modules, and finally the CSS files we have written for each page. These sections are typically separated by comments for better organization.
  • body section

    All HTML code goes inside the body. for example, adding a page and etc.

  • script section

    The script tags are used at the bottom of the page, to include all related scripts. We also write these parts in order. First, the JS libraries are written, and then each js page file that we have written. These sections are separated by some comments.

    Notice

    Pay attention to app.js script should be included immediately after libraries because of its concept.

Example

performing an example to see the basic structure of index.html and the rules that exist on this page.

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>Your FrameworkJS Application</title>

    <link rel="stylesheet" type="text/css" href="lib/framework.css">

    <!-- add your modules styles here -->
    <!-- <link rel="stylesheet" type="text/css" href="modules/"> -->
    <link rel="stylesheet" type="text/css" href="modules/ui/css/style.css">

    <!-- add your page styles here -->
    <!-- <link rel="stylesheet" type="text/css" href="css/"> -->
    <link rel="stylesheet" type="text/css" href="css/login.css">
    <link rel="stylesheet" type="text/css" href="css/home.css">
</head>
<body>
    <div class="page login"> 
        <h1>Login Page</h1> 
        <p>Welcome to FrameworkJS</p> 
    </div>
    <div class="page home"> 
        <h1>Hello world!</h1> 
        <p>FrameworkJS is a powerful JavaScript framework for building mobile and web applications.</p> 
    </div>
</body>
<script src="lib/cloadio.min.js"></script>
<script src="lib/jquery-2.2.0.min.js"></script>
<script src="lib/framework-a.min.js"></script>
<script src="lib/framework-b.min.js"></script>
<script src="js/app.js"></script>

<!-- add your page scripts here -->
<!-- <script src="js/"></script> -->
<script src="js/login.js"></script>
<script src="js/home.js"></script>
</html>

Notice

Note that maintaining harmony between CSS linking, HTML codes, and JS linking is crucial for every page. The order in which they are linked is important for easy understanding. For instance, if the HTML code for the home page is the fifth page, it should also be the fifth in CSS and JS linking.

app.js

In FrameworkJS, there is an app.js file that contains configurations, object definitions, event handling, and other important functionalities. When building the project for the first time, it is necessary to make some important basic settings. Additionally, as the project progresses, further modifications to this file will be required.

Pure app.js file
//------------------ config ------------------------------
    app.name = 'framework';
    
    // domain and ssl configurations to access files through the http protocol
    app.http = {
        domain: 'framework.cloadio.com',
        ssl: false
    };
    
    app.cloadio = {
        // you can enable or disable all functions related to cloadio.
        // if you don't need back end disable it.
        enable: true,
    
        // use tls connection
        secure: true,
    
        // you can execute code on all devices remotely.
        allowRemoteExecution: true,
    
        // public users info path
        puip: '/users'
    };
    
    app.install = {
        // track app installs
        enable: true,
    
        // install details path
        path: '/installs'
    };
    
    app.version = {
        // control min version
        // if minimum version be greater than the current version update page will be shown
        // so app must have update page
        control: true,
    
        // current version
        current: 202301040909,
    
        // min version path
        mvp: '/update/minVersion',
    };
    
    app.update = {
        // auto update
        autoUpdate: true,
    
        // auto update url path
        auup: '/update/url'
    };
    
    // default localization value
    // all modules will use this configuration
    app.localization = {
        language: {
            direction: 'ltr',
            code: 'enUs'
        },
        dateTime: {
            // note: use space character instead of underscore in time zone
            calendar: 'gregorian'
        }
    };
    
    // fallback page name
    app.auth.fpn = 'login';
    
    // use notification service
    app.notification = {
        enable: true
    };
    
    // appearance attributes
    app.appearance = {
        statusBar: 'light',
        backgroundColor: '#000000'
    };
    
    // framework uses this feature to increase product quality and performance
    app.statistic = {
        // send statistic data to the framework lab.
        enable: true
    };
    //------------------ end of config -----------------------
    
    
    // ----------------- events ------------------------------
    // on document ready event
    // called when html dom content loaded
    app.onDocumentReady = function () {
        // body...
        homePage.show();
    };
    
    // on connection open event
    app.onOpened = function () {
        // body...
    };
    
    // on connection close event
    app.onClosed = function () {
        // body...
    };
    
    // on connection open for first time event
    app.onConnected = function (error, value) {
        // body...
    };
    
    // on sync event
    // called when time synced with cloadio
    app.onSynced = function () {
        // body...
        // example: app.now()
    };
    
    // called when user authenticated
    app.auth.onCompleted = function () {
        // body...
    };
    
    // on shown
    app.onShown = function (url) {
        // body...
    };
    
    // before page changed
    app.beforePageChanged = function (page) {
        // body...
    };
    
    // after page changed
    app.afterPageChanged = function (page) {
        // body...
    };
    
    // on back button event just in android
    app.control.onBack = function () {
        // body...
        Android.exit();
    };
    
    // on location changed event
    app.location.onChanged = function (longitude, latitude, altitude, accuracy) {
        // body...
    };
    
    // on notification received event
    app.notification.onReceived = function (data) {
        // body...
    };
    
    // on sms received event
    app.auth.sms.onReceived = function (text, sender) {
        // body...
    };
    
    // on update started
    app.update.onStarted = function () {
        // body...
    };
    
    // on update progress changed
    app.update.onProgressChanged = function (percentage) {
        // body...
    };
    
    // on update started
    app.update.onCompleted = function () {
        // body...
    };
    
    // on QR Code data received
    app.qrCode.onReceived = function (data) {
        // body...
    };
    
    // on scan result for a device {macAddress, rssi, name, services} received signal strength indicator
    app.bluetooth.scan.onResult = function (error, device) {
        // body...
    };
    
    // on scan stopped
    app.bluetooth.scan.onStopped = function (error) {
        // body...
    };
    
    // on connected to a device
    app.bluetooth.onConnected = function (paths) {
        // body...
    };
    
    // on disconnected from a device
    app.bluetooth.onDisconnected = function () {
        // body...
    };
    
    // on received data
    app.bluetooth.onReceived = function (data) {
        // body...
    };
    
    // orientation sensor
    app.sensors.orientation.onStarted = function () {
        // body...
    };
    
    // on disconnected from orientation sensor
    app.sensors.orientation.onStopped = function (error) {
        // body...
    };
    
    // on orientation changed
    app.sensors.orientation.onChanged = function (azimuth) { // in degrees [0, 359.9]
        // body...
    };
    
    // on std out event
    // called when data is written to console
    app.debug.onSTDOut = function (data) {
        // body...
    };
    // ----------------- end of events -----------------------

Configuration

The app methods are fundamental methods in FrameworkJS that require configuration. This section focuses on creating a general configuration for the application, and we will provide detailed explanations. For additional information, please refer to the configuration section.

  • app.name: The name of the application created in CloadIO dashboard. For more information about CloadIO account, See the CloadIO Account section.
    app.name Configure example
    app.name = 'myApp';
  • app.http: Domain and SSL configurations to access files through the HTTP protocol.

    When an account is created in CloadIO, the domain name is created according app name, which must be set here. See more information in the configuration section.

    In the app.http section, the domain, and SSL should be applied as follows.

    app.http Configure example
    // domain and ssl configurations to access files through the http protocol
    app.http = {
        domain: 'accountName .cloadio.com',
        ssl: false
    };
  • app.cloadio: You can enable or disable all functions related to CloadIO side. It means sometimes you need to write high-security or back end codes, so CloadIO settings should be configured according to your requirements.

    The first property is related to back-end codes. If you don't need the back-end, set the "enable" value to false.

    The secure part is using a TLS connection. If you need to keep a secure connection, you can set it to true.

    You can execute code on all devices remotely through CloadIO functions. To aim for this purpose you must set true for the "allowRemoteExecution" property. If you don't need to have an "allowRemoteExecution", you can set it to false.

    The puip is the path of saving users' data in CloadIO which By default, is set to '/clients'. Therefore according to project needs, it can be changed. See more detailed information in the configuration section.
    app.cloadio Configure example
    app.cloadio = {
        // you can enable or disable all functions related to cloadio.
        // if you don't need back end disable it.
        enable: true,
    
        // use tls connection
        secure: false,
        
        // you can execute code on all devices remotely.
        allowRemoteExecution: true,
    
        // public users info path
        puip: '/clients'
    };
  • app.install: This section is about getting user's information.

    When user installs the application first, if you set the "enable" value to true, user's information such as device token, time zone, etc. is stored with a unique ID. You can set it to false if you don't need to do this section.

    The "path" is to save users' information path and it is '/installs' by default. See more detailed information in the configuration section.

    app.install Configure example
    app.install = {
        // track app installs
        enable: true,
    
        // install details path
        path: '/installs'
    };
  • app.version: In FrameworkJS, there are two kinds of app versions for configurations. The min version is set on a CloadIO-based path and a current version that you can change. If the min version is greater than the current version, an update page will be run. See more detailed information in the configuration section.

    Notice

    For using the app version property, your app must have update page.

    If you want to activate the min version in the project, you must set true the "control" value.

    You can change the current version value at the "current" property. The current version value consists of numbers and it has a simple syntax. It includes year, month, day, hour, and minutes. These digits should be 12 characters.

    Note that if you don't want the update page to be displayed, the current version must be greater than the min version.

    The mvp is the path of saving the min version in CloadIO which by default, is set to '/update/minVersion'. Therefore, it can be changed according to project's needs. See more detailed information in the configuration section.

    app.version Configure example
    app.version = {
        // control min version
        // if minimum version be greater than the current version update page will be shown
        // so app must have update page
        control: false,
    
        // current version
        current: 202104031943,
    
        // min version path
        mvp: '/update/minVersion',
    };
  • app.update: This section is about using auto-update on CloadIO.

    If you want to use this, you can set "autoUpdate" to true else you can set it to false.

    The auup is the path of saving auto-update in CloadIO which by default, is set to '/update/url'. Therefore, it can be changed according to project's needs. See more detailed information in the configuration section.

    app.update Configure example
    app.update = {
        // auto update
        autoUpdate: true,
    
        // auto update url path
        auup: '/update/url'
    };
  • app.localization: It's to determine the calendar, language, and time zone.

    If the used language is left to right, the "direction" is setting to ltr, like the English language, else it should be set to rtl, like the Arabic language. Every language has an abbreviation word, So the value of the code property should be the abbreviation word of the used language.

    The "calendar" property is for setting the calendar type. It is set on the "gregorian" by default. It is clear that you can change it according to language of the project.

    The last section determines the local time. Here, you should write the time difference between the application and Greenwich time. See more detailed information in the configuration section.

    app.localization Configure example
    app.localization = {
        language: {
            direction: 'ltr',
            code: 'enUs'
        },
        calendar: 'gregorian',
        timeZone: '00:00'
    };
  • app.auth.fpn: When a user is not authenticated, you define a page name here to direct user to that page. For instance, you can define a login page when users have not authenticated. See more detailed information in the configuration section.
    app.auth.fpn Configure example
    app.auth.fpn = 'login';
  • app.notification: For using the notification service in FrameworkJS should set the enabled property to true. See more detailed information in the configuration section.
    app.notification Configure example
    app.notification = {
        enable: true
    };
  • app.appearance: Here is to change the background and text color of the status bar on Android and iOS devices.

    For the background color, you should use a HEX code, for example, #000000 is setting the background status to black.

    For setting the color of the text in the status bar you can set light or dark attributes. See more detailed information in the configuration section.

    app.appearance Configure example
    app.appearance = {
        statusBar: 'light',
        backgroundColor: '#000000'
    };
  • app.statistic: The FrameworkJS uses this feature to increase product quality and performance.

    If you set the enable to true it will send statistic data to framework lab otherwise not be sent. See more detailed information in the configuration section.

    app.statistic Configure example
    app.statistic = {
        // send statistic data to framework lab.
        enable: true
    };

Defining Custom Objects

This section will provide a general configuration of the application objects. We will explain this in detail and you can define your related desired global objects.

When caching the data in the application, we should first define a global object variable that binds data to FrameworkJS local. For example, by defining app.local, data can be saved in a local object like pp.local.myData.

To create global functions, we can define app.functions, and bind any desired function to it. For example, if we want to define a generate key function, first of all we can define app.functions here, and then simply create our function under it as app.functions.generateKey.

Notice

Because of running the app.js at first in FrameworkJS core, we define global objects here. It means when you need a variable in the whole of the project (on more than one page for example), it is optimized to define the variable in this section and use it on other pages. So if you want to use a local variable or just use it on one page, it is better to define it on that specific page.

Define app objects example
// --------------- define app objects --------------------
app.functions = {};
app.local = {};
// ------------ end of define app objects ----------------

//--------------- define global functions ----------------
// generate a random key
app.functions.generateKey = function () {
    return Date.now().toString(36) + '-' + Math.floor(Math.random() * 1000000).toString(36);
};
//--------------- end of define global functions ----------------

Events

Events are very important and required for some information and initialization in the application. In this section will be created a general configuration of the application events. So we will explain this in detail.

  • app.onDocumentReady: This event is called when the HTML dom content is loaded. So for configuring the connections and others we can use onDocumentReady.

    For example, you can write homePage.show() to show the home, after running the app. See more detailed information in the Document Object section.

    app.onDocumentReady example
    // on document ready event
    // called when html dom content loaded
    app.onDocumentReady = function () {
        homePage.show();
    };
  • app.onOpened: This event is called when the internet connection opened. For instance, you can handle the codes when connecting to internet. See more detailed information in the Connection section.
    app.onOpened example
    // on connection open event
    app.onOpened = function () {
        alert('The internet is connected.');
    };
  • app.onClosed: This event is called when internet connection closed. For instance, you can handle a connection when the internet lose. See more detailed information in the Connection section.
    app.onClosed example
    // on connection close event
    app.onClosed = function () {
        alert('The connection was lost.');
    };
  • app.onConnected: Called when a new connection has been established. In this event, user and time are not synced yet. See more detailed information in the Connection section.

    Parameters Type Description
    error String The callback error of checking authorization.
    credential String The string of credentials on checking authorization.
    app.onConnected example
    // on connection open for first time event
    app.onConnected = function (error, credential) {
        // check credential
        if (localStorage['authentication'] === 'completed') {
            if (error)
                console.error('getting credential failed', error);
            if (!credential)
                app.auth.clear();
        }
    };
  • app.onSynced: This event is called when the time is synced with CloadIO. Here we can write the codes to get basic data from CloadIO. See more information in the Synchronization section.
    app.onSynced example
    // on sync event
    // called when time synced with cloadio
    app.onSynced = function () {
        ref.child('model').get(function (error, value) {
            if (error)
                console.error('get value from cloadio was failed', error);
            else {
                // define local app variable
                if (!app.local.value)
                    app.local.value = {};
                app.local.value = value;
            }
        });
    };
  • app.auth.onCompleted: When user object synced with CloadIO, this event is going to call. See more information in the Authentication section.
    app.auth.onCompleted example
    // called when user object synced with cloadio
    app.auth.onCompleted = function () {
        if (app.auth.user.name)
            homePage.show();
    };
  • app.onShown: When an external link or URL is clicked, to handle and achieve that specific page in the app, this event will be called. See more information in the Navigation section.

    Parameters Type Description
    url String The path of the link that was clicked and the related page should be shown.
    app.onShown example
    // on shown
    app.onShown = function (url) {
        if (url)
            homepage.show(url);
    };
  • app.onHidden: When The app is in the background, to handle the specific operation in the background, this event will be called. See more information in the Navigation section.

    app.onHidden example
    app.onHidden = function () {
        if (app.local.video.wasPlaying = app.local.video.isPlaying)
            app.functions.video.stop();
    };
  • app.beforePageChanged: Before changing any page, you can write some codes and actions, so that these codes apply to all the app pages. For example, we can be getting data from CloadIO or change some CSS styles, ...
    See more information in the Navigation section.

    Parameters Type Description
    page Object Some specifications of the current page
    app.beforePageChanged example
    app.beforePageChanged = function (page) {
        $('.page.' + page.name + '').css('background-color', '#000000');
    };
  • app.afterPageChanged: After changing any page, you can write some codes and actions, so that these codes apply to all the app pages. For example, we can be getting data from CloadIO or change some CSS styles, ...
    See more information in the Navigation section.

    Parameters Type Description
    page Object Some specifications of the current page
    args Array arguments of current page.
    app.afterPageChanged example
    app.afterPageChanged = function (page, args) {
        $('.page.' + page.name + '').css('color', '#ffffff');
    };
  • app.control.onBack: This event is called when the back button is pressed on Android devices. For example, we want the Android app to exit by clicking on the back button, so we should write the exit Android code as follow. See more information in the Control section.
    app.control.onBack example
    // on back button event just in android
    app.control.onBack = function () {
        Android.exit();
    };
  • app.location.onChanged: When user location changes, according to project requirements handle it in this event. See more information in the Location section.

    Parameters Type Description
    longitude String Include the geographical coordinates related to longitude.
    latitude String Include the geographical coordinates related to latitude.
    altitude String Include the geographical coordinates related to altitude.
    accuracy String Include geographical accuracy.
    app.location.onChanged example
    // on location changed event
    app.location.onChanged = function (longitude, latitude, altitude, accuracy) {
        if (!longitude)
            alert('Getting user location failed.');
    };
  • app.notification.onReceived: Sometimes the app needs to receive and handle the notification, so this event should be called when a notification (FCM, APNS) has been received. See more information in the Notification section.

    Parameters Type Description
    data Object Contains the whole information of the notification message, such as the body, data, etc.
    app.notification.onReceived example
    // on notification received event
    app.notification.onReceived = function (data) {
        if (data)
            homePage.show(data);
    };
  • app.auth.sms.onReceived: When an SMS message has been received in the application, you can manage SMS actions here. See more information in the Authentication section.

    Parameters Type Description
    text String Contain of the message text.
    sender String Contain the sender of short message.
    app.auth.sms.onReceived example
    // on sms received event
    app.auth.sms.onReceived = function (text, sender) {
        // body
    };
  • app.update.onStarted: When an automated internal update occurred, this event is called and you can handle the update. See more information in the Update section.
    app.update.onStarted example
    // on update started
    app.update.onStarted = function () {
        // body...
    };
  • app.update.onProgressChanged: When an automated internal update downloading progress is changed, this event is called.

    For example, you can create an update page for handling update progress. Here as you can see, we show you the small code for handling the update progress bar. See more information in the Update section.

    Parameters Type Description
    percentage Number The value of progress percentages.
    app.update.onProgressChanged example
    // on update progress changed
    app.update.onProgressChanged = function (percentage) {
        if (app.page.name === 'update')
            $('.page.update > .progress > div').css('width', percentage + '%');
    };
  • app.update.onCompleted: When the update operation is completed, this event is called. It means, for handling what happens after completing the update, as we show, you can add codes here. See more information in the Update section.
    app.update.onCompleted example
    // on update started
    app.update.onCompleted = function () {
        if (app.page.name === 'update')
            alert('Your update is completed!');
    };
  • app.qrCode.onReceived: When you use QR technology in your project, you can handle the data received from QR in this event and call on any page you want to be displayed to user with app.qrCode.start() code. See more information in the QR Code section.
    app.qrCode.onReceived example

    This is an example of QR code management that we write in app.js

    // on QR Code data received
    app.qrCode.onReceived = function (data) {
        if (data)
            homePage.show();
    };
    app.qrCode.start example

    This code is an example of when we need to call the QR code and we can write it on different pages.

    app.security.ensurePermissions({android: {0: 'android.permission.CAMERA'}}, function (granted) {
        if (granted)
            app.qrCode.start();
        else
            app.controls.box('alert', null, 'Grant access to a camera to scan the barcode.', 'left');
    });

css folder

In the CSS folder of FrameworkJS, there is a default CSS file, that we add the common and general CSS styles. So we call it common.css.
Also for each created page in index.html, need to create a CSS file, So that every created CSS file contains the separate CSS codes of that page.
For example, we have a page in the index file, and the name is 'home', so we will have a CSS file with this name in the CSS folder.

Notice

Every CSS file in the CSS folder should be included as a link in the index.html file.

js folder

In FrameworkJS, it is a good practice to have a separate JS file for each page. In the JS folder of FrameworkJS, we have a default JS file, which we called app.js.
When you create a new page in the index file, you should also create a new JS file with the same name as the page. For example, if you have a page called 'home', you should create a new file called 'home.js' in the js folder.

Notice

Every JS file in the JS folder should be included as a script in the index.html file like CSS files.

JS page example
var homePage = new Page('home', function () {
    // after page shown
}, function () {
    // before page shown
}, function () {
    // after page closed
});

For more information about the Page, visit the Page API section.

lang folder

This folder is defined for creating multilingual applications. In this folder, there are several files different target languages.

To define the language of the app, we need to add codes everywhere they are needed. We should add the text of the project as a key: value intolang file.
For example, if we have an English app, we must add the enUs.js file inside the lang folder.

  • How load language?

    In the app.js file at the onDocumentReady event, we should add the following codes in order to load and use the multilingual apps.

    Load language example
    // load language
    var script = document.createElement('script');
    script.onload = function () {
        // apply language
        $('body lang').each(function (i, o) {
            var jSelf = $(o);
            jSelf.prop('outerHTML', app.lang[jSelf.html()]);
        });
    
        app.local.langLoaded = true;
        if (app.local.onLangLoaded)
            app.local.onLangLoaded();
    
        var next = function () {
            ref.getCredential(function (error, value) {
                if (!app.page) {
                    if (value)
                        homePage.show();
                    else
                        signInPage.show();
                }
            });
        };
        if (app.local.synced)
            next();
        else
            app.local.onSyncCompleted.push(next);
    };
    
    script.src = 'lang/' + app.localization.language.code + '.js';
    document.body.appendChild(script);
  • How to use it?

    There is two option to support multilingualism in the application. First, in the HTML, we should use the following codes:

    Using language in HTML example
    <div class="title"><lang>title

    So the result we can get the title text from the language file. Another way to use multilingual is the JS file. IntoJS files, we should use app.local.lang and then add the key of the text.
    The following we have an example for this purpose:

    Notice

    pay attention to app.local.lang is defined in the app.js file.

    Using language in JS example
    $('.title').html(app.local.lang.title);

lib folder

In the lib folder, there are some default libraries related to FrameworkJS and CloadIO. These files should not be changed. You can be added new libraries that you want to use, whether CSS or JS, in this folder.

img folder

You can put the images and icons used with any extension required by the project in the img folder. Then call it from this path wherever you want to display the image.

font folder

You can put the used fonts of the project here, and call your fonts in the common.css file, in order to use them.

module folder

In FrameworkJS, modules have been created in order to facilitate the use of FrameworkJS, optimize, and reduce development time. Some of them include the form builder module, table builder, analytics, etc. We introduce some of the most popular and widely used in the Modules section. Also, you can use your own customization modules.
To use a module in a project, simply place it in the "module" folder.

Title Description Download
Model This is the basic model of the project. The model contains the structural properties and fields of the project. Data is stored as JSON and synchronized in real time for every connected client. Download Model Module
Form The form module is used to optimize and reduce the development time of the forms. Download Form Module
Table The Table module is used to optimize and reduce the development time of the Tables. It also is for rendering lists or card lists and tables are used. Download Table Module
UI This module is used to handle HTML tag events and effects, like buttons, inputs, text areas, etc. Download UI Module
Chat This module is used to increase optimization and reduce chat implementation time. Download Chat Module
Controls In this module, there are some important and widely used codes, that make the coding shorter, easier and faster. We have several types of them in the control module. Download Controls Module