/
ranking.js
220 lines (214 loc) · 8.73 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* 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
*/
/**
* Update answers part for ranking function
*
* @param {number} qId The qid of the question where apply.
*/
function doDragDropRank(qID, showpopups, samechoiceheight, samelistheight) {
// TODO : advanced setting in attributes
if (typeof showpopups === 'undefined'){showpopups=true;}
if (typeof samechoiceheight === 'undefined'){samechoiceheight=true;}
if (typeof samelistheight === 'undefined'){ samelistheight=true;}
var maxanswers= parseInt($("#ranking-"+qID+"-maxans").text(),10);
var rankingname= "javatbd"+$("#ranking-"+qID+"-name").text();
var rankingnamewidth=rankingname.length;
//Add a class to the question
$('#question'+qID+'').addClass('dragDropRanking');
// Hide the default answers list but display for media oral or screen reader
$('#question'+qID+' .answers-list').addClass("hide");
// We are in javascript, then default tip can be replaced
$('#question'+qID+' .em_default').html("<div class='hide'>"+$('#question'+qID+' .em_default').html()+"</div><div aria-hidden='true'>"+aRankingTranslations.rankhelp+"</div>");
$('#question'+qID+' .answers-list').on("change",".select-item",{source:false},function(event,data){
data = data || event.data;
if(data.source!='dragdrop')
loadDragDropRank(qID);;
});
// Add connected sortables elements to the question
// Actually a table : move it to a list is a good idea, but need reviewing template a lot.
var htmlCode = '<div class="dragDropTable" aria-hidden="true"> \
<div class="columns2">\
<strong class="SortableTitle">'+aRankingTranslations.choicetitle+'</strong>\
<div class="ui-state-default dragDropChoices"> \
<ul id="sortable-choice-'+qID+'" class="connectedSortable'+qID+' dragDropChoiceList"> \
<li>'+aRankingTranslations.choicetitle+'</li> \
</ul> \
</div> \
</div>\
<div class="columns2">\
<strong class="SortableTitle">'+aRankingTranslations.ranktitle+'</strong>\
<div class="ui-state-default dragDropRanks"> \
<ol id="sortable-rank-'+qID+'" class="connectedSortable'+qID+' dragDropRankList selectionSortable"> \
<li>'+aRankingTranslations.ranktitle+'</li> \
</ol> \
</div> \
</div> \
</div>';
$(htmlCode).insertAfter('#question'+qID+' .answers-list');
$('#sortable-choice-'+qID+' li, #sortable-rank-'+qID+' li').remove();
// Get the list of choices from the LimeSurvey question and copy them as items into the sortable choices list
var ranked =[];
$('#question'+qID+' .answers-list .select-item option:selected').each(function(index, Element) {
if($(this).val()!=''){
ranked.push($(this).val());
htmloption=$("#htmlblock-"+qID+'-'+$(this).val()).html();
var liCode = '<li class="ui-state-default choice" id="'+rankingname+$(this).val()+'">' + htmloption + '</li>'
$(liCode).appendTo('#sortable-rank-'+qID+'');
}
});
$('#question'+qID+' .answers-list .select-item:first option').each(function(index, Element) {
var thisvalue=$(this).val();
if(thisvalue!='' && jQuery.inArray(thisvalue,ranked)<0){
htmloption=$("#htmlblock-"+qID+'-'+$(this).val()).html();
var liCode = '<li class="ui-state-default choice" id="'+rankingname+$(this).val()+'">' + htmloption + '</li>'
$(liCode).appendTo('#sortable-choice-'+qID+'');
}
});
loadDragDropRank(qID);
// Set up the connected sortable
$('#sortable-choice-'+qID+', #sortable-rank-'+qID+'').sortable({
connectWith: '.connectedSortable'+qID+'',
forceHelperSize: true,
forcePlaceholderSize: true,
placeholder: 'ui-sortable-placeholder',
helper: 'clone',
delay: 200,
revert: 50,
receive: function(event, ui) {
if($(this).attr("id")=='sortable-rank-'+qID && $(maxanswers>0 && '#sortable-rank-'+qID+' li').length > maxanswers) {
sortableAlert (qID,showpopups,maxanswers);
if(showpopups){$(ui.sender).sortable('cancel');}
}
},
stop: function(event, ui) {
$('#sortable-choice-'+qID+'').sortable('refresh');
$('#sortable-rank-'+qID+'').sortable('refresh');
updateDragDropRank(qID);
}
}).disableSelection();
// Adapt choice and list height
fixChoiceListHeight(qID,samechoiceheight,samelistheight);
// Allow users to double click to move to selections from list to list
$('#sortable-choice-'+qID).delegate('li','dblclick', function() {
if($(maxanswers>0 && '#sortable-rank-'+qID+' li').length >= maxanswers) {
sortableAlert (qID,showpopups,maxanswers);
if(showpopups){return false;}
}
else {
$(this).appendTo('#sortable-rank-'+qID+'');
$('#sortable-choice-'+qID+'').sortable('refresh');
$('#sortable-rank-'+qID+'').sortable('refresh');
}
updateDragDropRank(qID);
});
$('#sortable-rank-'+qID).delegate('li','dblclick', function() {
$(this).appendTo('#sortable-choice-'+qID+'');
$('#sortable-choice-'+qID+'').sortable('refresh');
$('#sortable-rank-'+qID+'').sortable('refresh');
updateDragDropRank(qID);
});
$(function() { // Update height for IE7, maybe for other function too
fixChoiceListHeight(qID,samechoiceheight,samelistheight);
});
}
/**
* Update answers after updating drag and drop part
*
* @param {number} qId The qid of the question where apply.
*/
function updateDragDropRank(qID){
var maxanswers= parseInt($("#ranking-"+qID+"-maxans").text(),10);
var rankingname= "javatbd"+$("#ranking-"+qID+"-name").text();
var relevancename= "relevance"+$("#ranking-"+qID+"-name").text();
var rankingnamewidth=rankingname.length;
$('#question'+qID+' .select-item select').val('');
$('#sortable-rank-'+qID+' li').each(function(index) {
// Get value of ranked item
var liID = $(this).attr("id");
liValue = liID.substr(rankingnamewidth);
$('#question'+qID+' .select-item select').eq(index).val(liValue);
});
// Update #relevance and lauch checkconditions function
$("[id^=" + relevancename + "]").val('0');
$('#question'+qID+' .select-item select:lt('+maxanswers+')').each(function(index){
number=index+1;
if($(this).val()!="")
{
$("#"+relevancename+number).val("1");
}
$(this).trigger("change",{ source : 'dragdrop'});
});
$('#sortable-rank-'+qID+' li').removeClass("error");
$('#sortable-choice-'+qID+' li').removeClass("error");
$('#sortable-rank-'+qID+' li:gt('+(maxanswers*1-1)+')').addClass("error");
}
/**
* Show an alert if needed
*
* @param {number} qId The qid of the question where apply.
* @param {bool} showpopups Show or not the alert
*/
function sortableAlert (qID,showpopups)
{
if(showpopups){
txtAlert=$("#question"+qID+" .em_num_answers").text()
alert(txtAlert);
}
}
/**
* Set the drag and drop according to existing answers
*
* @param {number} qId The qid of the question where apply.
*/
function loadDragDropRank(qID){
var maxanswers= parseInt($("#ranking-"+qID+"-maxans").text(),10);
var rankingname= "javatbd"+$("#ranking-"+qID+"-name").text();
var relevancename= "relevance"+$("#ranking-"+qID+"-name").text();
var rankingnamewidth=rankingname.length;
// Update #relevance
$("[id^=" + relevancename + "]").val('0');
$('#sortable-rank-'+qID+' li').each(function(){
$(this).appendTo('#sortable-choice-'+qID+'');
});
$('#question'+qID+' .select-item select').each(function(index){
if($(this).val()!=''){
number=index+1;
$("#"+relevancename+number).val("1");
$('#sortable-choice-'+qID+' li#'+rankingname+$(this).val()).appendTo('#sortable-rank-'+qID);
}
});
updateDragDropRank(qID);// Update to reorder select
$('#sortable-rank-'+qID+' li').removeClass("error");
$('#sortable-choice-'+qID+' li').removeClass("error");
$('#sortable-rank-'+qID+' li:gt('+(maxanswers*1-1)+')').addClass("error");
}
/**
* Fix height of drag and drop according to question settings
*
* @param {number} qId The qid of the question where apply.
* @param {bool} samechoiceheight
* @param {bool} samelistheight
*/
function fixChoiceListHeight(qID,samechoiceheight,samelistheight){
if(samechoiceheight)
{
var maxHeight=0;
$('.connectedSortable'+qID+' li').each(function(){
if ($(this).actual('height')>maxHeight){
maxHeight=$(this).actual('height');
}
});
$('.connectedSortable'+qID+' li').css('min-height',maxHeight+'px');
}
if(samelistheight)
{
var totalHeight=0;
$('.connectedSortable'+qID+' li').each(function(){
totalHeight=totalHeight+$(this).actual('outerHeight',{includeMargin:true});;
});
$('.connectedSortable'+qID).css('min-height',totalHeight+'px');
}
}