我有以下代码来设置基于条件和设备的高度。但是,我得到一个错误 - 对于这个问题,环复杂度太高(28)。我该如何解决它?如何解决Javascript中的圈复杂度问题
function adjustHeightForAttributes() {
var elementHeight = document.getElementById('listSearchOptions');
var childNoLen = $('.attributelistContainer .requestedAttr span').length;
if (childNoLen >= 1) {
$('.attributelistContainer').css({"overflow-y":"auto"});
}
// Establishing media check
var widthMinCheck = window.matchMedia("(min-width: 768px)").matches;
var widthMaxCheck = window.matchMedia("(max-width: 1024px)").matches;
var orientationCheck = window.matchMedia("(orientation: landscape)").matches;
var orientationCheckPortrait = window.matchMedia("(orientation: portrait)").matches;
//True/False values check
var bothFalse = vm.searchByRoomNo === false && childNoLen === 0;
var bothTrue = vm.searchByRoomNo === true && childNoLen >= 1;
var roomSearchTrue = vm.searchByRoomNo === true && childNoLen === 0;
var childLenTrue = vm.searchByRoomNo === false && childNoLen >= 1;
//Check if scroll exists
var scrollExists = $('#listSearchOptions')[0].scrollHeight > $('#listSearchOptions')[0].clientHeight;
if (widthMinCheck && widthMaxCheck && orientationCheck) {
console.log("oreintationCheck.. " + orientationCheck);
if (bothFalse) {
if(scrollExists) {
console.log("Height..in adjustSearchAttributesheight6." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"height":"auto", "max-height":""});
}
else {
console.log("Height..in adjustSearchAttributesheight7." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"height":"52vh","max-height":"52vh"});
}
}
if (childLenTrue) {
if (scrollExists) {
console.log("Height..in adjustSearchAttributesheight1." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"38vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight2." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"40vh"});
}
}
if (bothTrue) {
if (scrollExists) {
console.log("Height..in adjustSearchAttributesheight3." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"30vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight4." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"32vh"});
}
}
if (roomSearchTrue) {
if ($('#listSearchOptions')[0].scrollHeight > $('#listSearchOptions')[0].clientHeight) {
console.log("Height..in adjustSearchAttributesheight5." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"44vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight5." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"46vh"});
}
}
}
else if (orientationCheckPortrait) {
if (bothFalse) {
if (scrollExists) {
console.log("Height..in adjustSearchAttributesheight6." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"height":"auto", "max-height":""});
}
else {
console.log("Height..in adjustSearchAttributesheight7." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"42vh"});
}
}
if (childLenTrue) {
if ($('#listSearchOptions')[0].scrollHeight > $('#listSearchOptions')[0].clientHeight) {
console.log("Height..in adjustSearchAttributesheight1." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"38vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight2." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"40vh"});
}
}
if (bothTrue) {
if (scrollExists) {
console.log("Height..in adjustSearchAttributesheight3." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"30vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight4." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"32vh"});
}
}
if (roomSearchTrue) {
if ($('#listSearchOptions')[0].scrollHeight > $('#listSearchOptions')[0].clientHeight) {
console.log("Height..in adjustSearchAttributesheight5." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"45vh"});
}
else{
console.log("Height..in adjustSearchAttributesheight5." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"48vh"});
}
}
}
else{
if (bothFalse) {
if (scrollExists) {
console.log("Height..in adjustSearchAttributesheight6." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"height":"auto", "max-height":""});
}
else {
console.log("Height..in adjustSearchAttributesheight7." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"42vh"});
}
}
if (childLenTrue) {
if ($('#listSearchOptions')[0].scrollHeight > $('#listSearchOptions')[0].clientHeight) {
console.log("Height..in adjustSearchAttributesheight8." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"27.2vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight9." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"27vh"});
}
}
if (roomSearchTrue) {
if (scrollExists) {
console.log("Height..in adjustSearchAttributesheight10." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"34vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight11." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"36vh"});
}
}
if (bothTrue) {
if (scrollExists) {
console.log("Height..in adjustSearchAttributesheight12." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"18.5vh"});
}
else {
console.log("Height..in adjustSearchAttributesheight13." + $('#listSearchOptions')[0].clientHeight);
$('#listSearchOptions').css({"max-height":"20.8vh"});
}
}
}
}
在我的答案中尝试重构逻辑。希望它能让你更好地控制代码。 –