1/**
2 * Dokuwki Survey Plugin Frontend Script
3 */
4
5function Dokuwiki_Survey(surveyId, surveyConfig) {
6
7    this.surveyId = surveyId;
8    this.surveyConfig = surveyConfig;
9
10    // Skip root
11
12    this.currentSurvey = this.surveyConfig._children[0];
13    this.currentSurvey.lastSurvey = this.surveyConfig;
14
15};
16
17Dokuwiki_Survey.prototype.goToSurvey = function (childId) {
18
19    var tmpLastSurvey;
20
21    if (childId == -1) {
22
23        this.currentSurvey = this.currentSurvey.lastSurvey;
24
25    } else {
26
27        tmpLastSurvey = this.currentSurvey;
28
29        this.currentSurvey = this.currentSurvey._children[childId]._children[0];
30        this.currentSurvey.lastSurvey = tmpLastSurvey;
31
32    }
33
34    this.makeSurvey();
35
36};
37
38Dokuwiki_Survey.prototype.makeSurvey = function () {
39
40    var answerHtml,
41        lastSurvey,
42        surveyElement,
43        surveyConfig,
44        surveyQuestion,
45        surveyAnswers,
46        currentAnswer;
47
48    surveyConfig = this.currentSurvey;
49
50    surveyElement = document.getElementById("survey_" + this.surveyId);
51    lastSurvey = surveyElement.getElementsByClassName("lastSurvey")[0];
52    surveyQuestion = surveyElement.getElementsByClassName("surveyQuestion")[0];
53    surveyAnswers = surveyElement.getElementsByClassName("surveyAnswers")[0];
54
55    if (surveyConfig.lastSurvey._name !== "root") {
56
57        lastSurvey.onClick = "survey[this.surveyId].goToSurvey(-1)";
58        lastSurvey.innerHTML = "<p onClick=\"" +
59            "survey[" +
60            this.surveyId +
61            "].goToSurvey(-1)\">" +
62            this.lang.back +
63            "</p>\n";
64
65        lastSurvey.style.display = "block";
66
67    } else {
68
69        lastSurvey.innerHTML = "";
70        lastSurvey.style.display = "none";
71
72    }
73
74    surveyQuestion.innerHTML = surveyConfig._name;
75    answerHtml = "<ul>";
76
77    for (currentAnswer in surveyConfig._children) {
78
79        if (surveyConfig._children.hasOwnProperty(currentAnswer)) {
80
81            if (surveyConfig._children[currentAnswer]["_hasChildren"]) {
82
83                answerHtml = answerHtml +
84                    "  <li onClick=\"" +
85                    "survey[" +
86                    this.surveyId +
87                    "].goToSurvey(" +
88                    currentAnswer +
89                    ")\">" +
90                    surveyConfig._children[currentAnswer]["_name"] +
91                    "</li>\n";
92            } else {
93
94                answerHtml = answerHtml +
95                    "  <li>" +
96                    surveyConfig._children[currentAnswer]["_name"] +
97                    "</li>\n";
98
99            }
100        }
101
102    }
103
104    answerHtml = answerHtml + "</ul>";
105
106    surveyAnswers.innerHTML = answerHtml;
107
108};