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

Last change on this file since 506 was 506, checked in by hendrikvanantwerpen, 11 years ago
  • Added subcodes to schema and config widgets.
  • Disallow empty strings in schema and strip objects before sending them to the server.
  • Finally managed proper change events in lists and complexvalues.
File size: 5.0 KB
Line 
1define([
2    "../app/Content",
3    "../app/Path",
4    "../app/Router",
5    "../model/classes/responses",
6    "../model/classes/surveyRuns",
7    "../model/classes/surveys",
8    "../widgets/LineWithActionsWidget",
9    "./_ObjectPage",
10    "dojo/Deferred",
11    "dojo/_base/array",
12    "dojo/_base/declare",
13    "dojo/_base/event",
14    "dojo/_base/lang",
15    "dojo/when",
16    "require",
17    "dojo/text!./templates/surveyRun.html"
18], function(Content, Path, Router, responses, surveyRuns, surveys, LineWithActionsWidget, _ObjectPage, Deferred, array, declare, event, lang, when, require, template) {
19    return declare([_ObjectPage],{
20        contextRequire: require,
21        templateString: template,
22        classStore: surveyRuns,
23        _isValid: false,
24        startup: function() {
25            if ( this._started ) { return; }
26            this.inherited(arguments);
27            this.own(this.surveyRunWidget.on("change",lang.hitch(this,'_handlePropChange')));
28            this._load();
29        },
30        _refresh: function(initial) {
31            if ( initial === true ) {
32                this.surveySummaryWidget.set('value',this.object.survey,null);
33                this.surveyRunWidget.set('value',this.object,null);
34                this._loadResponses();
35            }
36            this.titleNode.innerHTML = this.object.title || "";
37            if ( this.object.mode === "open" ) {
38                this.runURLNode.innerHTML =
39                    this._link(this._buildGeneralURL(this.object));
40            } else {
41                this.runURLNode.innerHTML =
42                    "No general URL. Add individual respondents below.";
43            }
44        },
45        _loadResponses: function() {
46            responses.query({surveyRunId:surveyRuns.getId(this.object)})
47            .then(lang.hitch(this,function(allResponses){
48                array.forEach(allResponses, function(response){
49                    var actions = {
50                        view: {
51                            callback: function(){},
52                            properties: {
53                                title: "View response"
54                            }
55                        }
56                    };
57                    if ( !response.publicationDate ) {
58                        actions.remove = {
59                            callback: function(){},
60                            properties: {
61                                title: "Remove response"
62                            }
63                        };
64                    }
65                    var w = new LineWithActionsWidget({
66                        actions: actions
67                    });
68                    var rid = responses.getId(response);
69                    w.set('title',this._link(this._buildResponseURL(response),rid),rid);
70                    w.placeAt(this.responsesNode);
71                }, this);
72            }));
73        },
74        _handlePropChange: function(e) {
75            this._updateObject();
76            this.markDirty();
77            this._refresh();
78        },
79        _buildGeneralURL: function(surveyRun) {
80            return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret});
81        },
82        _buildResponseURL: function(response) {
83            return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret});
84        },
85        _link: function(url,label) {
86            return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>';
87        },
88        _save: function() {
89            if ( this._updateObject() ) {
90                return this.inherited(arguments);
91            } else {
92                return new Deferred().reject({error:"Please correct invalid values."});
93            }
94        },
95        _updateObject: function() {
96            this._isValid = this.surveyRunWidget.validate();
97            lang.mixin(this.object,this.surveyRunWidget.get('value'));
98            return this._isValid;
99        },
100        _onSave: function(evt) {
101            this._save();
102            if ( evt ) { event.stop(evt); }
103            return false;
104        },
105        _onSaveAndClose: function(evt) {
106            this._save()
107            .then(function() {
108                Router.go(surveys.getCollectionPath());
109            });
110            if ( evt ) { event.stop(evt); }
111            return false;
112        },
113        _onDiscard: function(evt) {
114            this.markClean();
115            Router.go(surveys.getCollectionPath());
116            if ( evt ) { event.stop(evt); }
117            return false;
118        },
119        markDirty: function() {
120            this.saveBtn.set('disabled',!this._isValid);
121            this.saveAndCloseBtn.set('disabled',!this._isValid);
122            this.discardBtn.set('label','Discard & Close');
123            this.inherited(arguments);
124        },
125        markClean: function() {
126            this.saveBtn.set('disabled',true);
127            this.saveAndCloseBtn.set('disabled',true);
128            this.discardBtn.set('label','Close');
129            this.inherited(arguments);
130        }
131    });
132});
133
Note: See TracBrowser for help on using the repository browser.