diff options
Diffstat (limited to 'js/mediators/drag-drop-mediator.js')
-rwxr-xr-x | js/mediators/drag-drop-mediator.js | 108 |
1 files changed, 41 insertions, 67 deletions
diff --git a/js/mediators/drag-drop-mediator.js b/js/mediators/drag-drop-mediator.js index 14bdb0eb..049424f3 100755 --- a/js/mediators/drag-drop-mediator.js +++ b/js/mediators/drag-drop-mediator.js | |||
@@ -57,9 +57,7 @@ exports.DragDropMediator = Montage.create(Component, { | |||
57 | handleDropEvent: { | 57 | handleDropEvent: { |
58 | value: function(e){ | 58 | value: function(e){ |
59 | // | 59 | // |
60 | var i, files = e.dataTransfer.files, position = {x: e.offsetX, y: e.offsetY}, | 60 | var i, files = e.dataTransfer.files, position = {x: e.offsetX, y: e.offsetY}, self = this; |
61 | rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])), | ||
62 | rootUri = this.application.ninja.documentController.documentHackReference.root; | ||
63 | 61 | ||
64 | var xferString = e.dataTransfer.getData("text/plain"); | 62 | var xferString = e.dataTransfer.getData("text/plain"); |
65 | if(xferString) { | 63 | if(xferString) { |
@@ -74,70 +72,8 @@ exports.DragDropMediator = Montage.create(Component, { | |||
74 | // | 72 | // |
75 | for (i=0; files[i]; i++) { | 73 | for (i=0; files[i]; i++) { |
76 | if (files[i].type.indexOf('image') !== -1) { | 74 | if (files[i].type.indexOf('image') !== -1) { |
77 | var reader = new FileReader(), file = reader.readAsArrayBuffer(files[i]); | 75 | this.application.ninja.ioMediator.createFileFromBinary(files[i], {"addFileToStage" : self.addImageElement.bind(self), "position": position}); |
78 | reader.fileName = files[i].name, reader.fileType = files[i].type, reader.rootUrl = rootUrl, reader.rootUri = rootUri, reader.filePosition = position; | 76 | |
79 | reader.onload = function (e) { | ||
80 | // | ||
81 | var url, uri, dir, save, counter, tempName, element, rules, fileName; | ||
82 | if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'images'}).status === 204) { | ||
83 | uri = e.currentTarget.rootUri+'images'; | ||
84 | url = e.currentTarget.rootUrl+'images'; | ||
85 | } else if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'img'}).status === 204) { | ||
86 | uri = e.currentTarget.rootUri+'img'; | ||
87 | url = e.currentTarget.rootUrl+'img'; | ||
88 | } else { | ||
89 | dir = this.application.ninja.coreIoApi.createDirectory({uri: e.currentTarget.rootUri+'images'}); | ||
90 | if (dir.success && dir.status === 201) { | ||
91 | uri = e.currentTarget.rootUri+'images'; | ||
92 | url = e.currentTarget.rootUrl+'images'; | ||
93 | } else { | ||
94 | //TODO: HANDLE ERROR ON CREATING FOLDER | ||
95 | } | ||
96 | } | ||
97 | // | ||
98 | if (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+e.currentTarget.fileName}).status === 404) { | ||
99 | save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+e.currentTarget.fileName, contents: e.currentTarget.result, contentType: e.currentTarget.fileType}); | ||
100 | fileName = e.currentTarget.fileName; | ||
101 | } else { | ||
102 | counter = 1; | ||
103 | tempName = e.currentTarget.fileName.split('.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]))[0]; | ||
104 | tempName += '_'+counter+'.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]); | ||
105 | while (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+tempName}).status !== 404) { | ||
106 | counter++; | ||
107 | tempName = e.currentTarget.fileName.split('.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]))[0]; | ||
108 | tempName += '_'+counter+'.'+(e.currentTarget.fileName.split('.')[e.currentTarget.fileName.split('.').length-1]); | ||
109 | } | ||
110 | save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+tempName, contents: e.currentTarget.result, contentType: e.currentTarget.fileType}); | ||
111 | fileName = tempName; | ||
112 | } | ||
113 | if (save && save.success && save.status === 201) { | ||
114 | var self = this; | ||
115 | // | ||
116 | if (e.currentTarget.fileType.indexOf('svg') !== -1) { | ||
117 | element = NJUtils.make('embed', null, this.application.ninja.currentDocument);//TODO: Verify this is proper | ||
118 | element.type = 'image/svg+xml'; | ||
119 | element.src = url+'/'+fileName; | ||
120 | } else { | ||
121 | element = NJUtils.make('image', null, this.application.ninja.currentDocument); | ||
122 | element.src = url+'/'+fileName; | ||
123 | } | ||
124 | //Adding element once it is loaded | ||
125 | element.onload = function () { | ||
126 | element.onload = null; | ||
127 | self.application.ninja.elementMediator.addElements(element, rules, true); | ||
128 | }; | ||
129 | //Setting rules of element | ||
130 | rules = { | ||
131 | 'position': 'absolute', | ||
132 | 'top' : (parseInt(e.currentTarget.filePosition.y) - parseInt(this.application.ninja.stage.userContentTop)) + 'px', | ||
133 | 'left' : (parseInt(e.currentTarget.filePosition.x) - parseInt(this.application.ninja.stage.userContentLeft)) + 'px' | ||
134 | }; | ||
135 | // | ||
136 | self.application.ninja.elementMediator.addElements(element, rules, false); | ||
137 | } else { | ||
138 | //TODO: HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT | ||
139 | } | ||
140 | }.bind(this); | ||
141 | } else { | 77 | } else { |
142 | //TODO: NOT AN IMAGE, HANDLE SPECIAL CASE | 78 | //TODO: NOT AN IMAGE, HANDLE SPECIAL CASE |
143 | } | 79 | } |
@@ -145,5 +81,43 @@ exports.DragDropMediator = Montage.create(Component, { | |||
145 | //Not sure why return value should be, seemed as false to work | 81 | //Not sure why return value should be, seemed as false to work |
146 | return false; | 82 | return false; |
147 | } | 83 | } |
84 | }, | ||
85 | |||
86 | addImageElement:{ | ||
87 | value: function(status){ | ||
88 | var save = status.save, | ||
89 | fileName = status.filename, | ||
90 | url = status.url, | ||
91 | element, rules, self = this, | ||
92 | fileType = status.fileType, | ||
93 | filePosition = status.filePosition ? status.filePosition : {x: "100", y: "100"}; | ||
94 | |||
95 | if (save && save.success && save.status === 201) { | ||
96 | // | ||
97 | if (fileType.indexOf('svg') !== -1) { | ||
98 | element = NJUtils.make('embed', null, this.application.ninja.currentDocument);//TODO: Verify this is proper | ||
99 | element.type = 'image/svg+xml'; | ||
100 | element.src = url+'/'+fileName; | ||
101 | } else { | ||
102 | element = NJUtils.make('image', null, this.application.ninja.currentDocument); | ||
103 | element.src = url+'/'+fileName; | ||
104 | } | ||
105 | //Adding element once it is loaded | ||
106 | element.onload = function () { | ||
107 | element.onload = null; | ||
108 | self.application.ninja.elementMediator.addElements(element, rules, true); | ||
109 | }; | ||
110 | //Setting rules of element | ||
111 | rules = { | ||
112 | 'position': 'absolute', | ||
113 | 'top' : (parseInt(filePosition.y) - parseInt(this.application.ninja.stage.userContentTop)) + 'px', | ||
114 | 'left' : (parseInt(filePosition.x) - parseInt(this.application.ninja.stage.userContentLeft)) + 'px' | ||
115 | }; | ||
116 | // | ||
117 | self.application.ninja.elementMediator.addElements(element, rules, false); | ||
118 | } else { | ||
119 | //TODO: HANDLE ERROR ON SAVING FILE TO BE ADDED AS ELEMENT | ||
120 | } | ||
121 | } | ||
148 | } | 122 | } |
149 | }); | 123 | }); |