File

src/app/manage-learn/project/attachment-listing/attachment-listing.page.ts

Implements

OnInit

Metadata

Index

Properties
Methods

Constructor

constructor(db: DbService, platform: Platform, file: File, location: Location, headerService: AppHeaderService, translate: TranslateService, transfer: FileTransfer, fileOpener: FileOpener, photoViewer: PhotoViewer, routeParam: ActivatedRoute, util: UtilsService, alert: AlertController, toast: ToastService)
Parameters :
Name Type Optional
db DbService No
platform Platform No
file File No
location Location No
headerService AppHeaderService No
translate TranslateService No
transfer FileTransfer No
fileOpener FileOpener No
photoViewer PhotoViewer No
routeParam ActivatedRoute No
util UtilsService No
alert AlertController No
toast ToastService No

Methods

attachmentAction
attachmentAction(event)
Parameters :
Name Optional
event No
Returns : void
deleteAttachment
deleteAttachment(attachment)
Parameters :
Name Optional
attachment No
Returns : void
Async deleteConfirmation
deleteConfirmation(attachment)
Parameters :
Name Optional
attachment No
Returns : any
deleteImage
deleteImage(event)
Parameters :
Name Optional
event No
Returns : void
downloadFile
downloadFile(attachment)
Parameters :
Name Optional
attachment No
Returns : void
getAttachments
getAttachments()
Returns : void
getEvidences
getEvidences(attachments, evidence)
Parameters :
Name Optional
attachments No
evidence No
Returns : void
getProject
getProject()
Returns : void
Private handleBackButton
handleBackButton()
Returns : void
ionViewWillEnter
ionViewWillEnter()
Returns : void
ngOnInit
ngOnInit()
Returns : void
openFile
openFile(attachment)
Parameters :
Name Optional
attachment No
Returns : void
openImage
openImage(attachment)
Parameters :
Name Optional
attachment No
Returns : void
segmentChanged
segmentChanged(event)
Parameters :
Name Optional
event No
Returns : void
updateLocalDb
updateLocalDb()
Returns : void
viewDocument
viewDocument(attachment)
Parameters :
Name Optional
attachment No
Returns : void

Properties

attachments
Type : any
Private backButtonFunc
Type : Subscription
Public fileOpener
Type : FileOpener
headerConfig
Type : object
Default value : { showHeader: true, showBurgerMenu: false, pageTitle: '', actionButtons: [] }
path
project
projectcopy
projectId
selectedTab
statuses
Default value : statusType
tabs
tabsLength
Public transfer
Type : FileTransfer
type
Type : string
Default value : "image/jpeg"
viewOnly
Type : boolean
Default value : false
Private win
Type : any
Default value : window
import { Component, OnInit } from '@angular/core';
import { AppHeaderService } from '@app/services';
import { DbService } from '../../core/services/db.service';
import { AlertController, Platform } from "@ionic/angular";
import { File } from "@ionic-native/file/ngx";
import { Subscription } from 'rxjs';
import { Location } from '@angular/common';
import { TranslateService } from '@ngx-translate/core';
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { PhotoViewer } from '@ionic-native/photo-viewer/ngx';
import { ActivatedRoute } from '@angular/router';
import { statusType, ToastService, UtilsService } from '../../core';
import * as _ from "underscore";

@Component({
  selector: 'app-attachment-listing',
  templateUrl: './attachment-listing.page.html',
  styleUrls: ['./attachment-listing.page.scss'],
})
export class AttachmentListingPage implements OnInit {
  private backButtonFunc: Subscription;
  headerConfig = {
    showHeader: true,
    showBurgerMenu: false,
    pageTitle: '',
    actionButtons: []
  };
  private win: any = window;
  attachments: any;
  projectId;
  path;
  type = "image/jpeg";
  tabs;
  project;
  projectcopy;
  tabsLength;
  statuses = statusType;
  viewOnly: boolean = false;
  selectedTab;
  constructor(
    private db: DbService,
    private platform: Platform,
    private file: File,
    private location: Location,
    private headerService: AppHeaderService,
    private translate: TranslateService,
    public transfer: FileTransfer,
    public fileOpener: FileOpener,
    private photoViewer: PhotoViewer,
    private routeParam: ActivatedRoute,
    private util: UtilsService,
    private alert: AlertController,
    private toast: ToastService
  ) {
    this.path = this.platform.is("ios") ? this.file.documentsDirectory : this.file.externalDataDirectory;
    routeParam.params.subscribe(parameters => {
      this.projectId = parameters.id;
      this.tabs = this.util.getTabs();
      this.tabsLength = this.tabs.length;
      this.selectedTab = this.tabs[0].value;
      this.attachments = {
        project: {},
        tasks: []
      };
      this.getProject();
    })
  }

