"use strict"; angular.module("vocabulary") .component("lessonCard", { templateUrl: "/js/redesign-angular/lessons/lesson-card.html", controller: "LessonCardController", bindings: { lessonId: "<", isNew: "<", isPremade: "<", isBasal: "<", onDataRetrieved: "&", onCopyLesson: "&", onDeleteLesson: "&", onMoveLesson: "&", onCloseLesson: "&", lessonWordOrder: "<" } }) .controller("LessonCardController", ["lessonService", "$state", "messageHandler", "MessageBox","premadeService", "lessonTypeService", "$q", "premadeNavigationService", function (lessonService, $state, messageHandler, MessageBox, premadeService, lessonTypeService, $q, premadeNavigationService) { var ctrl = this; this.$onInit = init; ctrl.haveLessonData = false; ctrl.lessonData = null; ctrl.categoryId = 0; ctrl.topicId = null; ctrl.resourceId = null; ctrl.summaryTabTitle = "Book Summary"; function init() { ctrl.tab = 1; if(ctrl.isPremade){ var lessonType = premadeNavigationService.getLessonTypeBasedOnSelectedCategory(); var lessonInitialization = lessonService.initializeLessonFromPremadeId(ctrl.lessonId, lessonType); // todo - if the results of this method were cached, this redundant request for this data wouldn't cause a second api call. what if i cached premade data, then changed the options, then requested the premade data again? would that cause the retrieval of outdated data? var premadeDataRetrieval = premadeService.getPremadeLessonData(ctrl.lessonId, lessonType); $q.all([lessonInitialization, premadeDataRetrieval]) .then(function (combinedResponses){ ctrl.lessonData = lessonService.getLessonData(); ctrl.resourceId = lessonService.getPremadeId(); if(premadeNavigationService.isSazSelectedCategory()){ ctrl.summaryTabTitle = "Unit Description"; } ctrl.haveLessonData = true; if(ctrl.lessonData.type === lessonTypeService.RAZ_PREMADE){ ctrl.resourceId = combinedResponses[1].bookId; } ctrl.topicId = combinedResponses[1].topicId; ctrl.onDataRetrieved(); }); }else{ lessonService.initializeLessonData(ctrl.lessonId) .then(function () { ctrl.lessonData = lessonService.getLessonData(); ctrl.categoryId = lessonService.getChosenCategoryId(); ctrl.resourceId = ctrl.lessonData.id; ctrl.haveLessonData = true; ctrl.onDataRetrieved(); }); } } ctrl.setTab = function(tabId){ ctrl.tab = tabId; }; ctrl.isSet = function(tabId){ return ctrl.tab === tabId; }; ctrl.showTabOptions = function(){ return ctrl.isPremade && !ctrl.isBasal; }; ctrl.setMenuPopover = function (popoverCtrl) { ctrl.menuPopoverCtrl = popoverCtrl; }; ctrl.closeMenuPopover = function () { if (ctrl.menuPopoverCtrl) { ctrl.menuPopoverCtrl.close(); } }; ctrl.editLesson = function(){ var params = {'lessonId': ctrl.lessonId}; $state.go('lesson.edit', params); }; ctrl.closeLesson = function(){ ctrl.onCloseLesson(); }; ctrl.copyLesson = function(){ lessonService.copyLesson(ctrl.lessonId).then(function(newLessonId){ if(newLessonId > 0) { ctrl.onCopyLesson({ $event: { lessonId: newLessonId } }); } else{ messageHandler.publishError("Unable to copy lesson"); } }); }; ctrl.newYourLesson = function(){ if (ctrl.lessonData.type === lessonTypeService.USER_DEFINED) { lessonService.initializeLessonDataFromUserLessonCopy(ctrl.lessonId) .then(function() { $state.go('lesson.new'); }); } else { var params = { origin: ctrl.lessonData.type, id: ctrl.lessonId }; $state.go('lesson.premade', params); } }; ctrl.deleteLesson = function(){ ctrl.closeMenuPopover(); var deleteMessage = "Are you sure you want to delete this lesson?"; MessageBox.show({ message: deleteMessage, responses: [ { subtle: true, label: 'Cancel' }, { id: 'delete', confirm: true, label: 'Delete Lesson' } ] }).then(function(response){ if(response.id === 'delete'){ lessonService.deleteLesson(ctrl.lessonId).then(function (lessonAffected) { if (lessonAffected > 0) { var message = ctrl.lessonData.name + " has been deleted"; messageHandler.publishSuccess(message); ctrl.onDeleteLesson(); } else { messageHandler.publishError("Unable to delete lesson"); } }); } }); }; ctrl.moveToCategory = function(event){ if(lessonService.categoryIsChosen()) { lessonService.moveLesson().then(function(){ var message = ctrl.lessonData.name + " has been moved to " + event.categoryName; messageHandler.publishSuccess(message); ctrl.onMoveLesson(); }); } }; }]);