diff options
Diffstat (limited to 'js/controllers/document-controller.js')
-rwxr-xr-x | js/controllers/document-controller.js | 412 |
1 files changed, 412 insertions, 0 deletions
diff --git a/js/controllers/document-controller.js b/js/controllers/document-controller.js new file mode 100755 index 00000000..7f4feff9 --- /dev/null +++ b/js/controllers/document-controller.js | |||
@@ -0,0 +1,412 @@ | |||
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 | var code = Montage.create(TextDocument, {"source": {value: doc.content}}), docuuid = Uuid.generate(), textArea; | ||
182 | textArea = this.application.ninja.stage.stageView.createTextAreaElement(docuuid); | ||
183 | code.initialize(doc, docuuid, textArea, textArea.parentNode); | ||
184 | code.init(doc.name, doc.uri, doc.extension, null, docuuid); | ||
185 | code.textArea.value = doc.content; | ||
186 | this.application.ninja.stage.stageView.createTextView(code); | ||
187 | break; | ||
188 | } | ||
189 | } | ||
190 | }, | ||
191 | //////////////////////////////////////////////////////////////////// | ||
192 | |||
193 | openProjectWithURI: { | ||
194 | value: function(uri) { | ||
195 | console.log("URI is: ", uri); | ||
196 | } | ||
197 | }, | ||
198 | |||
199 | textDocumentOpened: { | ||
200 | value: function(doc) { | ||
201 | |||
202 | |||
203 | |||
204 | this.application.ninja.stage.stageView.createTextView(doc); | ||
205 | |||
206 | /* | ||
207 | DocumentManager._hideCurrentDocument(); | ||
208 | stageManagerModule.stageManager._scrollFlag = false; // TODO HACK to prevent type error on Hide/Show Iframe | ||
209 | DocumentManager.activeDocument = doc; | ||
210 | |||
211 | var type; | ||
212 | |||
213 | switch(doc.documentType) { | ||
214 | case "css" : | ||
215 | type = "css"; | ||
216 | break; | ||
217 | case "js" : | ||
218 | type = "javascript"; | ||
219 | break; | ||
220 | } | ||
221 | |||
222 | DocumentManager._codeEditor.editor = CodeMirror.fromTextArea(doc.textArea, { | ||
223 | lineNumbers: true, | ||
224 | mode: type, | ||
225 | onCursorActivity: function() { | ||
226 | DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.hline, null); | ||
227 | DocumentManager._codeEditor.hline = DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.editor.getCursor().line, "activeline"); | ||
228 | } | ||
22 |