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

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

Completed migration to API, without CouchDB proxy.

Move to API is now completed. The full API is password protected, a very
limited API is exposed for respondents, which works with secrets that
are passed in URLs.

Serverside the HTTPResult class was introduced, which is similar to
Promises, but specifically for HTTP. It carries a status code and
response and makes it easier to extract parts of async handling in
separate functions.

Fixed a bug in our schema (it seems optional attributes don't exist but
a required list does). Verification of our schema by grunt-tv4 didn't
work yet. Our schema is organized the wrong way (this is fixable),
but the json-schema schema has problems with simple types and $refs.

File size: 4.1 KB
Line 
1define([
2    "../app/Content",
3    "../app/Page",
4    "../lib/async",
5    "../model/classes/responses",
6    "dojo/_base/declare",
7    "dojo/_base/event",
8    "dojo/_base/json",
9    "dojo/_base/lang",
10    "dojo/promise/all",
11    "dojo/request",
12    "dojo/when",
13    "require",
14    "dojo/text!./templates/response.html"
15], function(Content, Page, async, responses, declare, event, json, lang, all, request, when, require, template) {
16    return declare([Page],{
17        contextRequire: require,
18        templateString: template,
19        response: null,
20        postCreate: function(){
21            this.options = this.options || {};
22        },
23        startup: function() {
24            if ( this._started ) { return; }
25            this.inherited(arguments);
26            this._disableSubmit();
27            if ( !this.response ) {
28                this._showInfo("<div>The url seems to be incorrect, no response found.</div>");
29            } else {
30                this.titleNode.innerHTML = this.response._surveyRun.survey.title || "";
31                this.surveyWidget.set('survey', this.response._surveyRun.survey);
32                this.surveyWidget.set('value', this.response.answers || {});
33                if ( this.response.publicationDate ) {
34                    this._showInfo("<div>You already submitted your survey and cannot change it anymore. You can still view your answers here.</div>");
35                    this._disableSubmit();
36                } else {
37                    this._enableSubmit();
38                }
39            }
40        },
41        _enableSubmit: function() {
42            this.submitButton.set('disabled',false);
43            this.continueButton.set('disabled',false);
44            this.cancelButton.set('disabled',false);
45            this.surveyWidget.set('disabled', 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('disabled', 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                Content.notify("Your response is saved.");
63            }), function(err){
64                Content.notify(err,'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                Content.notify("Your response is deleted.");
92            }), function(err){
93                Content.notify(err,'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.