Blame view

ihm/app/controller/Drop/FileDrop.js 1.96 KB
346b85c6   Benjamin Renard   First commit with...
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
Ext.define('treps.controller.Drop.FileDrop', {
	extend: 'Ext.app.Controller',
	
	views: [
		'Steps.SourceSelection.FileDropZone'
	],
	
	refs: [
		{
			ref: 'dropZone',
			selector: 'filedropzone'
		}
	],
	
	init: function() {
		var me = this;
		
		this.control({
			'filedropzone': {
				afterrender: me.onInitDropZone,
				scope: me
			}
		});
	},
	
	onInitDropZone: function() {
		var me = this;
		var dropZone = this.getDropZone();
		var documentDropTargetEl = dropZone.getEl().dom;
			
		if (typeof(File) === 'undefined' || typeof(FileReader) === 'undefined')
		{
			dropZone.update("Drag & Drop Feature not supported by your browser");
			return;
		}
			
		documentDropTargetEl.addEventListener('dragenter', function (evt) {
			dropZone.getEl().highlight();
			evt.stopPropagation();
			evt.preventDefault();
		}, false);
			
		documentDropTargetEl.addEventListener('dragover', function (evt) {
			evt.stopPropagation();
			evt.preventDefault();
		}, false);
			
		documentDropTargetEl.addEventListener('drop', function (evt) {
			var files = evt.target.files || evt.dataTransfer.files;
			
			evt.stopPropagation();
			evt.preventDefault();
				
			if (!files || files.length == 0)
			{
				treps.Messages.showError("Error to retrieve the dragged file.");
				return false;
			}
			if (files.length > 1)
			{
				treps.Messages.showError("Please select only one file.");
				return false;
			}
			
			var file = files[0];

			if (file.size > treps.Constants.DROP_MAX_FILESIZE)
			{
				treps.Messages.showError("File too large (limitation to "+treps.Constants.DROP_MAX_FILESIZE+" octets)");
				return false;
			}

			var reader = new FileReader();
			reader.onload = function (event) {
				me.application.fireEvent('receivesource','data', event.target.result);
			};

			try
			{
				reader.readAsDataURL(file);
			}
			catch (err)
			{
				treps.Messages.showError(err.message);
			}
				
		}, false);
	}
});