source: Dev/branches/rest-dojo-ui/client/qed/pages/surveyRun.js @ 421

Last change on this file since 421 was 421, checked in by hendrikvanantwerpen, 12 years ago

Show responses with links in SurveyRun? overview.

File size: 4.0 KB
Line 
1define([
2    'dojo/_base/declare',
3    'dojo/_base/event',
4    'dojo/_base/lang',
5    'dojo/when',
6    '../app/Content',
7    '../app/Router',
8    '../lib/func',
9    '../store',
10    '../app/Page',
11    '../model/classes/SurveyRun',
12    '../widgets/LineWithActionsWidget',
13    'dojo/text!./templates/surveyRun.html'
14],function(declare,event,lang,when,Content,Router,func,store,Page,SurveyRun,LineWithActionsWidget,template){
15    return declare([Page],{
16        templateString: template,
17        surveyRun: null,
18        startup: function() {
19            if ( this._started ) { return; }
20            this.inherited(arguments);
21            this.propertiesForm.on("blur",lang.hitch(this,'_onPropChange'));
22            if ( this.surveyRunId ) {
23                this._loadSurveyRun();
24                this._loadResponses();
25            } else {
26                throw "No valid uid or survey passed!";
27            }
28        },
29        _loadSurveyRun: function() {
30            when(store.get(this.surveyRunId))
31            .then(lang.hitch(this,function(surveyRun){
32                this.surveyRun = surveyRun;
33                this.refreshSurveyRun();
34            }));
35        },
36        refreshSurveyRun: function() {
37            this.titleNode.innerHTML = SurveyRun.DisplayTitle.get(this.surveyRun);
38            this.surveyNode.set('value',SurveyRun.Survey.get(this.surveyRun));
39            this.propertiesForm.set('value',this.surveyRun);
40            this._onPropChange();
41        },
42        _loadResponses: function() {
43            when(store.query("_design/responses/_view/by_surveyrun",{key:this.surveyRunId}))
44            .forEach(lang.hitch(this,function(response){
45                var actions = {
46                    view: {
47                        callback: function(){},
48                        properties: {
49                            title: "View response"
50                        }
51                    }
52                };
53                if ( !response.publicationDate ) {
54                    actions.remove = {
55                        callback: function(){},
56                        properties: {
57                            title: "Remove response"
58                        }
59                    };
60                }
61                var w = new LineWithActionsWidget({
62                    actions: actions
63                });
64                var responseId = store.getIdentity(response);
65                w.set('title',this._link(this._getResponseURL(this.surveyRunId,responseId),responseId));
66                w.placeAt(this.responsesNode);
67            }));
68        },
69        _onPropChange: function(e) {
70            var surveyRun = this.propertiesForm.get('value');
71            if ( surveyRun.mode === "open" ) {
72                this.runURLNode.innerHTML = this._link(this._getGeneralURL(store.getIdentity(this.surveyRun)));
73            } else {
74                this.runURLNode.innerHTML = "No general URL. Add individual respondents below.";
75            }
76        },
77        _getGeneralURL: function(surveyRunId) {
78            return 'response.html#!/'+surveyRunId;
79        },
80        _getResponseURL: function(surveyRunId,responseId) {
81            return 'response.html#!/'+surveyRunId+'!id='+responseId;
82        },
83        _link: function(url,label) {
84            return '<a target="_black" href="'+url+'">'+(label || url)+'</a>';
85        },
86        _onSave: function(evt) {
87            lang.mixin(this.surveyRun,this.propertiesForm.get('value'));
88            var not = function(p){ return !p; };
89            func.modPropIf(this.surveyRun,"startDate",not.compose(lang.isString),store.formatDate);
90            func.modPropIf(this.surveyRun,"endDate",not.compose(lang.isString),store.formatDate);
91            store.put(this.surveyRun)
92            .then(function() {
93                Router.go('/surveys');
94            },function(err){
95                Content.notify(err);
96            });
97            event.stop(evt);
98            return false;
99        },
100        _onDiscard: function(evt) {
101            Router.go('/surveys');
102        }
103    });
104});
105
Note: See TracBrowser for help on using the repository browser.