  ngOnInit() { }
  ionViewWillEnter() {
    let data;
    this.translate.get(["FRMELEMNTS_LBL_ATTACHMENTS"]).subscribe((text) => {
      data = text;
    });
    this.headerConfig = this.headerService.getDefaultPageConfig();
    this.headerConfig.actionButtons = [];
    this.headerConfig.showHeader = true;
    this.headerConfig.showBurgerMenu = false;
    this.headerConfig.pageTitle = data["FRMELEMNTS_LBL_ATTACHMENTS"];
    this.headerService.updatePageConfig(this.headerConfig);
    this.handleBackButton();
  }
  private handleBackButton() {
    this.backButtonFunc = this.platform.backButton.subscribeWithPriority(10, () => {
      this.location.back();
      this.backButtonFunc.unsubscribe();
    });
  }
  segmentChanged(event) {
    this.type = event.detail.value;
    this.tabs.find(tab => {
      if (tab.type == this.type) {
        this.selectedTab = tab.value;
      }
    })
    this.getAttachments();
  }
  getAttachments() {
    this.attachments = {
      project: {},
      tasks: []
    };
    if (this.project.status == this.statuses.submitted) {
      let evidence = {
        title: this.project.title,
        remarks: this.project.remarks ? this.project.remarks : '',
        attachments: []
      }
      if(this.project.attachments && this.project.attachments.length){
        this.getEvidences(this.project.attachments, evidence);
      }
      if ((this.type && evidence.remarks) || evidence.attachments.length) {
        this.attachments.project=evidence;
      }
    }
    if (this.project.tasks && this.project.tasks.length) {
      this.project.tasks.forEach(task => {
        if(!task.isDeleted){
          let evidence = {
            title: task.name,
            remarks: task.remarks ? task.remarks : '',
            attachments: []
          }
          if (task.attachments && task.attachments.length) {
            this.getEvidences(task.attachments, evidence);
          }
          if ((this.type  && evidence.remarks) || evidence.attachments.length) {
            this.attachments.tasks.push(evidence);
          }
        }
      });
    }
  }

  getEvidences(attachments, evidence) {
    attachments.forEach(attachment => {
      if (attachment.type == this.type) {
        if(attachment.type != 'link'){
          attachment.localUrl = !attachment.url ? this.win.Ionic.WebView.convertFileSrc(
            this.path + attachment.name
          ) : '';
        }
        evidence.attachments.push(attachment);
      }
    });
  }

  getProject() {
    this.db.query({ _id: this.projectId }).then(
      (success) => {
        if (success?.docs.length) {
          this.project = success.docs[0];
          this.viewOnly = this.project.status == statusType.submitted ? true : false;
          this.type=this.tabs[0].type;
          this.getAttachments();
        }
      },
      (error) => { }
    );
  }

