(function () { 'use strict'; var app = angular.module('showcases'); // A controller for the showcase builder app.controller('showcaseBuilderController', ['$rootScope', '$scope', '$q', '$location', '$routeParams', 'common', 'config', 'showcasesService', 'showcaseModals', 'virusScanModalService', 'showcasesDataContext', 'userDataContext', function ($rootScope, $scope, $q, $location, $routeParams, common, config, showcasesService, showcaseModals, virusScanModalService, showcasesDataContext, userDataContext) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("showcaseBuilderController", "success"); // Get the showcase details when the scope is loaded getShowcase(); // Show a notice on navigate if the user has made changes $scope.leaving = false; $scope.$on("$locationChangeStart", function (event, next) { if ($scope.leaving || !$scope.data.originalShowcase) { return; } var hasChanged = false; var showcase = $scope.data.currentShowcase; if (!showcase.isFlexibleTemplate) { Object.keys(showcase).forEach(function (key) { if (key !== 'orderedShowcaseItems' && key !== 'isTemporary') { if (!angular.equals(showcase[key], $scope.data.originalShowcase[key])) { hasChanged = true; } } }); } else { if (!$scope.data.originalShowcase.isFlexibleTemplate) { hasChanged = true; } } if (hasChanged || ($scope.isFlexibleTemplate && $scope.options.flexibleTemplateChanged)) { event.preventDefault(); showcaseModals.builderConfirm(function () { showcasesDataContext.deleteTemporaryShowcases($scope.data.currentShowcase.showcaseId); $scope.leaving = true; $location.path(next); }) } }); // Delete any temporary showcases when the window is unloaded (refreshed or closed) window.onbeforeunload = function () { showcasesDataContext.deleteTemporaryShowcases($scope.data.currentShowcase.showcaseId); }; // Loaded showcase data $scope.data = { showcases: [], currentShowcase: null, loaded: false, previewUrl: null }; // Options for the showcase builder $scope.options = { sidebar: true, nameUnique: true, flexibleTemplateChanged: false }; // Toggle the sidebar $scope.toggleSidebar = function() { $scope.options.sidebar = !$scope.options.sidebar; } // Get the showcase details function getShowcase() { return $q.all([ userDataContext.getProfile(), showcasesDataContext.getThemes(), showcasesDataContext.getShowcases(), showcasesDataContext.getShowcase($routeParams.showcaseId) ]) .then(function (data) { $scope.data.user = data[0]; $scope.data.themes = data[1]; $scope.data.showcases = data[2]; if (!data[3].theme) { data[3].theme = { name: 'default' }; } $scope.data.currentShowcase = data[3]; $scope.data.loaded = true; createPreviewUrl(); return showcasesDataContext.getItemsForShowcase(data[3].showcaseId); }) .then(function (items) { $scope.data.currentShowcase.orderedShowcaseItems = items; $scope.data.originalShowcase = angular.copy($scope.data.currentShowcase); $scope.data.embedItems = items.filter(function (item) { return item.displayType === 'embed-item'; }); }) .catch(function () { $location.path('/showcases'); }); } // Create the url to preview a showcase function createPreviewUrl() { $scope.data.previewUrl = $scope.data.previewUrl ? $scope.data.previewUrl + ' ' : encodeURI(config.siteUrl + 'preview?id=' + $scope.data.currentShowcase.showcaseId); } // Update the preview for the showcase $scope.updatePreview = function() { var showcase = $scope.data.currentShowcase; showcase.isTemporary = true; showcasesDataContext.manageShowcase(showcase).then(function () { showcase.backgroundFileAsBase64 = null; showcase.headerFileAsBase64 = null; createPreviewUrl(); }).catch(function (errorData) { if (errorData === "Virus scan positive.") { showcase.backgroundFileAsBase64 = null; showcase.headerFileAsBase64 = null; virusScanModalService.virusModal(); } }); } // Save changes to the showcase $scope.saveChanges = function () { var showcase = $scope.data.currentShowcase; showcase.isTemporary = false; return showcasesDataContext.manageShowcase(showcase).then(function () { $scope.data.originalShowcase = angular.copy(showcase); $scope.options.flexibleTemplateChanged = false; logSuccess("Showcase updated"); showcase.backgroundFileAsBase64 = null; showcase.headerFileAsBase64 = null; $rootScope.$broadcast('UpdateShowcases'); }).catch(function (errorData) { if (errorData === "Virus scan positive.") { showcase.backgroundFileAsBase64 = null; showcase.headerFileAsBase64 = null; virusScanModalService.virusModal(); } }); }; // Go back to manage the items in this showcase $scope.editItems = function () { showcasesService.setCurrentShowcaseId($scope.data.currentShowcase.showcaseId); $location.path('/stuff'); } // Toggle the privacy setting for the showcase $scope.togglePrivacy = function () { var showcase = $scope.data.currentShowcase; showcase.isPrivate = !showcase.isPrivate; showcasesDataContext.toggleShowcasePrivacy(showcase.showcaseId).then(function () { logSuccess("Showcase privacy updated"); }); } }]); // A controller for the showcase editor app.controller('showcaseEditorController', ['$scope', 'showcasesDataContext', 'itemsModals', 'itemsDataContext', 'common', function ($scope, showcasesDataContext, itemsModals, itemsDataContext, common) { var getLogFn = common.logger.getLogFn; var logSuccess = getLogFn("itemSummary", "success"); // Get the users showcase summary when the scope is loaded getShowcaseSummary(); // Update the showcase summary after a broadcast $scope.$on('UpdateShowcases', function (evt, showcaseId) { return getShowcaseSummary().then(function (showcaseSummary) { if (showcaseId) { var showcase = showcaseSummary.find(function (showcase) { return showcase.id == showcaseId; }); if (showcase) { $scope.setCurrentShowcase(showcase); } else { $scope.closeEditor(); } } }); }); // Remove an item from the showcase after a broadcast $scope.$on('RemoveShowcaseItem', function (evt, item) { $scope.fns.removeItemFromShowcase(item, false); }); // Loaded showcase summary data $scope.loaded = false; // Options for the showcase editor $scope.options = { editorVisible: false, extraItemsDragOptions: { animation: 300, group: { name: 'showcaseitems', pull: 'clone', put: false }, sort: false }, editorDragOptions: { animation: 50, delay: 0, group: { name: 'showcaseitems', pull: false, put: true }, onUpdate: function (evt) { itemsDataContext.manageShowcaseItems($scope.currentShowcase.id, evt.models) }, onAdd: function (evt) { if (evt.model.itemType !== 'Folder' && !evt.model.isTransferItem) { if (evt.model.isShowcaseItemOnly) { $scope.fns.showcaseAddInItem(evt.model, evt.newIndex); } else { $scope.showcaseItem(evt.model, evt.newIndex); } } else { $scope.currentShowcase.items = $scope.currentShowcase.items.filter(function(item) { return item.itemId !== evt.model.itemId; }); } } } } //Showcase Editor functions $scope.fns = { // Showcase an add-in item showcaseAddInItem: function (item, newIndex) { return itemsDataContext.showcaseAddInItem([$scope.currentShowcase.id], newIndex, item.addInType).then(function (result) { if (typeof newIndex == "undefined") { item.toAdd = true; } logSuccess("Added item"); item.details = ""; item.showcaseItemId = result.itemShowcasedResponses[0].showcaseItemId; item.areDetailsEditedForShowcase = false; itemsModals.editShowcaseItem($scope.currentShowcase, item, 'sm'); }); }, // Edit an item for this showcase editItemFromShowcase: function (item) { var openModal = function () { itemsModals.editShowcaseItem($scope.currentShowcase, item, item.addInType ? 'sm' : 'lg'); } if (item.allDetailsLoaded === false) { return itemsDataContext.getItem(item.itemId).then(function (result) { item.details = result.details; item.allDetailsLoaded = true; openModal(item); }); } else { openModal(item); } }, // Remove an item from the current showcase removeItemFromShowcase: function (item, log) { log = typeof log == "undefined" ? true : log; return itemsDataContext.deleteShowcaseItem(item.showcaseItemId).then(function () { var items = $scope.currentShowcase.items; var index = items.findIndex(function (showcaseItem) { return item.showcaseItemId == showcaseItem.showcaseItemId; }); items.splice(index, 1); return log && logSuccess("Item removed from showcase"); }) }, // Jump back to the Edit showcase dropdown jumpBack: function () { $("#select-showcase").focus(); } } // Get the users showcase summary from the Api function getShowcaseSummary() { return showcasesDataContext.getShowcaseSummary().then(function (items) { $scope.loaded = true; $scope.setShowcaseSummary(items); return items; }); } }]); })();