(function () { 'use strict'; var app = angular.module('items'); // A controller for the mystuff page app.controller('myStuffController', ['$scope', 'common', 'showcasesService', 'showcaseModals', 'folderModals', 'itemsDataContext', 'showcasesDataContext', 'itemService', function ($scope, common, showcasesService, showcaseModals, folderModals, itemsDataContext, showcasesDataContext) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("itemSummary", "success"); // Loaded showcases data $scope.data = { showcaseSummary: [], }; // MyStuff page functions $scope.fns = { // Create a new showcase using a modal createShowcase: showcaseModals.createShowcase, createFolder: folderModals.createFolder, // Set the showcase summary list setShowcaseSummary: function (showcaseSummary) { $scope.data.showcaseSummary = showcaseSummary; getCurrentShowcase(); }, // Set the current showcase and open the editor setCurrentShowcase: function (showcase) { return showcase ? getShowcaseItems(showcase) : $scope.fns.closeEditor(); }, // Showcase an item showcaseItem: function (item, newIndex) { item.allDetailsLoaded = false; return itemsDataContext.showcaseItem([$scope.data.currentShowcase.id], item.itemId, newIndex).then(function (result) { item.details = null; item.showcaseItemId = result.itemShowcasedResponses[0].showcaseItemId; if (typeof newIndex == "undefined") { $scope.data.currentShowcase.items.push(item); } logSuccess("Item showcased"); }); }, // Close the editor closeEditor: function () { showcasesService.setCurrentShowcaseId(null); $scope.data.currentShowcase = null; } }; // Get the current showcase from the service function getCurrentShowcase() { var currentId = showcasesService.getCurrentShowcaseId(); if (currentId) { $scope.fns.setCurrentShowcase($scope.data.showcaseSummary.find(function (showcase) { return showcase.id === currentId; })); } } // Get the items for a specific showcase when opening the editor function getShowcaseItems(showcase) { showcase.loadingItems = true; $scope.data.currentShowcase = showcase; return showcasesDataContext.getItemsForShowcase(showcase.id).then(function (items) { showcase.items = items; showcase.loadingItems = false; showcasesService.setCurrentShowcaseId(showcase.id); $scope.data.currentShowcase = showcase; }); } }]); app.controller('myItemsController', ['$scope', '$rootScope', '$filter', '$q', 'layoutService', 'itemsDataContext', 'userDataContext', 'search', 'foldersDataContext', 'itemService', 'common', 'myUsersDataContext', 'config', function ($scope, $rootScope, $filter, $q, layoutService, itemsDataContext, userDataContext, search, foldersDataContext, itemService, common, myUsersDataContext, config) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("itemSummary", "success"); // Loaded Items data $scope.data = { loaded: false, items: { all: [], filtered: [] }, categorizedItems: {}, tags: [], frameworks: [], competencies: [], associatedCompetencies: [], folder: null, transferItems: { all: [], filtered: [] }, transferFolder: null }, $scope.userOrgs = []; if ($rootScope.currentRole) getItems(null); // Update the items after a broadcast $rootScope.$on('roleTokenSet', function () { getItems(null); }); // Update the items after a broadcast $scope.$on('UpdateItems', function () { getItems(null); }); $scope.$on('BrowseFolderItems', function (event, data) { getItems(data); }); // Update the visible items when the current showcase is changed $scope.$watch('currentShowcase', filterItems, true); $scope.showItemsForTransfer = function () { $scope.showItemsForTransferEnabled = !$scope.showItemsForTransferEnabled; }; // Options for the mystuff items $scope.options = { sidebar: false, smallItems: false, filters: { query: '', selectedTag: null, selectedType: null, selectedCompetency: null }, itemDragOptions: { animation: 200, delay: 0, handle: '.action-container', ghostClass: 'sortable-ghost', group: { name: 'showcaseitems1', pull: 'clone', put: true }, sort: false, onUpdate: function (evt) { if (!evt.model.isTransferItem) { itemsDataContext.manageShowcaseItems($scope.currentShowcase.id, evt.models); } }, onAdd: function (evt) { if (!evt.model.isTransferItem) { if (evt.model.isShowcaseItemOnly) { $scope.fns.showcaseAddInItem(evt.model, evt.newIndex); } else { $scope.showcaseItem(evt.model, evt.newIndex); } } else if (evt.model.isTransferItem) { $scope.fns.transferItem(evt.model, evt.newIndex); } } }, itemOrderOptions: [ { name: "Order by date", value: { predicate: 'addedDate', reverse: true }, }, { name: "Order by type", value: { predicate: 'itemType' }, }, { name: "Order by title", value: { predicate: 'title' }, } ] }; // Set the default items order $scope.options.itemsOrder = $scope.options.itemOrderOptions[0]; // My items functions $scope.fns = { // Toggle the sidebar toggleSidebar: function () { $scope.options.sidebar = !$scope.options.sidebar; }, // Clear filters clearAllFilters: function () { Object.keys($scope.options.filters).forEach(function (filter) { $scope.options.filters[filter] = null; }); filterItems(); categorizeItems(); }, // Toggle the size of the items toggleItemSize: function () { $scope.options.smallItems = !$scope.options.smallItems; }, // Set a search filter for the items search: function (query) { $scope.fns.setFilter('query', query); }, // Set a filter for the items setFilter: function (filter, value) { $scope.options.filters[filter] = value; filterItems(); }, // Clear a filter for the items clearFilter: function (filter) { $scope.options.filters[filter] = null; filterItems(); categorizeItems(); }, // Display more items in the item list (e.g. if the user has many items and scrolls down to see more) loadItemsOnScroll: function () { if ($scope.options.itemsDisplayedInList < $scope.data.items.filtered.length) { $scope.options.itemsDisplayedInList += (layoutService.isMobile() ? 5 : 10); } }, loadTransferItems: function () { getItemsForTransfer(); }, // move to service transferItem: function (item, newIndex) { if (item.itemType === 'Folder') { $scope.data.items.filtered = $scope.data.items.filtered.filter(function (tItem) { return item.itemId !== tItem.itemId; }); $scope.data.items.all = $scope.data.items.all.filter(function (tItem) { return item.itemId !== tItem.itemId; }); } else { item.folderId = $scope.data.folder.itemId; itemService.editItemBeforeSaving(item); itemsDataContext.getItem(item.itemId).then(function (res) { res.folderId = item.folderId; return itemsDataContext.editItem(res).then(function (result) { $scope.data.transferItems.filtered = $scope.data.transferItems.filtered.filter(function (tItem) { return item.itemId !== tItem.itemId; }); $scope.data.transferItems.all = $scope.data.transferItems.all.filter(function (tItem) { return item.itemId !== tItem.itemId; }); logSuccess('Item moved to folder'); // $scope.data.items.all.push(result); // Todo update filters and use get items extra and not getItem return getItems($scope.data.folder); }); }) } }, } // Get all data for items function getItems(folder) { $scope.data.loaded = false; var folderId = folder !== null ? folder.itemId : null; $q.all([ getMyItems(folderId), getTagData(), ]).then(function () { filterItems(); categorizeItems(); $scope.data.loaded = true; $scope.data.folder = folder; if (folder == null || folder === undefined) { $scope.data.transferItems = []; $scope.showItemsForTransferEnabled = false; } }); } // Filter the items list based on items in the current showcase function filterItemsInCurrentShowcase(items) { var showcaseActive = ($scope.currentShowcase && $scope.currentShowcase.items); if (showcaseActive && $scope.currentShowcase.loadingItems) { return items; } if (items && items.length) { return !showcaseActive ? items : items.filter(function (item) { return $scope.currentShowcase.items.findIndex(function (showcaseItem) { return showcaseItem.itemId == item.itemId; }) == -1; }); } } // Filter the items list using the chosen filters function filterItems() { var items = $scope.data.items.all; var filters = $scope.options.filters; items = filterItemsInCurrentShowcase(items); if (items && items.length > 0) { items = $filter('tagfilter')(items, filters.selectedTag); items = $filter('competencyfilter')(items, filters.selectedCompetency); items = $filter('typefilter')(items, filters.selectedType); items = $filter('itemtextfilter')(items, filters.query); } $scope.data.items.filtered = items; $scope.options.itemsDisplayedInList = layoutService.isMobile() ? 8 : 20; if (!$scope.data.items.filtered) { $scope.data.items.filtered = []; } } // Categorize the filtered items by their tag, type and frameworks function categorizeItems() { var categorizedItems = { tags: {}, type: {}, itemIds: [] }; if ($scope.data.items.filtered) { $scope.data.items.filtered.forEach(function (item) { if (item.itemType !== 'Folder') { if (!item.tags || !item.tags.length) { categorizedItems.tags["No Tags"] = (categorizedItems.tags["No Tags"] || 0) + 1; } else { item.tags.forEach(function (tag) { categorizedItems.tags[tag.text] = (categorizedItems.tags[tag.text] || 0) + 1; }); } var type = item.itemType == "Embed" ? item.displayType.toLowerCase().replace("-", " ").replace(/\b[a-z]/g, function (letter) { return letter.toUpperCase(); }) : item.itemType; categorizedItems.type[type] = (categorizedItems.type[type] || 0) + 1; categorizedItems.itemIds.push(item.itemId); } }); } $scope.data.categorizedItems = categorizedItems; } // Get this users items function getMyItems(folderId) { return itemsDataContext.getMyItems(folderId).then(function (result) { //when that is done assign the item data to the view model $scope.data.items.all = result.items; // Get extra data and index if all data is not present if (!result.allDataLoaded) { return getMyItemsExtra(folderId); } else { $scope.data.associatedCompetencies = result.associatedCompetencies; search.indexItems($scope.data.items.all); } }); } // Get extra details about the users items function getMyItemsExtra(folderId) { return itemsDataContext.getMyItemsExtra($scope.data.items.all, folderId).then(function (result) { $scope.data.items.all = result.items; $scope.data.associatedCompetencies = result.associatedCompetencies; // Index the items for the free text search search.indexItems($scope.data.items.all); }); } // Get tags data for this users items function getTagData() { return userDataContext.getTagSummary().then(function (tags) { $scope.data.tags = tags; }); } }]); app.controller('myTransferItemsController', ['$rootScope','$scope', '$filter', '$q', 'layoutService', 'itemsDataContext', 'userDataContext', 'search', 'foldersDataContext', 'itemService', 'common', function ($rootScope, $scope, $filter, $q, layoutService, itemsDataContext, userDataContext, search, foldersDataContext, itemService, common) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("itemSummary", "success"); getItemsForTransfer(null); $scope.transferItemsDragOptions = { animation: 200, delay: 0, handle: '.action-container', ghostClass: 'sortable-ghost', group: { name: 'showcaseitems1', pull: 'clone', put: false }, sort: false, onUpdate: function (evt) { }, onAdd: function (evt) {} } $scope.transferOptions = { filters: { query: '', selectedTag: null, selectedType: null, selectedCompetency: null }, itemOrderOptions: [ { name: "Order by date", value: { predicate: 'addedDate', reverse: true }, }, { name: "Order by type", value: { predicate: 'itemType' }, }, { name: "Order by title", value: { predicate: 'title' }, } ] }; $scope.$on('TransferItemToFolder', function (event, data) { $scope.transferfns.transferItem(data); }); $scope.transferOptions.itemsOrder = $scope.transferOptions.itemOrderOptions[0]; $scope.transferfns = { // Set a search filter for the items search: function (query) { $scope.transferfns.setFilter('query', query); }, // Set a filter for the items setFilter: function (filter, value) { var current = $scope.transferOptions.filters[filter]; $scope.transferOptions.filters[filter] = current === value ? null : value; filterTransferItems(); }, // Clear a filter for the items clearFilter: function (filter) { $scope.options.filters[filter] = null; filterTransferItems(); }, // Display more items in the item list (e.g. if the user has many items and scrolls down to see more) loadItemsOnScroll: function () { if ($scope.transferOptions.itemsDisplayedInList < $scope.data.items.filtered.length) { $scope.transferOptions.itemsDisplayedInList += (layoutService.isMobile() ? 5 : 10); } }, loadTransferItems: function () { if ($scope.showItemsForTransferEnabled) { $scope.showItemsForTransferEnabled = false; } else { getItemsForTransfer(null); } }, // move to service transferItem: function (item, newIndex) { if (item.itemType === 'Folder') { $scope.data.items.filtered = $scope.data.items.filtered.filter(function (tItem) { return item.itemId !== tItem.itemId; }); $scope.data.items.all = $scope.data.items.all.filter(function (tItem) { return item.itemId !== tItem.itemId; }); } else { item.folderId = $scope.data.folder.itemId; itemService.editItemBeforeSaving(item); itemsDataContext.getItem(item.itemId).then(function (res) { res.folderId = item.folderId; return itemsDataContext.editItem(res).then(function (result) { $scope.data.transferItems.filtered = $scope.data.transferItems.filtered.filter(function (tItem) { return item.itemId !== tItem.itemId; }); $scope.data.transferItems.all = $scope.data.transferItems.all.filter(function (tItem) { return item.itemId !== tItem.itemId; }); logSuccess('Item moved to folder'); // Todo update filters and use get items extra and not getItem $rootScope.$broadcast('BrowseFolderItems', $scope.data.folder); }); }) } }, } function filterTransferItems() { var items = $scope.data.transferItems.all; var filters = $scope.transferOptions.filters; items = filterItemsInCurrentShowcase(items); if (items && items.length > 0) { items = $filter('tagfilter')(items, filters.selectedTag); items = $filter('competencyfilter')(items, filters.selectedCompetency); items = $filter('typefilter')(items, filters.selectedType); items = $filter('itemtextfilter')(items, filters.query); } $scope.data.transferItems.filtered = []; $scope.data.transferItems.filtered = items; $scope.transferOptions.transferItemsDisplayedInList = layoutService.isMobile() ? 8 : 20; if (!$scope.data.transferItems.filtered) { $scope.data.transferItems.filtered = []; } } function getItemsForTransfer(folder) { var folderId = null; if (folder !== null && folder !== undefined) { folderId = folder.itemId; } return itemsDataContext.getMyItems(folderId).then(function (result) { //when that is done assign the item data to the view model $scope.data.transferItems = {}; $scope.showItemsForTransferEnabled = false; if (!result.allDataLoaded) { return itemsDataContext.getMyItemsExtra(result.items, folderId).then(function (resExtra) { resExtra.items.forEach(function(item) { item.isTransferItem = true; }); $scope.data.transferItems.all = resExtra.items; $scope.data.transferItems.filtered = filterItemsInCurrentShowcase(resExtra.items); $scope.showItemsForTransferEnabled = true; $scope.data.transferFolder = folder; categorizeTransferItems(); }); } else { resExtra.items.forEach(function(item) { item.isTransferItem = true; }); $scope.data.transferItems.all = result.items; $scope.data.transferItems.filtered = filterItemsInCurrentShowcase(result.items); $scope.showItemsForTransferEnabled = true; $scope.data.transferFolder = folder; categorizeTransferItems(); } }); } function filterItemsInCurrentShowcase(items) { var showcaseActive = ($scope.currentShowcase && $scope.currentShowcase.items); if (showcaseActive && $scope.currentShowcase.loadingItems) { return items; } if (items && items.length) { return !showcaseActive ? items : items.filter(function (item) { return $scope.currentShowcase.items.findIndex(function (showcaseItem) { return showcaseItem.itemId == item.itemId; }) == -1; }); } } // Categorize the filtered items by their tag, type and frameworks function categorizeTransferItems() { var itemTypes = [{ name: 'All', value: null }]; if ($scope.data.transferItems.filtered) { var dupItemTypes = []; $scope.data.transferItems.filtered.forEach(function (item) { var type = item.itemType; if (dupItemTypes.indexOf(type) === -1 && type !== 'Folder') { itemTypes.push({ name: type, value: type }); dupItemTypes.push(type); } }); } $scope.transferOptions.itemTypes = itemTypes; $scope.transferOptions.selectedItemType = itemTypes[0]; } $scope.$on('BrowseTransferFolderItems', function (event, data) { getItemsForTransfer(data); }); $scope.$on('UpdateTransferItems', function () { getItemsForTransfer(null); }); }]); })();