diff options
Diffstat (limited to 'js/controllers/document-controller.js')
-rwxr-xr-x | js/controllers/document-controller.js | 443 |
1 files changed, 443 insertions, 0 deletions
diff --git a/js/controllers/document-controller.js b/js/controllers/document-controller.js new file mode 100755 index 00000000..a6228a55 --- /dev/null +++ b/js/controllers/document-controller.js | |||
@@ -0,0 +1,443 @@ | |||
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 | handleExecuteSave: { | ||
102 | value: function(event) { | ||
103 | this.activeDocument.save(); | ||
104 | } | ||
105 | }, | ||
106 | |||
107 | createNewFile:{ | ||
108 | value:function(newFileObj){ | ||
109 | //console.log(newFileObj);//contains the template uri and the new file uri | ||
110 | if(!newFileObj) return; | ||
111 | this.application.ninja.ioMediator.fileNew(newFileObj.newFilePath, newFileObj.fileTemplateUri, this.openNewFileCallback.bind(this)); | ||
112 | |||
113 | if((newFileObj.fileExtension !== ".html") && (newFileObj.fileExtension !== ".htm")){//open code view | ||
114 | |||
115 | } else { | ||
116 | //open design view | ||
117 | } | ||
118 | } | ||
119 | }, | ||
120 | |||
121 | /** | ||
122 | * Public method | ||
123 | * doc contains: | ||
124 | * type : file type, like js, css, etc | ||
125 | * name : file name | ||
126 | * source : file content | ||
127 | * uri : file uri | ||
128 | */ | ||
129 | openNewFileCallback:{ | ||
130 | value:function(doc){ | ||
131 | var response = doc || null;//default just for testing | ||
132 | if(!!response && response.success && !!response.uri){ | ||
133 | this.application.ninja.ioMediator.fileOpen(response.uri, this.openFileCallback.bind(this)); | ||
134 | } | ||
135 | } | ||
136 | }, | ||
137 | |||
138 | openFileWithURI: { | ||
139 | value: function(uriArrayObj) { | ||
140 | var uri = "", fileContent = "", response=null, filename="", fileType="js"; | ||
141 | if(!!uriArrayObj && !!uriArrayObj.uri && (uriArrayObj.uri.length > 0)){ | ||
142 | uri = uriArrayObj.uri[0]; | ||
143 | } | ||
144 | //console.log("URI is: ", uri); | ||
145 | if(!!uri){ | ||
146 | this.application.ninja.ioMediator.fileOpen(uri, this.openFileCallback.bind(this)); | ||
147 | } | ||
148 | } | ||
149 | }, | ||
150 | |||
151 | //////////////////////////////////////////////////////////////////// | ||
152 | // | ||
153 | openFileCallback:{ | ||
154 | value:function(response){ | ||
155 | //TODO: Add UI to handle error codes, shouldn't be alert windows | ||
156 | if(!!response && (response.status === 204)) { | ||
157 | //Sending full response object | ||
158 | this.openDocument(response); | ||
159 | } else if (!!response && (response.status === 404)){ | ||
160 | alert("Unable to open file.\n [Error: File does not exist]"); | ||
161 | } else if (!!response && (response.status === 500)){ | ||
162 | alert("Unable to open file.\n Check if Ninja Local Cloud is running."); | ||
163 | } else{ | ||
164 | alert("Unable to open file."); | ||
165 | } | ||
166 | |||
167 | } | ||
168 | }, | ||
169 | //////////////////////////////////////////////////////////////////// | ||
170 | // | ||
171 | openDocument: { | ||
172 | value: function(doc) { | ||
173 | // | ||
174 | switch (doc.extension) { | ||
175 | case 'html': case 'html': | ||
176 | //Open in designer view | ||
177 | Montage.create(HTMLDocument).initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument); | ||
178 | break; | ||
179 | default: | ||
180 | //Open in code view | ||
181 | break; | ||
182 | } | ||
183 | |||
184 | return; | ||
185 | |||
186 | |||
187 | |||
188 | |||
189 | |||
190 | var newDoc; | ||
191 | |||
192 | if(!doc) return false; | ||
193 | |||
194 | // try { | ||
195 | if (doc.type === 'html' || doc.type === 'htm') { | ||
196 | console.log('hello'); | ||
197 | newDoc = Montage.create(HTMLDocument); | ||
198 | newDoc.initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument); | ||
199 | } else { | ||
200 | newDoc = Montage.create(TextDocument, { | ||
201 | "source": { value: doc.source } | ||
202 | }); | ||
203 | var docUuid = Uuid.generate(); | ||
204 | var textArea = this.application.ninja.stage.stageView.createTextAreaElement(docUuid); | ||
205 | newDoc.initialize(doc, docUuid, textArea, textArea.parentNode); | ||
206 | |||
207 | // Tmp this will be filled with the real content | ||
208 | newDoc.textArea.value = doc.source; //this.tmpSourceForTesting; | ||
209 | |||
210 | this.textDocumentOpened(newDoc); | ||
211 | |||
212 | } | ||
213 | |||
214 | // } catch (err) { | ||
215 | // console.log("Could not open Document ", err); | ||
216 | // } | ||
217 | } | ||
218 | }, | ||
219 | //////////////////////////////////////////////////////////////////// | ||
220 | |||
221 | openProjectWithURI: { | ||
222 | value: function(uri) { | ||
223 | console.log("URI is: ", uri); | ||
224 | } | ||
225 | }, | ||
226 | |||
227 | textDocumentOpened: { | ||
228 | value: function(doc) { | ||
229 | |||
230 | |||