Skip to content

4. Discovering the UIView

Marcos Vinícius edited this page Aug 11, 2022 · 1 revision

UIView is the super-class that will allow us to create the views on the page
A view is basically a "screen" of our app.

The UIView class will do the necessary abstraction to transform everything we do here into a view in the browser

import { UIView, ViewLayout } from "./Objective-UI";
export class HelloWorld extends UIView
{
    // +----------------------------------------------------------+
    // + here is where we will declare our visual controls        +
    // + (a.k.a "Widget's") in the form of representative objects +
    // +----------------------------------------------------------+    

    // These functions are provided and called by UIView
    buildLayout(): ViewLayout
    {
        // This is where we will define the layout of our View, 
        // which is based on Bootstrap's Grid system, demarcating rows and columns.
    }
    composeView(): void
    {
        // Here is where we will relate the visual controls (a.k.a "Widget") 
        // to the Divs that were created by ViewLayout
    }   
    onViewDidLoad(): void
    {
        // At this moment the View has been fully loaded in the browser and 
        // all controls are available to change state and style
    }
}

Understanding UIView Core Functions

As you noted, the UIView requires the inherited class to override 3 essential functions

buildLayout(): ViewLayout
composeView(): void
onViewDidLoad(): void

buildLayout()

Here we need to define a Layout for our View, which is represented by Bootstrap's grid system.
See more at: https://getbootstrap.com/docs/4.0/layout/grid/

We can do this in two ways, the first is via object:

    buildLayout(): ViewLayout
    {
        return new ViewLayout('app', [
            new Row('row-div', {
                rowHeidth: '50px',
                columns: [
                    new Col('left-col-div', {
                        colClass: 'col-lg-2 col-md-2 col-xs-6 col-sm-6'
                    }),
                    new Col('right-col-div', {
                        colClass: 'col'
                    })
                ]
            })
        ])
    }

Or we can do the exact equivalent via html:

    buildLayout(): ViewLayout
    {
        return new ViewLayout('app').fromHTML(`
        <div class="row" id="row-div" style="height:50px">
            <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6" id="left-col-div">
            </div>
            <div class="col" id="right-col-div">
            </div>
        </div>`);
    }

You should avoid declaring elements here, unless it's purely aesthetic and you don't
really need some dynamic control over them.

composeView(): void

Here is where we will distribute the elements that will be rendered by our Widgets in
the divs id="left-col-div" and id="right-col-div" defined in the Layout

export class HelloWorld extends UIView {
    private h1Hello = new UIHead({ name: 'lbHello', headType: 'h1', text: 'Hello World! ' })
...
}

And inside composeView() do the following:

 composeView(): void { 
     this.addWidgets('left-col-div', this.h1Hello)
 ...
}

This will cause the h1Hello variable we declared to be rendered inside the div

<div class="col-lg-3 col-md-3 col-xs-6 col-sm-6" id="left-col-div">
</div>

How about changing the style of our Widget?

    private h1Hello = new UIHead({ name: 'lbHello', headType: 'h1', text: 'Hello World! ' })
...
    onViewDidLoad(): void
    {
        this.h1Hello.cssFromString('color:blue')
    }

The code we've done so far will result in this:

Lifecycle of a UIView

As you can see, the UIView allows us to isolate the construction steps of the View,
with each of the base functions being responsible for a part of the process.

Clone this wiki locally