ext-starfall/vflow.js

145 lines
3.1 KiB
JavaScript

"use strict";
const cl = global.botanLoader;
const Dragonfly = global.Dragonfly;
const doT = require("dot");
const Chain = require( "./chain" );
var VFlow = function( controller )
{
var _self = this;
return function( so, ro )
{
Dragonfly.Debug( "Using Flow: Chain" );
// Retain the scope of caller
_self.scope = this;
_self.chains = new Chain( _self.Render.bind( _self ) );
_self.chains.onError = _self.onError.bind( _self );
_self.init( so, ro );
new controller( _self, _self.viewData );
};
};
VFlow.prototype.endFlow = function( layout, ext )
{
var _self = this;
var viewData = this.viewData;
var options = viewData.templating || {};
// Layout Chain: Two Column
var twoColHandle = function( def, data, _class )
{
var view = doT.template( data, options[ _class ], def );
viewData.context = view( viewData );
ElemHandle();
};
// Element Header / Footer
var ElemHandle = function ()
{
_self.chains.HFC( viewData, viewData, HCFHandle );
};
// Layout Chain: Header - Content - Footer
var HCFHandle = function ()
{
_self.chains.Template( "Layout.HCF", BackboneHandle );
};
// Html Backbone
var BackboneHandle = function ( def, data, _class )
{
if( data )
{
var view = doT.template( data, options[ _class ], def );
viewData.body = view( viewData );
}
var jc = _self.chains.Multi(
"BotanJs"
, () => _self.chains.Template( "Layout.HTML", FinalHandle )
, 2 );
viewData.modules.addListener( "error", function( e, b )
{
throw new Error( e + "\n" + b );
} );
viewData.modules.addListener( "complete", jc );
viewData.modules.compile( "js" );
viewData.modules.compile( "css" );
};
var FinalHandle = function( def, data, _class )
{
var view = doT.template( data, options[ _class ], def );
viewData[ "_process_time" ] = () => _self.scope.ProcessTime;
_self.Render( view( viewData ) );
};
if( layout === undefined ) layout = "Elem";
var chainLayouts = (
({ // Defined layout chains
"TwoCol": function()
{
viewData.modules.push( "Astro.Starfall.Layout.TwoColumn" );
_self.chains.Template( "Layout.TwoColumn", twoColHandle );
}
, "PureCol": function()
{
viewData.modules.push( "Astro.Starfall.Layout.PureColumn" );
_self.chains.Template( "Layout.PureColumn", BackboneHandle );
}
, "Elem": ElemHandle
, "Backbone": BackboneHandle
}) [ layout ]
// Fallback to try out undefined template chains
|| function() { _self.chains.Template( layout, FinalHandle, ext ); }
)();
};
VFlow.prototype.init = function( so, ro )
{
this.so = so;
this.routeObj = ro;
this.viewData = {};
};
VFlow.prototype.onError = function( err )
{
if( err )
{
Dragonfly.Error( err );
this.routeObj.reRoute( "500", true );
}
return err;
};
VFlow.prototype.Render = function( r )
{
Dragonfly.Info( "Render" );
this.scope.result = r;
this.scope.plantResult( this.useCache, this.cacheTime );
};
VFlow.create = function( c )
{
// Proxy the controller
// Do not use arrow function
// Because the scope will be binded here
// We don't wanna do that
return function( a, b )
{
var cf = new VFlow( c );
return cf.bind( this )( a, b );
};
};
module.exports = VFlow;