Initial Commit - NG UI
[osm/NG-UI.git] / src / app / utilities / dragDropUpload / DragDirective.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18 /**
19  * @file Drag and Drop feature.
20  */
21 import { Directive, EventEmitter, HostBinding, HostListener, Output } from '@angular/core';
22 import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
23
24 /** Interface for FileHandle */
25 export interface FileHandle {
26   file: File;
27   url: SafeUrl;
28 }
29
30 /**
31  * Creating Directive
32  * @Directive for handling the files.
33  */
34 // tslint:disable-next-line:export-name
35 @Directive({
36   selector: '[appDrag]'
37 })
38 /** Exporting a class @exports DragDirective */
39 export class DragDirective {
40   /** To publish the details of files @public */
41   @Output() public files: EventEmitter<FileList> = new EventEmitter();
42
43   /** To set the background of drag and drop region @public */
44   @HostBinding('style.background') private background: string = '#e6f3fe';
45
46   /** To set the background of drag and drop region @public */
47   @HostBinding('style.color') private color: string = '#6a7a8c';
48
49   /** To trust the SecurityURL @public */
50   private sanitizer: DomSanitizer;
51
52   constructor(sanitizer: DomSanitizer) {
53     this.sanitizer = sanitizer;
54   }
55
56   /** To handle the Drag over Event @public */
57   @HostListener('dragover', ['$event']) public onDragOver(evt: DragEvent): void {
58     evt.preventDefault();
59     evt.stopPropagation();
60     this.background = '#087add';
61     this.color = '#fff';
62   }
63   /** To handle Drag leave Event @public */
64   @HostListener('dragleave', ['$event']) public onDragLeave(evt: DragEvent): void {
65     evt.preventDefault();
66     evt.stopPropagation();
67     this.background = '#e6f3fe';
68     this.color = '#6a7a8c';
69   }
70   /** To handle Drop Event @public */
71   @HostListener('drop', ['$event']) public onDrop(evt: DragEvent): void {
72     evt.preventDefault();
73     evt.stopPropagation();
74     this.background = '#e6f3fe';
75     this.color = '#6a7a8c';
76
77     const files: FileHandle[] = [];
78     Array.from(evt.dataTransfer.files).forEach((listFiles: File, index: number) => {
79       const file: File = listFiles;
80       const url: SafeUrl = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(file));
81       files.push({ file, url });
82     });
83     if (files.length > 0) {
84       this.files.emit(evt.dataTransfer.files);
85     }
86   }
87 }