aboutsummaryrefslogtreecommitdiff
path: root/js/io/ui/save-as-dialog.reel/save-as-dialog.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/ui/save-as-dialog.reel/save-as-dialog.js')
-rw-r--r--js/io/ui/save-as-dialog.reel/save-as-dialog.js232
1 files changed, 232 insertions, 0 deletions
diff --git a/js/io/ui/save-as-dialog.reel/save-as-dialog.js b/js/io/ui/save-as-dialog.reel/save-as-dialog.js
new file mode 100644
index 00000000..55a09fa8
--- /dev/null
+++ b/js/io/ui/save-as-dialog.reel/save-as-dialog.js
@@ -0,0 +1,232 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7var Montage = require("montage/core/core").Montage,
8 Component = require("montage/ui/component").Component;
9
10var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, {
11
12 hasReel: {
13 value: true
14 },
15
16 fileName : {
17 enumerable: true,
18 writable: true,
19 value: ""
20 },
21
22 folderUri:{
23 enumerable: true,
24 writable: true,
25 value: ""
26 },
27
28 callback : {
29 enumerable: true,
30 writable: true,
31 value: null
32 },
33
34 callbackScope : {
35 enumerable: true,
36 writable: true,
37 value: null
38 },
39
40 willDraw: {
41 enumerable: false,
42 value: function() {}
43 },
44 draw: {
45 enumerable: false,
46 value: function() {}
47 },
48 didDraw: {
49 enumerable: false,
50 value: function() {
51 var self = this;
52 this.fileInputField.selectDirectory = true;
53 this.fileInputField.pickerName = "saveAsDirectoryPicker";
54 this.newFileName.value = this.fileName;
55 this.fileInputField.newFileDirectory.value = this.folderUri;
56
57 this.newFileName.addEventListener("keyup", function(evt){self.handleNewFileNameOnkeyup(evt);}, false);
58 this.eventManager.addEventListener("newFileDirectorySet", function(evt){self.handleNewFileDirectorySet(evt);}, false);
59
60 this.okButton.addEventListener("click", function(evt){self.handleOkButtonAction(evt);}, false);
61 this.cancelButton.addEventListener("click", function(evt){self.handleCancelButtonAction(evt);}, false);
62
63 this.enableOk();
64 }
65 },
66
67 handleNewFileDirectorySet:{
68 value:function(evt){
69 if(!!evt._event.newFileDirectory){
70 this.folderUri = evt._event.newFileDirectory;
71 if(this.isValidUri(this.folderUri)){
72 this.enableOk();
73 }
74 }
75 }
76 },
77
78 handleNewFileNameOnkeyup:{
79 value:function(evt){
80 this.fileName = this.newFileName.value;
81 if(this.fileName !== ""){
82 if(this.isValidFileName(this.fileName)){
83 this.enableOk();
84 }
85 }
86 }
87 },
88
89
90 enableOk:{
91 value: function(){
92 if(this.isValidFileName(this.fileName) && this.isValidUri(this.folderUri)){
93 this.okButton.removeAttribute("disabled");
94 this.error.innerHTML="";
95 }
96 }
97 },
98
99 handleCancelButtonAction :{
100 value:function(evt){
101 //clean up memory
102 //this.cleanup();
103
104 if(this.popup){
105 this.popup.hide();
106 }
107
108 }
109 },
110
111 handleOkButtonAction:{
112 value: function(evt){
113 var filename = this.fileName,
114 newFileDirectory = this.newFileDirectory,
115 success = true;
116 if(this.isValidFileName(this.fileName) && this.isValidUri(this.folderUri) && !this.checkFileExists(this.fileName, this.folderUri)){
117 try{
118 //validate file name and folder path
119 //check if file already exists
120 if(!!this.callback && !!this.callbackScope){//inform document-controller if save successful
121 this.callback.call(this.callbackScope, {"filename":filename, "destination": newFileDirectory});//document-controller api
122 }else{
123 //send save as event
124 var saveAsEvent = document.createEvent("Events");
125 saveAsEvent.initEvent("saveAsFile", false, false);
126 saveAsEvent.saveAsOptions = {"filename":filename, "destination": newFileDirectory};
127 this.eventManager.dispatchEvent(saveAsEvent);
128 }
129 }catch(e){
130 success = false;
131 console.log("[ERROR] Failed to save: "+ this.fileName + " at "+ this.newFileDirectory);
132 console.log(e.stack);
133 }
134
135 if(success){
136 //clean up memory
137 //this.cleanup();
138
139 if(this.popup){
140 this.popup.hide();
141 }
142 }
143 }else{
144 if(this.error.innerHTML === ""){
145 this.showError("! Name and Location should be valid.");
146 }
147 //disable ok
148 if(!this.okButton.hasAttribute("disabled")){
149 this.okButton.setAttribute("disabled", "true");
150 }
151 }
152 }
153 },
154
155 isValidUri:{
156 value: function(uri){
157 var status= this.application.ninja.coreIoApi.isValidUri(uri);
158 if(uri !== ""){
159 if(!status){
160 this.showError("! Invalid directory.");
161 }
162 }
163 return status;
164 }
165 },
166 isValidFileName:{
167 value: function(fileName){
168 var status = this.validateFileName(fileName);
169 if(fileName !== ""){
170 if(!status){
171 this.showError("! Invalid file name.");
172 }
173 }
174 return status;
175 }
176 },
177 checkFileExists:{
178 value: function(fileName, folderUri, fileType){
179 var uri = "", response=null, status=true;
180 //prepare absolute uri
181 if(/[^/\\]$/g.test(folderUri)){
182 folderUri = folderUri + "/";
183 }
184 if(!!fileType && (fileName.lastIndexOf(fileType) !== (fileName.length - fileType.length))){
185 fileName = fileName+fileType;
186 }
187 uri = ""+folderUri+fileName;
188 response= this.application.ninja.coreIoApi.fileExists({"uri":uri});
189 if(!!response && response.success && (response.status === 204)){
190 status = true;
191 }else if(!!response && response.success && (response.status === 404)){
192 status = false;
193 }else{
194 status = false;
195 }
196
197 if(status){
198 this.showError("! File already exists.");
199 }
200 return status;
201 }
202 },
203
204 showError:{
205 value:function(errorString){
206 this.error.innerHTML = "";
207 this.error.innerHTML=errorString;
208 //disable ok
209 if(!this.okButton.hasAttribute("disabled")){
210 this.okButton.setAttribute("disabled", "true");
211 }
212 }
213 },
214
215 /***
216 * file name validation
217 */
218 validateFileName:{
219 value: function(fileName){
220 var status = false;
221 if(fileName !== ""){
222 fileName = fileName.replace(/^\s+|\s+$/g,"");
223 status = !(/[/\\]/g.test(fileName));
224