diff options
author | Ananya Sen | 2012-01-30 14:18:21 -0800 |
---|---|---|
committer | Ananya Sen | 2012-01-30 14:18:21 -0800 |
commit | 92161460a6cbbdebfd1b0263ec6eb790091920a9 (patch) | |
tree | 48136ac734f6e46d902b720387ac5693ed29ee8d /js/io/workflow/save-as-dialog.reel/save-as-dialog.js | |
parent | 9c46c73141259ab4478935033cb4d8dda43f6bf2 (diff) | |
download | ninja-92161460a6cbbdebfd1b0263ec6eb790091920a9.tar.gz |
Moving changes from Gerrit, on 1/27, to the github branch:
- added file existence check and validation to save as dialog
- minor css fix for file picker, new file dialog
Signed-off-by: Ananya Sen <Ananya.Sen@motorola.com>
Diffstat (limited to 'js/io/workflow/save-as-dialog.reel/save-as-dialog.js')
-rw-r--r--[-rwxr-xr-x] | js/io/workflow/save-as-dialog.reel/save-as-dialog.js | 138 |
1 files changed, 104 insertions, 34 deletions
diff --git a/js/io/workflow/save-as-dialog.reel/save-as-dialog.js b/js/io/workflow/save-as-dialog.reel/save-as-dialog.js index 1255a1bd..52e5ab82 100755..100644 --- a/js/io/workflow/save-as-dialog.reel/save-as-dialog.js +++ b/js/io/workflow/save-as-dialog.reel/save-as-dialog.js | |||
@@ -5,7 +5,8 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | var Montage = require("montage/core/core").Montage, | 7 | var Montage = require("montage/core/core").Montage, |
8 | Component = require("montage/ui/component").Component; | 8 | Component = require("montage/ui/component").Component, |
9 | fileUtils = require("js/io/utils/file-utils").FileUtils; | ||
9 | 10 | ||
10 | var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, { | 11 | var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, { |
11 | 12 | ||
@@ -19,6 +20,12 @@ var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, { | |||
19 | value: "" | 20 | value: "" |
20 | }, | 21 | }, |
21 | 22 | ||
23 | folderUri:{ | ||
24 | enumerable: true, | ||
25 | writable: true, | ||
26 | value: "" | ||
27 | }, | ||
28 | |||
22 | callback : { | 29 | callback : { |
23 | enumerable: true, | 30 | enumerable: true, |
24 | writable: true, | 31 | writable: true, |
@@ -42,10 +49,48 @@ var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, { | |||
42 | didDraw: { | 49 | didDraw: { |
43 | enumerable: false, | 50 | enumerable: false, |
44 | value: function() { | 51 | value: function() { |
52 | var self = this; | ||
45 | this.fileInputField.selectDirectory = true; | 53 | this.fileInputField.selectDirectory = true; |
46 | this.fileInputField.pickerName = "saveAsDirectoryPicker"; | 54 | this.fileInputField.pickerName = "saveAsDirectoryPicker"; |
47 | this.newFileName.value = this.fileName; | 55 | this.newFileName.value = this.fileName; |
48 | this.fileInputField.newFileDirectory.value = this.folderUri; | 56 | this.fileInputField.newFileDirectory.value = this.folderUri; |
57 | |||
58 | this.newFileName.addEventListener("blur", function(evt){self.handleNewFileNameOnblur(evt);}, false); | ||
59 | this.eventManager.addEventListener("newFileDirectorySet", function(evt){self.handleNewFileDirectorySet(evt);}, false); | ||
60 | |||
61 | this.enableOk(); | ||
62 | } | ||
63 | }, | ||
64 | |||
65 | handleNewFileDirectorySet:{ | ||
66 | value:function(evt){ | ||
67 | if(!!evt._event.newFileDirectory){ | ||
68 | this.folderUri = evt._event.newFileDirectory; | ||
69 | if(this.folderUri !== ""){ | ||
70 | this.enableOk(); | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | }, | ||
75 | |||
76 | handleNewFileNameOnblur:{ | ||
77 | value:function(evt){ | ||
78 | this.fileName = this.newFileName.value; | ||
79 | if(this.fileName !== ""){ | ||
80 | if(this.fileName !== ""){ | ||
81 | this.enableOk(); | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | }, | ||
86 | |||
87 | |||
88 | enableOk:{ | ||
89 | value: function(){ | ||
90 | if(this.isValidFileName(this.fileName) && this.isValidUri(this.folderUri) && !this.checkFileExists(this.fileName, this.folderUri)){ | ||
91 | this.okButton.removeAttribute("disabled"); | ||
92 | this.error.innerHTML=""; | ||
93 | } | ||
49 | } | 94 | } |
50 | }, | 95 | }, |
51 | 96 | ||
@@ -66,29 +111,40 @@ var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, { | |||
66 | var filename = this.fileName, | 111 | var filename = this.fileName, |
67 | newFileDirectory = this.newFileDirectory, | 112 | newFileDirectory = this.newFileDirectory, |
68 | success = true; | 113 | success = true; |
69 | try{ | 114 | if(this.isValidFileName(this.fileName) && this.isValidUri(this.folderUri) && !this.checkFileExists(this.fileName, this.folderUri)){ |
70 | //validate file name and folder path | 115 | try{ |
71 | //check if file already exists | 116 | //validate file name and folder path |
72 | if(!!this.callback && !!this.callbackScope){//inform document-controller if save successful | 117 | //check if file already exists |
73 | this.callback.call(this.callbackScope, {"filename":filename, "destination": newFileDirectory});//document-controller api | 118 | if(!!this.callback && !!this.callbackScope){//inform document-controller if save successful |
74 | }else{ | 119 | this.callback.call(this.callbackScope, {"filename":filename, "destination": newFileDirectory});//document-controller api |
75 | //send save as event | 120 | }else{ |
76 | var saveAsEvent = document.createEvent("Events"); | 121 | //send save as event |
77 | saveAsEvent.initEvent("saveAsFile", false, false); | 122 | var saveAsEvent = document.createEvent("Events"); |
78 | saveAsEvent.saveAsOptions = {"filename":filename, "destination": newFileDirectory}; | 123 | saveAsEvent.initEvent("saveAsFile", false, false); |
79 | this.eventManager.dispatchEvent(saveAsEvent); | 124 | saveAsEvent.saveAsOptions = {"filename":filename, "destination": newFileDirectory}; |
125 | this.eventManager.dispatchEvent(saveAsEvent); | ||
126 | } | ||
127 | }catch(e){ | ||
128 | success = false; | ||
129 | console.log("[ERROR] Failed to save: "+ this.fileName + " at "+ this.newFileDirectory); | ||
130 | console.log(e.stack); | ||
80 | } | 131 | } |
81 | }catch(e){ | ||
82 | success = false; | ||
83 | console.log("[ERROR] Failed to save: "+ this.fileName + " at "+ this.newFileDirectory); | ||
84 | } | ||
85 | 132 | ||
86 | if(success){ | 133 | if(success){ |
87 | //clean up memory | 134 | //clean up memory |
88 | //this.cleanup(); | 135 | //this.cleanup(); |
89 | 136 | ||
90 | if(this.popup){ | 137 | if(this.popup){ |
91 | this.popup.hide(); | 138 | this.popup.hide(); |
139 | } | ||
140 | } | ||
141 | }else{ | ||
142 | if(this.error.innerHTML !== ""){ | ||
143 | this.showError("! Name and Location should be valid."); | ||
144 | } | ||
145 | //disable ok | ||
146 | if(!this.okButton.hasAttribute("disabled")){ | ||
147 | this.okButton.setAttribute("disabled", "true"); | ||
92 | } | 148 | } |
93 | } | 149 | } |
94 | } | 150 | } |
@@ -96,31 +152,45 @@ var SaveAsDialog = exports.SaveAsDialog = Montage.create(Component, { | |||
96 | 152 | ||
97 | isValidUri:{ | 153 | isValidUri:{ |
98 | value: function(uri){ | 154 | value: function(uri){ |
99 | var isWindowsUri=false, isUnixUri=false,status=false; | 155 | var status= fileUtils.isValidUri(uri); |
100 | if(uri !== ""){ | 156 | if(uri !== ""){ |
101 | uri = uri.replace(/^\s+|\s+$/g,""); // strip any leading or trailing spaces | 157 | if(!status){ |
102 | 158 | this.showError("! Invalid directory."); | |
103 | //for local machine folder uri | 159 | } |
104 | isWindowsUri = /^([a-zA-Z]:)(\\[^<>:"/\\|?*]+)*\\?$/gi.test(uri); | ||
105 | isUnixUri = /^(\/)?(\/(?![.])[^/]*)*\/?$/gi.test(uri);//folders beginning with . are hidden on Mac / Unix | ||
106 | status = isWindowsUri || isUnixUri; | ||
107 | if(isWindowsUri && isUnixUri){status = false;} | ||
108 | } | 160 | } |
109 | return status; | 161 | return status; |
110 | } | 162 | } |
111 | }, | 163 | }, |
112 | isValidFileName:{ | 164 | isValidFileName:{ |
113 | value: function(fileName){ | 165 | value: function(fileName){ |
114 | var status = false; | 166 | var status = fileUtils.isValidFileName(fileName); |
115 | if(fileName !== ""){ | 167 | if(fileName !== ""){ |
116 | fileName = fileName.replace(/^\s+|\s+$/g,""); | 168 | if(!status){ |
117 | status = !(/[/\\]/g.test(fileName)); | 169 | this.showError("! Invalid file name."); |
118 | if(status && navigator.userAgent.indexOf("Macintosh") != -1){//for Mac files beginning with . are hidden | ||
119 | status = !(/^\./g.test(fileName)); | ||
120 | } | 170 | } |
121 | } | 171 | } |
122 | return status; | 172 | return status; |
123 | } | 173 | } |
174 | }, | ||
175 | checkFileExists:{ | ||
176 | value: function(fileUri, folderUri, fileType){ | ||
177 | var status= fileUtils.checkFileExists(fileUri, folderUri, fileType); | ||
178 | if(status){ | ||
179 | this.showError("! File already exists."); | ||
180 | } | ||
181 | return status; | ||
182 | } | ||
183 | }, | ||
184 | |||
185 | showError:{ | ||
186 | value:function(errorString){ | ||
187 | this.error.innerHTML = ""; | ||
188 | this.error.innerHTML=errorString; | ||
189 | //disable ok | ||
190 | if(!this.okButton.hasAttribute("disabled")){ | ||
191 | this.okButton.setAttribute("disabled", "true"); | ||
192 | } | ||
193 | } | ||
124 | } | 194 | } |
125 | 195 | ||