Advertisement
Guest User

Untitled

a guest
Jul 16th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, Input, ViewChild} from '@angular/core';
  2. import { Subscription } from "rxjs/Subscription";
  3. import { ActivatedRoute, Router } from "@angular/router";
  4. import { RequestManagementViewModel } from '../../view-models/abstract/organization-management-view-models/request-management-view-model';
  5. import { OrganizationManagementRequestService } from "../../services/concrete/organization-management/organization-management-request.service";
  6. import { GoodsTypeViewModel } from "../../view-models/concrete/goodsType-view.model";
  7. import { GoodsCategoryViewModel } from "../../view-models/concrete/goodsCategory-view.model";
  8. import { AmazonUploadComponent } from "../../shared/components/amazonUploader/amazon-upload.component";
  9. import { RequestedImageViewModel } from "../../view-models/abstract/organization-management-view-models/requested-item-view.model";
  10. import { SpinnerComponent } from "../../shared/components/spinner/spinner.component";
  11.  
  12. @Component({
  13.     selector: 'manage-request',
  14.     template: require('./organization-manage-request.component.html'),
  15.     styles: [require('./organization-manage-request.component.css')],
  16.     providers: [OrganizationManagementRequestService]
  17. })
  18.  
  19. export class OrganizationManageRequestComponent implements OnInit {
  20.     public uploader: AmazonUploadComponent = new AmazonUploadComponent();
  21.  
  22.     private _requestedItem: RequestManagementViewModel = new RequestManagementViewModel();
  23.     private _errorMessage: string;
  24.     private _goodsTypes: GoodsTypeViewModel[];
  25.     private _selecteType: GoodsTypeViewModel;
  26.     private _subscription: Subscription;
  27.     private _sub: Subscription;
  28.     private _requestedItemId: number;
  29.     private _currentOrgId: number;
  30.     private _currentImageUrl: string[] = [];
  31.     //@ViewChild(SpinnerComponent) spinner: SpinnerComponent;
  32.  
  33.     ngOnInit(): void {
  34.         this._requestedItem.images = [];
  35.         this.fillGoodtypes();
  36.         this._route.params.subscribe(params => {
  37.             this._currentOrgId = +params["idOrganization"];
  38.             this._requestedItemId = +params["idRequest"];
  39.         });
  40.      
  41.         if (this._requestedItemId > 0) {
  42.             this.getByIdRequestedItem(this._requestedItemId);
  43.         }      
  44.     }
  45.  
  46.     /**
  47.      * Initialize new instance of OrganizationCreateRequestComponent
  48.      * @param _service
  49.      */
  50.     constructor(private _service: OrganizationManagementRequestService,
  51.                 private _route: ActivatedRoute,
  52.                 private _router: Router) { }
  53.  
  54.     /**
  55.      * Fills goods type dropdown
  56.      */
  57.     private fillGoodtypes() {
  58.         this._service.getAllGoodsTypes()
  59.             .subscribe(r => {
  60.                 this._goodsTypes = r;
  61.             },
  62.             error => {
  63.                 this._errorMessage = <any>error;
  64.             })
  65.     }
  66.  
  67.     /**
  68.      * Add new requested item
  69.      */
  70.     private addRequestedItem() {
  71.         this._service.addRequestedItem(this._requestedItem)
  72.             .subscribe(error => this._errorMessage = <any>error);
  73.     }
  74.  
  75.     /**
  76.      * Edit requested item
  77.      * @param itemId
  78.      */
  79.     private getByIdRequestedItem(itemId: number) {
  80.         this._service.getRequestedItemById(itemId)
  81.             .subscribe(c => {
  82.                 if (c.errorMessage != "") {
  83.                     this._requestedItem = c;
  84.                     this.setGoodsType(this._requestedItem.goodsTypeId);
  85.                 }
  86.                 else {
  87.                     this._errorMessage = c.errorMessage;
  88.                 }
  89.             },
  90.             error => this._errorMessage = <any>error);
  91.     }
  92.  
  93.     private deleteCurrentImage(currentImage: RequestedImageViewModel) {
  94.         if (currentImage.id > 0) {
  95.             this._service.deleteCurrentImage(currentImage.id)
  96.                 .subscribe(data => this.deleteImageFromList(currentImage.imageUrl),
  97.                 error => this._errorMessage = <any>error);
  98.         }
  99.         else {
  100.             this.deleteImageFromList(currentImage.imageUrl);
  101.         }
  102.     }
  103.  
  104.     private deleteImageFromList(imageUrl: string) {
  105.         this._requestedItem.images.splice(this._requestedItem.images.findIndex(i => i.imageUrl == imageUrl), 1)        
  106.     }
  107.  
  108.     /**
  109.      * Edit requested item
  110.      * @param item
  111.      */
  112.     private editRequestetItem(item: RequestManagementViewModel) {
  113.         this._service.editRequestedItem(item)
  114.             .subscribe(r => this._requestedItem = r,
  115.             error => this._errorMessage = <any>error);
  116.     }
  117.  
  118.     /**
  119.      * Manage requested items wich method will be called
  120.      */
  121.     private manageRequestedItems() {
  122.         if (this._requestedItemId > 0) {
  123.             this.editRequestetItem(this._requestedItem);
  124.         }
  125.         else {
  126.             this.addRequestedItem();
  127.         }
  128.     }
  129.  
  130.     private setGoodsType(goodTypeId: number) {
  131.         this._selecteType = this._goodsTypes.find(c => c.id == goodTypeId);
  132.     }
  133.  
  134.     /**
  135.      * Gets extension of specified file
  136.      * @param fileName: name of the file extension of which is needed to be retrieved
  137.      */
  138.     private getFileExtension(fileName: string): string {
  139.         return fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length) || fileName;
  140.     }
  141.  
  142.     /**
  143.      * Saves passed file in Amazon Web Storage
  144.      * @param fileInput: file to be saved in AWS
  145.      */
  146.     private saveFileInAws(fileInput: any): void {
  147.         var that = this;
  148.         var maxFileSize = 4000000;
  149.         let file = fileInput.target.files[0];
  150.         if (file) {
  151.             var currentDate = new Date();
  152.             let uploadedFileName = this._requestedItem.name + currentDate.getHours() + currentDate.getMinutes() + currentDate.getSeconds() + '.' + this.getFileExtension(file.name);
  153.             if (file.size != null && file.size < maxFileSize) {
  154.                 this.uploader.UploadImageToAmazon(file, uploadedFileName).then(function (data) {
  155.                     let requestedItemImage = new RequestedImageViewModel();
  156.                     requestedItemImage.requestedItemId = 0;
  157.                     requestedItemImage.imageUrl = data.Location;
  158.                     console.log(data.Location);
  159.  
  160.                     if (that._requestedItem.images == null) {
  161.                         that._requestedItem.images = [];
  162.                     }
  163.  
  164.                     that._requestedItem.images.push(requestedItemImage);
  165.                 })
  166.             }
  167.             else {
  168.                 alert('Розмр файлу не може перевищувати ' + Math.ceil(maxFileSize / 1000000) + 'МБ');
  169.             }
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement