This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-upload.directive.ts
179 lines (165 loc) · 6.05 KB
/
file-upload.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Name: ng2-dropzone-wrapper v0.1
* GitHub: https://github.com/akshayat/ng2-dropzone-wrapper
* License: https://github.com/akshayat/ng2-dropzone-wrapper/blob/master/LICENSE
*/
import { Directive, ElementRef, Input, OnDestroy, AfterViewInit, OnChanges } from '@angular/core';
declare var $: any;
declare var Dropzone: any;
@Directive({
selector: 'file-upload'
})
export class FileUploadDirective implements OnDestroy, AfterViewInit, OnChanges {
wrapperId: string;
wrapperId$: any;
id: string;
id$: any;
dzId: string;
css: string = `<wrapper><style id="file-upload-style">
.showcase{border:1px dashed #000;background:#c5c5c5;color:white;padding:5px;cursor: pointer;}
.fu-remove{padding-left:5px;color:#e74c3c;}
.fu-undo{padding-left:5px;color:#3498db;}
.hidden{display:none;visibility:hidden;}
</style></wrapper>`;
defaultMessage: string = 'Click here to upload file';
thisDropzone: any;
@Input('file-id') fileId: string;
@Input('file-name') fileName: string;
@Input() accept: string;
@Input('url') url: string;
constructor(
private el: ElementRef
) {
this.id = Math.random().toString().replace('.', '');
this.createElement();
}
createElement() {
this.el.nativeElement.innerHTML = this.getHtml();
}
getHtml(): string {
let template =
`<wrapper><div class="file-upload-main">
<div class="showcase"></div>
</div></wrapper>`;
let $template = $(template);
$template.find('.file-upload-main').attr('id', this.id).find('.showcase').text(this.defaultMessage);
return $template.html();
}
ngAfterViewInit() {
let _thisDirective = this;
// add css
this.id$ = $('#' + this.id);
this.dzId = 'fu-dz-' + this.id;
this.wrapperId = this.id$.parent().attr('id');
this.wrapperId$ = $('#' + this.wrapperId);
if (!$('#file-upload-style')[0]) {
$('body').append($(this.css).html());
}
// add dropzone input
$('body').append('<div class="hidden" id="' + this.dzId + '"></div>');
setTimeout(() => {
_thisDirective.thisDropzone = new Dropzone('#' + this.dzId, {
url: this.url,
addRemoveLinks: true,
removedfile: (file: any) => {
let _ref: any;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
},
maxFilesize: 5,
acceptedFiles: this.accept ? this.accept : null,
// dictDefaultMessage: 'Click here or Drop files to upload',
params: { type: '' },
success: (file: any, response: any) => {
file.serverId = response.id;
_thisDirective.uploaded(response);
},
error: (file: any, error: any) => {
alert('Error' + error);
},
processing: (file: any, other: any) => {
_thisDirective.id$.find('.showcase').html('Uploading ' + file.name);
},
});
}, 100);
setTimeout(() => {
this.bindEvent();
}, 200);
}
bindEvent() {
this.id$.on('click', () => {
$('#' + this.dzId).trigger('click');
});
}
bindRemoveEvent() {
let _thisDirective = this;
this.id$.find('.fu-remove').off('click').on('click', (evt: any) => {
evt.preventDefault();
evt.stopPropagation();
_thisDirective.removeFile();
});
}
bindUndoEvent() {
let _thisDirective = this;
this.id$.find('.fu-undo').off('click').on('click', (evt: any) => {
evt.preventDefault();
evt.stopPropagation();
_thisDirective.fillWithNames();
});
}
uploaded(document: any) {
this.id$.find('.showcase').html('File ' + document.name + ' uploaded');
setTimeout(() => {
this.id$.find('.showcase').html(document.name + '<a class="fu-remove"><u>Remove</u></a>');
if (this.fileId && this.fileName) {
this.id$.find('.showcase').append('<a class="fu-undo"><u>Undo</u></a>');
this.bindUndoEvent();
}
this.bindRemoveEvent();
}, 2000);
let createdBy = document.createdBy;
let createdOn = document.createdOn;
let modifiedOn = document.modifiedOn;
let modifiedBy = document.modifiedBy;
delete document.createdBy;
delete document.createdOn;
delete document.modifiedOn;
delete document.modifiedBy;
this.wrapperId$.data('file', document);
this.wrapperId$.data('created-by', createdBy);
this.wrapperId$.data('created-on', createdOn);
this.wrapperId$.data('modified-on', modifiedOn);
this.wrapperId$.data('modified-by', modifiedBy);
}
removeFile() {
if (this.id$ && this.wrapperId$) {
if (!this.fileId && !this.fileName) {
this.id$.find('.showcase').html(this.defaultMessage);
} else {
this.id$.find('.showcase').html(this.defaultMessage + '<a class="fu-undo"><u>Undo</u></a>');
this.bindUndoEvent();
}
this.wrapperId$.data('file', null);
}
}
ngOnChanges(changes: any) {
if (this.fileId && this.fileName) {
this.fillWithNames();
} else {
this.removeFile();
}
}
fillWithNames() {
this.wrapperId$.data('file', { id: this.fileId, name: this.fileName });
this.id$.find('.showcase').html(this.fileName + '<a class="fu-remove"><u>Remove</u></a>');
this.bindRemoveEvent();
}
ngOnDestroy() {
this.id$.off('click');
this.id$.find('.fu-remove').off('click');
this.id$.find('.fu-undo').off('click');
if (this.thisDropzone) {
this.thisDropzone.destroy();
}
$('#' + this.dzId).remove();
}
}