diff options
author | Valerio Virgillito | 2012-02-14 11:07:21 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-14 11:07:21 -0800 |
commit | f060190a3bffd9a16718f2ce0499699103d46372 (patch) | |
tree | a308fe7eacec2156c8212dff0892a3de925d5f9a /js/controllers/document-controller.js | |
parent | f766cc203f30ea43ae8b83cf4b65d45cc4435ee9 (diff) | |
parent | 33bc9d62b8e6694500bf14d5b18187bd99a520a3 (diff) | |
download | ninja-f060190a3bffd9a16718f2ce0499699103d46372.tar.gz |
Merge branch 'FileIO' of https://github.com/joseeight/ninja-internal into integration
Diffstat (limited to 'js/controllers/document-controller.js')
-rwxr-xr-x | js/controllers/document-controller.js | 430 |
1 files changed, 430 insertions, 0 deletions
diff --git a/js/controllers/document-controller.js b/js/controllers/document-controller.js new file mode 100755 index 00000000..84b994ce --- /dev/null +++ b/js/controllers/document-controller.js | |||
@@ -0,0 +1,430 @@ | |||
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 | //////////////////////////////////////////////////////////////////////// | ||
8 | // | ||
9 | var Montage = require("montage/core/core").Montage, | ||
10 | Component = require("montage/ui/component").Component, | ||
11 | Uuid = require("montage/core/uuid").Uuid, | ||
12 | HTMLDocument = require("js/io/document/html-document").HTMLDocument, | ||
13 | TextDocument = require("js/io/document/text-document").TextDocument, | ||
14 | DocumentController; | ||
15 | //////////////////////////////////////////////////////////////////////// | ||
16 | // | ||
17 | DocumentController = exports.DocumentController = Montage.create(Component, { | ||
18 | hasTemplate: { | ||
19 | value: false | ||
20 | }, | ||
21 | |||
22 | _documents: { | ||
23 | value: [] | ||
24 | }, | ||
25 | |||
26 | _activeDocument: { value: null }, | ||
27 | _iframeCounter: { value: 1, enumerable: false }, | ||
28 | _iframeHolder: { value: null, enumerable: false }, | ||
29 | _textHolder: { value: null, enumerable: false }, | ||
30 | _codeMirrorCounter: {value: 1, enumerable: false}, | ||
31 | |||
32 | tmpSourceForTesting: { | ||
33 | value: "function CodeMirror(place, givenOptions) {" + | ||
34 | "// Determine effective options based on given values and defaults." + | ||
35 | "var options = {}, defaults = CodeMirror.defaults; }" | ||
36 | }, | ||
37 | |||
38 | activeDocument: { | ||
39 | get: function() { | ||
40 | return this._activeDocument; | ||
41 | }, | ||
42 | set: function(doc) { | ||
43 | if(this._activeDocument) this._activeDocument.isActive = false; | ||
44 | |||
45 | if(this._documents.indexOf(doc) === -1) this._documents.push(doc); | ||
46 | |||
47 | this._activeDocument = doc; | ||
48 | this._activeDocument.isActive = true; | ||
49 | |||
50 | if(!!this._activeDocument.editor){ | ||
51 | this._activeDocument.editor.focus(); | ||
52 | } | ||
53 | |||
54 | } | ||
55 | }, | ||
56 | |||
57 | deserializedFromTemplate: { | ||
58 | value: function() { | ||
59 | this.eventManager.addEventListener("appLoaded", this, false); | ||
60 | this.eventManager.addEventListener("executeFileOpen", this, false); | ||
61 | this.eventManager.addEventListener("executeNewFile", this, false); | ||
62 | this.eventManager.addEventListener("executeSave", this, false); | ||
63 | |||
64 | this.eventManager.addEventListener("recordStyleChanged", this, false); | ||
65 | |||
66 | // Temporary testing opening a new file after Ninja has loaded | ||
67 | this.eventManager.addEventListener("executeNewProject", this, false); | ||
68 | } | ||
69 | }, | ||
70 | |||
71 | handleAppLoaded: { | ||
72 | value: function() { | ||
73 | //this.openDocument({"type": "html"}); | ||
74 | } | ||
75 | }, | ||
76 | |||
77 | handleExecuteNewProject: { | ||
78 | value: function() { | ||
79 | this.openDocument({"type": "html"}); | ||
80 | } | ||
81 | }, | ||
82 | |||
83 | handleExecuteFileOpen: { | ||
84 | value: function(event) { | ||
85 | var pickerSettings = event._event.settings || {}; | ||
86 | pickerSettings.callback = this.openFileWithURI.bind(this); | ||
87 | pickerSettings.pickerMode = "read"; | ||
88 | pickerSettings.inFileMode = true; | ||
89 | this.application.ninja.filePickerController.showFilePicker(pickerSettings); | ||
90 | } | ||
91 | }, | ||
92 | |||
93 | handleExecuteNewFile: { | ||
94 | value: function(event) { | ||
95 | var newFileSettings = event._event.settings || {}; | ||
96 | newFileSettings.callback = this.createNewFile.bind(this); | ||
97 | this.application.ninja.newFileController.showNewFileDialog(newFileSettings); | ||
98 | } | ||
99 | }, | ||
100 | |||
101 | |||
102 | //////////////////////////////////////////////////////////////////// | ||
103 | //TODO: Check for appropiate structures | ||
104 | handleExecuteSave: { | ||
105 | value: function(event) { | ||
106 | //Text and HTML document classes should return the same save object for fileSave | ||
107 | this.application.ninja.ioMediator.fileSave(this.activeDocument.save(), this.clearDocumentDirtyFlag.bind(this)); | ||
108 | } | ||
109 | }, | ||
110 | |||
111 | clearDocumentDirtyFlag:{ | ||
112 | value: function(){ | ||
113 | this.activeDocument.dirtyFlag = false; | ||
114 | } | ||
115 | }, | ||
116 | //////////////////////////////////////////////////////////////////// | ||
117 | |||
118 | |||
119 | createNewFile:{ | ||
120 | value:function(newFileObj){ | ||
121 | //console.log(newFileObj);//contains the template uri and the new file uri | ||
122 | if(!newFileObj) return; | ||
123 | this.application.ninja.ioMediator.fileNew(newFileObj.newFilePath, newFileObj.fileTemplateUri, this.openNewFileCallback.bind(this)); | ||
124 | |||
125 | if((newFileObj.fileExtension !== ".html") && (newFileObj.fileExtension !== ".htm")){//open code view | ||
126 | |||
127 | } else { | ||
128 | //open design view | ||
129 | } | ||
130 | } | ||
131 | }, | ||
132 | |||
133 | /** | ||
134 | * Public method | ||
135 | * doc contains: | ||
136 | * type : file type, like js, css, etc | ||
137 | * name : file name | ||
138 | * source : file content | ||
139 | * uri : file uri | ||
140 | */ | ||
141 | openNewFileCallback:{ | ||
142 | value:function(doc){ | ||
143 | var response = doc || null;//default just for testing | ||
144 | if(!!response && response.success && (response.status!== 500) && !!response.uri){ | ||
145 | this.application.ninja.ioMediator.fileOpen(response.uri, this.openFileCallback.bind(this)); | ||
146 | }else if(!!response && !response.success){ | ||
147 | //Todo: restrict directory path to the sandbox, in the dialog itself | ||
148 | alert("Unable to create file.\n [Error: Forbidden directory]"); | ||
149 | } | ||
150 | } | ||
151 | }, | ||
152 | |||
153 | openFileWithURI: { | ||
154 | value: function(uriArrayObj) { | ||
155 | var uri = "", fileContent = "", response=null, filename="", fileType="js"; | ||
156 | if(!!uriArrayObj && !!uriArrayObj.uri && (uriArrayObj.uri.length > 0)){ | ||
157 | uri = uriArrayObj.uri[0]; | ||
158 | } | ||
159 | //console.log("URI is: ", uri); | ||
160 | if(!!uri){ | ||
161 | this.application.ninja.ioMediator.fileOpen(uri, this.openFileCallback.bind(this)); | ||
162 | } | ||
163 | } | ||
164 | }, | ||
165 | |||
166 | //////////////////////////////////////////////////////////////////// | ||
167 | // | ||
168 | openFileCallback:{ | ||
169 | value:function(response){ | ||
170 | //TODO: Add UI to handle error codes, shouldn't be alert windows | ||
171 | if(!!response && (response.status === 204)) { | ||
172 | //Sending full response object | ||
173 | this.openDocument(response); | ||
174 | } else if (!!response && (response.status === 404)){ | ||
175 | alert("Unable to open file.\n [Error: File does not exist]"); | ||
176 | } else if (!!response && (response.status === 500)){ | ||
177 | alert("Unable to open file.\n Check if Ninja Local Cloud is running."); | ||
178 | } else{ | ||
179 | alert("Unable to open file."); | ||
180 | } | ||
181 | |||
182 | } | ||
183 | }, | ||
184 | //////////////////////////////////////////////////////////////////// | ||
185 | // | ||
186 | openDocument: { | ||
187 | value: function(doc) { | ||
188 | // | ||
189 | switch (doc.extension) { | ||
190 | case 'html': case 'html': | ||
191 | //Open in designer view | ||
192 | Montage.create(HTMLDocument).initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument); | ||
193 | break; | ||
194 | default: | ||
195 | //Open in code view | ||
196 | var code = Montage.create(TextDocument, {"source": {value: doc.content}}), docuuid = Uuid.generate(), textArea; | ||
197 | textArea = this.application.ninja.stage.stageView.createTextAreaElement(docuuid); | ||
198 | code.initialize(doc, docuuid, textArea, textArea.parentNode); | ||
199 | //code.init(doc.name, doc.uri, doc.extension, null, docuuid); | ||
200 | code.textArea.value = doc.content; | ||
201 | this.application.ninja.stage.stageView.createTextView(code); | ||
202 | break; | ||
203 | } | ||
204 | } | ||
205 | }, | ||
206 | //////////////////////////////////////////////////////////////////// | ||
207 | |||
208 | openProjectWithURI: { | ||
209 | value: function(uri) { | ||
210 | console.log("URI is: ", uri); | ||
211 | } | ||
212 | }, | ||
213 | |||
214 | textDocumentOpened: { | ||
215 | value: function(doc) { | ||
216 | |||
217 | |||
218 | |||
219 | this.application.ninja.stage.stageView.createTextView(doc); | ||
220 | |||
221 | /* | ||
222 |