source: Dev/branches/rest-dojo-ui/client/rft/content.js @ 311

Last change on this file since 311 was 311, checked in by hendrikvanantwerpen, 13 years ago

[Client] Use toaster to show errors from server
[Client] Create widget for mainmenu, cleaner loading of index.html now.
[Server] Renamed answerset datetime to more meaningful creationdate

File size: 6.6 KB
Line 
1/** rft/ui/content - Load page content
2 *
3 * The rft/ui/content module is responsible for loading content in the app.
4 * Content is loaded by calling:
5 *    content.goTo('pageId');
6 * The page is then loaded from pages/pageId.html. This should contain one top node.
7 * This top node gets a _Page widget instantiated on it. The template should not
8 * specify another widget on the topnode. A page controller can be specified by
9 * the data-rft-mixin attribute to a class inherited from _PageController.
10 * You can also specify methods in the template in script tags as:
11 *     <script type="rft/method" data-rft-method-name="onVisit|onLeave">
12 *        // code
13 *     </script>
14 * The two relevant events are onVisit and onLeave. After loading and starting
15 * the complete page onVisit is called. Use this to link widgets together and
16 * load any dynamic content not handled by widget logic already.
17 * The event onLeave is called before navigating to another page. If false is
18 * returned from this function, the navigation will be cancelled. You can use
19 * this e.g. to ask confirmation of changed values are not saved.
20 */
21define(['dojo/_base/declare','dojo/_base/connect','dojo/_base/xhr','dojo/_base/json',
22    'dojo/_base/lang','dojo/_base/Deferred','dojo/hash','dojo/dom-attr','dojo/dom-construct',
23    'dojo/io-query','dijit','./util','./ui/_Page'],
24    function(declare,connect,xhr,json,lang,Deferred,hash,attr,domConstruct,uriQuery,dijit,util,_Page){
25        return new (function() {
26            var self = this;
27
28            var HRI = declare(null,{
29                constructor: function() {
30                    this._path = this._fixPath('/');
31                    this._args = {};
32                    if ( arguments.length == 1 ) {
33                        this.hash(arguments[0]);
34                    } else if ( arguments.length == 2 ) {
35                        this.path(arguments[0]);
36                        this.args(arguments[1]);
37                    }
38                },
39                path: function(path) {
40                    if ( path )
41                        this._path = this._fixPath(path);
42                    return this._path;
43                },
44                args: function(args) {
45                    if ( args && lang.isObject(args) )
46                        this._args = args;
47                    return this._args;
48                },
49                hash: function(hash) {
50                    if ( hash && lang.isString(hash) ) {
51                        var parts = hash.split('!');
52                        if ( parts[1] )
53                            this._path = this._fixPath(parts[1]);
54                        if ( parts[2] )
55                            this._args = uriQuery.queryToObject(parts[2]);
56                    }
57                    return '!'+this._path+'!'+uriQuery.objectToQuery(this._args);
58                },
59                _fixPath: function(path) {
60                    if ( !lang.isString(path) || util.isEmptyString(path) ) {
61                        path = "/";
62                    }
63                    if ( path[0] != '/' ) {
64                        path = '/'+path;
65                    }
66                    if ( path[path.length-1] == '/' ) {
67                        path = path + "index";
68                    }
69                    return path;
70                }
71            });
72
73            var currentHri = null;
74            var currentPage = null;
75           
76            function _goTo(hri,replace) {
77                var contentPane = dijit.byId('content');
78                var dfd = new Deferred();
79               
80                // if already there, return
81                if ( currentHri && currentHri.hash() === hri.hash() ) {
82                    dfd.resolve();
83                    return dfd.promise;
84                }
85               
86                // check if we can leave current page
87                if ( currentPage ) {
88                    if ( currentPage.onLeave() === false ) {
89                        // restore hash if changed by hand or back button
90                        hash(currentHri.hash());
91                        dfd.reject();
92                        return dfd.promise;
93                    }
94                    currentPage = null;
95                }
96               
97                function mixinArgs(node) {
98                    var props = {};
99                    if ( attr.has(node,'data-dojo-props') ) {
100                        props = json.fromJson(attr.get(node,'data-dojo-props'));
101                    }
102                    lang.mixin(props,{pageArgs:hri.args()});
103                    var jsonStr = json.toJson(props);
104                    attr.set(node,'data-dojo-props',jsonStr.slice(1,jsonStr.length-1));
105                }
106               
107                // update hash
108                currentHri = hri;
109                hash(hri.hash(),replace);
110               
111                // load html
112                var pageUrl = 'rft/pages'+hri.path()+'.html';
113                xhr.get({
114                    url: pageUrl,
115                    failOk: true
116                })
117               
118                // initialize page or create error message
119                .then(function(html){
120                    var rootNode = domConstruct.toDom(html);
121                    mixinArgs(rootNode);
122                    contentPane.set('content',rootNode);
123                    currentPage = dijit.byNode(rootNode);
124                    if ( !currentPage ) {
125                        currentPage = new _Page({},rootNode);
126                    }
127                    dfd.resolve();
128                },function(){
129                    contentPane.set('content',"<div>Page "+hri.path()+" not found.</div>");
130                    dfd.reject();
131                });
132                return dfd.promise;
133            }
134
135            self.initial = function(path,args) {
136                if ( currentHri ) {
137                    var dfd = new Deferred();
138                    dfd.resolve();
139                    return dfd.promise;
140                }
141                if ( hash() ) {
142                    var hri = new HRI(hash());
143                    return _goTo(hri, true);
144                } else {
145                    return _goTo(new HRI(path,args));
146                }
147            };
148
149            self.goTo = function(path,args) {
150                return _goTo(new HRI(path,args));
151            }
152
153            self.getArgs = function() {
154                if ( currentHri ) {
155                    return currentHri.args();
156                } else {
157                    return {};
158                }
159            };
160
161            connect.subscribe('/dojo/hashchange', function(){
162                _goTo(new HRI(hash()));
163            });
164
165        })();
166    });
Note: See TracBrowser for help on using the repository browser.