/* * Common logic for change values for dependent sliders group * Created by Antoshkin during the code refactoring operation * ENV-672 */ var fired = false; var showConfirmDialog = false; /* * Recalculates values for movable dependent sliders for the group against the slider, changed by user. * Assignes new recalculated values to movable dependent sliders or shows warn message, if recalculate values are wrong * @param {object} slidersData - parameters and required data for recalculation. Struct elements must be as shown below: * sliders[] - collection of the sliders groups. Each group contains some dependent sliders * movableSliders[] - collection of all movable sliders in all existing on then page groups of sliders * allocations[] - collection of value sets for all groups of sliders * activeSliderId - slider, that was moved by user * newActiveSliderValue - new value for moved by user slider * warnMessage - message to show in dialog, if recalculated values become wrong * activeIndex - index of the current slider group in the collection of sliders */ function recalculateDependentSliders(slidersData) { if (fired) return; var value = parseInt(slidersData.newActiveSliderValue); // Get current total var total = 0; var totalofothers = 0; var nonmovablesum = 0; var availableTotal = 100; var i = slidersData.activeIndex; var activeSliderContainerSelector = "#" + slidersData.activeSliderId + "_container"; var activeSlider = $(activeSliderContainerSelector); slidersData.movableSliders[i] = $(slidersData.movableSliders[i]).not(activeSlider); //putting this slider off collection of autoadjustable sliders $(activeSlider).addClass("ui-slider-success"); if (slidersData.movableSliders[i].length == 0 && slidersData.sliders[i].length > 1) { fired = true; $(slidersData.sliders[i]).slider("disable"); // When user finish sliding, warning dialog must be thrown showConfirmDialog = true; } $(slidersData.sliders[i]).not(activeSlider).each(function () { totalofothers += slidersData.allocations[i][$(this).attr('id').split("_")[0]]; //calculate precise total for all allocatios except for one that we moved }); $(slidersData.movableSliders[i]).not(activeSlider).each(function () { nonmovablesum += slidersData.allocations[i][$(this).attr('id').split("_")[0]]; //calculate precise total for all allocatios except for one that we moved }); nonmovablesum = totalofothers - nonmovablesum; total = totalofothers + value; var change = availableTotal - total; //amount to ditribute betwee autoadjustable sliders var sliderscount = slidersData.movableSliders[i].length; if (change != 0) { var tempsum = value + Math.round(nonmovablesum); var tempprecisesum = value + nonmovablesum; for (k = slidersData.movableSliders[i].length - 1 ; k >= 0 ; k--) { var currentSliderId = $(slidersData.movableSliders[i][k]).attr('id').split("_")[0]; var currvalue = slidersData.allocations[i][currentSliderId]; //getting value of one of autoadjustable sliders var newprecisevalue = currvalue + change / sliderscount; if (newprecisevalue < 0) { newprecisevalue = 0; } if (k == 0) { //for the last slider we set value as 100-other values if (tempprecisesum <= 100) { slidersData.allocations[i][currentSliderId] = 100 - tempprecisesum; $(slidersData.movableSliders[i][k]).slider("value", 100 - tempsum); } else { slidersData.allocations[i][currentSliderId] = 0; var remainder = (tempprecisesum - 100) / ((sliderscount - 1 > 0 ? sliderscount - 1 : 1)); for (j = 1; j < slidersData.movableSliders[i].length; j++) { var tmpSliderId = $(slidersData.movableSliders[i][j]).attr('id').split("_")[0]; currvalue = slidersData.allocations[i][tmpSliderId]; currvalue -= remainder; if (currvalue < 0) currvalue = 0; slidersData.allocations[i][tmpSliderId] = currvalue; $(slidersData.movableSliders[i][j]).slider("value", Math.round(slidersData.allocations[i][tmpSliderId])); } } } else { slidersData.allocations[i][currentSliderId] = newprecisevalue; $(slidersData.movableSliders[i][k]).slider("value", Math.round(newprecisevalue)); tempsum += Math.round(newprecisevalue); tempprecisesum += newprecisevalue; } } } slidersData.allocations[i][slidersData.activeSliderId] = value; $(activeSlider).children(".sliderTitle").html(value + '%'); } /* * Does some initialization, when user starts to move slider * @param {object} slidersData - information about the slider group */ function registerSliderStartMovement(slidersData) { showConfirmDialog = false; } /* * Shows warning message, if slider group values became wrong during the last sliding process * @param {object} slidersData - information about the slider group * warnMessage - message to show in dialog, if recalculated values become wrong */ function registerSliderStopMovement(slidersData) { if (!showConfirmDialog) { // ENV-672. Final validation of the summary group value. If not 100%, warning must be thrown var currentSliderId; var currvalue; var allocationsSumm = 0; var availableTotal = 100; var i = slidersData.activeIndex; for (k = 0; k < slidersData.sliders[i].length; k++) { currentSliderId = $(slidersData.sliders[i][k]).attr('id').split("_")[0]; currvalue = slidersData.allocations[i][currentSliderId]; allocationsSumm += Math.round(currvalue); showSliderValue(slidersData.sliders[i][k], Math.round(currvalue)); } allocationsSumm = Math.floor(allocationsSumm); if (Math.abs(allocationsSumm - availableTotal) < 2) { currvalue = slidersData.allocations[i][slidersData.activeSliderId]; currvalue -= (allocationsSumm - availableTotal); slidersData.allocations[i][slidersData.activeSliderId] = currvalue; var activeSliderContainerSelector = "#" + slidersData.activeSliderId + "_container"; $(activeSliderContainerSelector).slider("value", Math.round(currvalue)); } else showConfirmDialog = true; } if (showConfirmDialog) { // Some values of the sliders group are wrong (not 100% summary). var result = confirm(slidersData.warnMessage); processConfirmed(result, slidersData, 100); } } function processConfirmed(result, slidersData, availableTotal) { var i = slidersData.activeIndex; var activeSliderContainerSelector = "#" + slidersData.activeSliderId + "_container"; var activeSlider = $(activeSliderContainerSelector); if (!result) { fired = false; } else { $(slidersData.sliders[i]).removeClass("ui-slider-success"); // slidersData.movableSliders[i] = $(slidersData.sliders[i]).not(activeSlider); slidersData.movableSliders[i] = $(slidersData.sliders[i]); } var valuetoset = availableTotal; $(slidersData.sliders[i]).not(activeSlider).each(function (k, div) { availableTotal -= $(this).slider("value"); }); $(activeSliderContainerSelector).slider("value", availableTotal); fired = false; $(slidersData.sliders[i]).slider("enable"); } // /* * Inits sliders and recalculates group values after adding or removing new sliders in the group * @param {object} slidersData - information about the slider group * @param {bool} recalculateSliderGroup - if TRUE, slider values will be recaculated */ function restoreSliderActivity(slidersData, recalculateSliderGroup) { var i = slidersData.activeIndex; $(slidersData.sliders[i]).removeClass("ui-slider-success"); slidersData.movableSliders[i] = $(slidersData.sliders[i]); $(slidersData.sliders[i]).slider("enable"); if (recalculateSliderGroup) { // All sliders in group need to be recalculated slidersData.activeSliderId = slidersData.sliders[i][0].id.split("_")[0]; recalculateDependentSliders(slidersData); $(slidersData.sliders[i]).removeClass("ui-slider-success"); } } function showSliderValue(slider, value) { $(slider).children(".sliderValue").val(value); $(slider).children(".sliderTitle").html(value + '%'); }