/
questions.js
306 lines (280 loc) · 12.9 KB
/
questions.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* LimeSurvey (tm)
* Copyright (C) 2012-2016 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v3 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
var LS = LS || {
onDocumentReady: {}
};
var QuestionFunctions = function () {
var
/**
* Validate question object before submit : actually only title need to be validated
* This disallow submitting if Question code are not unique (else loose all fields)
*/
validateQuestion = function (jqObject) {
if (typeof jqObject == "undefined") {
jqObject = $([]);
}
$.post(
validateUrl,
{
title: $('#frmeditquestion [name="title"]:first').val(),
other: $('#frmeditquestion [name="other"]:checked:first').val(),
},
function (data) {
// Remove all custom validity
if (hasFormValidation) {
$('#frmeditquestion input.has-error').each(function () {
if (hasFormValidation) {
$(this)[0].setCustomValidity('');
}
$(this).removeClass("has-error");
$(this).next('.errorMessage').remove();
});
}
// No error : submit
if ($.isEmptyObject(data)) {
if ($(jqObject).is(":submit")) {
$(jqObject).trigger('click', {
validated: true
});
}
} else {
// Add error information for each input
$.each(data, function (name, aError) {
if ($(jqObject).is(":submit")) {
$("#frmeditquestion").closest("#tabs").find(".ui-tabs-anchor:first").click();
$('#frmeditquestion [type!=hidden][name="' + name + '"]').focus(); // Focus on the first input
}
$('#frmeditquestion [type!=hidden][name="' + name + '"]').addClass("has-error");
if (!$('#frmeditquestion [type!=hidden][name="' + name + '"]:last').next('.errorMessage').length) // :last for radio list
{
$("<span class='errorMessage text-warning' />").insertAfter('#frmeditquestion [type!=hidden][name="' + name + '"]:last');
}
$.each(aError, function (i, error) {
if (hasFormValidation) {
$('#frmeditquestion [type!=hidden][name="' + name + '"]').each(function () {
$(this)[0].setCustomValidity(error);
});
}
$('#frmeditquestion [type!=hidden][name="' + name + '"]').next('.errorMessage').text(error);
});
});
}
},
dataType = "json"
);
},
getQuestionTypeImage = function (questioncode) {
var multiple = 1;
if (questioncode == 'S') multiple = 2;
if (questioncode == ":") questioncode = "COLON";
else if (questioncode == "|") questioncode = "PIPE";
else if (questioncode == "*") questioncode = "EQUATION";
var imageArray = [];
for (i = 1; i <= multiple; i++) {
imageArray.push(imgurl + "/screenshots/" + questioncode + (i == 1 ? '' : i) + ".png");
}
return imageArray.reduce(function (prev, curr, idx) {
return prev + '<img src="' + curr + '" />';
}, '');
},
init = function () {
var oldQuestionTemplate = '';
updatequestionattributes('');
$('#questionTypeSelector').on('change', function(){ // Simple Question type selector in General settings
$('#question_type').val($(this).val()).trigger('change');
updatequestionattributes();
updateQuestionTemplateOptions();
});
$('#question_type').on('change', function(){
OtherSelection(this.value);
});
$(document).on('click', '#question_template', function(){
// save old value before the change
oldQuestionTemplate = $(this).val();
}).on('change', '#question_template', function() {
updatequestionattributes($(this).val(), oldQuestionTemplate);
updateQuestionTemplateOptions('question_template');
});
/**
* Validate question object on blur on title element
*/
$('#frmeditquestion :not(:hidden)[name="title"]').on('blur', function () {
validateQuestion($(this));
});
/**
* Validate question object before click on a submit button
*/
$('#frmeditquestion :submit').on('click', function (event, data) {
data = data || event.data || false;
if (data && data.validated) {
return true;
} else {
validateQuestion($(this));
return false;
}
});
};
return {
init: init
};
}
$(document).on('ready pjax:scriptcomplete', function () {
window.questionFunctions = window.questionFunctions || (new QuestionFunctions());
window.questionFunctions.init();
});
function updatequestionattributes(question_template_name = '', oldQuestionTemplate = '') {
var type = $('#question_type').val();
OtherSelection(type);
$('.loader-advancedquestionsettings').removeClass("hidden");
$('.panel-advancedquestionsettings').remove();
var selected_value = $("#question_type").val();
var postData = {
'qid': $('#qid').val(),
'question_type': selected_value,
'sid': $('input[name=sid]').val(),
'old_question_template': oldQuestionTemplate
};
if (Object.prototype.toString.call(question_template_name) == '[object String]'){
postData['question_template'] = question_template_name;
}
window.LS.unrenderBootstrapSwitch();
$.ajax({
url: attr_url,
data: postData,
method: 'POST',
success: function (data) {
$('#container-advanced-question-settings').html(data);
$('.loader-advancedquestionsettings').addClass("hidden");
window.LS.renderBootstrapSwitch();
if(question_template_name) {
//$('#collapse-cat1').collapse('toggle');
}
$('label[title]').qtip({
style: {
name: 'cream',
tip: true,
color: '#111111',
border: {
width: 1,
radius: 5,
color: '#EADF95'
}
},
position: {
adjust: {
screen: true,
scroll: true
},
corner: {
target: 'bottomRight'
}
},
show: {
effect: {
length: 50
}
}
});
}
});
}
function updateQuestionTemplateOptions(selector = '') { // selector is only set when this function is called from #question_template
var type = $('#question_type').val();
var template = $('#question_template').val();
$.ajax({
url: get_question_template_options_url,
data: {'type': type},
method: 'POST',
success: function (data) {
if ( data[template] != undefined ) {
$("#QuestionTemplatePreview img").attr('src', data[template]['preview']);
} else {
$("#QuestionTemplatePreview img").attr('src', data['core']['preview']);
}
if (selector === ''){ // selector is not called from #question_template
$("#question_template").html("");
$.each(data, function (key, value) {
$("#question_template").append("<option value="+key+">"+value.title+"</option>");
});
}
}
});
}
var qtypes = new Array();
var qnames = new Array();
var qhelp = new Array();
var qcaption = new Array();
function OtherSelection(QuestionType) {
if (QuestionType == undefined) {
//console.log('Error: OtherSelection: QuestionType must not be undefined');
return;
}
try{
if (QuestionType == '') {
QuestionType = document.getElementById('question_type').value;
}
if (QuestionType == 'M' || QuestionType == 'P' || QuestionType == 'L' || QuestionType == '!') {
document.getElementById('OtherSelection').style.display = '';
document.getElementById('Validation').style.display = 'none';
document.getElementById('MandatorySelection').style.display = '';
} else if (QuestionType == 'W' || QuestionType == 'Z') {
document.getElementById('OtherSelection').style.display = '';
document.getElementById('Validation').style.display = 'none';
document.getElementById('MandatorySelection').style.display = '';
} else if (QuestionType == '|') {
document.getElementById('OtherSelection').style.display = 'none';
document.getElementById('Validation').style.display = 'none';
document.getElementById('MandatorySelection').style.display = 'none';
} else if (QuestionType == 'F' || QuestionType == 'H') {
document.getElementById('OtherSelection').style.display = 'none';
document.getElementById('Validation').style.display = 'none';
document.getElementById('MandatorySelection').style.display = '';
} else if (QuestionType == ':' || QuestionType == ';') {
document.getElementById('OtherSelection').style.display = 'none';
document.getElementById('Validation').style.display = '';
document.getElementById('MandatorySelection').style.display = '';
} else if (QuestionType == '1') {
document.getElementById('OtherSelection').style.display = 'none';
document.getElementById('Validation').style.display = 'none';
document.getElementById('MandatorySelection').style.display = '';
} else if (QuestionType == 'S' || QuestionType == 'T' || QuestionType == 'U' || QuestionType == 'N' || QuestionType == '' || QuestionType == 'K') {
document.getElementById('Validation').style.display = '';
document.getElementById('OtherSelection').style.display = 'none';
if (document.getElementById('ON')) {
document.getElementById('ON').checked = true;
}
document.getElementById('MandatorySelection').style.display = '';
} else if (QuestionType == 'X') {
document.getElementById('Validation').style.display = 'none';
document.getElementById('OtherSelection').style.display = 'none';
document.getElementById('MandatorySelection').style.display = 'none';
} else if (QuestionType == 'Q') {
document.getElementById('Validation').style.display = '';
document.getElementById('OtherSelection').style.display = 'none';
document.getElementById('MandatorySelection').style.display = '';
} else {
document.getElementById('OtherSelection').style.display = 'none';
if (document.getElementById('ON')) {
document.getElementById('ON').checked = true;
}
document.getElementById('Validation').style.display = 'none';
document.getElementById('MandatorySelection').style.display = '';
}
if (QuestionType == 'H'){ // hide relevance equation input for array by column question type until it is fixed
document.getElementById('relevanceContainer').style.display = 'none';
} else {
document.getElementById('relevanceContainer').style.display = '';
}
} catch(e) {
if(window.debugState.backend) console.ls.error(e);
}
}