"use strict"; angular.module("vocabulary") .component("chosenWords", { templateUrl: "/js/redesign-angular/lessonBuilder/chosenWords/chosen-words.html", controller: "ChosenWordsController" }) .controller("ChosenWordsController", ['$q', '$scope', 'lessonService', "partsOfSpeechService", "orderByFilter", "loginAndContinueService", "lessonWordOrderApi", function ($q, $scope, lessonService, partsOfSpeechService, orderByFilter, loginAndContinueService, lessonWordOrderApi) { var ctrl = this; ctrl.$onInit = function () { setOrder(); }; ctrl.getChosenWords = function () { var words = lessonService.getChosenWords(); var wordCount = lessonService.countChosenWords(); if (wordCount > 0) { ctrl.noWordsError = false; } return loginAndContinueService.isAuthorized() ? sortWords(words) : words; }; ctrl.discardWord = lessonService.discardWord; ctrl.abbreviatePartOfSpeech = partsOfSpeechService.abbreviate; ctrl.getLessonId = function () { return lessonService.getLessonId(); }; function setOrder() { var requests = []; requests.push(lessonWordOrderApi.getLessonWordOrderOptions()); if(lessonService.getLessonId()) { requests.push(lessonWordOrderApi.getLessonWordOrderOptionId(lessonService.getLessonId())); } $q.all(requests) .then(function (data) { ctrl.orderOptions = data[0]; ctrl.chosenWordOrderId = data[1] ? data[1] : ctrl.orderOptions[1].optionId; lessonService.setWordOrderId(ctrl.chosenWordOrderId); }); } function sortWords(words) { if(!ctrl.orderOptions) { return []; } var wordOrder = ctrl.orderOptions.filter(function (option) { return option.optionId === ctrl.chosenWordOrderId; }).pop().optionType; switch (wordOrder) { case 'As Entered': return orderByFilter(words, 'lessonEntryOrder'); case 'Alphabetical': return orderByFilter(words, 'wordName'); default: return orderByFilter(words, 'lessonEntryOrder'); } } }]);