(function () { "use strict"; angular .module('shared') .component('interactiveLessonPartSelector', { transclude: true, templateUrl: '/js/angular/unit-assignment/interactive-lesson-part-selector.html', controller: 'interactiveLessonPartSelector', bindings: { lessonParts: "<", selectedMaterials: "=", currentLevel: "=" } }) .controller('interactiveLessonPartSelector', interactiveLessonPartSelector); interactiveLessonPartSelector.$inject = ['$scope']; function interactiveLessonPartSelector($scope) { var ctrl = this; ctrl.$onInit = function() { _.each(ctrl.lessonParts, function (interactiveLessonPart) { ctrl.selectedMaterials[interactiveLessonPart.material.id] = true; }); ctrl.currentLevel = hasLowMidLevelParts() ? 'lowmid' : 'high'; onISLLevelSwitch(ctrl.currentLevel); }; ctrl.hasLowMidLevelParts = hasLowMidLevelParts; ctrl.hasHighLevelParts = hasHighLevelParts; ctrl.onISLLevelSwitch = onISLLevelSwitch; function hasLowMidLevelParts() { return !!_.find(ctrl.lessonParts, function(parts) { return parts.material.readingLevel === 'mid'; }); } function hasHighLevelParts() { return !!_.find(ctrl.lessonParts, function(parts) { return parts.material.readingLevel === 'high'; }); } $scope.$watch(function() { return ctrl.currentLevel; }, function(newValue, oldValue) { if(oldValue != newValue) ctrl.onISLLevelSwitch(newValue); }); function onISLLevelSwitch(currentLevel) { var levelPartMaterialIds = getLessonPartMaterialIdsForLessonByLevel(currentLevel); _.each(ctrl.selectedMaterials, function(material, id) { ctrl.selectedMaterials[id] = levelPartMaterialIds.indexOf(parseInt(id)) > -1; }); } function getLessonPartMaterialIdsForLessonByLevel(level) { var trueLevel = level === "high" ? "high" : "mid"; var partsOfLevel = []; _.each(ctrl.lessonParts, function(part) { if(part.material.readingLevel === trueLevel) partsOfLevel.push(part.material.id); }); return partsOfLevel; } } })();