  viewDocument(attachment) {
    if(attachment.type == 'link'){
      const options
      = 'hardwareback=yes,clearcache=no,zoom=no,toolbar=yes,disallowoverscroll=yes';
    (window as any).cordova.InAppBrowser.open(attachment.name, '_blank', options)
    }else if (attachment.url) {
      this.downloadFile(attachment);
    } else {
      this.openFile(attachment);
    }
  }
  downloadFile(attachment) {
    const fileTransfer: FileTransferObject = this.transfer.create();
    fileTransfer.download(attachment.url, this.path + '/' + attachment.name).then(success => {
      this.openFile(attachment)
    })
  }
  openImage(attachment) {
    this.photoViewer.show(attachment)
  }
  openFile(attachment) {
    this.fileOpener.open(this.path + '/' + attachment.name, attachment.type)
      .then(() => { console.log('File is opened'); })
      .catch(e => {
        this.toast.showMessage(e.message,'danger');
      });
  }
  async deleteConfirmation(attachment) {
    let data;
    this.translate.get(['FRMELEMNTS_LBL_ATTACHMENT_DELETE_CONFIRMATION', 'YES', 'NO']).subscribe((text) => {
      data = text;
    });
    const alert = await this.alert.create({
      cssClass: 'attachment-delete-alert',
      message: data['FRMELEMNTS_LBL_ATTACHMENT_DELETE_CONFIRMATION'] + ' ' + this.selectedTab + '?',
      buttons: [
        {
          text: data['YES'],
          handler: () => {
            this.deleteAttachment(attachment);
          },
        }, {
          text: data['NO'],
          role: "cancel",
          cssClass: "secondary",
          handler: (blah) => {

          },
        },
      ],
    });
    await alert.present();
  }
  deleteImage(event) {
    this.deleteConfirmation(event.data);
  }
  deleteAttachment(attachment) {
    if (this.project.tasks && this.project.tasks.length) {
      let loopAgain : boolean = true;
      this.project.tasks.forEach(task => {
        if(loopAgain && task.attachments && task.attachments.length){
          let i = _.findIndex(task.attachments, (item) => {
          if(item.type == this.type){
              return item.name == attachment.name;
          }
          });
          if(i >= 0){
            task.attachments.splice(i, 1);
           loopAgain = false;
          }
        }
      });
    }
    this.updateLocalDb();
    this.getAttachments();
  }
  attachmentAction(event) {
    console.log(event,"event");
    if (event.action == 'delete') {
      this.deleteConfirmation(event.attachment);
    } else if (event.action == 'view') {
      this.viewDocument(event.attachment)
    }
  }
  updateLocalDb() {
    this.project.isEdit = true;
    this.db.update(this.project).then(success => {
      this.project._rev = success.rev;
    })
  }
}
<ion-content *ngIf="project" class="ion-padding">
  <div class="project-segments segment-wrapper">
    <ion-segment [(ngModel)]="type" (ionChange)="segmentChanged($event)" class="segment-card">
      <div *ngFor="let tab of tabs;let i=index; let last = last" class="segments">
        <ion-segment-button value="{{tab.type}}">
          <ion-label>{{tab.name | translate}}</ion-label>
        </ion-segment-button>
        <div class="partition" *ngIf="!last">
        </div>
      </div>
    </ion-segment>
  </div>

  <div *ngIf="type == 'image/jpeg'">
    <div class="header" *ngIf="attachments?.project?.attachments?.length || attachments?.project?.remarks">
      <h5 class="sb--card__title projects-labels"><span>{{'FRMELEMNTS_LBL_PROJECT_LEVEL' | translate}} {{'FRMELEMNTS_LBL_EVIDENCE' | translate | lowercase}}</span></h5>
    </div>
    <app-attachment-card *ngIf="attachments?.project"  [data]="attachments?.project" [viewOnly]="viewOnly"
      (deleteAttachment)="deleteImage($event)">
    </app-attachment-card>
    <div class="header" *ngIf="attachments?.tasks?.length">
      <h5 class="sb--card__title projects-labels"><span>{{'FRMELEMNTS_LBL_TASK_LEVEL' | translate}} {{'FRMELEMNTS_LBL_EVIDENCE' | translate | lowercase}}</span></h5>
    </div>
    <div *ngIf="attachments?.tasks?.length">
      <app-attachment-card *ngFor="let task of attachments?.tasks" [data]="task" [viewOnly]="viewOnly"
        (deleteAttachment)="deleteImage($event)">
      </app-attachment-card>
    </div>
  </div>

  <div *ngIf="type != 'image/jpeg'">
    <div *ngIf="attachments?.project?.attachments?.length">
      <div   class="header">
        <h5 class="sb--card__title projects-labels"><span>{{'FRMELEMNTS_LBL_PROJECT_LEVEL' | translate}} {{'FRMELEMNTS_LBL_EVIDENCE' | translate | lowercase}}</span></h5>
      </div>
       <app-attachment-lists  *ngIf="project?.attachments?.length" [type]="type" [title]="attachments?.project?.title" [attachments]="attachments?.project?.attachments" [viewOnly]="viewOnly" (eventAction)="attachmentAction($event)"> </app-attachment-lists>
    </div>
    <div class="header" *ngIf="attachments?.tasks?.length">
      <h5 class="sb--card__title projects-labels"><span>{{'FRMELEMNTS_LBL_TASK_LEVEL' | translate}} {{'FRMELEMNTS_LBL_EVIDENCE' | translate | lowercase}}</span></h5>
    </div>
    <div *ngFor="let task of attachments?.tasks">
      <app-attachment-lists  *ngIf="task?.attachments?.length" [type]="type" [title]="task.title" [attachments]="task.attachments" [viewOnly]="viewOnly" (eventAction)="attachmentAction($event)"> </app-attachment-lists>
    </div>
  </div>
  <div *ngIf="!attachments?.project?.attachments?.length && !attachments?.tasks?.length">
    <app-no-data [message]="'FRMELEMENTS_NO_EVIDENCES_FOR_FILE'" [color]="'text-primary'"></app-no-data>
  </div>
</ion-content>

./attachment-listing.page.scss

@import "../../../../assets/styles/variables";
@import "../../../../assets/styles/base/variables";

.segments{
display: flex;
}
.partition{
   border: 1px solid $gray;
    margin: 5px 5px;
}
.segment-wrapper{
    margin-top: 8%;
}
.view-more {
    --background: var(--app-primary-background) !important;
    --border-color: var(--app-primary-medium) !important;
    --color: var(--app-primary-medium) !important;
    width: 90%;
    border-radius: 5px !important;
 }
 ion-card{
    margin: 0 0 10px 0;
 }
 .header{
    margin-left: 0px;
 }

 h5 {
   position: relative;
 }
 
 h5 span {
   background-color: var(--app-primary-background);
   padding-right: 10px;
 }
 
 h5:after {
   content: "";
     position: absolute;
     bottom: 0;
     left: 0;
     right: 0;
     height: 0.76rem;
     border-top: 3px solid yellow;
     z-index: -1;
 }
 .projects-labels{
   font-weight: 600;
   margin: 20px 0px;
   }

 .segment-card{
  justify-content: space-between;
  overflow: auto;
 }
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""