UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

135 lines (118 loc) 5.27 kB
(function (module) { 'use strict'; module .controller('BannerShowController', BannerShowController); BannerShowController.$inject = ['$scope', '$timeout', '$state', 'BannerTemplate', 'Store', 'Category', '$stateParams', '$uibModal', 'toastr', 'Upload', '$translate', 'lodash']; function BannerShowController($scope, $timeout, $state, BannerTemplate, Store, Category, $stateParams, $uibModal, toastr, Upload, $translate,lodash) { $scope.banner = BannerTemplate .findOne({filter: {where: {id: $stateParams.bannerId}}}); var originalBanner; $scope.bannerStore = null; $scope.stores = Store.find(); $scope.categories = Category.find(); $scope.bannerCategories = []; $scope.banner.$promise .then(function () { originalBanner = $scope.banner.toJSON(); originalBanner.imagesList = lodash.clone(originalBanner.imagesList); return $scope.stores.$promise; }) .then(function (b) { $scope.$watch('banner.storeId', function (newId) { $scope.bannerStore = lodash.find($scope.stores, {id: newId}); }); return $scope.categories.$promise; }) .then(function (a) { $scope.$watchCollection('banner.categoriesIds', function (val) { console.log(val); $scope.bannerCategories = lodash.map(lodash.filter($scope.categories, function (category) { return $scope.banner.categoriesIds.indexOf(category.id) > -1; }),'name'); }); }); $scope.addStore = function () { var modalInstance = $uibModal.open({ animation: true, templateUrl: 'partials/admin.stores.create.html', controller: 'AdminStoreCreateController' }); modalInstance.result.then(function (newStore) { $scope.stores.push(newStore); $scope.banner.storeId = newStore.id; }); }; $scope.uploadImage = function ($file) { if ($file) { $scope.profilePhoto = $file; var imagesList = $scope.banner.imagesList; imagesList.unshift(0, 0, 'img/wait.png'); $scope.banner.$resolved = false; $timeout(function () { $scope.banner.$resolved = true; }, 0); Upload .upload({ url: 'http://uniko.co:3000/api/v2/bannertemplates/' + $scope.banner.id + '/addImage', data: { image: $file } }) .then(function (response) { $scope.banner.$resolved = false; $timeout(function () { imagesList.splice(0, 1, response.data.url); originalBanner.imagesList = lodash.clone(imagesList); if (!$scope.banner.image) { $scope.banner.image = response.data.url; } $scope.banner.$resolved = true; }, 0); }); } }; $scope.removeImage = function (url) { $scope.banner.$resolved = false; $timeout(function () { var removed = lodash.pullAt($scope.banner.imagesList, $scope.banner.imagesList.indexOf(url)); if ($scope.banner.image === removed[0]) { $scope.banner.image = $scope.banner.imagesList[0]; } $scope.banner.$resolved = true; }, 0); }; $scope.setImageAsDefault = function (url) { $scope.banner.image = url; }; $scope.update = function () { $scope.banner.$save(function () { originalBanner = $scope.banner.toJSON(); $translate('success-save-data').then(function(translation){ toastr.success(translation); }); }, function (err) { }); }; $scope.reset = function () { $scope.banner.$resolved = false; $timeout(function () { lodash.assign($scope.banner, originalBanner); originalBanner.imagesList = lodash.clone(originalBanner.imagesList); $scope.banner.$resolved = true; }, 0); }; $scope.delete = function () { $scope.banner.$remove(function () { $scope.banner = null; $state.go('admin.banners'); $translate('success-delete-article').then(function(translation){ toastr.success(translation); }); }, function () { $translate('error-delete-article').then(function(translation){ toastr.error(translation); }); }); } } })(angular.module('uniko.admin.banners'));