(function () { 'use strict'; // Service for all items API functions var serviceId = 'itemsDataContext'; angular.module('items').factory(serviceId, ['appDataContext', 'config', 'itemsConfig', 'showcasesConfig', 'manageItemsDataContext', itemsDataContext]); function itemsDataContext(appDataContext, config, itemsConfig, showcasesConfig, manageItemsDataContext) { var service = {}; /** * Get extra details for an item, clearing both the items * and showcases cache if the item could not be retrieved */ service.getItem = function (itemId) { return appDataContext.get(itemsConfig.myItemRemoteServiceUrl + itemId).catch(function () { appDataContext.clearCaches([itemsConfig.cache, showcasesConfig.cache]); }); } /** * Get all users items */ service.getMyItems = function (folderId) { var queryString = folderId !== null ? '?folderId=' + folderId : ''; return appDataContext.get(itemsConfig.myItemRemoteServiceUrl + queryString, itemsConfig.cache, itemsConfig.myItemsCacheItem, true).then(function (result) { return (result.items) ? result : { items: result }; }); } service.getMyItemsDetail = function (folderId) { return appDataContext.get(itemsConfig.myItemRemoteServiceUrl + 'all/detail?folderId=' + folderId, itemsConfig.cache, itemsConfig.myItemsCacheItem).then(function (result) { return (result.items) ? result : { items: result }; }); } /** * Get extra data for all users items, adding the * extra data for each item to the cache */ service.getMyItemsExtra = function (items, folderId) { const queryString = folderId !== null ? '?folderId=' + folderId : ''; return appDataContext.get(itemsConfig.myItemRemoteServiceUrl + 'extra' + queryString).then(function (result) { items = items ? items.map(function (item, index) { return Object.assign(item, result.itemSummary[index]); }) : []; var data = { items: items, associatedCompetencies: result.associatedCompetencies, allDataLoaded: true }; appDataContext.updateCache(itemsConfig.cache, itemsConfig.myItemsCacheItem, data); return data; }) } /** * Put an item into the recycle bin */ service.recycleBinItem = function(itemId) { appDataContext.clearCaches([itemsConfig.cache, showcasesConfig.cache]); return appDataContext.get(itemsConfig.myItemRemoteServiceUrl + itemId + "/movetorecyclebin"); } /** * Create evidence for a deployed assignment (framework competency) */ service.createEvidence = function (evidence, deployedAssignmentId) { return appDataContext.post(config.apigatewayUrl + 'deployedassignment/EvidenceForMe/' + deployedAssignmentId, evidence); } /** * Delete evidence for a deployed assignment */ service.deleteEvidence = function(evidenceId, deployedAssignmentId) { return appDataContext.delete(config.apigatewayUrl + 'deployedassignment/EvidenceForMe/' + deployedAssignmentId + '/evidence/' + evidenceId); } /** * Create a new item using the manage items data context */ service.createItem = function (item) { if (item) switch (item.type) { case 'status': return manageItemsDataContext.createStatusItem(item); case 'image': return manageItemsDataContext.createImageItem(item); case 'badge': return manageItemsDataContext.createBadgeItem(item); case 'badgecollection': return manageItemsDataContext.createBadgeCollectionItem(item); case 'video': return manageItemsDataContext.createVideoItem(item); case 'file': return manageItemsDataContext.createFileItem(item); case 'weblink': return manageItemsDataContext.createWeblinkItem(item); case 'reflection': return manageItemsDataContext.createReflectionItem(item); default: return manageItemsDataContext.createEmbedItem(item); } } /** * Edit an existing item using the manage items data context */ service.editItem = function (item) { switch (item.type) { case 'status': return manageItemsDataContext.editStatusItem(item); case 'image': return manageItemsDataContext.editImageItem(item); case 'badge': return manageItemsDataContext.editBadgeItem(item); case 'badgecollection': return manageItemsDataContext.editBadgeCollectionItem(item); case 'video': return manageItemsDataContext.editVideoItem(item); case 'file': return manageItemsDataContext.editFileItem(item); case 'myprogress assessment': return manageItemsDataContext.editAssessmentItem(item); case 'weblink': return manageItemsDataContext.editWeblinkItem(item); case 'evernote': return manageItemsDataContext.editEvernoteItem(item); case 'form': return manageItemsDataContext.editFormItem(item); case 'reflection': return manageItemsDataContext.editReflectionItem(item); default: return manageItemsDataContext.editEmbedItem(item); } } /** * Add an item to a list of showcases */ service.showcaseItem = function (showcaseIds, itemId, index) { appDataContext.clearCaches([itemsConfig.cache, showcasesConfig.cache]); var data = { showcaseIds: showcaseIds }; return appDataContext.post(itemsConfig.myItemRemoteServiceUrl + itemId + "/showcase" + (index ? "/" + index : ''), data); } /** * Add an extra item (add in) to a list of showcases * name, level, isGroup, children, childrenWithData, details */ service.showcaseAddInItem = function(showcaseIds, index, addInType, data) { appDataContext.clearCaches([itemsConfig.cache, showcasesConfig.cache]); var postData = { showcaseIds: showcaseIds } if (data) { postData = Object.assign(data, postData); } return appDataContext.post(itemsConfig.myItemRemoteServiceUrl + "showcaseaddin/" + addInType + "/" + (index ? "/" + index : ''), postData); } /** * Edit / manage items in a showcase */ service.manageShowcaseItems = function (showcaseId, showcaseItems) { appDataContext.clearCaches([itemsConfig.cache, showcasesConfig.cache]); return appDataContext.post(showcasesConfig.showcaseRemoteServiceUrl + showcaseId + "/manageitems/", showcaseItems); } /** * Delete a showcase item using its showcaseitemid */ service.deleteShowcaseItem = function (showcaseItemId) { appDataContext.clearCaches([itemsConfig.cache, showcasesConfig.cache]); return appDataContext.delete(itemsConfig.myItemRemoteServiceUrl + showcaseItemId + "/showcaseitem"); } /** * Remove an item from a showcase using its showcaseid and itemid */ service.removeItemFromShowcase = function (showcaseId, itemId) { appDataContext.clearCaches([itemsConfig.cache, showcasesConfig.cache]); return appDataContext.delete(itemsConfig.myItemRemoteServiceUrl + itemId + "/showcase/" + showcaseId); } /** * Create a form based item (an item from a MyForms form) */ service.createFormItem = function (formItem) { appDataContext.clearCaches([itemsConfig.cache]); return appDataContext.post(itemsConfig.formItemRemoteServiceUrl, formItem); } /** * Edit a MyForms item */ service.editFormItem = function (formItem) { appDataContext.clearCaches([itemsConfig.cache]); return appDataContext.put(itemsConfig.formItemRemoteServiceUrl + formItem.itemId, formItem); } return service; } // Service for all create item and edit item API functions, that is used by the itemsDataContext angular.module('items').factory('manageItemsDataContext', ['appDataContext', 'itemsConfig', manageItemsDataContext]); function manageItemsDataContext(appDataContext, config) { var service = {}; /** * Clear the items cache when an item is added or edited */ function clearCache() { appDataContext.clearCaches(config.cache); } /** * Create a new status (AKA text note) item */ service.createStatusItem = function (statusItem) { clearCache(); return appDataContext.post(config.statusItemRemoteServiceUrl, statusItem); } /** * Create a new reflection item */ service.createReflectionItem = function (reflectionItem) { clearCache(); return appDataContext.post(config.reflectionItemRemoteServiceUrl, reflectionItem); } /** * Create a new embed item (vimeo, prezzi etc.) */ service.createEmbedItem = function(item) { clearCache(); return appDataContext.post(config.embedItemRemoteServiceUrl, item); } /** * Create a new image item */ service.createImageItem = function(imageItem) { clearCache(); return appDataContext.post(config.imageItemRemoteServiceUrl, imageItem); } /** * Create a new video item */ service.createVideoItem = function(videoItem) { clearCache(); return appDataContext.post(config.videoItemRemoteServiceUrl, videoItem); } /** * Create a new badge (open badges) item */ service.createBadgeItem = function(badgeItem) { clearCache(); return appDataContext.post(config.badgeItemRemoteServiceUrl, badgeItem); } /** * Create a new file item */ service.createFileItem = function(fileItem) { clearCache(); return appDataContext.post(config.fileItemRemoteServiceUrl, fileItem); } /** * Create a new weblink item */ service.createWeblinkItem = function(weblinkItem) { clearCache(); weblinkItem.generatingItem = true; return appDataContext.post(config.weblinkItemRemoteServiceUrl, weblinkItem); } /** * Create a new BadgeCollection item */ service.createBadgeCollectionItem = function (badgeCollectionItem) { clearCache(); return appDataContext.post(config.badgeCollectionItemRemoteServiceUrl, badgeCollectionItem); } /** * Edit a status item */ service.editStatusItem = function(statusItem) { clearCache(); return appDataContext.put(config.statusItemRemoteServiceUrl + statusItem.itemId, statusItem); } /** * Edit a reflection item */ service.editReflectionItem = function(reflectionItem) { clearCache(); return appDataContext.put(config.reflectionItemRemoteServiceUrl + reflectionItem.itemId, reflectionItem); } /** * Edit a weblink (like a webclipping) item */ service.editWeblinkItem = function(weblinkItem) { clearCache(); return appDataContext.put(config.weblinkItemRemoteServiceUrl + weblinkItem.itemId, weblinkItem); } /** * Edit a BadgeCollection item */ service.editBadgeCollectionItem = function (badgeCollectionItem) { clearCache(); return appDataContext.put(config.badgeCollectionItemRemoteServiceUrl + badgeCollectionItem.itemId, badgeCollectionItem); } /** * Edit a form item */ service.editFormItem = function(formItem) { clearCache(); return appDataContext.put(config.formItemRemoteServiceUrl + formItem.itemId, formItem); } /** * Edit an image item */ service.editImageItem = function(imageItem) { clearCache(); return appDataContext.put(config.imageItemRemoteServiceUrl + imageItem.itemId, imageItem); } /** * Edit a video item */ service.editVideoItem = function(videoItem) { clearCache(); return appDataContext.put(config.videoItemRemoteServiceUrl + videoItem.itemId, videoItem); } /** * Edit a badge item */ service.editBadgeItem = function(badgeItem) { clearCache(); return appDataContext.put(config.badgeItemRemoteServiceUrl + badgeItem.itemId, badgeItem); } /** * Edit a file item */ service.editFileItem = function(fileItem) { clearCache(); return appDataContext.put(config.fileItemRemoteServiceUrl + fileItem.itemId, fileItem); } /** * Edit an assessment item */ service.editAssessmentItem = function(assessmentItem) { clearCache(); return appDataContext.put(config.assessmentItemRemoteServiceUrl + assessmentItem.itemId, assessmentItem); } /** * Edit an embed item */ service.editEmbedItem = function(embedItem) { clearCache(); return appDataContext.put(config.embedItemRemoteServiceUrl + embedItem.itemId, embedItem) } return service; } })();