source: Dev/trunk/src/client/qed-client/pages/response.js @ 506

Last change on this file since 506 was 503, checked in by hendrikvanantwerpen, 11 years ago

Don't delete elements from certain lists if they are disabled
or readOnly. Didn't include this in ListWidget?.removeItem, since
programmatically things should keep working even when disabled, I would
say. So this needs to be done in the place of the event.

File size: 4.2 KB
Line 
1define([
2    "../app/Page",
3    "../model/classes/responses",
4    "dojo/_base/declare",
5    "dojo/_base/event",
6    "dojo/_base/json",
7    "dojo/_base/lang",
8    "dojo/promise/all",
9    "dojo/request",
10    "dojo/when",
11    "require",
12    "dojo/text!./templates/response.html"
13], function(Page, responses, declare, event, json, lang, all, request, when, require, template) {
14    return declare([Page],{
15        contextRequire: require,
16        templateString: template,
17        response: null,
18        postCreate: function(){
19            this.options = this.options || {};
20        },
21        startup: function() {
22            if ( this._started ) { return; }
23            this.inherited(arguments);
24            this._disableSubmit();
25            if ( !this.response ) {
26                this._showInfo("<div>The url seems to be incorrect, no response found.</div>");
27            } else {
28                this.titleNode.innerHTML = this.response._surveyRun.survey.title || "";
29                this.surveyWidget.set('survey', this.response._surveyRun.survey);
30                this.surveyWidget.set('value', this.response.answers || {});
31                if ( this.response.publicationDate ) {
32                    this._showInfo("<div>You already submitted your survey and cannot change it anymore. You can still view your answers here.</div>");
33                    this._disableSubmit();
34                } else {
35                    this._enableSubmit();
36                }
37            }
38        },
39        _enableSubmit: function() {
40            this.submitButton.set('disabled',false);
41            this.continueButton.set('disabled',false);
42            var canDelete = (this.response &&
43                             this.response._surveyRun.respondentCanDeleteOwnResponse);
44            this.cancelButton.set('disabled',canDelete||false);
45            this.surveyWidget.set('readOnly', false);
46        },
47        _disableSubmit: function() {
48            this.submitButton.set('disabled',true);
49            this.continueButton.set('disabled',true);
50            this.cancelButton.set('disabled',true);
51            this.surveyWidget.set('readOnly', true);
52        },
53        _showInfo: function(html) {
54            this.infoNode.innerHTML = html;
55        },
56        _getAnswersAndSaveResponse: function() {
57            var answers = this.surveyWidget.get('value');
58            this.response.answers = answers;
59            return responses.putWithSecret(this.response,this.response.secret)
60            .then(lang.hitch(this,function(response){
61                this.response = response;
62                this.notify("Your response is saved.");
63            }), lang.hitch(this,function(err){
64                this.notify(err.error,'error');
65            }));
66        },
67        _onSubmit: function(e) {
68            this.response.publicationDate = new Date();
69            this._getAnswersAndSaveResponse()
70            .then(lang.hitch(this,function(){
71                this._showInfo("<div>Thanks for filling in the survey. You cannot edit your answers anymore.</div>");
72                this._disableSubmit();
73            }));
74            if ( e ) { event.stop(e); }
75            return false;
76        },
77        _onContinueLater: function(e) {
78            this._getAnswersAndSaveResponse()
79            .then(lang.hitch(this,function(){
80                this._showInfo("<div>To continue with this survey later, just save the URL in the location bar and revisit it later. Your answers will still be there.</div>");
81            }));
82            if ( e ) { event.stop(e); }
83            return false;
84        },
85        _onCancel: function(e) {
86            this._disableSubmit();
87            this.surveyWidget.destroy();
88            responses.removeWithSecret(this.response,this.response.secret)
89            .then(lang.hitch(this,function(res){
90                this._showInfo("<div>Your response has been deleted, no answers have been saved.</div>");
91                this.notify("Your response is deleted.");
92            }), lang.hitch(this,function(err){
93                this.notify(err.error,'error');
94            }));
95            if ( e ) { event.stop(e); }
96            return false;
97        },
98        _ignoreEvent: function(e) {
99            if ( e ) { event.stop(e); }
100            return false;
101        }
102    });
103});
Note: See TracBrowser for help on using the repository browser.