Quantcast
Viewing all articles
Browse latest Browse all 17529

Equation in multiple numercial inputs - by: tpartner

Bug found and fixed - I have updated the Workarounds section - manual.limesurvey.org/Workarounds:_Manip...ascript#Version_2.5x

This is the new code for template.js:

function shuffleArray(array) {
	for (var i = array.length - 1; i > 0; i--) {
		var j = Math.floor(Math.random() * (i + 1));
		var temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
	return array;
}
 
 
/*****
    A jQuery plugin to facilitate the Van Westendorp pricing research methodology
    Copyright (C) 2016 - Tony Partner (http://partnersurveys.com)
    Licensed MIT, GPL
    Version - 2.0
    Create date - 19/10/16
*****/
(function( $ ){
 
	$.fn.vWPricing25 = function(options) {
 
		var opts = $.extend( {
			order: [1, 2, 3, 4],
			randomOrder: false
		}, options);
 
		return this.each(function() {
 
			var thisQuestion = $(this);
 
			thisQuestion.addClass('vwp-question');
 
			// Add some attributes and set the slider max/min values
			var itemsLength = $('.question-item', thisQuestion).length;
			$('.question-item', thisQuestion).each(function(i) {
				var thisInput = $('input[type=text]', this);
				var thisVal = $(thisInput).val();
 
				$(this).attr('data-item', (i+1));
				$(thisInput).attr('data-slider-index', (i+1));
 
				// Slider initial settings
				setTimeout(function() {
					var thisSliderMin = $(thisInput).bootstrapSlider('getAttribute', 'min');
					$(thisInput).bootstrapSlider('setAttribute', 'min', thisSliderMin+(i));
					var thisSliderMax = $(thisInput).bootstrapSlider('getAttribute', 'max');
					$(thisInput).bootstrapSlider('setAttribute', 'max', thisSliderMax-((itemsLength-1)-i));
 
					if(thisVal == '') {
						$(thisInput).val('');
					}
 
				}, 200);
 
			});
 
			// Slider order
			if(opts.randomOrder == true) {
				shuffleArray(opts.order);
			}
			$(opts.order).each(function(i, val) {
				$('.subquestion-list.questions-list', thisQuestion).append($('.question-item[data-item="'+val+'"]', thisQuestion));
			});
 
			// Listeners on the sliders
			$('input[type=text]', thisQuestion).on('slide', function(event) {
				handleVwpSliders(this, $(this).bootstrapSlider('getValue'));
			});
			$('input[type=text]', thisQuestion).on('slideStop', function(event) {
				handleVwpSliders(this, $(this).bootstrapSlider('getValue'));
			});
 
			// A function to handle the siders
			function handleVwpSliders(el, sliderVal) {
				var movedIndexNum = $(el).attr('data-slider-index');
 
				var lowerSliders = $('input[type=text]', thisQuestion).filter(function() {
					return $(this).attr('data-slider-index') < movedIndexNum;
				});
				var higherSliders = $('input[type=text]', thisQuestion).filter(function() {
					return $(this).attr('data-slider-index') > movedIndexNum;
				});
 
				$(lowerSliders).each(function(i) {
					var thisIndexNum = $(this).attr('data-slider-index');
					var newSliderVal = sliderVal-(movedIndexNum-thisIndexNum);
					if($(this).bootstrapSlider('getValue') > newSliderVal) {
						var thisRow = $(this).closest('.question-item');
						var thisTooltip = $('.tooltip', thisRow);
						$(this).bootstrapSlider('setValue', newSliderVal);
						$('.tooltip-inner', thisTooltip).text($('input.text', thisRow).val());
						thisTooltip.show().css('margin-left', '-'+(thisTooltip.width()/2)+'px');
 
						var validationInput =  $('input.em_sq_validation', thisRow);
						$(validationInput).val($('input.text', thisRow).val())
						fixnum_checkconditions($(validationInput).attr('value'), $(validationInput).attr('name'), $(validationInput).attr('type'));
					}
				});
				$(higherSliders).each(function(i) {
					var thisIndexNum = $(this).attr('data-slider-index');
					var newSliderVal = sliderVal+(thisIndexNum-movedIndexNum);
					if($(this).bootstrapSlider('getValue') < newSliderVal) {
						var thisRow = $(this).closest('.question-item');
						var thisTooltip = $('.tooltip', thisRow);
						$(this).bootstrapSlider('setValue', newSliderVal);
						$('.tooltip-inner', thisTooltip).text($('input.text', thisRow).val());
						thisTooltip.show().css('margin-left', '-'+(thisTooltip.width()/2)+'px');
 
						var validationInput =  $('input.em_sq_validation', thisRow);
						$(validationInput).val($('input.text', thisRow).val())
						fixnum_checkconditions($(validationInput).attr('value'), $(validationInput).attr('name'), $(validationInput).attr('type'));
					}
				});
			}
 
			// Listener on resizing (override the bootstrap callout behaviour)
			$(window).resize(function() {
				setTimeout(function() {
					$('input[type=text]', thisQuestion).each(function(i) {
						if($(this).val() != '') {
							var thisRow = $(this).closest('.question-item');
							var thisTooltip = $('.tooltip', thisRow);
							$('.tooltip-inner', thisTooltip).text($.trim($(this).val()));
							thisTooltip.show().css('margin-left', '-'+(thisTooltip.width()/2)+'px');
						}
					});
				}, 1);
			});
		});
	};
})( jQuery );

Viewing all articles
Browse latest Browse all 17529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>