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

Last change on this file since 494 was 494, checked in by hendrikvanantwerpen, 11 years ago
  • Removed all Coffeescript from codebase (build process is still there).
  • Nicer message on failed login.
  • Own all event subscriptions from widgets.
  • Update objects in _ObjectPage with invalid info too, or our refresh will overwrite what user did.
File size: 5.0 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,'_onPropChange')));
29            this._load();
30        },
31        _refresh: function() {
32            this.titleNode.innerHTML = this.object.title || "";
33            this.surveySummaryWidget.set('value',this.object.survey,null);
34            this.surveyRunWidget.set('value',this.object,null);
35            this._refreshURL();
36            this._loadResponses();
37        },
38        _refreshURL: function() {
39            if ( this.object.mode === "open" ) {
40                this.runURLNode.innerHTML =
41                    this._link(this._buildGeneralURL(this.object));
42            } else {
43                this.runURLNode.innerHTML =
44                    "No general URL. Add individual respondents below.";
45            }
46        },
47        _loadResponses: function() {
48            responses.query({surveyRunId:surveyRuns.getId(this.object)})
49            .then(lang.hitch(this,function(allResponses){
50                array.forEach(allResponses, function(response){
51                    var actions = {
52                        view: {
53                            callback: function(){},
54                            properties: {
55                                title: "View response"
56                            }
57                        }
58                    };
59                    if ( !response.publicationDate ) {
60                        actions.remove = {
61                            callback: function(){},
62                            properties: {
63                                title: "Remove response"
64                            }
65                        };
66                    }
67                    var w = new LineWithActionsWidget({
68                        actions: actions
69                    });
70                    var rid = responses.getId(response);
71                    w.set('title',this._link(this._buildResponseURL(response),rid),rid);
72                    w.placeAt(this.responsesNode);
73                }, this);
74            }));
75        },
76        _onPropChange: function(e) {
77            if ( this._updateObject() ) {
78                this._refreshURL();
79            }
80            this.markDirty();
81        },
82        _buildGeneralURL: function(surveyRun) {
83            return 'response.html#'+Path.format(surveyRuns.getObjectPath(surveyRun),{secret:surveyRun.secret});
84        },
85        _buildResponseURL: function(response) {
86            return 'response.html#'+Path.format(responses.getObjectPath(response),{secret:response.secret});
87        },
88        _link: function(url,label) {
89            return '<a target="_blank" href="'+url+'">'+(label || url)+'</a>';
90        },
91        _save: function() {
92            if ( this._updateObject() ) {
93                return this.inherited(arguments);
94            } else {
95                return new Deferred.reject();
96            }
97        },
98        _updateObject: function() {
99            this._isValid = this.surveyRunWidget.validate();
100            lang.mixin(this.object,this.surveyRunWidget.get('value'));
101            return this._isValid;
102        },
103        _onSave: function(evt) {
104            this._save();
105            if ( evt ) { event.stop(evt); }
106            return false;
107        },
108        _onSaveAndClose: function(evt) {
109            this._save()
110            .then(function() {
111                Router.go(surveys.getCollectionPath());
112            });
113            if ( evt ) { event.stop(evt); }
114            return false;
115        },
116        _onDiscard: function(evt) {
117            this.markClean();
118            Router.go(surveys.getCollectionPath());
119            if ( evt ) { event.stop(evt); }
120            return false;
121        },
122        markDirty: function() {
123            this.saveBtn.set('disabled',!this._isValid);
124            this.saveAndCloseBtn.set('disabled',!this._isValid);
125            this.discardBtn.set('label','Discard & Close');
126            this.inherited(arguments);
127        },
128        markClean: function() {
129            this.saveBtn.set('disabled',true);
130            this.saveAndCloseBtn.set('disabled',true);
131            this.discardBtn.set('label','Close');
132            this.inherited(arguments);
133        }
134    });
135});
136
Note: See TracBrowser for help on using the repository browser.