angular.module('Postern.controllers', []) .controller('HomeController', ['$scope', '$window', '$location', function($scope, $window, $location) { $scope.showCategories = function() { $window.location.href = '/joygrams'; }; }]) .controller('AllJoygramsController', ['$scope', '$window', function($scope, $window) { $scope.pickCategory = function(category) { $window.location.href = '/joygrams/' + category; }; }]) .controller('PickSubcategoryController', ['$scope', '$window', function($scope, $window) { $scope.pickSubcategory = function(category, subcategory) { $window.location.href = '/joygrams/' + category + '/' + subcategory; }; }]) .controller('PickGramController', ['$uibModal', '$scope', '$window', '$location', function($uibModal, $scope, $window, $location) { $scope.pick = function(category, subcategory, currentGram) { $window.location.href = '/joygrams/' + category + '/' + subcategory + '/' + currentGram; }; $scope.open = function (imgUrl, title) { var modalInstance = $uibModal.open({ ariaLabelledBy: 'modal-title', ariaDescribedBy: 'modal-body', templateUrl: 'cardPreviewModal.html', controller: 'CardPreviewModalController', size: 'lg', resolve: { url: function () { return imgUrl; }, title: function () { return title; } } }); modalInstance.result.then(function () { }, function () { }); }; }]) .controller('CardPreviewModalController', ['$uibModalInstance', '$scope', 'url', 'title', function($uibModalInstance, $scope, url, title) { $scope.url = url; $scope.title = title; $scope.cancel = function() { $uibModalInstance.dismiss('cancel'); }; }]) .controller('SendCardController', ['$uibModal', '$scope', '$location', '$window', '$http', '$timeout', function($uibModal, $scope, $location, $window, $http, $timeout) { $scope.messageMaxLength = 300; $scope.submitButtonText = "Checkout"; $scope.previewButtonText = "Generate Preview"; $scope.showLoadingOverlay = false; $scope.zipCodeRegex = '\\d\\d\\d\\d\\d(-\\d\\d\\d\\d)?' $scope.fromEnabled = false; $scope.fromRequired = false; $scope.card = getFormFromStorage() $scope.open = function (imgUrl, title) { var modalInstance = $uibModal.open({ ariaLabelledBy: 'modal-title', ariaDescribedBy: 'modal-body', templateUrl: 'cardPreviewModal.html', controller: 'CardPreviewModalController', size: 'lg', resolve: { url: function () { return imgUrl; }, title: function () { return title; } } }); modalInstance.result.then(function () { }, function () { }); }; $scope.doSubmit = function(category, subcategory, gram, tokenId, email) { $scope.error = false; $scope.sending = true; $scope.submitButtonText = "Processing Your JoyGram..."; $scope.showLoadingOverlay = true; var data = { message: $scope.card.message, toAddress: $scope.card.to, paymentToken: tokenId, customerId: $scope.customerId, email: email }; if ($scope.fromRequired) { data.fromAddress = $scope.card.from; } $http({ method: 'POST', url: '/ajax/' + category + '/' + subcategory + '/' + gram, data: data }).then(function success(response) { $scope.showLoadingOverlay = false; if (response.data.result == "success") { clearFormStorage(); $scope.success = true; $scope.successData = response.data; } else { $scope.invalidRequest = true; $scope.invalidRequestData = response.data; } }, function error(response) { $scope.showLoadingOverlay = false; $scope.sending = false; $scope.error = "There was a problem sending your JoyGram. We're looking into!"; $scope.submitButtonText = "Checkout"; }); } $scope.doPreview = function(category, subcategory, gram) { $scope.previewSuccess = false; $scope.previewError = false; $scope.previewSending = true; $scope.previewButtonText = "Generating preview..."; $scope.showLoadingOverlay = true; var data = { message: $scope.card.message, toAddress: $scope.card.to, customerId: $scope.customerId, }; if ($scope.fromRequired) { data.fromAddress = $scope.card.from; } $http({ method: 'POST', url: '/ajax/' + category + '/' + subcategory + '/' + gram + '/preview', data: data }).then(function success(response) { $timeout(function() { $scope.showLoadingOverlay = false; $scope.previewSuccess = true; $scope.previewUrl = response.data.url; }, 2500); }, function error(response) { $scope.showLoadingOverlay = false; $scope.previewSending = false; $scope.previewError = "There was a problem getting a preview for your JoyGram. Please try again."; $scope.previewButtonText = "Generate Preview"; }); } $scope.$watch("card", function() { if ($scope.previewSuccess) { $scope.resetPreview(); $scope.previewSuccess = false; } storeForm($scope.card); }, true); $scope.resetPreview = function() { $scope.previewError = false; $scope.previewSending = false; $scope.previewButtonText = "Generate Preview"; $scope.previewSuccess = false; } $scope.init = function(category, subcategory, gram, paymentId, customerId) { $scope.customerId = customerId; $scope.stripeHandler = StripeCheckout.configure({ key: paymentId, image: 'https://stripe.com/img/documentation/checkout/marketplace.png', locale: 'auto', token: function(token) { $scope.doSubmit(category, subcategory, gram, token.id, token.email); } }); $scope.getPreview = function() { $scope.doPreview(category, subcategory, gram); }; }; $scope.submitForm = function(isValid) { if (isValid) { $scope.stripeHandler.open({ name: 'JoyGram', description: "Send your JoyGram!", amount: 499, zipCode: true }); } }; $scope.calculateLettersRemaining = function(length, error) { if (error) { return 0; }; if (!length) { return 300; } return $scope.messageMaxLength - length; }; }]);