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