-
Notifications
You must be signed in to change notification settings - Fork 988
/
ranking.js
184 lines (171 loc) · 7.26 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
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
$('#question'+qID+' .answers-list').hide();
// 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"> \
<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);
var sortableDelay = 200;
if (!$.support.leadingWhitespace) {
// IE 7 and 8 (bug 08019)
sortableDelay = 0;
}
// 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: sortableDelay,
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();
if(samechoiceheight){fixChoiceHeight(qID);}
if(samelistheight){fixListHeight(qID);}
// 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
if(samechoiceheight){fixChoiceHeight(qID);}
if(samelistheight){fixListHeight(qID);}
});
}
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').each(function(index){
number=index+1;
if($(this).val()!=""){$("#"+relevancename+number).val("1");}
checkconditions($(this).val(),$(this).attr("name"),'select-one','onchange');
});
$('#sortable-rank-'+qID+' li').removeClass("error");
$('#sortable-choice-'+qID+' li').removeClass("error");
$('#sortable-rank-'+qID+' li:gt('+(maxanswers*1-1)+')').addClass("error");
}
function sortableAlert (qID,showpopups)
{
if(showpopups){
txtAlert=$("#question"+qID+" .em_num_answers").text()
alert(txtAlert);
}
}
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');
$('#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);
}
});
$('#sortable-rank-'+qID+' li').removeClass("error");
$('#sortable-choice-'+qID+' li').removeClass("error");
$('#sortable-rank-'+qID+' li:gt('+(maxanswers*1-1)+')').addClass("error");
}
// All choice at same height
function fixChoiceHeight(qID){
maxHeight=0;
$('.connectedSortable'+qID+' li').each(function(){
if ($(this).actual('height')>maxHeight){
maxHeight=$(this).actual('height');
}
});
$('.connectedSortable'+qID+' li').height(maxHeight);
}
// Make the 2 list at maximum height
function fixListHeight(qID){
totalHeight=0;
$('.connectedSortable'+qID+' li').each(function(){
totalHeight=totalHeight+$(this).actual('outerHeight',{includeMargin:true});
});
$('.connectedSortable'+qID).height(totalHeight);
}