(function () { 'use strict'; var serviceId = 'itemService'; angular.module('items').factory(serviceId, ['itemsDataContext', 'config', itemService]); // Items Service function itemService(itemsDataContext, config) { var service = {}; // Get the icon class from the item type service.getItemIcon = function (item) { var type = item.itemType == 'File' ? ( item.displayType.length && item.displayType.split('/')[1] ) : item.displayType; if (!type) { type = item.itemType; } return type.toLowerCase().replace(/\s/g, '-'); } // Get the description from the item type service.getItemDescription = function (item) { const text = "This is "; switch (item.itemType) { case 'Status': return text + "a text note."; case 'Image': return text + "an uploaded image."; case 'File': return text + "a document."; case 'heading': return text + "a heading for this showcase."; case 'youtube': return text + "a YouTube video."; case 'vimeo': return text + "a Vimeo video."; case 'google-maps': return text + "a Google Map."; case 'prezi': return text + "a Prezi presentation."; case 'dailymotion': return text + "a DailyMotion video."; case 'sketchfab': return text + "a Sketchfab image."; case 'flickr': return text + "an image from Flickr."; case 'Evernote': return text + "an Evernote presentation. "; case 'slideshare': return text + "a slideshow from SlideShare."; } switch (item.addInType) { case 'heading': return text + "a heading."; case 'richtext': return 'This is text for this showcase.'; } } // Parse the Json response for a myprogress item service.parseResponse = function (jsonResponse) { try { var parsedResponse = JSON.parse(jsonResponse, function (key, value) { if (key.length < 2 || key[0] === key[0].toUpperCase() || key === "allocatedAssessmentGuid" || key === "publishedResourceId" || key === "responseIdentifier" || key === "usingEmail" || key === "data" || key === "type" || key === "width" || key === "height" || key === "src") { return value; } else { var newKey = key.replace(key[0], key[0].toUpperCase()); this[newKey] = value; return undefined; //removes old key } }); // Recursively return result properties to camel case. We can't do this in the reviver function when parsing // because only properties in the result object hierarchy should be changed, and some share names with properties // in other objects parsedResponse.Result = lowerFirstLetterObjectKeys(parsedResponse.Result); // StreamedResource.Type is an exception to the rule that all 'type' properties are lowercase parsedResponse.StreamedResources.forEach(function (resource) { if (resource.Type == undefined && resource.type != undefined) { resource.Type = resource.type; } }); return parsedResponse; } catch (e) { return {}; } } // Recursively return result properties to camel case function lowerFirstLetterObjectKeys(input) { if (typeof input !== 'object' || input == null) return input; if (Array.isArray(input)) return input.map(lowerFirstLetterObjectKeys); return Object.keys(input).reduce(function (newObj, key) { let val = input[key]; let newVal = (typeof val === 'object') ? lowerFirstLetterObjectKeys(val) : val; newObj[key.replace(key[0], key[0].toLowerCase())] = newVal; return newObj; }, {}); } // Save evidence for an item when it is created or edited by comparing // the item's list of selected competencies with the framework assignments // which have been viewed by the user service.saveEvidenceForItem = function (item, retrievedAssignments) { if (retrievedAssignments) { var actionedCompetencyArray = []; for (var assignment in retrievedAssignments) { // Save any changes to the evidence actionedCompetencyArray = discoverAndCommitChangesToEvidence(retrievedAssignments[assignment], item, assignment, actionedCompetencyArray); } } } // Traverse the branches for each competency to find any new competencies // that the user has mapped to this item function discoverAndCommitChangesToEvidence(competency, item, assignmentId, actionedCompetencyArray) { var linkedAlready = competencyHasThisItem(competency, item.itemId); if (!linkedAlready && item.competencies.indexOf(competency.id) > -1 && actionedCompetencyArray.indexOf(competency.id) == -1) { //Add new evidence var evidenceModel = {}; evidenceModel.title = item.title; evidenceModel.WhatForDetailId = competency.id; evidenceModel.sourceSystem = 'myshowcase'; evidenceModel.sourceId = item.itemId; evidenceModel.subType = item.type; evidenceModel.sourceDetailUri = config.myShowcaseItemApiUrl + item.type.replace(/\s/g, '') + 'items/' + item.itemId; evidenceModel.Summary = item.summary; evidenceModel.ThumbnailUri = item.thumbnailUrl; actionedCompetencyArray.push(competency.id); itemsDataContext.createEvidence(evidenceModel, assignmentId).then(function (data) { console.log('Adding this item as evidence for' + competency.name); }); } else if (linkedAlready && item.competencies.indexOf(competency.id) == -1 && actionedCompetencyArray.indexOf(competency.id) == -1) { //Remove existing evidence if (competency) { var evidenceItem = null; for (var i = 0; i < competency.simpleEvidence.length; i++) { if (competency.simpleEvidence[i].sourceId === item.itemId) { evidenceItem = competency.simpleEvidence[i]; } } if (evidenceItem) { actionedCompetencyArray.push(competency.id); itemsDataContext.deleteEvidence(evidenceItem.id, assignmentId).then(function (data) { console.log('Removing this item as evidence for' + competency.name); }); } } } if (competency.branches && competency.branches.length > 0) { competency.branches.forEach(function (branch) { actionedCompetencyArray = discoverAndCommitChangesToEvidence(branch, item, assignmentId, actionedCompetencyArray); }); } return actionedCompetencyArray; } // Check if evidence for this item has already been created for a given competency function competencyHasThisItem(competency, itemId) { if (!competency || !itemId || !competency.simpleEvidence) return false; for (var i = 0; i < competency.simpleEvidence.length; i++) { if (competency.simpleEvidence[i].sourceId === itemId) { return true; } } return false; }; // Edit the item before saving to ensure that the thumbnail urls // are correct and always use https service.editItemBeforeSaving = function (item) { if (item.embedThumbnail) { item.embedThumbnail = item.embedThumbnail.replace("http://", "https://"); } if (item.embedType === 'google-maps' && item.embedThumbnail.indexOf("&key=") == -1) { item.embedThumbnail = item.embedThumbnail + '&key=AIzaSyCEQR5hO9GVE8yIZ1Nd5ZnaJ8hHpFlHjxg'; } if (!item.competencies) item.competencies = []; } return service; } })();