(function() { "use strict"; angular .module('shared') .service('standardsService', ['$http', '$q', '$rootScope','currentSubFolders', function ($http, $q, $rootScope,currentSubFolders) { var standardLimits = { SELECTED_STANDARD_LIMIT: 30, MAX_CODE_LENGTH: 30, MAX_DESCRIPTION_LENGTH: 1000 }; var SELECTED_STANDARDS_UPDATE = 'selectedStandardsUpdate'; var CUSTOM_STANDARDS_UPDATE = 'customStandardsUpdate'; var stateStandardsUrl = '/api/statestandards'; var customStandardsUrl = '/api/customstandards'; var folderStandards = {}; var selectedStandards = {}; var folderCustomStandards = {}; var selectedCustomStandards = {}; var lastStrand; var lastRemovedCustomStandard; function registerOnSelectedStandardsUpdate(scope, callback) { var eventListenerDestructor = $rootScope.$on(SELECTED_STANDARDS_UPDATE, callback); scope.$on('$destroy', eventListenerDestructor); } function emitSelectedStandardsUpdate() { $rootScope.$emit(SELECTED_STANDARDS_UPDATE); } function registerOnCustomStandardsUpdate(scope, callback) { var eventListenerDestructor = $rootScope.$on(CUSTOM_STANDARDS_UPDATE, callback); scope.$on('$destroy', eventListenerDestructor); } function emitCustomStandardsUpdate() { $rootScope.$emit(CUSTOM_STANDARDS_UPDATE); } function getStateStandardsStates() { var urlStr = stateStandardsUrl + '/states'; return $q(function (resolve, reject){ $http.get(urlStr).then( function (response) { resolve(response.data); }, function (reason) { reject(reason); } ); }); } function getStateStandardsStrands(stateId, grade) { var urlStr = stateStandardsUrl + '/strands/state/' + stateId + '/grade/' + grade; return $q(function (resolve, reject){ $http.get(urlStr).then( function (response) { resolve(response.data); }, function (reason) { reject(reason); } ); }); } function getStateStandards(stateStandardStrandId) { var urlStr = stateStandardsUrl + '/standards/' + stateStandardStrandId; return $q(function (resolve, reject){ $http.get(urlStr).then( function (response) { var result = response.data; setSelectedStandards(result); resolve(result); }, function (reason) { reject(reason); } ); }); } function getCustomStandards(everyoneFolders) { var urlStr = customStandardsUrl + '/standards'; if (everyoneFolders) { urlStr = customStandardsUrl + '/standardsEveryone'; } return $q(function (resolve, reject){ $http.get(urlStr).then( function (response) { var result = response.data; setSelectedCustomStandards(result); resolve(result); }, function (reason) { reject(reason); } ); }); } function createCustomStandard(data) { var urlStr = customStandardsUrl + '/standard'; return $q(function (resolve, reject) { $http.post(urlStr, data).then( function (response) { resolve(response.data); }, function (reason) { reject(reason); } ) }) } function editCustomStandard(data) { var urlStr = customStandardsUrl + '/standard'; return $q(function (resolve, reject) { $http.put(urlStr, data).then( function (response) { resolve(response.data); }, function (reason) { reject(reason); } ) }) } function deleteCustomStandard(customStandardId) { var urlStr = customStandardsUrl + '/standard/' + customStandardId + '/' + (currentSubFolders.getAreFoldersForEveryone() ? 'y' : 'n'); return $q(function (resolve, reject) { $http.delete(urlStr).then( function (response) { resolve(response.data); }, function (reason) { reject(reason); } ) }) } function selectStandard(standard) { selectedStandards[parseInt(standard.state_standard_id)] = standard; } function selectCustomStandard(standard) { selectedCustomStandards[parseInt(standard.custom_standard_id)] = standard; } function deselectStandard(standard) { delete selectedStandards[parseInt(standard.state_standard_id)]; } function deselectCustomStandard(standard) { lastRemovedCustomStandard = standard; delete selectedCustomStandards[parseInt(standard.custom_standard_id)]; } function setFolderStandards(standards) { folderStandards = {}; selectedStandards = {}; standards.forEach(function(standard) { folderStandards[parseInt(standard.state_standard_id)] = standard; selectedStandards[parseInt(standard.state_standard_id)] = standard; }); } function setFolderCustomStandards(standards) { folderCustomStandards = {}; selectedCustomStandards = {}; standards.forEach(function(standard) { folderCustomStandards[parseInt(standard.custom_standard_id)] = standard; selectedCustomStandards[parseInt(standard.custom_standard_id)] = standard; }) } function setLastStrand(strand) { lastStrand = strand; } function getLastStrand() { return lastStrand; } function getLastRemovedCustomStandard() { return lastRemovedCustomStandard; } function resetSelectedStandards() { selectedStandards = {}; Object.keys(folderStandards).forEach(function(standardId) { selectedStandards[standardId] = folderStandards[standardId]; }) } function resetSelectedCustomStandards() { selectedCustomStandards = {}; Object.keys(folderCustomStandards).forEach(function(standardId) { selectedCustomStandards[standardId] = folderCustomStandards[standardId]; }) } function getSelectedStandards() { return selectedStandards; } function getSelectedCustomStandards() { return selectedCustomStandards; } function setSelectedStandards(standards) { standards.forEach(function(standard) { if (selectedStandards[parseInt(standard.state_standard_id)]) { standard.selected = true; } if (standard.children) { setSelectedStandards(standard.children); } }); } function setSelectedCustomStandards(standards) { standards.forEach(function(standard) { if (selectedCustomStandards[parseInt(standard.custom_standard_id)]) { standard.selected = true; } }) } function getStandardIdsToAdd() { var standardIdsToAdd = []; Object.keys(selectedStandards).forEach(function(selectedStandardId) { if (!folderStandards[selectedStandardId]) { standardIdsToAdd.push(selectedStandardId); } }); return standardIdsToAdd; } function getCustomStandardIdsToAdd() { var customStandardIdsToAdd = []; Object.keys(selectedCustomStandards).forEach(function(selectedCustomStandardId) { if (!folderCustomStandards[selectedCustomStandardId]) { customStandardIdsToAdd.push(selectedCustomStandardId); } }); return customStandardIdsToAdd; } function getStandardIdsToRemove() { var standardIdsToRemove = []; Object.keys(folderStandards).forEach(function(folderStandardId) { if (!selectedStandards[parseInt(folderStandardId)]) { standardIdsToRemove.push(folderStandardId); } }); return standardIdsToRemove; } function getCustomStandardIdsToRemove() { var customStandardIdsToRemove = []; Object.keys(folderCustomStandards).forEach(function(folderCustomStandardId) { if (!selectedCustomStandards[parseInt(folderCustomStandardId)]) { customStandardIdsToRemove.push(folderCustomStandardId); } }); return customStandardIdsToRemove; } function hasReachedStandardsLimit() { return (Object.keys(selectedStandards).length + Object.keys(selectedCustomStandards).length) === standardLimits.SELECTED_STANDARD_LIMIT; } function hasSelectedStandards() { return (Object.keys(selectedStandards).length + Object.keys(selectedCustomStandards).length) > 0; } function updateSelectedCustomStandards(customStandard, isRemoved) { var selectedCustomStandard = selectedCustomStandards[parseInt(customStandard.custom_standard_id)]; if (selectedCustomStandard) { if (isRemoved) { delete selectedCustomStandards[parseInt(customStandard.custom_standard_id)]; } else { selectedCustomStandard.custom_code = customStandard.custom_code; selectedCustomStandard.custom_description = customStandard.custom_description; } } } return { registerOnSelectedStandardsUpdate: registerOnSelectedStandardsUpdate, emitSelectedStandardsUpdate: emitSelectedStandardsUpdate, registerOnCustomStandardsUpdate: registerOnCustomStandardsUpdate, emitCustomStandardsUpdate: emitCustomStandardsUpdate, getStateStandardsStates: getStateStandardsStates, getStateStandardsStrands: getStateStandardsStrands, getStateStandards: getStateStandards, getCustomStandards: getCustomStandards, createCustomStandard: createCustomStandard, editCustomStandard: editCustomStandard, deleteCustomStandard: deleteCustomStandard, selectStandard: selectStandard, selectCustomStandard: selectCustomStandard, deselectStandard: deselectStandard, deselectCustomStandard: deselectCustomStandard, setFolderStandards: setFolderStandards, setFolderCustomStandards: setFolderCustomStandards, setLastStrand: setLastStrand, getLastStrand: getLastStrand, getLastRemovedCustomStandard: getLastRemovedCustomStandard, resetSelectedStandards: resetSelectedStandards, resetSelectedCustomStandards: resetSelectedCustomStandards, getSelectedStandards: getSelectedStandards, getSelectedCustomStandards: getSelectedCustomStandards, getStandardIdsToAdd: getStandardIdsToAdd, getCustomStandardIdsToAdd: getCustomStandardIdsToAdd, getStandardIdsToRemove: getStandardIdsToRemove, getCustomStandardIdsToRemove: getCustomStandardIdsToRemove, hasReachedStandardsLimit: hasReachedStandardsLimit, hasSelectedStandards: hasSelectedStandards, updateSelectedCustomStandards: updateSelectedCustomStandards, standardLimits: standardLimits } }]); }());