]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/videos/+video-watch/modal/video-download.component.ts
add url field to download modal
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / modal / video-download.component.ts
index 1a73ea6df0de8796e62ad23f4935db614c1bee1c..b1b2c06237714527b792d32d27115e27e274b875 100644 (file)
@@ -1,6 +1,8 @@
-import { Component, Input, OnInit, ViewChild } from '@angular/core'
-import { ModalDirective } from 'ngx-bootstrap/modal'
+import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core'
 import { VideoDetails } from '../../../shared/video/video-details.model'
+import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
+import { I18n } from '@ngx-translate/i18n-polyfill'
+import { NotificationsService } from 'angular2-notifications'
 
 @Component({
   selector: 'my-video-download',
@@ -10,35 +12,57 @@ import { VideoDetails } from '../../../shared/video/video-details.model'
 export class VideoDownloadComponent implements OnInit {
   @Input() video: VideoDetails = null
 
-  @ViewChild('modal') modal: ModalDirective
+  @ViewChild('modal') modal: ElementRef
 
-  downloadType: 'direct' | 'torrent' = 'torrent'
-  resolution = -1
+  downloadType: 'direct' | 'torrent' | 'magnet' = 'torrent'
+  resolutionId: number | string = -1
 
-  constructor () {
-    // empty
-  }
+  constructor (
+    private notificationsService: NotificationsService,
+    private modalService: NgbModal,
+    private i18n: I18n
+  ) { }
 
   ngOnInit () {
-    this.resolution = this.video.files[0].resolution
+    this.resolutionId = this.video.files[0].resolution.id
   }
 
   show () {
-    this.modal.show()
+    this.modalService.open(this.modal)
   }
 
-  hide () {
-    this.modal.hide()
+  download () {
+    window.location.assign(this.getLink())
   }
 
-  download () {
-    const file = this.video.files.find(f => f.resolution === this.resolution)
+  getLink () {
+    // HTML select send us a string, so convert it to a number
+    this.resolutionId = parseInt(this.resolutionId.toString(), 10)
+
+    const file = this.video.files.find(f => f.resolution.id === this.resolutionId)
     if (!file) {
-      console.error('Could not find file with resolution %d.', this.resolution)
+      console.error('Could not find file with resolution %d.', this.resolutionId)
       return
     }
 
-    const link = this.downloadType === 'direct' ? file.fileUrl : file.torrentUrl
-    window.open(link)
+    const link = (() => {
+      switch (this.downloadType) {
+        case 'direct': {
+          return file.fileDownloadUrl
+        }
+        case 'torrent': {
+          return file.torrentDownloadUrl
+        }
+        case 'magnet': {
+          return file.magnetUri
+        }
+      }
+    })()
+
+    return link
+  }
+
+  activateCopiedMessage () {
+    this.notificationsService.success(this.i18n('Success'), this.i18n('Copied'))
   }
 }