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

Last change on this file since 491 was 490, checked in by hendrikvanantwerpen, 11 years ago
  • Mark content as dirty to prevent moving away from unsaved data.
  • Better change propagation from lists and our own widgets.
  • Generate notifications for errors and show correct message.
  • Moved all path/url generation to the class stores, not everywhere we use it.
  • Give user always a choice between Save and Save & Close.
  • Better refresh behaviour on form changes and saves.
  • Don't generate duplicate code error when existing object is the one you're storing.
File size: 4.1 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            this.cancelButton.set('disabled',false);
43            this.surveyWidget.set('disabled', false);
44        },
45        _disableSubmit: function() {
46            this.submitButton.set('disabled',true);
47            this.continueButton.set('disabled',true);
48            this.cancelButton.set('disabled',true);
49            this.surveyWidget.set('disabled', true);
50        },
51        _showInfo: function(html) {
52            this.infoNode.innerHTML = html;
53        },
54        _getAnswersAndSaveResponse: function() {
55            var answers = this.surveyWidget.get('value');
56            this.response.answers = answers;
57            return responses.putWithSecret(this.response,this.response.secret)
58            .then(lang.hitch(this,function(response){
59                this.response = response;
60                this.notify("Your response is saved.");
61            }), lang.hitch(this,function(err){
62                this.notify(err.error,'error');
63            }));
64        },
65        _onSubmit: function(e) {
66            this.response.publicationDate = new Date();
67            this._getAnswersAndSaveResponse()
68            .then(lang.hitch(this,function(){
69                this._showInfo("<div>Thanks for filling in the survey. You cannot edit your answers anymore.</div>");
70                this._disableSubmit();
71            }));
72            if ( e ) { event.stop(e); }
73            return false;
74        },
75        _onContinueLater: function(e) {
76            this._getAnswersAndSaveResponse()
77            .then(lang.hitch(this,function(){
78                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>");
79            }));
80            if ( e ) { event.stop(e); }
81            return false;
82        },
83        _onCancel: function(e) {
84            this._disableSubmit();
85            this.surveyWidget.destroy();
86            responses.removeWithSecret(this.response,this.response.secret)
87            .then(lang.hitch(this,function(res){
88                this._showInfo("<div>Your response has been deleted, no answers have been saved.</div>");
89                this.notify("Your response is deleted.");
90            }), lang.hitch(this,function(err){
91                this.notify(err.error,'error');
92            }));
93            if ( e ) { event.stop(e); }
94            return false;
95        },
96        _ignoreEvent: function(e) {
97            if ( e ) { event.stop(e); }
98            return false;
99        }
100    });
101});
Note: See TracBrowser for help on using the repository browser.