aboutsummaryrefslogtreecommitdiff
path: root/js/io/document/document-controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/document/document-controller.js')
-rwxr-xr-xjs/io/document/document-controller.js455
1 files changed, 455 insertions, 0 deletions
diff --git a/js/io/document/document-controller.js b/js/io/document/document-controller.js
new file mode 100755
index 00000000..6dc7e670
--- /dev/null
+++ b/js/io/document/document-controller.js
@@ -0,0 +1,455 @@
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@module js/document/documentManager
9@requires montage/core/core
10@requires montage/ui/component
11@requires js/document/html-document
12@requires js/document/text-document
13*/
14
15var Montage = require("montage/core/core").Montage,
16 Component = require("montage/ui/component").Component,
17 Uuid = require("montage/core/uuid").Uuid,
18 HTMLDocument = require("js/io/document/html-document").HTMLDocument,
19 TextDocument = require("js/io/document/text-document").TextDocument;
20
21var DocumentController = exports.DocumentController = Montage.create(Component, {
22 hasTemplate: {
23 value: false
24 },
25
26 _documents: {
27 value: []
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 tmpSourceForTesting: {
37 value: "function CodeMirror(place, givenOptions) {" +
38 "// Determine effective options based on given values and defaults." +
39 "var options = {}, defaults = CodeMirror.defaults; }"
40 },
41
42 activeDocument: {
43 get: function() {
44 return this._activeDocument;
45 },
46 set: function(doc) {
47 if(this._activeDocument) this._activeDocument.isActive = false;
48
49 if(this._documents.indexOf(doc) === -1) this._documents.push(doc);
50
51 this._activeDocument = doc;
52 this._activeDocument.isActive = true;
53
54 if(!!this._activeDocument.editor){
55 this._activeDocument.editor.focus();
56 }
57
58 }
59 },
60
61 deserializedFromTemplate: {
62 value: function() {
63 this.eventManager.addEventListener("appLoaded", this, false);
64 this.eventManager.addEventListener("executeFileOpen", this, false);
65 this.eventManager.addEventListener("executeNewFile", this, false);
66 this.eventManager.addEventListener("executeSave", this, false);
67
68 this.eventManager.addEventListener("recordStyleChanged", this, false);
69
70 // Temporary testing opening a new file after Ninja has loaded
71 this.eventManager.addEventListener("executeNewProject", this, false);
72 }
73 },
74
75 handleAppLoaded: {
76 value: function() {
77 //this.openDocument({"type": "html"});
78 }
79 },
80
81 handleExecuteNewProject: {
82 value: function() {
83 this.openDocument({"type": "html"});
84 }
85 },
86
87 handleExecuteFileOpen: {
88 value: function(event) {
89 var pickerSettings = event._event.settings || {};
90 pickerSettings.callback = this.openFileWithURI.bind(this);
91 pickerSettings.pickerMode = "read";
92 pickerSettings.inFileMode = true;
93 this.application.ninja.filePickerController.showFilePicker(pickerSettings);
94 }
95 },
96
97 handleExecuteNewFile: {
98 value: function(event) {
99 var newFileSettings = event._event.settings || {};
100 newFileSettings.callback = this.createNewFile.bind(this);
101 this.application.ninja.newFileController.showNewFileDialog(newFileSettings);
102 }
103 },
104
105 handleExecuteSave: {
106 value: function(event) {
107 this.activeDocument.save();
108 }
109 },
110
111 createNewFile:{
112 value:function(newFileObj){
113 //console.log(newFileObj);//contains the template uri and the new file uri
114 if(!newFileObj) return;
115 this.application.ninja.ioMediator.fileNew(newFileObj.newFilePath, newFileObj.fileTemplateUri, this.openNewFileCallback.bind(this));
116
117 if((newFileObj.fileExtension !== ".html") && (newFileObj.fileExtension !== ".htm")){//open code view
118
119 } else {
120 //open design view
121 }
122 }
123 },
124
125 /**
126 * Public method
127 * doc contains:
128 * type : file type, like js, css, etc
129 * name : file name
130 * source : file content
131 * uri : file uri
132 */
133 openNewFileCallback:{
134 value:function(doc){
135 var response = doc || null;//default just for testing
136 if(!!response && response.success && !!response.uri){
137 this.application.ninja.ioMediator.fileOpen(response.uri, this.openFileCallback.bind(this));
138 }
139 }
140 },
141
142 openFileWithURI: {
143 value: function(uriArrayObj) {
144 var uri = "", fileContent = "", response=null, filename="", fileType="js";
145 if(!!uriArrayObj && !!uriArrayObj.uri && (uriArrayObj.uri.length > 0)){
146 uri = uriArrayObj.uri[0];
147 }
148 //console.log("URI is: ", uri);
149 if(!!uri){
150 this.application.ninja.ioMediator.fileOpen(uri, this.openFileCallback.bind(this));
151 }
152 }
153 },
154
155 /**
156 * Public method
157 */
158 openFileCallback:{
159 value:function(response){
160 //Return Object Description
161// Object.status (Always presents for handling)
162// 204: File exists (Success)
163// 404: File does not exists (Failure)
164// 500: Unknown (Probably cloud API not running)
165//
166// (Below only present if succesfull 204)
167//
168// Object.content
169// Object.extension
170// Object.name
171// Object.uri
172// Object.creationDate
173// Object.modifiedDate
174// Object.readOnly
175// Object.size
176
177 var fileDetails = {};
178 if(!!response && (response.status === 204)){
179 /**
180 * fileDetails format:
181 * {"type": "js", "name": "test", "source": fileContent, "uri": uri}
182 */
183 fileDetails.type = response.extension;
184 fileDetails.source = response.content;
185 fileDetails.name = response.name;
186 fileDetails.uri = response.uri;
187
188 this.openDocument(fileDetails);
189 }else if(!!response && (response.status === 404)){
190 alert("Unable to open file.\n [Error: File does not exist]");
191 }else if(!!response && (response.status === 500)){
192 alert("Unable to open file.\n Check if Ninja Local Cloud is running.");
193 }else{
194 alert("Unable to open file.");
195 }
196
197 }
198 },
199
200 openProjectWithURI: {
201 value: function(uri) {
202 console.log("URI is: ", uri);
203
204 }
205 },
206
207 /** Open a Document **/
208 openDocument: {
209 value: function(doc) {
210 var newDoc;
211
212 if(!doc) return false;
213
214 // try {
215 if (doc.type === 'html' || doc.type === 'htm') {
216 newDoc = Montage.create(HTMLDocument);
217 newDoc.initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument);
218 } else {
219 newDoc = Montage.create(TextDocument, {
220 "source": { value: doc.source }
221 });
222 var docUuid = Uuid.generate();
223 var textArea = this.application.ninja.stage.stageView.createTextAreaElement(docUuid);
224 newDoc.initialize(doc, docUuid, textArea, textArea.parentNode);
225
226 // Tmp this will be filled with the real content
227 newDoc.textArea.value = doc.source; //this.tmpSourceForTesting;
228
229 this.textDocumentOpened(newDoc);
230
231 }