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

Last change on this file since 452 was 443, checked in by hendrikvanantwerpen, 12 years ago

Reorganized for Node --- the SVN gods hate us all!

Lost all historical info on moved files, because SVN is a f *.

Also we have Node now, serving both the static content and forwarding
database requests.

File size: 4.0 KB
Line 
1define([
2    "../app/Content",
3    "../app/Page",
4    "../app/Router",
5    "../lib/func",
6    "../model/classes/SurveyRun",
7    "../store",
8    "../widgets/LineWithActionsWidget",
9    "dojo/_base/declare",
10    "dojo/_base/event",
11    "dojo/_base/lang",
12    "dojo/when",
13    "require",
14    "dojo/text!./templates/surveyRun.html"
15], function(Content, Page, Router, func, SurveyRun, store, LineWithActionsWidget, declare, event, lang, when, require, template) {
16    return declare([Page],{
17        contextRequire: require,
18        templateString: template,
19        surveyRun: null,
20        startup: function() {
21            if ( this._started ) { return; }
22            this.inherited(arguments);
23            this.propertiesForm.on("blur",lang.hitch(this,'_onPropChange'));
24            if ( this.surveyRunId ) {
25                this._loadSurveyRun();
26                this._loadResponses();
27            } else {
28                throw "No valid uid or survey passed!";
29            }
30        },
31        _loadSurveyRun: function() {
32            when(store.get(this.surveyRunId))
33            .then(lang.hitch(this,function(surveyRun){
34                this.surveyRun = surveyRun;
35                this.refreshSurveyRun();
36            }));
37        },
38        refreshSurveyRun: function() {
39            this.titleNode.innerHTML = SurveyRun.DisplayTitle.get(this.surveyRun);
40            this.surveyNode.set('value',SurveyRun.Survey.get(this.surveyRun));
41            this.propertiesForm.set('value',this.surveyRun);
42            this._onPropChange();
43        },
44        _loadResponses: function() {
45            when(store.query("_design/responses/_view/by_surveyrun",{key:this.surveyRunId}))
46            .forEach(lang.hitch(this,function(response){
47                var actions = {
48                    view: {
49                        callback: function(){},
50                        properties: {
51                            title: "View response"
52                        }
53                    }
54                };
55                if ( !response.publicationDate ) {
56                    actions.remove = {
57                        callback: function(){},
58                        properties: {
59                            title: "Remove response"
60                        }
61                    };
62                }
63                var w = new LineWithActionsWidget({
64                    actions: actions
65                });
66                var responseId = store.getIdentity(response);
67                w.set('title',this._link(this._getResponseURL(this.surveyRunId,responseId),responseId));
68                w.placeAt(this.responsesNode);
69            }));
70        },
71        _onPropChange: function(e) {
72            var surveyRun = this.propertiesForm.get('value');
73            if ( surveyRun.mode === "open" ) {
74                this.runURLNode.innerHTML = this._link(this._getGeneralURL(store.getIdentity(this.surveyRun)));
75            } else {
76                this.runURLNode.innerHTML = "No general URL. Add individual respondents below.";
77            }
78        },
79        _getGeneralURL: function(surveyRunId) {
80            return 'response.html#!/'+surveyRunId;
81        },
82        _getResponseURL: function(surveyRunId,responseId) {
83            return 'response.html#!/'+surveyRunId+'!id='+responseId;
84        },
85        _link: function(url,label) {
86            return '<a target="_black" href="'+url+'">'+(label || url)+'</a>';
87        },
88        _onSave: function(evt) {
89            lang.mixin(this.surveyRun,this.propertiesForm.get('value'));
90            var not = function(p){ return !p; };
91            func.modPropIf(this.surveyRun,"startDate",not.compose(lang.isString),store.formatDate);
92            func.modPropIf(this.surveyRun,"endDate",not.compose(lang.isString),store.formatDate);
93            store.put(this.surveyRun)
94            .then(function() {
95                Router.go('/surveys');
96            },function(err){
97                Content.notify(err);
98            });
99            event.stop(evt);
100            return false;
101        },
102        _onDiscard: function(evt) {
103            Router.go('/surveys');
104        }
105    });
106});
107
Note: See TracBrowser for help on using the repository browser.