"use strict"; angular.module("vocabulary") .component("premadeNavigation", { templateUrl: "/js/redesign-angular/premadeLessons/premade-navigation.html", controller: "PremadeNavigationController", bindings:{ selectedParentCategoryId: "<", selectedSubCategoryId: "<" } }) .controller("PremadeNavigationController", ["$filter", "premadeNavigationService", "$state", "FeatureCheck", function ($filter, premadeNavigationService, $state, FeatureCheck) { var ctrl = this; if(!FeatureCheck.isFeatureEnabled('VAZ_PREMADE_NAV_REDESIGN')) { ctrl.showCategoryList = false; } var RAZ_CATEGORY_ID = 3; var SAZ_CATEGORY_ID = 416; var ELL_CATEGORY_ID = 1639; var RAZ_MATH_BOOKS_SUBCATEGORY = 2199; var RAZ_VOCAB_BOOKS_SUBCATEGORY = 1081; var RAZ_VOCAB_SERIAL_SUBCATEGORY = 1307; var RAZ_VOCAB_NONFICTION_SERIAL_SUBCATEGORY = 1308; var RAZ_SHARED_READING_SUBCATEGORY = 1584; var RAZ_GRAPHIC_BOOKS_SUBCATEGORY = 3437; var ELL_POWER_PACK_SUBCATEGORY = 2752; var ELL_VOCAB_BOOKS_SUBCATEGORY = 2089; this.$onInit = init; function init(){ if(FeatureCheck.isFeatureEnabled('VAZ_PREMADE_NAV_REDESIGN')) { // todo - the initialization of the category tree (api call requiring asynchrony) should occur in state transition or similar. premadeNavigationService.getPremadeCategoryTree().then(function(categories){ var selected = premadeNavigationService.getSelectedPremadeCategory(); // todo - the following 2 properties are passed in as bindings whose values aren't resolved by ui-router, and therefore are null until being initialized here. // the only purpose for the bindings is for value manipulation by "on-update-navigation" in premade-lessons.html // if ui-router manages this data initialization, there will no longer be a need for "on-update-navigation", as the update will occur automatically via the transition // for the transition to be triggered in all premadeLessons substates by url category id change (sref parameters), the url parameters would need to be defined as part of the base premadeLesson state // these changes would also remove the requirement for the "wait until we have the data to display the tree" functionality controlled by flag: ctrl.showCategoryList // the transition would not complete until the data has been successfully retrieved ctrl.selectedParentCategoryId = parseInt(selected['parentCategoryId'], 10); ctrl.selectedSubCategoryId = parseInt(selected['subCategoryId'], 10); ctrl.categoryList = categories; }); } else { premadeNavigationService.getPremadeCategoryTree().then(function(categories){ ctrl.categoryList = categories; var selected = premadeNavigationService.getSelectedPremadeCategory(); ctrl.selectedParentCategoryId = parseInt(selected['parentCategoryId'], 10); ctrl.selectedSubCategoryId = parseInt(selected['subCategoryId'], 10); ctrl.showCategoryList = true; }); } } function findDefaultSubCategoryForParentSelected(categoryId){ for(var index = ctrl.categoryList.length - 1; index >= 0; index--) { if(ctrl.categoryList[index].categoryId == categoryId ) { return ctrl.categoryList[index]['subcategories'][0]; } } return -1; } ctrl.routeToSelectedSubCategory = function(subCategoryId){ ctrl.selectedSubCategoryId = subCategoryId; var params = { 'parentCategoryId': ctrl.selectedParentCategoryId, 'categoryId': ctrl.selectedSubCategoryId }; var route = "premadeLessons.collection"; if (ctrl.selectedParentCategoryId === RAZ_CATEGORY_ID) { route = "premadeLessons.razLeveledBooks"; if (ctrl.selectedSubCategoryId === RAZ_MATH_BOOKS_SUBCATEGORY) { route = "premadeLessons.razMathBooks"; } else if (ctrl.selectedSubCategoryId === RAZ_VOCAB_BOOKS_SUBCATEGORY) { route = "premadeLessons.razVocabBooks"; } else if(ctrl.selectedSubCategoryId === RAZ_VOCAB_SERIAL_SUBCATEGORY){ route = "premadeLessons.razFictionSeries"; } else if(ctrl.selectedSubCategoryId === RAZ_VOCAB_NONFICTION_SERIAL_SUBCATEGORY){ route="premadeLessons.razNonFictionSeries"; } else if (ctrl.selectedSubCategoryId === RAZ_SHARED_READING_SUBCATEGORY){ route="premadeLessons.razSharedReading"; } else if(ctrl.selectedSubCategoryId === RAZ_GRAPHIC_BOOKS_SUBCATEGORY){ route ="premadeLessons.razGraphic"; } } else if (ctrl.selectedParentCategoryId === SAZ_CATEGORY_ID) { route = "premadeLessons.science"; } else if (ctrl.selectedParentCategoryId === ELL_CATEGORY_ID) { route = "premadeLessons.ellLanguageSkill"; if(ctrl.selectedSubCategoryId === ELL_POWER_PACK_SUBCATEGORY){ route="premadeLessons.ellPowerPack"; }else if(ctrl.selectedSubCategoryId === ELL_VOCAB_BOOKS_SUBCATEGORY){ route="premadeLessons.ellVocabBooks" } } $state.go(route, params); }; // todo - this method is not required. ctrl.categoryList is already available for use in the template. ctrl.getCategoryList = function(){ return ctrl.categoryList; }; ctrl.onEnter = function(keyEvent, subcategoryid){ if(keyEvent.which === 13){ ctrl.routeToSelectedSubCategory(subcategoryid); } }; ctrl.getBaseCategory = function () { return $filter('filter')(ctrl.categoryList, function (category) { return ctrl.selectedParentCategoryId == category.categoryId; }).pop(); }; ctrl.onSelectParent = function(parentCategoryId){ if(parentCategoryId !== ctrl.selectedParentCategoryId) { ctrl.selectedParentCategoryId = parentCategoryId; var defaultSubCategory = findDefaultSubCategoryForParentSelected(parentCategoryId); if(defaultSubCategory != -1){ ctrl.routeToSelectedSubCategory(defaultSubCategory['categoryId']); } } }; ctrl.isNotProcess = function(subcategoryName){ return subcategoryName !== 'Process'; }; ctrl.isSelectedParent = function(value){ return ctrl.selectedParentCategoryId == value; }; ctrl.isSelectedSubCategory = function(value){ return ctrl.selectedSubCategoryId == value; }; // todo - this name can be changed in the database. I believe the data change was delayed to support port. no longer required. ctrl.getDisplayName = function(categoryName){ if(categoryName === 'Science A-Z.com'){ return 'Science A-Z'; } else{ return categoryName; } }; }]);