Top
craft-uikit
craft-widget-

DefaultRootViewController

DefaultRootViewController

Default RootViewContoller.

Supports:

  • didReceivePopstate with seeking hash
  • pushState to window.history.pushState

Constructor

new DefaultRootViewController()

Source:

DefaultRootViewController Constructor

Example
class PageController extends Craft.UI.DefaultRootViewController {
    constructor(){
        super();
        this.tags = '';
        this.items = '';
    }
    viewDidLoad(callback){
        this.appendView(new Header());
        this.tags = new Tags();
        this.items = new Items();
        let container = new Container();
        container.loadView();
        container.appendView(this.tags);
        container.appendView(this.items);
        this.appendView(container);
        this.appendView(new Footer());
    }
    resolveRoutingRequest(route){
        this.selectTag(route);
        
        if( !route.path ){ route.path = ''; }
        let match = route.path.match(/(\w*)/);
        let tag = match[1];
        if( tag ){ this.selectTag(tag,route); }
    }
    selectTag(route){
        let match = route.path.match(/(\w*)/);
        let tag = match[1];
        if( route.launch ){
            // being in launching app. you should update history
            this.replaceState({state:{tag:tag},path:'/#/'+tag});
        }
        document.title = "Tag: "+tag;
        this.items.selectTag(tag);
    }
    style(componentId){
        return `
            * { box-sizing:border-box; margin:0; padding:0; }
            .root { display:flex; flex-direction:column; width:75%; margin-left:auto; margin-right:auto; }
        `;
    }
    template(componentId){
        return `<div id="root" class="root"></div>`;
    }
}

Classes

DefaultRootViewController

Methods

bringup()

Source:

Bringup routing by RootViewController

didReceivePopstate(event, launch)

Source:

Popstate event entrance

By default, popstate event is managed as hash routing.
Please implement your own strategy.

Parameters:
Name Type Description
event PopStateEvent

PopStateEvent should be handled by your DefaultRootViewController#resolveRoutingRequest

launch Boolean

true if this is the first launch, or access from out side of application history scope caused by browser back/foward

pushState(options)

Source:

Wrapping history.pushState to be able to call with named object

Parameters:
Name Type Description
options Object

options

Properties
Name Type Description
state Object

state

title String

page title

path String

path (hash)

replaceState(options)

Source:

Wrapping history.replaceState to be able to call with named object

Parameters:
Name Type Description
options Object

options

Properties
Name Type Description
state Object

state

title String

page title

path String

path (hash)

resolveRoutingRequest(route)

Source:

Routing request handler.
You have to implement your routing here.

Parameters:
Name Type Description
route Craft.Core.Route

route object

options.launch Boolean

true if this is the first launch, or access from out side of application history scope caused by browser back/foward

options.path String

parsed path. parsing is responsibility of Router implementation

options.event PopStateEvent

PopStateEvent if defined