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

Last change on this file since 492 was 492, checked in by hendrikvanantwerpen, 11 years ago
  • Enable/disable buttons on content change.
  • One place to do date formatting, because it was going wrong again.
  • Serialize questions in survey properly.
  • _ComplexValueMixin consumes submit events, but does trigger outer forms if present.
  • Trigger dialog show/hide for login only after previous effect is finished.
  • Check that documents are actually valid, not just that validator returned a result.
  • Validate email and timestamp formats.
  • Prepared for live runs.
File size: 4.9 KB
Line 
1define([
2    "../app/Content",
3    "../app/Path",
4    "../app/Router",
5    "../lib/func",
6    "../model/classes/responses",
7    "../model/classes/surveyRuns",
8    "../model/classes/surveys",
9    "../widgets/LineWithActionsWidget",
10    "./_ObjectPage",
11    "dojo/Deferred",
12    "dojo/_base/array",
13    "dojo/_base/declare",
14    "dojo/_base/event",
15    "dojo/_base/lang",
16    "dojo/when",
17    "require",
18    "dojo/text!./templates/surveyRun.html"
19], function(Content, Path, Router, func, responses, surveyRuns, surveys, LineWithActionsWidget, _ObjectPage, Deferred, array, declare, event, lang, when, require, template) {
20    return declare([_ObjectPage],{
21        contextRequire: require,
22        templateString: template,
23        classStore: surveyRuns,
24        startup: function() {
25            if ( this._started ) { return; }
26            this.inherited(arguments);
27            this.surveyRunWidget.on("change",lang.hitch(this,'_onPropChange'));
28            this._load();
29        },
30        _refresh: function() {
31            this.titleNode.innerHTML = this.object.title || "";
32            this.surveySummaryWidget.set('value',this.object.survey);
33            this.surveyRunWidget.set('value',this.object);
34            this._refreshURL();
35            this._loadResponses();
36        },
37        _refreshURL: function() {
38            if ( this.object.mode === "open" ) {
39                this.runURLNode.innerHTML =
40                    this._link(this._buildGeneralURL(this.object));
41            } else {
42                this.runURLNode.innerHTML =
43                    "No general URL. Add individual respondents below.";
44            }
45        },
46        _loadResponses: function() {
47            responses.query({surveyRunId:surveyRuns.getId(this.object)})
48            .then(lang.hitch(this,function(allResponses){
49                array.forEach(allResponses, function(response){
50                    var actions = {
51                        view: {
52                            callback: function(){},
53                            properties: {
54                                title: "View response"
55                            }
56                        }
57                    };
58                    if ( !response.publicationDate ) {
59                        actions.remove = {
60                            callback: function(){},
61                            properties: {
62                                title: "Remove response"
63                            }
64                        };
65                    }
66                    var w = new LineWithActionsWidget({
67                        actions: actions
68                    });
69                    var rid = responses.getId(response);
70                    w.set('title',this._link(this._buildResponseURL(response),rid),rid);
71                    w.placeAt(this.responsesNode);
72                }, this);
73            }));
74        },
75        _onPropChange: function(e) {
76            if ( this._updateObject() ) {
77                this._refreshURL();
78            }
79            this.markDirty();
80        },
81        _buildGeneralURL: function(surveyRun) {
82            return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret});
83        },
84        _buildResponseURL: function(response) {
85            return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret});
86        },
87        _link: function(url,label) {
88            return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>';
89        },
90        _save: function() {
91            if ( this._updateObject() ) {
92                return this.inherited(arguments);
93            } else {
94                return new Deferred.reject();
95            }
96        },
97        _updateObject: function() {
98            var valid = this.surveyRunWidget.validate();
99            if ( valid ) {
100                lang.mixin(this.object,this.surveyRunWidget.get('value'));
101            }
102            return valid;
103        },
104        _onSave: function(evt) {
105            this._save();
106            if ( evt ) { event.stop(evt); }
107            return false;
108        },
109        _onSaveAndClose: function(evt) {
110            this._save()
111            .then(function() {
112                Router.go(surveys.getCollectionPath());
113            });
114            if ( evt ) { event.stop(evt); }
115            return false;
116        },
117        _onDiscard: function(evt) {
118            this.markClean();
119            Router.go(surveys.getCollectionPath());
120            if ( evt ) { event.stop(evt); }
121            return false;
122        },
123        markDirty: function() {
124            this.saveBtn.set('disabled',false);
125            this.saveAndCloseBtn.set('disabled',false);
126            this.discardBtn.set('label','Discard & Close');
127            this.inherited(arguments);
128        },
129        markClean: function() {
130            this.saveBtn.set('disabled',true);
131            this.saveAndCloseBtn.set('disabled',true);
132            this.discardBtn.set('label','Close');
133            this.inherited(arguments);
134        }
135    });
136});
137
Note: See TracBrowser for help on using the repository browser.