diff options
Diffstat (limited to 'js/controllers/document-controller.js')
-rwxr-xr-x | js/controllers/document-controller.js | 463 |
1 files changed, 463 insertions, 0 deletions
diff --git a/js/controllers/document-controller.js b/js/controllers/document-controller.js new file mode 100755 index 00000000..1f339fe7 --- /dev/null +++ b/js/controllers/document-controller.js | |||
@@ -0,0 +1,463 @@ | |||
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 | handleWebRequest: { | ||
70 | value: function (request) { | ||
71 | if (request.url.indexOf('js/document/templates/montage-html') !== -1) { | ||
72 | |||
73 | console.log(request); | ||
74 | |||
75 | //TODO: Figure out why active document is not available here | ||
76 | |||
77 | if (this._hackRootFlag) { | ||
78 | |||
79 | //console.log(request.url.split('/')[request.url.split('/').length-1]); | ||
80 | //console.log(this.application.ninja.coreIoApi.rootUrl+this.application.ninja.documentController._activeDocument.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+request.url.split('/')[request.url.split('/').length-1]); | ||
81 | |||
82 | 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]}; | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | }, | ||
87 | |||
88 | |||
89 | handleAppLoaded: { | ||
90 | value: function() { | ||
91 | // | ||
92 | |||
93 | chrome.webRequest.onBeforeRequest.addListener(this.handleWebRequest.bind(this), {urls: ["<all_urls>"]}, ["blocking"]); | ||
94 | |||
95 | } | ||
96 | }, | ||
97 | |||
98 | handleExecuteFileOpen: { | ||
99 | value: function(event) { | ||
100 | var pickerSettings = event._event.settings || {}; | ||
101 | pickerSettings.callback = this.openFileWithURI.bind(this); | ||
102 | pickerSettings.pickerMode = "read"; | ||
103 | pickerSettings.inFileMode = true; | ||
104 | this.application.ninja.filePickerController.showFilePicker(pickerSettings); | ||
105 | } | ||
106 | }, | ||
107 | |||
108 | handleExecuteNewFile: { | ||
109 | value: function(event) { | ||
110 | var newFileSettings = event._event.settings || {}; | ||
111 | newFileSettings.callback = this.createNewFile.bind(this); | ||
112 | this.application.ninja.newFileController.showNewFileDialog(newFileSettings); | ||
113 | } | ||
114 | }, | ||
115 | |||
116 | |||
117 | //////////////////////////////////////////////////////////////////// | ||
118 | //TODO: Check for appropiate structures | ||
119 | handleExecuteSave: { | ||
120 | value: function(event) { | ||
121 | //Text and HTML document classes should return the same save object for fileSave | ||
122 | this.application.ninja.ioMediator.fileSave(this.activeDocument.save(), this.fileSaveResult.bind(this)); | ||
123 | } | ||
124 | }, | ||
125 | //////////////////////////////////////////////////////////////////// | ||
126 | // | ||
127 | fileSaveResult: { | ||
128 | value: function (result) { | ||
129 | if(result.status === 204){ | ||
130 | this.clearDocumentDirtyFlag(); | ||
131 | } | ||
132 | } | ||
133 | }, | ||
134 | //////////////////////////////////////////////////////////////////// | ||
135 | |||
136 | |||
137 | clearDocumentDirtyFlag:{ | ||
138 | value: function(){ | ||
139 | this.activeDocument.dirtyFlag = false; | ||
140 | } | ||
141 | }, | ||
142 | |||
143 | |||
144 | createNewFile:{ | ||
145 | value:function(newFileObj){ | ||
146 | //console.log(newFileObj);//contains the template uri and the new file uri | ||
147 | if(!newFileObj) return; | ||
148 | this.application.ninja.ioMediator.fileNew(newFileObj.newFilePath, newFileObj.fileTemplateUri, this.openNewFileCallback.bind(this)); | ||
149 | |||
150 | if((newFileObj.fileExtension !== ".html") && (newFileObj.fileExtension !== ".htm")){//open code view | ||
151 | |||
152 | } else { | ||
153 | //open design view | ||
154 | } | ||
155 | } | ||
156 | }, | ||
157 | |||
158 | /** | ||
159 | * Public method | ||
160 | * doc contains: | ||
161 | * type : file type, like js, css, etc | ||
162 | * name : file name | ||
163 | * source : file content | ||
164 | * uri : file uri | ||
165 | */ | ||
166 | openNewFileCallback:{ | ||
167 | value:function(doc){ | ||
168 | var response = doc || null;//default just for testing | ||
169 | if(!!response && response.success && (response.status!== 500) && !!response.uri){ | ||
170 | this.application.ninja.ioMediator.fileOpen(response.uri, this.openFileCallback.bind(this)); | ||
171 | }else if(!!response && !response.success){ | ||
172 | //Todo: restrict directory path to the sandbox, in the dialog itself | ||
173 | alert("Unable to create file.\n [Error: Forbidden directory]"); | ||
174 | } | ||
175 | } | ||
176 | }, | ||
177 | |||
178 | openFileWithURI: { | ||
179 | value: function(uriArrayObj) { | ||
180 | var uri = "", fileContent = "", response=null, filename="", fileType="js"; | ||
181 | if(!!uriArrayObj && !!uriArrayObj.uri && (uriArrayObj.uri.length > 0)){ | ||
182 | uri = uriArrayObj.uri[0]; | ||
183 | } | ||
184 | //console.log("URI is: ", uri); | ||
185 | if(!!uri){ | ||
186 | this.application.ninja.ioMediator.fileOpen(uri, this.openFileCallback.bind(this)); | ||
187 | } | ||
188 | } | ||
189 | }, | ||
190 | |||
191 | //////////////////////////////////////////////////////////////////// | ||
192 | // | ||
193 | openFileCallback:{ | ||
194 | value:function(response){ | ||
195 | //TODO: Add UI to handle error codes, shouldn't be alert windows | ||
196 | if(!!response && (response.status === 204)) { | ||
197 | //Sending full response object | ||
198 | this.openDocument(response); | ||
199 | } else if (!!response && (response.status === 404)){ | ||
200 | alert("Unable to open file.\n [Error: File does not exist]"); | ||
201 | } else if (!!response && (response.status === 500)){ | ||
202 | alert("Unable to open file.\n Check if Ninja Local Cloud is running."); | ||
203 | } else{ | ||
204 | alert("Unable to open file."); | ||
205 | } | ||
206 | |||
207 | } | ||
208 | }, | ||
209 | //////////////////////////////////////////////////////////////////// | ||
210 | // | ||
211 | openDocument: { | ||
212 | value: function(doc) { | ||
213 | |||
214 | // | ||
215 | this.documentHackReference = doc; | ||
216 | // | ||
217 | switch (doc.extension) { | ||
218 | case 'html': case 'html': | ||
219 | //Open in designer view | ||
220 | Montage.create(HTMLDocument).initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument.bind(this)); | ||
221 | break; | ||
222 | default: | ||
223 | //Open in code view | ||
224 | var code = Montage.create(TextDocument, {"source": {value: doc.content}}), docuuid = Uuid.generate(), textArea; | ||
225 | textArea = this.application.ninja.stage.stageView.createTextAreaElement(docuuid); | ||
226 | code.initialize(doc, docuuid, textArea, textArea.parentNode); | ||
227 | //code.init(doc.name, doc.uri, doc.extension, null, docuuid); | ||
228 | code.textArea.value = |