Commit 6a73268e authored by Chenu Denis's avatar Chenu Denis

[feature] dropDown (multiple) by another question

parent 684edf5d
/*!
* Style
*
* Copyright 2017 Denis Chenu <http://www.sondages.pro>
* Licensed under the Apache License v2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
.selectBySurveyQuestion-item .select2-selection--multiple .select2-selection__choice{float:none}
/**
* @file Description
* @author Denis Chenu
* @copyright Denis Chenu <http://www.sondages.pro>
* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat (MIT)
*/
$(function() {
$("[data-selectQuestionBy='simple']").select2({
theme: "bootstrap",
dropdownParent: $('.answer-item')
});
$("[data-selectQuestionBy='multiple']").select2({
theme: "bootstrap",
//~ dropdownParent: $('.answer-item'),
escapeMarkup: function (markup) { return markup; },
templateSelection:function (data) {
if(data.element && $(data.element).data("group") ) {
return "<strong>["+$(data.element).data("group")+"]</strong> "+data.text;
}
return data.text
},
templateResult:function (data) {
return data.text
},
matcher:function(params, data) {
return groupMatcher (params, data);
}
});
$("[data-selectQuestionBy='multiple']").on("change", function (e) {
if($(this).val()){
$("#"+$(this).data('update')).val($(this).val().join(',')).trigger("keyup");
} else {
$("#"+$(this).data('update')).val("").trigger("keyup");
}
});
});
$(document).on("select2:unselecting","[data-selectQuestionBy='multiple'][data-forceone=1]",function(e){
if($(this).val().length < 2) {
e.preventDefault();
return false;
}
});
function groupMatcher (params, data) {
data.parentText = data.parentText || "";
// Always return the object if there is nothing to compare
if ($.trim(params.term) === '') {
return data;
}
// Do a recursive check for options with children
if (data.children && data.children.length > 0) {
// Clone the data object if there are children
// This is required as we modify the object to remove any non-matches
var match = $.extend(true, {}, data);
// Check each child of the option
for (var c = data.children.length - 1; c >= 0; c--) {
var child = data.children[c];
child.parentText += data.parentText + " " + data.text;
var matches = groupMatcher(params, child);
// If there wasn't a match, remove the object in the array
if (matches == null) {
match.children.splice(c, 1);
}
}
// If any children matched, return the new object
if (match.children.length > 0) {
return match;
}
// If there were no matching children, check just the plain object
return modelMatcher(params, match);
}
// If the typed-in term matches the text of this term, or the text from any
// parent term, then it's a match.
var original = (data.parentText + ' ' + data.text).toUpperCase();
var term = params.term.toUpperCase();
// Check if the text contains the term
if (original.indexOf(term) > -1) {
return data;
}
// If it doesn't contain the term, don't return anything
return null;
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment