diff options
author | Valerio Virgillito | 2012-02-15 16:09:47 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-15 16:09:47 -0800 |
commit | d366c0bd1af6471511217ed574083e15059519b5 (patch) | |
tree | c8c9f9af761457e3c5f4c6774fb0fbba851df0c4 /js/io/ui/save-as-dialog.reel/save-as-dialog.js | |
parent | 997ce3fb65f27b3d6f331f63b5dc22d3c7fb8f1e (diff) | |
parent | b85bfb54aaca3ccca3c1ef09115de925cd67f4e9 (diff) | |
download | ninja-d366c0bd1af6471511217ed574083e15059519b5.tar.gz |
Merge branch 'refs/heads/integration'
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.js | 215 |
1 files changed, 215 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..786ef5c9 --- /dev/null +++ b/js/io/ui/save-as-dialog.reel/save-as-dialog.js | |||
@@ -0,0 +1,215 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No 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 | |||
7 | var Montage = require("montage/core/core").Montage, | ||
8 | Component = require("montage/ui/component").Component; | ||
9 | |||
10 | var 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(fileUri, folderUri, fileType){ | ||
179 | var status= this.application.ninja.coreIoApi.checkFileExists(fileUri, folderUri, fileType); | ||
180 | if(status){ | ||
181 | this.showError("! File already exists."); | ||
182 | } | ||
183 | return status; | ||
184 | } | ||
185 | }, | ||
186 | |||
187 | showError:{ | ||
188 | value:function(errorString){ | ||
189 | this.error.innerHTML = ""; | ||
190 | this.error.innerHTML=errorString; | ||
191 | //disable ok | ||
192 | if(!this.okButton.hasAttribute("disabled")){ | ||
193 | this.okButton.setAttribute("disabled", "true"); | ||
194 | } | ||
195 | } | ||
196 | }, | ||
197 | |||
198 | /*** | ||
199 | * file name validation | ||
200 | */ | ||
201 | validateFileName:{ | ||
202 | value: function(fileName){ | ||
203 | var status = false; | ||
204 | if(fileName !== ""){ | ||
205 | fileName = fileName.replace(/^\s+|\s+$/g,""); | ||
206 | status = !(/[/\\]/g.test(fileName)); | ||
207 | if(status && navigator.userAgent.indexOf("Macintosh") != -1){//for Mac files beginning with . are hidden | ||
208 | status = !(/^\./g.test(fileName)); | ||
209 | } | ||
210 | } | ||
211 | return status; | ||
212 | } | ||
213 | } | ||
214 | |||
215 | }); \ No newline at end of file | ||