aboutsummaryrefslogtreecommitdiff
path: root/js/io/document/document-controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/document/document-controller.js')
-rw-r--r--js/io/document/document-controller.js326
1 files changed, 326 insertions, 0 deletions
diff --git a/js/io/document/document-controller.js b/js/io/document/document-controller.js
new file mode 100644
index 00000000..99177de0
--- /dev/null
+++ b/js/io/document/document-controller.js
@@ -0,0 +1,326 @@
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
15// TODO : Fix deps from Montage V4 Archi
16
17var Montage = require("montage/core/core").Montage,
18 Component = require("montage/ui/component").Component,
19 Uuid = require("montage/core/uuid").Uuid;
20
21var HTMLDocument = require("js/io/document/html-document").HTMLDocument;
22var TextDocument = require("js/io/document/text-document").TextDocument;
23
24var DocumentController = exports.DocumentController = Montage.create(Component, {
25 hasTemplate: { value: false },
26
27 _documents: { value: [] },
28 _documentsHash: { value: {} },
29 _activeDocument: { value: null },
30 _iframeCounter: { value: 1, enumerable: false },
31 _iframeHolder: { value: null, enumerable: false },
32 _textHolder: { value: null, enumerable: false },
33 _codeMirrorCounter: {value: 1, enumerable: false},
34
35 _codeEditor: {
36 value: {
37 "editor": {
38 value: null,
39 enumerable: false
40 },
41 "hline": {
42 value: null,
43 enumerable: false
44 }
45 }
46 },
47
48 activeDocument: {
49 get: function() {
50 return this._activeDocument;
51 },
52 set: function(doc) {
53 if(this._activeDocument) {
54 if(this.activeDocument.documentType === "htm" || this.activeDocument.documentType === "html") {
55 // TODO selection should use the document own selectionModel
56 //this._activeDocument.selectionModel = selectionManagerModule.selectionManager._selectedItems;
57 }
58
59 this._activeDocument.isActive = false;
60 }
61
62 if(this._documents.indexOf(doc) === -1) {
63 //this._documentsHash[doc.uuid] = this._documents.push(doc) - 1;
64 this._documents.push(doc);
65 }
66
67 this._activeDocument = doc;
68 this._activeDocument.isActive = true;
69
70 if(this.activeDocument.documentType === "htm" || this.activeDocument.documentType === "html") {
71 // TODO selection should use the document own selectionModel
72 //selectionManagerModule.selectionManager._selectedItems = this._activeDocument.selectionModel;
73 }
74 }
75 },
76
77 deserializedFromTemplate: {
78 value: function() {
79 this.eventManager.addEventListener("appLoaded", this, false);
80 }
81 },
82
83 handleAppLoaded: {
84 value: function() {
85 this.openDocument({"type": "html"});
86 }
87 },
88
89 /** Open a Document **/
90 openDocument: {
91 value: function(doc) {
92 var d;
93
94 if(!doc) return false;
95
96 try {
97 if (doc.type === 'html' || doc.type === 'htm') {
98 d = Montage.create(HTMLDocument);
99 d.initialize(doc, Uuid.generate(), this._createIframeElement(), this._onOpenDocument);
100 } else {
101 d = Montage.create(TextDocument);
102 d.initialize(doc, Uuid.generate(), this._createTextAreaElement(), this._onOpenTextDocument);
103 }
104
105 } catch (err) {
106 console.log("Could not open Document ", err);
107 }
108 }
109 },
110
111 closeDocument: {
112 value: function(id) {
113 var doc = this._findDocumentByUUID(id);
114 this._removeDocumentView(doc.container);
115
116 this._documents.splice(this._findIndexByUUID(id), 1);
117
118 if(this.activeDocument.uuid === id && this._documents.length > 0) {
119 this.switchDocument(this._documents[0].uuid)
120 }
121 }
122 },
123
124 switchDocument: {
125 value: function(id) {
126 this._hideCurrentDocument();
127 this.activeDocument = this._findDocumentByUUID(id);
128
129 this.application.ninja.stage._scrollFlag = false; // TODO HACK to prevent type error on Hide/Show Iframe
130 this._showCurrentDocument();
131
132 if(this.activeDocument.documentType === "htm" || this.activeDocument.documentType === "html") {
133 this.application.ninja.stage._scrollFlag = true; // TODO HACK to prevent type error on Hide/Show Iframe
134 // TODO dispatch event here
135// appDelegateModule.MyAppDelegate.onSetActiveDocument();
136 }
137 }
138 },
139
140 switchViews: {
141 value: function() {
142 this.application.ninja.stage.saveScroll();
143 this._hideCurrentDocument();
144
145 if(this.activeDocument.currentView === "design") {
146 this._textHolder.style.display = "none";
147 this.activeDocument.container.style["display"] = "block";
148 this.application.ninja.stage._scrollFlag = true;
149 //this._showCurrentDocument();
150 this.application.ninja.stage.restoreScroll();
151
152 } else {
153 this.application.ninja.stage._scrollFlag = false; // TODO HACK to prevent type error on Hide/Show Iframe
154 var codeview = this._createTextAreaElement();
155 this._textHolder.style.display = "block";
156 codeview.firstChild.innerHTML = this.activeDocument.iframe.contentWindow.document.body.parentNode.innerHTML;
157
158 this._codeEditor.editor = CodeMirror.fromTextArea(codeview.firstChild, {
159 lineNumbers: true,
160 mode: "htmlmixed",
161 onCursorActivity: function() {
162 DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.hline, null);
163 DocumentManager._codeEditor.hline = DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.editor.getCursor().line, "activeline");
164 }
165 });
166 this._codeEditor.hline = DocumentManager._codeEditor.editor.setLineClass(0, "activeline");
167 }
168 }
169 },
170
171 // Document has been loaded into the Iframe. Dispatch the event.
172 // Event Detail: Contains the current ActiveDocument
173 _onOpenDocument: {
174 value: function(doc){
175 //var data = DocumentManager.activeDocument;
176 //DocumentManager._hideCurrentDocument();
177
178 //stageManagerModule.stageManager.toggleCanvas();
179
180 DocumentController.activeDocument = doc;
181
182 NJevent("onOpenDocument", doc);
183// appDelegateModule.MyAppDelegate.onSetActiveDocument();
184
185 }
186 },
187
188 _onOpenTextDocument: {
189 value: function(doc) {
190 DocumentManager._hideCurrentDocument();
191 this.application.ninja.stage._scrollFlag = false; // TODO HACK to prevent type error on Hide/Show Iframe
192 DocumentManager.activeDocument = doc;
193
194 var type;
195
196 switch(doc.documentType) {
197 case "css" :
198 type = "css";
199 break;
200 case "js" :
201 type = "javascript";
202 break;
203 }
204
205 DocumentManager._codeEditor.editor = CodeMirror.fromTextArea(doc.textArea, {
206 lineNumbers: true,
207 mode: type,
208 onCursorActivity: function() {
209 DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.hline, null);
210 DocumentManager._codeEditor.hline = DocumentManager._codeEditor.editor.setLineClass(DocumentManager._codeEditor.editor.getCursor().line, "activeline");
211 }
212 });
213 DocumentManager._codeEditor.hline = DocumentManager._codeEditor.editor.setLineClass(0, "activeline");
214
215 }
216 },
217
218 /**
219 * VIEW Related Methods
220 */
221 // PUBLIC
222 ShowActiveDocument: {
223 value: function() {
224 this.activeDocument.iframe.style.opacity = 1.0;
225 }
226 },
227
228 // PRIVATE
229 _findDocumentByUUID: {
230 value: function(uuid) {
231 var len = this._documents.length;
232