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
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component,
NJUtils = require("js/lib/NJUtils").NJUtils;
exports.DragDropMediator = Montage.create(Component, {
dropTarget: {
value: null,
writable: true
},
dropDelegate: {
value: null
},
deserializedFromTemplate: {
value: function() {
this.eventManager.addEventListener("appLoaded", this, false);
}
},
handleAppLoaded: {
value: function() {
this.dropTarget = this.application.ninja.stage.drawingCanvas;
this.dropTarget.addEventListener("dragover", this, false);
this.dropTarget.addEventListener("dragend", this, false);
this.dropTarget.addEventListener("drop", this, false);
}
},
handleEvent: {
value: function(event){
event.preventDefault();
event.stopImmediatePropagation();
switch(event.type) {
case "dragover":
return false;
case "dragend":
return false;
case "drop":
this.handleDropEvent(event);
break;
default:
console.log("Default");
break;
}
}
},
handleDropEvent: {
value: function(e){
//
var i, files = e.dataTransfer.files, position = {x: e.offsetX, y: e.offsetY},
rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])),
rootUri = this.application.ninja.documentController.documentHackReference.root;
//
for (i=0; files[i]; i++) {
if (files[i].type.indexOf('image') !== -1) {
var reader = new FileReader(), file = reader.readAsArrayBuffer(files[i]);
reader.fileName = files[i].name, reader.fileType = files[i].type, reader.rootUrl = rootUrl, reader.rootUri = rootUri, reader.filePosition = position;
reader.onload = function (e) {
//
var url, uri, dir, save, counter, tempName, element, rules, fileName;
if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'images'}).status === 204) {
uri = e.currentTarget.rootUri+'images';
url = e.currentTarget.rootUrl+'images';
} else if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'img'}).status === 204) {
uri = e.currentTarget.rootUri+'img';
url = e.currentTarget.rootUrl+'img';
} else {
dir = this.application.ninja.coreIoApi.createDirectory({uri: e.currentTarget.rootUri+'images'});
if (dir.success && dir.status === 201) {
uri = e.currentTarget.rootUri+'images';
url = e.currentTarget.rootUrl+'images';
} else {
//TODO: HANDLE ERROR ON CREATING FOLDER
}
}
//
if (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+e.currentTarget.fileName}).status === 404) {
save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+e.currentTarget.fileName, contents: e.currentTarget.result, contentType: e.currentTarget.fileType});
fileName = e.currentTarget.fileName;
} else {
counter = 1;
tempName = e.currentTarget.fileName.split('.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]))[0];
tempName += '_'+counter+'.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]);
while (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+tempName}).status !== 404) {
counter++;
tempName = e.currentTarget.fileName.split('.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]))[0];
tempName += '_'+counter+'.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]);
}
save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+tempName, contents: e.currentTarget.result, contentType: e.currentTarget.fileType});
fileName = tempName;
}
if (save && save.success && save.status === 201) {
//
if (e.currentTarget.fileType.indexOf('svg') !== -1) {
element = NJUtils.makeNJElement('embed', 'SVG', 'block');//TODO: Verify this is proper
element.src = url+'/'+fileName;
element.type = 'image/svg+xml';
} else {
element = NJUtils.makeNJElement('image', 'image', 'image');
element.src = url+'/'+fileName;
}
//
rules = {
'position': 'absolute',
'top' : e.currentTarget.filePosition.y + 'px',
'left' : e.currentTarget.filePosition.x + 'px'
};
//
NJevent("elementAdding", {el: element, data: rules});
} else {
//TODO: HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT
}
}.bind(this);
} else {
//TODO: NOT AN IMAGE, HANDLE SPECIAL CASE
}
}
//Not sure why return value should be, seemed as false to work
return false;
}
}
});
|