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

Last change on this file since 500 was 500, checked in by hendrikvanantwerpen, 11 years ago
  • Indicate of refresh is due to load or due to save in _ObjectPage.
  • _ComplexValueMixin & ListWidget? catch all change events and drops them if still being created.
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        _isValid: false,
25        startup: function() {
26            if ( this._started ) { return; }
27            this.inherited(arguments);
28            this.own(this.surveyRunWidget.on("change",lang.hitch(this,'_handlePropChange')));
29            this._load();
30        },
31        _refresh: function(initial) {
32            if ( initial === true ) {
33                this.surveySummaryWidget.set('value',this.object.survey,null);
34                this.surveyRunWidget.set('value',this.object,null);
35                this._loadResponses();
36            }
37            this.titleNode.innerHTML = this.object.title || "";
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        _handlePropChange: function(e) {
76            this._updateObject();
77            this.markDirty();
78            this._refresh();
79        },
80        _buildGeneralURL: function(surveyRun) {
81            return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret});
82        },
83        _buildResponseURL: function(response) {
84            return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret});
85        },
86        _link: function(url,label) {
87            return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>';
88        },
89        _save: function() {
90            if ( this._updateObject() ) {
91                return this.inherited(arguments);
92            } else {
93                return new Deferred.reject();
94            }
95        },
96        _updateObject: function() {
97            this._isValid = this.surveyRunWidget.validate();
98            lang.mixin(this.object,this.surveyRunWidget.get('value'));
99            return this._isValid;
100        },
101        _onSave: function(evt) {
102            this._save();
103            if ( evt ) { event.stop(evt); }
104            return false;
105        },
106        _onSaveAndClose: function(evt) {
107            this._save()
108            .then(function() {
109                Router.go(surveys.getCollectionPath());
110            });
111            if ( evt ) { event.stop(evt); }
112            return false;
113        },
114        _onDiscard: function(evt) {
115            this.markClean();
116            Router.go(surveys.getCollectionPath());
117            if ( evt ) { event.stop(evt); }
118            return false;
119        },
120        markDirty: function() {
121            this.saveBtn.set('disabled',!this._isValid);
122            this.saveAndCloseBtn.set('disabled',!this._isValid);
123            this.discardBtn.set('label','Discard & Close');
124            this.inherited(arguments);
125        },
126        markClean: function() {
127            this.saveBtn.set('disabled',true);
128            this.saveAndCloseBtn.set('disabled',true);
129            this.discardBtn.set('label','Close');
130            this.inherited(arguments);
131        }
132    });
133});
134
Note: See TracBrowser for help on using the repository browser.