/
ranking.js
201 lines (180 loc) · 8.28 KB
/
ranking.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
/*
* This file is part of LimeSurvey
* See COPYRIGHT.php for copyright notices and details.
* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GNU/GPL License v2 or later
*/
var RankingQuestion = function (options) {
"use strict";
//Define option parameters
var max_answers = options.max_answers,
min_answers = options.min_answers,
rankingName = options.rankingName,
showpopups = options.showpopups || true,
samechoiceheight = options.samechoiceheight || true,
samelistheight = options.samelistheight || true,
questionId = options.questionId;
//define reused variables
var relevancename= "relevance"+rankingName,
rankingID = "javatbd" + rankingName;
//define HTML snippets
var screenReader = "<div class='sr-only'>" + $('#question' + questionId + ' .em_default').html() + "</div><div aria-hidden='true'>" + LSvar.lang.rankhelp + "</div>"
//define functions
var createSorting = function(){
//Add a class to the question
$('#question' + questionId + '').addClass('sortable-activated');
// Set up the connected sortable
$('#sortable-choice-' + questionId).sortable({
group: "sortable-" + questionId,
ghostClass: "ls-rank-placeholder",
sort: false,
scroll: false,
onMove: function(ev) {
if (max_answers > 0 && $('#sortable-rank-' + questionId + ' li').length >= max_answers) {
//sortableAlert();
return false;
}
}
});
$('#sortable-rank-' + questionId).sortable({
group: "sortable-" + questionId,
ghostClass: "ls-rank-placeholder",
onSort: function (evt) {
if ($(evt.item).hasClass("disabled")) {
/* see https://github.com/RubaXa/Sortable/issues/933 */
$(evt.item).appendTo('#sortable-choice-' + questionId);
/* send a console notice 'from is undefined' in jquery.fn.sortable : must fix Sortable before */
} else {
updateDragDropRank.call(self,questionId);
}
}
});
$('#question' + questionId + ' .ls-remove').remove();
// Adapt choice and list height
fixChoiceListHeight();
},
fixScreenReaderCompatibility = function(){
// Hide the default answers list but display for media oral or screen reader
// We are in javascript, then default tip can be replaced
$('#question' + questionId + ' .em_default').html(screenReader);
$('#question' + questionId + ' .answers-list').on("change", ".select-item",
{source: false},
function (event, data) {
data = data || event.data;
if (data.source != 'dragdrop')
loadDragDropRank();
}
);
},
bindActions = function () {
// Allow users to double click to move to selections from list to list
$('#sortable-choice-' + questionId).on('dblclick', 'li:not(.disabled)', function () {
if (max_answers > 0 && $('#sortable-rank-' + questionId + ' li').length >= max_answers) {
sortableAlert();
return false;
} else {
$(this).appendTo('#sortable-rank-' + questionId + '');
}
updateDragDropRank();
});
$('#sortable-rank-' + questionId).on('dblclick', 'li', function () {
$(this).appendTo('#sortable-choice-' + questionId + '');
updateDragDropRank();
});
},
doDragDropRank = function () {
fixScreenReaderCompatibility();
createSorting();
loadDragDropRank();
bindActions();
},
/**
* Update answers after updating drag and drop part
*/
updateDragDropRank = function() {
$('#question' + questionId + ' .select-item select').val('');
$('#sortable-rank-' + questionId + ' li').each(function (index) {
$('#question' + questionId + ' .select-item select').eq(index).val($(this).data("value"));
});
// Update #relevance and lauch checkconditions function
$("[id^=" + relevancename + "]").val('0');
$('#question' + questionId + ' .select-item select:lt(' + max_answers + ')').each(function (index) {
if ($(this).val() != "") {
$("#" + relevancename + (index+1) ).val("1");
}
$(this).trigger("change", { source: 'dragdrop' });
});
$('#sortable-rank-' + questionId + ' li').removeClass("text-error");
$('#sortable-choice-' + questionId + ' li').removeClass("text-error");
$('#sortable-rank-' + questionId + ' li:gt(' + (max_answers * 1 - 1) + ')').addClass("text-error");
},
sortableAlert = function() {
if (showpopups) {
var txtAlert = $("#question" + questionId + " .em_num_answers").text()
alertSurveyDialog(txtAlert, '');
}
},
loadDragDropRank = function () {
// Update #relevance
$("[id^=" + relevancename + "]").val('0');
$('#sortable-rank-' + questionId + ' li').each(function () {
$(this).appendTo('#sortable-choice-' + questionId );
});
$('#question' + questionId + ' .select-item select :selected').each(function (index) {
if ($(this).val() != '') {
$("#" + relevancename + (index+1)).val("1");
$('#sortable-choice-' + questionId + ' li#' + rankingID + $(this).val()).appendTo('#sortable-rank-' + questionId);
}
});
updateDragDropRank(); // Update to reorder select
$('#sortable-rank-' + questionId + ' li').removeClass("error");
$('#sortable-choice-' + questionId + ' li').removeClass("error");
$('#sortable-rank-' + questionId + ' li:gt(' + (max_answers * 1 - 1) + ')').addClass("error");
},
fixChoiceListHeight = function() {
if (samechoiceheight) {
var maxHeight = 0;
$('#question' + questionId + ' .list-samechoiceheight .ls-choice').each(function () {
if ($(this).actual('height') > maxHeight) {
maxHeight = $(this).actual('height');
}
});
$('#question' + questionId + ' .list-samechoiceheight .ls-choice').height(maxHeight);
}
if (samelistheight) {
var totalHeight = 0;
$('#question' + questionId + ' .list-samelistheight .ls-choice').each(function () {
totalHeight = totalHeight + $(this).actual('outerHeight', {
includeMargin: true
}); /* Border not inside */
});
/* Add the padding to min-height */
$('#sortable-choice-' + questionId + ',#sortable-rank-' + questionId).height(totalHeight);
}
},
triggerEmRelevanceSortable = function() {
$(".sortable-item").on('relevance:on', function (event, data) {
//~ if(event.target != this) return; // not needed now, but after maybe (2016-11-07)
//~ data = $.extend({style:'hidden'}, data);
$(event.target).closest(".ls-answers").find("option[value=" + $(event.target).data("value") + "]").prop('disabled', false);
$(event.target).removeClass("disabled");
});
$(".sortable-item").on('relevance:off', function (event, data) {
//~ if(event.target != this) return; // not needed now, but after maybe (2016-11-07)
//~ data = $.extend({style:'hidden'}, data);
$(event.target).closest(".ls-answers").find("option[value=" + $(event.target).data("value") + "]").prop('disabled', true);
$(event.target).addClass("disabled");
/* reset already ranked item */
if ($(event.target).parent().hasClass("sortable-rank")) {
$(event.target).appendTo($(event.target).closest(".answers-list").find(".sortable-choice"));
var questionId = $(event.target).closest("[id^='question']").attr('id').replace("question", "");
updateDragDropRank();
}
});
};
return {
init : function(){
doDragDropRank();
triggerEmRelevanceSortable();
}
}
};