File

src/app/manage-learn/shared/components/attachment/attachment.component.ts

Metadata

Index

Properties
Methods
Inputs

Constructor

constructor(photoViewer: PhotoViewer, streamingMedia: StreamingMedia, platform: Platform, fileTransfer: FileTransfer, file: File, toast: ToastService, loader: LoaderService, filePath: FilePath, fileOpener: FileOpener)
Parameters :
Name Type Optional
photoViewer PhotoViewer No
streamingMedia StreamingMedia No
platform Platform No
fileTransfer FileTransfer No
file File No
toast ToastService No
loader LoaderService No
filePath FilePath No
fileOpener FileOpener No

Inputs

extension
Type : string
url
Type : string

Methods

createFileName
createFileName()
Returns : string
downloadFile
downloadFile(link)
Parameters :
Name Optional
link No
Returns : void
openDocument
openDocument(link)
Parameters :
Name Optional
link No
Returns : void
openFile
openFile(attachment)
Parameters :
Name Optional
attachment No
Returns : void
openImage
openImage(link)
Parameters :
Name Optional
link No
Returns : void
playAudio
playAudio(link)
Parameters :
Name Optional
link No
Returns : void
playVideo
playVideo(link)
Parameters :
Name Optional
link No
Returns : void

Properties

audioFormats
Type : string[]
Default value : FileExtension.audioFormats
Public fileOpener
Type : FileOpener
Public filePath
Type : FilePath
imageFormats
Type : string[]
Default value : FileExtension.imageFormats
Public loader
Type : LoaderService
path
pdfFormats
Type : string[]
Default value : FileExtension.pdfFormats
spreadSheetFormats
Type : string[]
Default value : FileExtension.spreadSheetFormats
Public toast
Type : ToastService
videoFormats
Type : string[]
Default value : FileExtension.videoFormats
wordFormats
Type : string[]
Default value : FileExtension.wordFormats
import { Component, Input } from '@angular/core';
import { FileTransfer,FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { PhotoViewer } from '@ionic-native/photo-viewer/ngx';
import { StreamingMedia, StreamingVideoOptions } from '@ionic-native/streaming-media/ngx';
import { Platform } from '@ionic/angular';
import { FileExtension } from '../../fileExtension';
import { FilePath } from '@ionic-native/file-path/ngx';
import { File } from '@ionic-native/file/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { LoaderService, ToastService } from '@app/app/manage-learn/core';

@Component({
  selector: 'attachments',
  templateUrl: './attachment.component.html',
  styleUrls: ['./attachment.component.scss'],
})
export class AttachmentComponent {
  @Input() url: string;
  @Input() extension: string;
  path;
  imageFormats: string[] = FileExtension.imageFormats;
  videoFormats: string[] = FileExtension.videoFormats;
  audioFormats: string[] = FileExtension.audioFormats;
  pdfFormats: string[] = FileExtension.pdfFormats;
  wordFormats: string[] = FileExtension.wordFormats;
  spreadSheetFormats: string[] = FileExtension.spreadSheetFormats;

  constructor(private photoViewer: PhotoViewer, private streamingMedia: StreamingMedia, private platform: Platform,
    private fileTransfer: FileTransfer,
    private file: File,
    public toast: ToastService,
    public loader: LoaderService,
    public filePath: FilePath,
    public fileOpener: FileOpener) {
    this.path = this.platform.is("ios") ? this.file.documentsDirectory : this.file.externalDataDirectory;
  }

  playVideo(link) {
    let options: StreamingVideoOptions = {
      successCallback: () => {
        console.log('Video played');
      },
      errorCallback: (e) => {
        console.log('Error streaming');
      },
      orientation: 'landscape',
      shouldAutoClose: true,
      controls: false,
    };

    this.streamingMedia.playVideo(link, options);
  }

  playAudio(link) {
    let options: StreamingVideoOptions = {
      successCallback: () => {
        console.log('Video played');
      },
      errorCallback: (e) => {
        console.log('Error streaming');
      },
      shouldAutoClose: true,
      controls: true,
    };

    this.streamingMedia.playAudio(link, options);
  }

  openImage(link) {
    if (this.platform.is('ios')) {
      const options = {
        share: true,
        closeButton: true,
        copyToReference: true,
        headers: "",
        piccasoOptions: {}
      };
      const newLink = link ? link.split('?') : "";
      link = (newLink && newLink.length) ? newLink[0] : ""
    }
    this.photoViewer.show(link);
  }

  openDocument(link) {
    // const url = encodeURIComponent(link);
    // const browser = this.iab.create(
    //   'https://docs.google.com/viewer?url=' + url,
    //   '_blank',
    //   'location=no,toolbar=no,clearcache=yes'
    // );
    // browser.show();
    // (window as any).cordova.InAppBrowser.open(
    //   'https://docs.google.com/viewer?url=' + encodeURIComponent(link),
    //   '',
    //   'location=no,toolbar=no,clearcache=yes'
    // );
    window.open(link, '_system', 'location=yes,enableViewportScale=yes,hidden=no');
  }
  downloadFile(link) {
    this.loader.startLoader();
    const fileTransfer: FileTransferObject = this.fileTransfer.create();
    let fileName = this.createFileName();
    fileTransfer.download(link, this.path + '/' +fileName ).then(success => {
      let attachment ={
        name:fileName,
        type:'application/pdf'
      }
      this.loader.stopLoader();
      this.openFile(attachment);
    },error =>{
    this.loader.stopLoader();
    })
  }
  openFile(attachment) {
    this.fileOpener.open(this.path + '/' + attachment.name, attachment.type)
      .then(() => { console.log('File is opened'); })
      .catch(e => console.log('Error opening file', e));
  }
  createFileName() {
    let d = new Date(),
      n = d.getTime(),
      newFileName = n + "." + this.extension;
    return newFileName;
  }
}
<ion-card class="thumbnail-card">
  <img [src]="url" *ngIf="imageFormats.includes(extension)" (click)="openImage(url)" alt=""/>
  <ion-icon
    name="videocam"
    class="attachmentIcon"
    *ngIf="videoFormats.includes(extension)"
    (click)="playVideo(url)"
  ></ion-icon>

  
  <ion-icon
    name="volume-medium"
    *ngIf="audioFormats.includes(extension)"
    class="attachmentIcon"
    (click)="playAudio(url)"
  ></ion-icon>

  <ion-icon
    name="document"
    class="attachmentIcon"
    *ngIf="pdfFormats.includes(extension)"
    (click)="downloadFile(url)"
  ></ion-icon>

  <ion-icon
  name="document"
  class="attachmentIcon"
  *ngIf="spreadSheetFormats.includes(extension) || wordFormats.includes(extension) "
  (click)="openDocument(url)"
></ion-icon>
</ion-card>

./attachment.component.scss

.thumbnail-card {
  height: 6.125rem;
  display: flex;
  justify-content: space-around;
  flex-direction: column;
  align-items: center;
}
.thumbnail-card ion-icon {
  font-size: 3.2rem;
}
.attachmentIcon {
  margin-top: 10px;
  font-size: 3.125rem;
  // color: $primary-color;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""