source: Dev/trunk/src/client/qed-client/pages/surveyRun.js @ 466

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

Added authentication (fixed user now).

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