UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

202 lines (178 loc) 7.57 kB
(function () { 'use strict'; angular.module('uniko.authSatellizer') .controller('PhotosCtrl', PhotosCtrl) PhotosCtrl.$inject = ['$scope', '$rootScope', 'PredefinedBanner', 'CoupleAccount', 'LoopBackAuth', 'toastr', 'Upload', '$http', '$uibModal', 'Auth', '$translate', '$timeout', 'lodash']; function PhotosCtrl($scope, $rootScope, PredefinedBanner, CoupleAccount, LoopBackAuth, toastr, Upload, $http, $uibModal, Auth, $translate, $timeout, lodash) { $scope.newCover = null; $scope.isUpdated = false; $scope.profilePhoto = null; $scope.cropablePhoto = null; $scope.banners = PredefinedBanner.find(); $scope.weddingData = {}; $scope.slickConfig = { enabled: false, dots: true, centerMode: true, prevArrow: '.btn-prev', nextArrow: '.btn-next', appendDots: '.carousel' }; $timeout(function () { console.log("opened"); $scope.toggleSlick(); }, 1000); $scope.account = CoupleAccount.getCurrent(); $scope.account .$promise .then(function () { $scope.originalProfile = $scope.account.weddingData.profilePhoto; $scope.originalCover = $scope.account.weddingData.coverPhoto; $scope.$watch(function () { return $scope.account ? JSON.stringify($scope.account.toJSON()) : null; }, function (oldVal, newVal) { if (oldVal === newVal) { return; } oldVal = JSON.parse(oldVal); newVal = JSON.parse(newVal); delete oldVal.weddingData.profilePhoto; delete oldVal.updatedAt; delete newVal.weddingData.profilePhoto; delete newVal.updatedAt; var toldVal = JSON.stringify(oldVal); var tnewVal = JSON.stringify(newVal); if (toldVal !== tnewVal) { $scope.update(); } }); }); $scope.update = function () { CoupleAccount.updateMainAttributes($scope.account, showUpdateSuccess, function (err) { toastr.error(err); }); }; function showUpdateSuccess() { // toastr.success('Los datos se han almacenado correctamente'); $scope.url = 'http://front.uniko.co/' + $scope.account.url; $scope.weddingData = lodash.cloneDeep($scope.account.weddingData); if ($scope.account.weddingData.date) { $scope.weddingData.date = $scope.date = new Date($scope.account.weddingData.date); } } $scope.uploadPhotoProfile = function ($file, $invalidFiles) { if ($invalidFiles && $invalidFiles.length > 0) { lodash.forEach($invalidFiles, function (file) { if (file.$errorMessages.maxSize) { $translate('error-image').then(function (translation) { toastr.success(translation); }); } }); return; } $scope.originalProfile = null; $scope.cropablePhotoProfile = $file.$ngfBlobUrl; }; $scope.uploadPhotoCover = function ($file, $invalidFiles) { if ($invalidFiles && $invalidFiles.length > 0) { lodash.forEach($invalidFiles, function (file) { if (file.$errorMessages.maxSize) { $translate('error-image').then(function (translation) { toastr.success(translation); }); } }); return; } $scope.originalCover = null; $scope.cropablePhotoCover = $file.$ngfBlobUrl; }; $scope.setOriginalCover = function () { $scope.originalCover = $scope.account.weddingData.coverPhoto; } $scope.setOriginalProfile = function () { $scope.originalProfile = $scope.account.weddingData.profilePhoto; } $scope.selectPredefined = function (url) { console.log("pasa"); $scope.account.weddingData.coverPhoto = url; }; $scope.saveProfile = function () { $scope.disabled = true; Upload .upload({ url: 'http://uniko.co:3000/api/v2/coupleaccounts/' + $scope.account.id + '/wedding/addimageprofile', data: { image: Upload.dataUrltoBlob($scope.profilePhoto) } }) .then(function (response) { $scope.account.weddingData.profilePhoto = $scope.originalProfile = response.data.url; $rootScope.$broadcast('imageProfile', $scope.account.weddingData.profilePhoto); }); }; $scope.saveCover = function () { if ($scope.originalCover) { $scope.account.weddingData.coverPhoto = $scope.originalCover; } else { $scope.disabled = true; Upload .upload({ url: 'http://uniko.co:3000/api/v2/coupleaccounts/' + $scope.account.id + '/wedding/addCoverPhoto', data: { image: Upload.dataUrltoBlob($scope.coverPhoto) } }) .then(function (response) { $scope.account.weddingData.coverPhoto = response.data.url; }); } }; $scope.editPhoto = function () { var modalInstance = $uibModal.open({ animation: true, templateUrl: 'partials/auth.wedding.photo.add.html', controller: 'PhotoNewController', size: 'lg', backdrop: 'static', resolve: { currentAccount: function () { return $scope.account; } } }); modalInstance.result .then(function (result) { }); }; $scope.editBanner = function () { var modalInstance = $uibModal.open({ animation: true, templateUrl: 'partials/auth.wedding.banner.add.html', controller: 'BannerNewController', size: 'lg', backdrop: 'static', resolve: { currentAccount: function () { return $scope.account; } } }); modalInstance.result .then(function (result) { getMixpanel().people.set({'Cover photo Added': true}); // if (result.isUpdated) { // $scope.newCover = result.coverPhoto; // } else { $scope.account.weddingData.coverPhoto = result.coverPhoto; // } }); }; $scope.toggleSlick = function () { $scope.slickConfig.enabled = !$scope.slickConfig.enabled; }; } })();