-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy patheditsurveyquestions.tpl
183 lines (167 loc) · 6.82 KB
/
editsurveyquestions.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<div>
<input type="hidden" name="survey_questions_supplied" value="1">
<table id="questionTable" class="table table-bordered">
<tr>
<th>
Question
</th>
<th>
Text
</th>
<th>
Type
</th>
<th>Actions</th>
</tr>
</table>
<button type="button" class="button" id="newQuestionButton">New Question</button>
</div>
{literal}
<script>
$(document).ready(function () {
function updateType(ev) {
var target = $(ev.target);
var options = target.closest('tbody').find('.questionOptions');
switch (target.val()) {
case "Radio":
case "Dropdown":
case "Multiselect":
case "Matrix":
options.show();
break;
case "Rating":
case "Scale":
case "DateTime":
case "Date":
case "Textbox":
case "Checkbox":
case "Text":
default:
options.hide();
break;
}
}
function addOption(ev, data) {
var target = $(ev.target);
var questionIndex = target.data('question-index');
var clearImage = 'themes/' + SUGAR.themes.theme_name + "/images/id-ff-clear.png";
var list = target.closest('td').find('ul');
var html = "<li>"
+ "<input type='hidden' class='survey_question_options_id' name='survey_questions_options_id[" + questionIndex + "][]'/>"
+ "<input type='hidden' class='survey_question_options_deleted' name='survey_questions_options_deleted[" + questionIndex + "][]' value='0'/>"
+ "<input class='survey_question_options' name='survey_questions_options[" + questionIndex + "][]' type='text'/>"
+ "<button type='button' class='button deleteQuestionOption'><img src='" + clearImage + "'/></button>"
+ "</li>";
var item = $(html);
if (data) {
item.find('.survey_question_options').val(data.name);
item.find('.survey_question_options_id').val(data.id);
}
item.find('.deleteQuestionOption').click(deleteQuestionOption);
list.append(item);
}
function deleteQuestionOption(e) {
var target = $(e.target);
var li = target.closest('li');
li.hide();
li.find('.survey_question_options_deleted').val(1);
}
function moveQuestionUp(e) {
var target = $(e.target);
var tbody = target.closest('tbody');
tbody.insertBefore(tbody.prev('.questionBody'));
fixSortOrders();
}
function moveQuestionDown(e) {
var target = $(e.target);
var tbody = target.closest('tbody');
tbody.insertAfter(tbody.next());
fixSortOrders();
}
function deleteQuestion(e) {
var target = $(e.target);
var tbody = target.closest('tbody');
tbody.find('.surveyQuestionDeleted').val(1);
tbody.hide();
fixSortOrders();
}
function fixSortOrders() {
var count = 0;
$('.surveyQuestionSortOrder').each(function (ind, e) {
var elem = $(e);
elem.val(count);
var deleted = elem.siblings('.surveyQuestionDeleted').get(0).value;
if (deleted == 1) {
return;
}
elem.closest('tbody').find('.nameSpan').text("Q" + (count + 1));
count++;
});
}
function createQuestion(data, existing) {
var table = $('#questionTable');
var newRow = "<tbody class='questionBody'><tr data-question-index='" + createQuestion.questionCount + "'>";
newRow += "<td class='nameCell'><span class='nameSpan'>Q" + (createQuestion.questionCount + 1) + "</span>"
+ "<input type='hidden' class='surveyQuestionId' name='survey_questions_ids[" + createQuestion.questionCount + "]'> "
+ "<input type='hidden' class='surveyQuestionDeleted' name='survey_questions_deleted[" + createQuestion.questionCount + "]' value='0'> "
+ "<input type='hidden' class='surveyQuestionSortOrder' name='survey_questions_sortorder[" + createQuestion.questionCount + "]' value='" + createQuestion.questionCount + "'> "
+ "</td>";
newRow += "<td><input class='surveyQuestionName' name='survey_questions_names[" + createQuestion.questionCount + "]' type='text'></td>";
newRow += "<td><select class='surveyQuestionType' name='survey_questions_types[" + createQuestion.questionCount + "]'>{/literal}{$question_type_options}{literal}</select></td>";
newRow += "<td>";
var clearImage = 'themes/' + SUGAR.themes.theme_name + "/images/id-ff-clear.png";
var upImage = 'themes/' + SUGAR.themes.theme_name + "/images/arrow_up.png";
var downImage = 'themes/' + SUGAR.themes.theme_name + "/images/arrow_down.png";
newRow += "<button type='button' class='button moveQuestionUp'><img src='" + upImage + "'/></button>";
newRow += "<button type='button' class='button moveQuestionDown'><img src='" + downImage + "'/></button>";
newRow += " ";
newRow += "<button type='button' class='button deleteQuestion'><img src='" + clearImage + "'/></button>";
newRow += "</td>";
newRow += "</tr>";
newRow += "<tr class='questionOptions' style='display: none;'>";
newRow += "<td>";
newRow += "</td>";
newRow += "<td>";
newRow += "</td>";
newRow += "<td>";
newRow += "<strong>Options</strong>";
newRow += "<ul style='list-style: none;' class='optionList'>";
newRow += "</ul>";
newRow += "<button class='addOptionButton button' data-question-index='" + createQuestion.questionCount + "' type='button'>Add option</button>";
newRow += "</td>";
newRow += "<td>";
newRow += "</td>";
newRow += "</tr>";
newRow += "</tbody>";
newRow = $(newRow);
if (existing) {
newRow.find('.surveyQuestionName').val(data.name);
newRow.find('.surveyQuestionType').val(data.type);
newRow.find('.surveyQuestionId').val(data.id);
}
newRow.find('.addOptionButton').click(addOption);
if (typeof data.options !== 'undefined') {
for (var x = 0; x < data.options.length; x++) {
addOption({target: newRow.find('.addOptionButton')}, data.options[x]);
}
}
newRow.find('.surveyQuestionType').change(updateType);
newRow.find('.moveQuestionUp').click(moveQuestionUp);
newRow.find('.moveQuestionDown').click(moveQuestionDown);
newRow.find('.deleteQuestion').click(deleteQuestion);
//Add to DOM first so we trigger any external change listeners.
table.append(newRow);
newRow.find('.surveyQuestionType').change();
createQuestion.questionCount++;
fixSortOrders();
}
createQuestion.questionCount = 0;
{/literal}
{foreach from=$questions item=question}
createQuestion({$question|@json_encode}, true);
{/foreach}
{literal}
$('#newQuestionButton').click(createQuestion);
});
</script>
{/literal}