(function($, global) { var PersonaTools = function() { } PersonaTools.prototype = { init: function() { $('.personatool').each(function() { (new PersonaTool($(this))).init(); }) } } var PersonaTool = function(context) { this.context = context; this.questionList = this.context.find('.pt-questions'); this.results = this.context.find('.pt-results .pt-result'); } PersonaTool.prototype = { init: function() { var _this = this; this.questionList.slick({ adaptiveHeight: true, infinite: false, swipe: false, touchMove: false, swipeToSlide: false, accessibility: false, dots: false, speed: 180, arrows: false }); this.context.bind('click', function(ev) { var button = $(ev.target).closest('.button'); if (!button.length || button.hasClass('inactive')) return; ev.preventDefault(); var slickGotoSlide = _this.questionList.slick('slickCurrentSlide'); if (button.hasClass('next')) slickGotoSlide++; if (button.hasClass('prev')) slickGotoSlide--; if (button.hasClass('result')) slickGotoSlide = _this.calculateResultPage(slickGotoSlide); _this.questionList.slick('slickGoTo', slickGotoSlide); }); this.context.bind('change', function(ev) { var currentSlide = $(ev.target).closest('.pt-question'); currentSlide.find('.inactive').removeClass('inactive'); }); }, calculateResultPage: function(currentSlide) { var _this = this; var questions = this.questionList.find('.pt-question'); var results = []; questions.each(function(index, question) { var values = ($(question).find('input:checked').val() || '').split(','); for (var i = 0; i < values.length; i++) { if (values[i]) { results[values[i]] = results[values[i]] || { count:0, weight: parseInt(_this.results.filter('[data-result-type=' + values[i] + ']').attr('data-weight'), 10) }; results[values[i]].count++; } } }); var allValues = Object.keys(results); allValues.sort(function(a,b){ if (results[a].count>results[b].count) return 1; if (results[a].countresults[b].weight) return 1; if (results[a].weight