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