diff options
Diffstat (limited to 'js/controllers/document-controller.js')
-rwxr-xr-x | js/controllers/document-controller.js | 467 |
1 files changed, 467 insertions, 0 deletions
diff --git a/js/controllers/document-controller.js b/js/controllers/document-controller.js new file mode 100755 index 00000000..1c9d9d59 --- /dev/null +++ b/js/controllers/document-controller.js | |||
@@ -0,0 +1,467 @@ | |||
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/document/html-document").HTMLDocument, | ||
13 | TextDocument = require("js/document/text-document").TextDocument, | ||
14 | DocumentController; | ||
15 | //////////////////////////////////////////////////////////////////////// | ||
16 | // | ||
17 | var DocumentController = exports.DocumentController = Montage.create(Component, { | ||
18 | hasTemplate: { | ||
19 | value: false | ||
20 | }, | ||
21 | |||
22 | _documents: { | ||
23 | value: [] | ||
24 | }, | ||
25 | |||
26 | _hackRootFlag: { | ||
27 | value: false | ||
28 | }, | ||
29 | |||
30 | _activeDocument: { value: null }, | ||
31 | _iframeCounter: { value: 1, enumerable: false }, | ||
32 | _iframeHolder: { value: null, enumerable: false }, | ||
33 | _textHolder: { value: null, enumerable: false }, | ||
34 | _codeMirrorCounter: {value: 1, enumerable: false}, | ||
35 | |||
36 | activeDocument: { | ||
37 | get: function() { | ||
38 | return this._activeDocument; | ||
39 | }, | ||
40 | set: function(doc) { | ||
41 | if(!!this._activeDocument) this._activeDocument.isActive = false; | ||
42 | |||
43 | this._activeDocument = doc; | ||
44 | if(!!this._activeDocument){ | ||
45 | |||
46 | if(this._documents.indexOf(doc) === -1) this._documents.push(doc); | ||
47 | this._activeDocument.isActive = true; | ||
48 | if(!!this._activeDocument.editor){ | ||
49 | this._activeDocument.editor.focus(); | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | }, | ||
54 | |||
55 | deserializedFromTemplate: { | ||
56 | value: function() { | ||
57 | this.eventManager.addEventListener("appLoaded", this, false); | ||
58 | this.eventManager.addEventListener("executeFileOpen", this, false); | ||
59 | this.eventManager.addEventListener("executeNewFile", this, false); | ||
60 | this.eventManager.addEventListener("executeSave", this, false); | ||
61 | |||
62 | this.eventManager.addEventListener("recordStyleChanged", this, false); | ||
63 | |||
64 | } | ||
65 | }, | ||
66 | |||
67 | |||
68 | |||
69 | |||
70 | |||
71 | |||
72 | |||
73 | //////////////////////////////////////////////////////////////////// | ||
74 | // | ||
75 | handleWebRequest: { | ||
76 | value: function (request) { | ||
77 | if (this._hackRootFlag && request.url.indexOf('js/document/templates/montage-html') !== -1) { | ||
78 | //TODO: Optimize creating string | ||
79 | return {redirectUrl: this.application.ninja.coreIoApi.rootUrl+this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+request.url.split('/')[request.url.split('/').length-1]}; | ||
80 | } | ||
81 | } | ||
82 | }, | ||
83 | //////////////////////////////////////////////////////////////////// | ||
84 | // | ||
85 | handleAppLoaded: { | ||
86 | value: function() { | ||
87 | //Checking for app to be loaded through extension | ||
88 | var check; | ||
89 | if (chrome && chrome.app) { | ||
90 | check = chrome.app.getDetails(); | ||
91 | } | ||
92 | if (check !== null) { | ||
93 | //Adding an intercept to resources loaded to ensure user assets load from cloud simulator | ||
94 | chrome.webRequest.onBeforeRequest.addListener(this.handleWebRequest.bind(this), {urls: ["<all_urls>"]}, ["blocking"]); | ||
95 | } | ||
96 | } | ||
97 | }, | ||
98 | //////////////////////////////////////////////////////////////////// | ||
99 | |||
100 | |||
101 | |||
102 | |||
103 | |||
104 | |||
105 | |||
106 | |||
107 | handleExecuteFileOpen: { | ||
108 | value: function(event) { | ||
109 | var pickerSettings = event._event.settings || {}; | ||
110 | pickerSettings.callback = this.openFileWithURI.bind(this); | ||
111 | pickerSettings.pickerMode = "read"; | ||
112 | pickerSettings.inFileMode = true; | ||
113 | this.application.ninja.filePickerController.showFilePicker(pickerSettings); | ||
114 | } | ||
115 | }, | ||
116 | |||
117 | handleExecuteNewFile: { | ||
118 | value: function(event) { | ||
119 | var newFileSettings = event._event.settings || {}; | ||
120 | newFileSettings.callback = this.createNewFile.bind(this); | ||
121 | this.application.ninja.newFileController.showNewFileDialog(newFileSettings); | ||
122 | } | ||
123 | }, | ||
124 | |||
125 | |||
126 | //////////////////////////////////////////////////////////////////// | ||
127 | //TODO: Check for appropiate structures | ||
128 | handleExecuteSave: { | ||
129 | value: function(event) { | ||
130 | if(!!this.activeDocument){ | ||
131 | //Text and HTML document classes should return the same save object for fileSave | ||
132 | this.application.ninja.ioMediator.fileSave(this.activeDocument.save(), this.fileSaveResult.bind(this)); | ||
133 | } | ||
134 | } | ||
135 | }, | ||
136 | //////////////////////////////////////////////////////////////////// | ||
137 | // | ||
138 | fileSaveResult: { | ||
139 | value: function (result) { | ||
140 | if(result.status === 204){ | ||
141 | this.activeDocument.needsSave = false; | ||
142 | } | ||
143 | } | ||
144 | }, | ||
145 | |||
146 | createNewFile:{ | ||
147 | value:function(newFileObj){ | ||
148 | //console.log(newFileObj);//contains the template uri and the new file uri | ||
149 | if(!newFileObj) return; | ||
150 | this.application.ninja.ioMediator.fileNew(newFileObj.newFilePath, newFileObj.fileTemplateUri, this.openNewFileCallback.bind(this)); | ||
151 | |||
152 | if((newFileObj.fileExtension !== ".html") && (newFileObj.fileExtension !== ".htm")){//open code view | ||
153 | |||
154 | } else { | ||
155 | //open design view | ||
156 | } | ||
157 | } | ||
158 | }, | ||
159 | |||
160 | /** | ||
161 | * Public method | ||
162 | * doc contains: | ||
163 | * type : file type, like js, css, etc | ||
164 | * name : file name | ||
165 | * source : file content | ||
166 | * uri : file uri | ||
167 | */ | ||
168 | openNewFileCallback:{ | ||
169 | value:function(doc){ | ||
170 | var response = doc || null;//default just for testing | ||
171 | if(!!response && response.success && (response.status!== 500) && !!response.uri){ | ||
172 | this.application.ninja.ioMediator.fileOpen(response.uri, this.openFileCallback.bind(this)); | ||
173 | }else if(!!response && !response.success){ | ||
174 | //Todo: restrict directory path to the sandbox, in the dialog itself | ||
175 | alert("Unable to create file.\n [Error: Forbidden directory]"); | ||
176 | } | ||
177 | } | ||
178 | }, | ||
179 | |||
180 | openFileWithURI: { | ||
181 | value: function(uriArrayObj) { | ||
182 | var uri = "", fileContent = "", response=null, filename="", fileType="js"; | ||
183 | if(!!uriArrayObj && !!uriArrayObj.uri && (uriArrayObj.uri.length > 0)){ | ||
184 | uri = uriArrayObj.uri[0]; | ||
185 | } | ||
186 | //console.log("URI is: ", uri); | ||
187 | if(!!uri){ | ||
188 | this.application.ninja.ioMediator.fileOpen(uri, this.openFileCallback.bind(this)); | ||
189 | } | ||
190 | } | ||
191 | }, | ||
192 | |||
193 | //////////////////////////////////////////////////////////////////// | ||
194 | // | ||
195 | openFileCallback:{ | ||
196 | value:function(response){ | ||
197 | //TODO: Add UI to handle error codes, shouldn't be alert windows | ||
198 | if(!!response && (response.status === 204)) { | ||
199 | //Sending full response object | ||
200 | this.openDocument(response); | ||
201 | } else if (!!response && (response.status === 404)){ | ||
202 | alert("Unable to open file.\n [Error: File does not exist]"); | ||
203 | } else if (!!response && (response.status === 500)){ | ||
204 | alert("Unable to open file.\n Check if Ninja Local Cloud is running."); | ||
205 | } else{ | ||
206 | alert("Unable to open file."); | ||
207 | } | ||
208 | |||
209 | } | ||
210 | }, | ||
211 | //////////////////////////////////////////////////////////////////// | ||
212 | // | ||
213 | openDocument: { | ||
214 | value: function(doc) { | ||
215 | |||
216 | // | ||
217 | this.documentHackReference = doc; | ||
218 | // | ||
219 | switch (doc.extension) { | ||
220 | case 'html': case 'html': | ||
221 | //Open in designer view | ||
222 | Montage.create(HTMLDocument).initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument.bind(this)); | ||
223 | break; | ||
224 | default: | ||
225 | //Open in code view | ||
226 | var code = Montage.create(TextDocument, {"source": {value: doc.content}}), docuuid = Uuid.generate(), textArea; | ||
227 | textArea = this.application.ninja.stage.stageView.createTextAreaElement(docuuid); | ||
228 | code.initialize(doc, docuuid, textArea, textArea.parentNode); | ||
229 | //code.init(doc.name, doc.uri, doc.extension, null, docuuid); | ||