/** rft/ui/content - Load page content * * The rft/ui/content module is responsible for loading content in the app. * Content is loaded by calling: * content.goTo('pageId'); * The page is then loaded from pages/pageId.html. This should contain one top node. * This top node gets a _Page widget instantiated on it. The template should not * specify another widget on the topnode. A page controller can be specified by * the data-rft-mixin attribute to a class inherited from _PageController. * You can also specify methods in the template in script tags as: * * The two relevant events are onVisit and onLeave. After loading and starting * the complete page onVisit is called. Use this to link widgets together and * load any dynamic content not handled by widget logic already. * The event onLeave is called before navigating to another page. If false is * returned from this function, the navigation will be cancelled. You can use * this e.g. to ask confirmation if changed values are not saved. */ define([ 'dojo/_base/Deferred', 'dojo/_base/json', 'dojo/_base/lang', 'dojo/_base/xhr', 'dojo/dom-attr', 'dojo/dom-construct', 'dijit/registry', './ui/_Page', 'dojo/domReady!' ],function(Deferred,json,lang,xhr,attr,domConstruct,registry,_Page){ return new (function(){ var self = this; var inited = false; var okay = new Deferred(); okay.resolve(); okay = okay.promise; var fail = new Deferred(); fail.reject("No implementation available."); fail = fail.promise; var contentPane = null; self.goToImpl = null; self.initialImpl = null; self.init = function() { contentPane = registry.byId('content'); inited = true; }; self.goTo = function(path,args) { if ( !inited ) { return fail; } if ( self.goToImpl !== null ) { return self.goToImpl(path,args); } else { return fail; } }; self.initial = function(path,args) { if ( !inited ) { return fail; } if ( self.initialImpl !== null ) { return self.initialImpl(path,args); } else { return fail; } }; self._loadPage = function(path,args) { if ( !inited ) { return fail; } var dfd = new Deferred(); function getFirstNode(html) { var nodeOrFragment = domConstruct.toDom(html); if (nodeOrFragment instanceof Element) { return nodeOrFragment; } if (nodeOrFragment instanceof DocumentFragment) { console.warn("Fragment found, will only use first Element"); for (var i in nodeOrFragment.childNodes) { var node = nodeOrFragment.childNodes[i]; if (node instanceof Element) { return node; } } } return domConstruct.toDom('
No Element found in template.
'); } function mixinArgs(node) { var props = {}; if ( attr.has(node,'data-dojo-props') ) { props = json.fromJson(attr.get(node,'data-dojo-props')); } lang.mixin(props,{pageArgs:args}); var jsonStr = json.toJson(props); attr.set(node,'data-dojo-props',jsonStr.slice(1,jsonStr.length-1)); } // load html var pageUrl = 'rft/pages'+path+'.html'; xhr.get({ url: pageUrl, failOk: true }) // initialize page or create error message .then(function(html){ var rootNode = getFirstNode(html); mixinArgs(rootNode); contentPane.set('content',rootNode); var page = registry.byNode(rootNode); if ( !page ) { page = new _Page({},rootNode); } dfd.resolve(page); },function(){ contentPane.set('content',"
Page "+path+" not found.
"); dfd.reject(); }); return dfd.promise; }; })(); });