commit 252a1555ba90e8915c498dc44fca890563576083 Author: 斟酌 鵬兄 Date: Tue May 31 23:31:25 2016 +0800 Initial commit diff --git a/chain.js b/chain.js new file mode 100644 index 0000000..d3087b5 --- /dev/null +++ b/chain.js @@ -0,0 +1,43 @@ +"use strict"; + +const Dragonfly = global.Dragonfly; +const cl = global.botanLoader; +const doT = require("dot"); + +const OChain = cl.load( "botansx.mvc.model.chain" ); + +class Chain extends OChain +{ + HFC( viewData, hfData, handler ) + { + var _self = this; + + viewData.modules.push( + "Astro.Starfall.Element.Header" + , "Astro.Starfall.Layout.MainFrame" + , "Astro.Starfall.Element.Footer" + ); + + var options = hfData.templating || {}; + + var chainHandler = function( def, data, _class ) + { + var view = doT.template( data, options[ _class ], _self.utils ); + viewData["footer"] = view( hfData ); + + handler(); + }; + + var chainFooter = function( def, data, _class ) + { + var view = doT.template( data, options[ _class ], _self.utils ); + viewData["header"] = view( hfData ); + + _self.Template( "Element.Footer", chainHandler ); + }; + + this.Template( "Element.Header", chainFooter ); + } +} + +module.exports = Chain; diff --git a/vflow.js b/vflow.js new file mode 100644 index 0000000..1dc0051 --- /dev/null +++ b/vflow.js @@ -0,0 +1,143 @@ +"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 ); + _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(); +}; + +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;