(function() { "use strict"; angular .module('shared') .factory('ParentLetterCustomize', ['LazModalService', 'studentList', 'DocsForHomeConstants', '_', '$window', 'AuthGo', function (LazModalService, studentList, DocsForHomeConstants, _, $window, AuthGo) { function ParentLetterCustomize() { var self = this; function ParentLetterCustomizeCtrl(close) { var ctrl = this; studentList.get().$promise.then(function (students) { ctrl.students = angular.copy(students); }); ctrl.LANGUAGES = DocsForHomeConstants.PARENT_LETTER_LANGUAGES; ctrl.NONE_LANGUAGE_KEY = 'none'; ctrl.ENGLISH_LANGUAGE_KEY = 'en'; ctrl.selectedLanguageKey = ctrl.ENGLISH_LANGUAGE_KEY; ctrl.FORMATS = { 'pdf': {'displayName': 'Single PDF', 'description': 'Parent letters can be downloaded as a single pdf file, ideal for printing.'}, 'zip': {'displayName': 'Zipped Folder', 'description': 'Parent letters can be downloaded as a zip file of individual PDF files per student, for attaching to email.'} }; ctrl.selectedFormatKey = 'pdf'; ctrl.allSelectedToggleState = false; ctrl.downloadButtonDisabled = true; ctrl.studentCheckboxes = {}; ctrl.customChoices = {}; ctrl.customized = ''; function init() { defineCustomized(); } init(); ctrl.toggleAllSelected = function() { _.each(ctrl.students, function(student) { var studentId = student.student_id; ctrl.studentCheckboxes[studentId] = ctrl.allSelectedToggleState; ctrl.customChoices[studentId] = retrieveLanguageKeyForStudent(studentId); ctrl.downloadButtonDisabled = !ctrl.allSelectedToggleState; student.selected = ctrl.allSelectedToggleState; }); reformCustomized(); }; ctrl.toggleStudentCheckbox = function(student) { var studentId = student.student_id; ctrl.customChoices[studentId] = retrieveLanguageKeyForStudent(studentId); ctrl.downloadButtonDisabled = areAllChoicesNone(); student.selected = ctrl.studentCheckboxes[studentId]; reformCustomized(); }; ctrl.onLanguageSelected = function(selectedLanguageKey) { ctrl.selectedLanguageKey = selectedLanguageKey ? selectedLanguageKey : null; _.each(ctrl.students, function(student) { var studentId = student.student_id; ctrl.customChoices[studentId] = retrieveLanguageKeyForStudent(studentId); }); reformCustomized(); }; ctrl.isDownloadButtonDisabled = function() { return ctrl.downloadButtonDisabled; }; ctrl.downloadLetters = function() { if (ctrl.selectedFormatKey === 'zip') { $window.location.href = AuthGo.getUrl("kaz", "/main/ParentLetter/lang/custom/action/downloadaszip?" + ctrl.customized); } else { $window.location.href = AuthGo.getUrl("kaz", "/main/ParentLetter/lang/custom/action/download?" + ctrl.customized); } }; ctrl.cancel = function() { close(); }; function retrieveLanguageKeyForStudent(studentId) { return ctrl.studentCheckboxes[studentId] ? ctrl.selectedLanguageKey : ctrl.NONE_LANGUAGE_KEY; } function defineCustomized() { _.each(ctrl.students, function(student) { ctrl.customChoices[student.student_id] = ctrl.NONE_LANGUAGE_KEY; }); reformCustomized(); } function reformCustomized() { ctrl.customized = ''; for (var studentId in ctrl.customChoices) { var languageKey = ctrl.customChoices[studentId]; ctrl.customized += 'customized[' + studentId + ']=' + languageKey + '&' } ctrl.customized = ctrl.customized.slice(0, -1); } function areAllChoicesNone() { return _.every(ctrl.customChoices, function(languageKey){ return languageKey === ctrl.NONE_LANGUAGE_KEY }); } } self.show = function() { LazModalService.showModal({ controller: ['close', ParentLetterCustomizeCtrl], controllerAs: '$ctrl', overrideClass: 'modalWindow', onClose: '$ctrl.cancel', templateUrl: '/shared/js/angular/ui/parent-letter-customize.html' }); } } return new ParentLetterCustomize(); } ]); }());