aboutsummaryrefslogtreecommitdiff
path: root/js/mediators
diff options
context:
space:
mode:
authorValerio Virgillito2012-06-21 16:26:52 -0700
committerValerio Virgillito2012-06-21 16:26:52 -0700
commit2af33f19de48bb8fe269c51ceddd1bc1f591651f (patch)
tree564528119093e4e3408ccf180d55ac3d5e413813 /js/mediators
parentc411f76d89c543837548085ff468fee0fb4f2ff9 (diff)
parent47be0919aa271e97fd7ca86e65cdf93aeee2bf81 (diff)
downloadninja-2af33f19de48bb8fe269c51ceddd1bc1f591651f.tar.gz
Merge pull request #320 from ananyasen/cut-copy-paste
copy cut paste - project phase 1 check in
Diffstat (limited to 'js/mediators')
-rwxr-xr-xjs/mediators/drag-drop-mediator.js108
-rw-r--r--js/mediators/io-mediator.js59
-rwxr-xr-xjs/mediators/keyboard-mediator.js2
3 files changed, 100 insertions, 69 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});
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index c487fef5..c6e7d960 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -205,9 +205,66 @@ exports.IoMediator = Montage.create(Component, {
205 value: function (file, callback) { 205 value: function (file, callback) {
206 //TODO: Implement Delete functionality 206 //TODO: Implement Delete functionality
207 } 207 }
208 } 208 },
209 //////////////////////////////////////////////////////////////////// 209 ////////////////////////////////////////////////////////////////////
210 //////////////////////////////////////////////////////////////////// 210 ////////////////////////////////////////////////////////////////////
211 createFileFromBinary:{
212 enumerable: false,
213 value: function(blob, callback){
214 var reader = new FileReader(), file = reader.readAsArrayBuffer(blob), url, uri, dir, save, counter, tempName, element, rules, fileName, fileNameOverride,
215 rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])),
216 rootUri = this.application.ninja.documentController.documentHackReference.root;
217
218 reader.fileName = blob.name, reader.fileType = blob.type, reader.rootUrl = rootUrl, reader.rootUri = rootUri;
219 if(callback && callback.position){reader.filePosition = callback.position;}
220
221 reader.onload = function (e) {
222 if(e.currentTarget.fileType.indexOf('image') !== -1 ){
223 if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'images'}).status === 204) {
224 uri = e.currentTarget.rootUri+'images';
225 url = e.currentTarget.rootUrl+'images';
226 } else if (this.application.ninja.coreIoApi.directoryExists({uri: e.currentTarget.rootUri+'img'}).status === 204) {
227 uri = e.currentTarget.rootUri+'img';
228 url = e.currentTarget.rootUrl+'img';
229 } else {
230 dir = this.application.ninja.coreIoApi.createDirectory({uri: e.currentTarget.rootUri+'images'});
231 if (dir.success && dir.status === 201) {
232 uri = e.currentTarget.rootUri+'images';
233 url = e.currentTarget.rootUrl+'images';
234 } else {
235 //TODO: HANDLE ERROR ON CREATING FOLDER
236 }
237 }
238 //fileName is undefined while pasting image from clipboard
239 fileNameOverride = e.currentTarget.fileName ? e.currentTarget.fileName : ("image." + e.currentTarget.fileType.substring((e.currentTarget.fileType.indexOf("/")+1), e.currentTarget.fileType.length));//like image.png
240
241 if (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+fileNameOverride}).status === 404) {
242 save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+fileNameOverride, contents: e.currentTarget.result, contentType: e.currentTarget.fileType});
243 fileName = fileNameOverride;
244 } else {
245 counter = 1;
246 tempName = fileNameOverride.split('.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]))[0];
247 tempName += '_'+counter+'.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]);
248 while (this.application.ninja.coreIoApi.fileExists({uri: uri+'/'+tempName}).status !== 404) {
249 counter++;
250 tempName = fileNameOverride.split('.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]))[0];
251 tempName += '_'+counter+'.'+(fileNameOverride.split('.')[fileNameOverride.split('.').length-1]);
252 }
253 save = this.application.ninja.coreIoApi.createFile({uri: uri+'/'+tempName, contents: e.currentTarget.result, contentType: e.currentTarget.fileType});
254 fileName = tempName;
255 }
256
257 if(callback && callback.addFileToStage && (typeof callback.addFileToStage === "function")){
258 callback.addFileToStage({"save": save, "url": url, "filename": fileName, "filePosition": e.currentTarget.filePosition, "fileType":e.currentTarget.fileType});
259 }
260
261 }else{
262 //TODO: HANDLE NOT AN IMAGE
263 }