angular.module('kalgudiApp.profile').controller('catalogueBaseProductController', ["$scope","$rootScope", "S3UploadService", "$ionicModal", function($scope,$rootScope, S3UploadService, $ionicModal) { $scope.opencommodity_modal = false; $scope.attachmentObject = { type : 'IMG' }; $scope.currentCommodity = { units : {} }; $scope.catalogue = { product : {}, name: '', urlName:'', urlId: '', productImageUrls : { type : 'MEDIA', title: 'Picture Collection', data : [] }, description: { title: 'Description', data: [] } } function init () { initializeModal(); } // Creating a model function initializeModal () { $ionicModal.fromTemplateUrl('my-modal.html', { scope: $scope, animation: 'slide-in-up' }).then(function(modal) { $scope.modal = modal; }); } $scope.openProductPicker = function () { $scope.opencommodity_modal = true; } $scope.selectedProduct = function (product) { $scope.catalogue.product = product; $scope.opencommodity_modal = false; } $scope.uploadImage = function(files, file, newFile, duplicateFiles, invalidFiles, event) { if(invalidFiles.length) { $scope.errorImageFlag = true; $scope.errorFileFlag = false; } else { $scope.errorImageFlag = false; } if(file) { S3UploadService.compressImage(file).then( function (compressedImage) { $scope.attachmentObject.name = compressedImage.name; $scope.attachmentObject.size = compressedImage.size; $scope.currentImageBase64 = S3UploadService.base64Image.target.result; delete S3UploadService.base64Image; $scope.openModal(); $scope.getUploadPolicy(file, compressedImage); }); } } // will call a service to get policy for uploading object to S3 $scope.getUploadPolicy=function(file, compressesFile) { // if final compressed file size is less than 2 MB if(compressesFile.size < 2097152) { var fileType = file.type; $scope.uploadFileToS3(compressesFile, fileType); } else { $rootScope.showAlertBox($scope.contentLiterals.searchByKey('FILE_SIZE_WARN'), $scope.contentLiterals.searchByKey('SIZE_EXCEEDED')); $scope.closeModal(true); } } // will upload the given file to S3 $scope.uploadFileToS3 = function (fileToUpload, fileType) { // To show spinner and hide submit button while uploading the file $scope.enableOk = false; // checking for file size, should be less than 5 MB if(fileToUpload.size > 5000000) { $rootScope.commonTopErrorAlert("Files with more than 5MB size, can not be attached"); $scope.modal.hide(); return false; } S3UploadService.uploadToS3(fileToUpload, fileType).then( function (res) { $scope.enableOk = true; $scope.attachmentObject.url = res; }, function (err) { $scope.enableOk = true; $scope.attachmentObject.fileType = fileType; $scope.attachmentObject.fileName = fileToUpload.name; console.error (err); }); } // Will open description model $scope.openModal = function() { $scope.modal.show(); }; // Will cloase the model, and broadcast attached object $scope.closeModal = function(cancel) { $scope.modal.hide(); if(!cancel) { $scope.attachmentObject.localFile = $scope.currentImageBase64; $scope.catalogue.productImageUrls.data.push($scope.attachmentObject); } $scope.attachmentObject = { "msgType" : "", "name" : "", "size" : "", "url" : "", "context" : "", "localFile" : "", "type" : 'IMG' }; $scope.currentImageBase64 = ""; }; $scope.deselectAttachment = function (index) { $scope.catalogue.productImageUrls.data.splice(index, 1); } //Cleanup the modal when we're done with it! $scope.$on('$destroy', function() { $scope.modal.remove(); }); $scope.saveBasicDetails = function () { for (i in $scope.catalogue.productImageUrls.data) { delete $scope.catalogue.productImageUrls.data [i].localFile; delete $scope.catalogue.productImageUrls.data [i].name; delete $scope.catalogue.productImageUrls.data [i].size; delete $scope.catalogue.productImageUrls.data [i].msgType; delete $scope.catalogue.productImageUrls.data [i].context; } $scope.catalogue.urlName = $scope.catalogue.name; console.log($scope.catalogue); localStorage['catalogue'] = JSON.stringify($scope.catalogue); $rootScope.showAlertBox('Catalogue basic product details saved successfully!', 'Saved sucessfully', 'success'); history.back(); } init(); } ]);