Ignore:
Timestamp:
08/15/12 18:30:58 (13 years ago)
Author:
hendrikvanantwerpen
Message:

Added survey viewer page.

Added view.html page to view surveys. Widgets from the questions are
displayed, no answers are saved yet.

The content module is split for index.html and view.html, so viewers
cannot navigate to other pages. Widgets to pre-load are now seperated
in stddeps.js module and shared between run.js & view.js.

Location:
Dev/branches/rest-dojo-ui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/rest-dojo-ui

    • Property svn:ignore
      •  

        old new  
        11nbproject
         2.project
  • Dev/branches/rest-dojo-ui/client/rft/content.js

    r347 r399  
    1919 * this e.g. to ask confirmation if changed values are not saved.
    2020 */
    21 define(['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;
     21define([
     22    'dojo/_base/Deferred',
     23    'dojo/_base/json',
     24    'dojo/_base/lang',
     25    'dojo/_base/xhr',
     26    'dojo/dom-attr',
     27    'dojo/dom-construct',
     28    'dijit/registry',
     29    './ui/_Page',
     30    'dojo/domReady!'
     31],function(Deferred,json,lang,xhr,attr,domConstruct,registry,_Page){
     32    return new (function(){
     33        var self = this;
     34        var inited = false;
    2735
    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]);
     36        var okay = new Deferred();
     37        okay.resolve();
     38        okay = okay.promise;
     39
     40        var fail = new Deferred();
     41        fail.reject();
     42        fail = fail.promise;
     43
     44        var contentPane = null;
     45        self.goToImpl = null;
     46        self.initialImpl = null;
     47
     48        self.init = function() {
     49            contentPane = registry.byId('content');
     50            inited = true;
     51        };
     52
     53        self.goTo = function(path,args) {
     54            if ( !inited ) { return fail; }
     55            if ( self.goToImpl !== null ) {
     56                return self.goToImpl(path,args);
     57            } else {
     58                return fail;
     59            }
     60        };
     61
     62        self.initial = function(path,args) {
     63            if ( !inited ) { return fail; }
     64            if ( self.initialImpl !== null ) {
     65                return self.initialImpl(path,args);
     66            } else {
     67                return fail;
     68            }
     69        };
     70
     71        self._loadPage = function(path,args) {
     72            if ( !inited ) { return fail; }
     73            var dfd = new Deferred();
     74
     75            function getFirstNode(html) {
     76                var nodeOrFragment = domConstruct.toDom(html);
     77                if (nodeOrFragment instanceof Element) {
     78                    return nodeOrFragment;
     79                }
     80                if (nodeOrFragment instanceof DocumentFragment) {
     81                    console.warn("Fragment found, will only use first Element");
     82                    for (var i in nodeOrFragment.childNodes) {
     83                        var node = nodeOrFragment.childNodes[i];
     84                        if (node instanceof Element) {
     85                            return node;
     86                        }
    3787                    }
    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;
    7088                }
     89                return domConstruct.toDom('<div>No Element found in template.</div>');
     90            }
     91
     92            function mixinArgs(node) {
     93                var props = {};
     94                if ( attr.has(node,'data-dojo-props') ) {
     95                    props = json.fromJson(attr.get(node,'data-dojo-props'));
     96                }
     97                lang.mixin(props,{pageArgs:args});
     98                var jsonStr = json.toJson(props);
     99                attr.set(node,'data-dojo-props',jsonStr.slice(1,jsonStr.length-1));
     100            }
     101               
     102            // load html
     103            var pageUrl = 'rft/pages'+path+'.html';
     104            xhr.get({
     105                url: pageUrl,
     106                failOk: true
     107            })
     108            // initialize page or create error message
     109            .then(function(html){
     110                var rootNode = getFirstNode(html);
     111                mixinArgs(rootNode);
     112                contentPane.set('content',rootNode);
     113                var page = registry.byNode(rootNode);
     114                if ( !page ) {
     115                    page = new _Page({},rootNode);
     116                }
     117                dfd.resolve(page);
     118            },function(){
     119                contentPane.set('content',"<div>Page "+path+" not found.</div>");
     120                dfd.reject();
    71121            });
    72122
    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 getFirstNode(html) {
    98                     var nodeOrFragment = domConstruct.toDom(html);
    99                     if (nodeOrFragment instanceof Element) {
    100                         return nodeOrFragment;
    101                     }
    102                     if (nodeOrFragment instanceof DocumentFragment) {
    103                         console.warn("Fragment found, will only use first Element");
    104                         for (i in nodeOrFragment.childNodes) {
    105                             var node = nodeOrFragment.childNodes[i];
    106                             if (node instanceof Element) {
    107                                 return node;
    108                             }
    109                         }
    110                     }
    111                     return domConstruct.toDom('<div>No Element found in template.</div>');
    112                 }
     123            return dfd.promise;
     124        };
    113125
    114                 function mixinArgs(node) {
    115                     var props = {};
    116                     if ( attr.has(node,'data-dojo-props') ) {
    117                         props = json.fromJson(attr.get(node,'data-dojo-props'));
    118                     }
    119                     lang.mixin(props,{pageArgs:hri.args()});
    120                     var jsonStr = json.toJson(props);
    121                     attr.set(node,'data-dojo-props',jsonStr.slice(1,jsonStr.length-1));
    122                 }
    123                
    124                 // update hash
    125                 currentHri = hri;
    126                 hash(hri.hash(),replace);
    127                
    128                 // load html
    129                 var pageUrl = 'rft/pages'+hri.path()+'.html';
    130                 xhr.get({
    131                     url: pageUrl,
    132                     failOk: true
    133                 })
    134                
    135                 // initialize page or create error message
    136                 .then(function(html){
    137                     var rootNode = getFirstNode(html);
    138                     mixinArgs(rootNode);
    139                     contentPane.set('content',rootNode);
    140                     currentPage = dijit.byNode(rootNode);
    141                     if ( !currentPage ) {
    142                         currentPage = new _Page({},rootNode);
    143                     }
    144                     dfd.resolve();
    145                 },function(){
    146                     contentPane.set('content',"<div>Page "+hri.path()+" not found.</div>");
    147                     dfd.reject();
    148                 });
    149                 return dfd.promise;
    150             }
    151 
    152             self.initial = function(path,args) {
    153                 if ( currentHri ) {
    154                     var dfd = new Deferred();
    155                     dfd.resolve();
    156                     return dfd.promise;
    157                 }
    158                 if ( hash() ) {
    159                     var hri = new HRI(hash());
    160                     return _goTo(hri, true);
    161                 } else {
    162                     return _goTo(new HRI(path,args));
    163                 }
    164             };
    165 
    166             self.goTo = function(path,args) {
    167                 return _goTo(new HRI(path,args));
    168             }
    169 
    170             self.getArgs = function() {
    171                 if ( currentHri ) {
    172                     return currentHri.args();
    173                 } else {
    174                     return {};
    175                 }
    176             };
    177 
    178             connect.subscribe('/dojo/hashchange', function(){
    179                 _goTo(new HRI(hash()));
    180             });
    181 
    182         })();
    183     });
     126    })();
     127});
Note: See TracChangeset for help on using the changeset viewer.