Skip to content

Commit 564cc64

Browse files
Sergey AkopkokhyantsSergey Akopkokhyants
authored andcommitted
Merge branch 'JuergenGutsch-feature/#51'
2 parents 6c08842 + 6f36719 commit 564cc64

File tree

3 files changed

+106
-17
lines changed

3 files changed

+106
-17
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,96 @@ class Widget {
679679
}
680680
```
681681

682+
# Retreiving files in a drop zone
683+
684+
Sinze it is possible to drag and drop one or more files to a drop zone, you need to handle the incomming files.
685+
686+
```js
687+
import {Component} from '@angular/core';
688+
import {Http, Headers} from '@angular/http';
689+
import {DND_PROVIDERS, DND_DIRECTIVES} from 'ng2-dnd/ng2-dnd';
690+
import {bootstrap} from '@angular/platform-browser-dynamic';
691+
692+
bootstrap(AppComponent, [
693+
DND_PROVIDERS // It is required to have 1 unique instance of your service
694+
]);
695+
696+
@Component({
697+
selector: 'app',
698+
directives: [DND_DIRECTIVES],
699+
template: `
700+
<h4>Simple Drag-and-Drop</h4>
701+
<div class="row">
702+
703+
<div class="col-sm-3">
704+
<div dnd-droppable class="panel panel-info"
705+
(onDropSuccess)="transferDataSuccess($event)">>
706+
<div class="panel-heading">Place to drop</div>
707+
<div class="panel-body">
708+
</div>
709+
</div>
710+
</div>
711+
</div>
712+
`
713+
})
714+
export class AppComponent {
715+
716+
constructor(private _http: Http) { }
717+
718+
/**
719+
* The $event is a structure:
720+
* {
721+
* dragData: any,
722+
* mouseEvent: MouseEvent
723+
* }
724+
*/
725+
transferDataSuccess($event) {
726+
// let attachmentUploadUrl = 'assets/data/offerspec/offerspec.json';
727+
// loading the FileList from the dataTransfer
728+
let dataTransfer: DataTransfer = $event.mouseEvent.dataTransfer;
729+
if (dataTransfer && dataTransfer.files) {
730+
731+
// needed to support posting binaries and usual form values
732+
let headers = new Headers();
733+
headers.append('Content-Type', 'multipart/form-data');
734+
735+
let files: FileList = dataTransfer.files;
736+
737+
// uploading the files one by one asynchrounusly
738+
for (let i = 0; i < files.length; i++) {
739+
let file: File = files[i];
740+
741+
// just for debugging
742+
console.log('Name: ' + file.name + '\n Type: ' + file.type + '\n Size: ' + file.size + '\n Date: ' + file.lastModifiedDate);
743+
744+
// collecting the data to post
745+
var data = new FormData();
746+
data.append('file', file);
747+
data.append('fileName', file.name);
748+
data.append('fileSize', file.size);
749+
data.append('fileType', file.type);
750+
data.append('fileLastMod', file.lastModifiedDate);
751+
752+
// posting the data
753+
this._http
754+
.post(attachmentUploadUrl, data, {
755+
headers: headers
756+
})
757+
.toPromise()
758+
.catch(reason => {
759+
console.log(JSON.stringify(reason));
760+
});
761+
}
762+
}
763+
}
764+
}
765+
```
766+
682767
# Credits
683768
- [Francesco Cina](https://github.com/ufoscout)
684769
- [Valerii Kuznetsov](https://github.com/solival)
685770
- [Shane Oborn](https://github.com/obosha)
771+
- [Juergen Gutsch](https://github.com/JuergenGutsch)
686772

687773
# License
688774
[MIT](/LICENSE)

src/abstract.component.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ export abstract class AbstractComponent {
200200
//****** Droppable *******//
201201
private _onDragEnter(event: Event): void {
202202
// console.log('ondragenter._isDropAllowed', this._isDropAllowed);
203-
if (this._isDropAllowed) {
203+
if (this._isDropAllowed(event)) {
204204
// event.preventDefault();
205205
this._onDragEnterCallback(event);
206206
}
207207
}
208208

209209
private _onDragOver(event: Event) {
210210
// // console.log('ondragover._isDropAllowed', this._isDropAllowed);
211-
if (this._isDropAllowed) {
211+
if (this._isDropAllowed(event)) {
212212
// The element is over the same source element - do nothing
213213
if (event.preventDefault) {
214214
// Necessary. Allows us to drop.
@@ -218,36 +218,29 @@ export abstract class AbstractComponent {
218218
this._onDragOverCallback(event);
219219
}
220220
}
221-
221+
222222
private _onDragLeave(event: Event): void {
223223
// console.log('ondragleave._isDropAllowed', this._isDropAllowed);
224-
if (this._isDropAllowed) {
224+
if (this._isDropAllowed(event)) {
225225
// event.preventDefault();
226226
this._onDragLeaveCallback(event);
227227
}
228228
}
229229

230230
private _onDrop(event: Event): void {
231+
// Necessary. Allows us to drop.
232+
this._preventAndStop(event);
231233
// console.log('ondrop._isDropAllowed', this._isDropAllowed);
232-
if (this._isDropAllowed) {
233-
if (event.preventDefault) {
234-
// Necessary. Allows us to drop.
235-
event.preventDefault();
236-
}
237-
238-
if (event.stopPropagation) {
239-
// Necessary. Allows us to drop.
240-
event.stopPropagation();
241-
}
234+
if (this._isDropAllowed(event)) {
242235

243236
this._onDropCallback(event);
244237

245238
this.detectChanges();
246239
}
247240
}
248241

249-
private get _isDropAllowed(): boolean {
250-
if (this._dragDropService.isDragged && this.dropEnabled) {
242+
private _isDropAllowed(event: any): boolean {
243+
if ((this._dragDropService.isDragged || (event.dataTransfer && event.dataTransfer.files)) && this.dropEnabled) {
251244
// First, if `allowDrop` is set, call it to determine whether the
252245
// dragged element can be dropped here.
253246
if (this.allowDrop) {
@@ -268,6 +261,15 @@ export abstract class AbstractComponent {
268261
return false;
269262
}
270263

264+
private _preventAndStop(event: Event): any {
265+
if (event.preventDefault) {
266+
event.preventDefault();
267+
}
268+
if (event.stopPropagation) {
269+
event.stopPropagation();
270+
}
271+
}
272+
271273
//*********** Draggable **********//
272274

273275
private _onDragStart(event: Event): void {

src/droppable.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ export class DroppableComponent extends AbstractComponent {
7878
};
7979

8080
_onDropCallback (event: MouseEvent) {
81-
if (this._dragDropService.isDragged) {
81+
let dataTransfer = (event as any).dataTransfer;
82+
if (this._dragDropService.isDragged || (dataTransfer && dataTransfer.files)) {
8283
this.onDropSuccess.emit({dragData: this._dragDropService.dragData, mouseEvent: event});
8384
if (this._dragDropService.onDragSuccessCallback) {
8485
this._dragDropService.onDragSuccessCallback.emit({dragData: this._dragDropService.dragData, mouseEvent: event});

0 commit comments

Comments
 (0)