aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
authorPushkar Joshi2012-02-24 12:08:49 -0800
committerPushkar Joshi2012-02-24 12:08:49 -0800
commit03ca7a5ed13c25faaa9100bb666e062fd15335e6 (patch)
treec51112223ceb9121cd595a60335eb2795215590f /js/mediators/io-mediator.js
parentfcb12cc09eb3cd3b42bd215877ba18f449275b75 (diff)
parent053fc63a2950c7a5ee4ebf98033b64d474a3c46e (diff)
downloadninja-03ca7a5ed13c25faaa9100bb666e062fd15335e6.tar.gz
Merge branch 'pentool' into brushtool
Conflicts: imports/codemirror/mode/scheme/scheme.js js/tools/BrushTool.js
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js237
1 files changed, 237 insertions, 0 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
new file mode 100644
index 00000000..56869839
--- /dev/null
+++ b/js/mediators/io-mediator.js
@@ -0,0 +1,237 @@
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 FileIo = require("js/io/system/fileio").FileIo,
12 ProjectIo = require("js/io/system/projectio").ProjectIo;
13////////////////////////////////////////////////////////////////////////
14//
15exports.IoMediator = Montage.create(Component, {
16 ////////////////////////////////////////////////////////////////////
17 //
18 hasTemplate: {
19 enumerable: false,
20 value: false
21 },
22 ////////////////////////////////////////////////////////////////////
23 //
24 deserializedFromTemplate: {
25 enumerable: false,
26 value: function () {
27 //
28 }
29 },
30 ////////////////////////////////////////////////////////////////////
31 //
32 fio: {
33 enumerable: false,
34 value: FileIo
35 },
36 ////////////////////////////////////////////////////////////////////
37 //
38 pio: {
39 enumerable: false,
40 value: ProjectIo
41 },
42 ////////////////////////////////////////////////////////////////////
43 //
44 fileNew: {
45 enumerable: false,
46 value: function (file, template, callback) {
47 //Loading template from template URL
48 var xhr = new XMLHttpRequest(), result;
49 xhr.open("GET", template, false);
50 xhr.send();
51 if (xhr.readyState === 4) {
52 //Making call to create file, checking for return code
53 switch (this.fio.newFile({uri: file, contents: xhr.response})) {
54 case 201:
55 result = {status: 201, success: true, uri: file};
56 break;
57 case 204:
58 result = {status: 204, success: false, uri: file};
59 break;
60 case 400:
61 result = {status: 400, success: false, uri: file};
62 break;
63 default:
64 result = {status: 500, success: false, uri: file};
65 break;
66 }
67 } else {
68 result = {status: 500, success: false, uri: file};
69 }
70 //Sending result to callback if requested for handling
71 if (callback) callback(result);
72 //Codes
73 // 204: File exists | 400: File exists
74 // 201: File succesfully created | 500: Unknown (Probably cloud API not running)
75 }
76 },
77 ////////////////////////////////////////////////////////////////////
78 //
79 fileOpen: {
80 enumerable: false,
81 value: function (file, callback) {
82 //Reading file (Ninja doesn't really open a file, all in browser memory)
83 var read = this.fio.readFile({uri: file}), result;
84 //Checking for status
85 switch(read.status) {
86 case 204:
87 //Creating and formatting result object for callbak
88 result = read.file.details;
89 result.root = read.file.details.uri.replace(read.file.details.name, "");
90 //Checking for type of content to returns
91 if (result.extension !== 'html' && result.extension !== 'htm') {
92 //Simple string
93 result.content = read.file.content;
94 } else {
95 //Object to be used by Ninja Template
96 result.content = this.parseHtmlToNinjaTemplate(read.file.content);
97 }
98 //Status of call
99 result.status = read.status;
100 //Calling back with result
101 if (callback) callback(result);
102 break;
103 case 404:
104 //File does not exists
105 if (callback) callback({status: read.status});
106 break;
107 default:
108 //Unknown
109 if (callback) callback({status: 500});
110 break;
111 }
112 /*
113 ////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////
115 //Return Object Description
116 Object.status (Always presents for handling)
117 204: File exists (Success)
118 404: File does not exists (Failure)
119 500: Unknown (Probably cloud API not running)
120
121 (Below only present if succesfull 204)
122
123 Object.content
124 Object.extension
125 Object.name
126 Object.uri
127 Object.creationDate
128 Object.modifiedDate
129 Object.readOnly
130 Object.size
131 ////////////////////////////////////////////////////////////
132 ////////////////////////////////////////////////////////////
133 */
134 }
135 },
136 ////////////////////////////////////////////////////////////////////
137 //
138 fileSave: {
139 enumerable: false,
140 value: function (file, callback) {
141 //
142 var contents, save;
143 //
144 switch (file.mode) {
145 case 'html':
146 //Copy webGL library if needed
147 if (file.webgl.length > 0) {
148 for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) {
149 //if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'Assets' || this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') {
150 if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') {
151 this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(file.document.root, (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase());
152 }
153 }
154 }
155 //
156 contents = this.parseNinjaTemplateToHtml(file);
157 break;
158 default:
159 contents = file.content;
160 break;
161 }
162 //
163 save = this.fio.saveFile({uri: file.document.uri, contents: contents});
164 //
165 if (callback) callback(save);
166 }
167 },
168 ////////////////////////////////////////////////////////////////////
169 //
170 fileSaveAs: {
171 enumerable: false,
172 value: function (copyTo, copyFrom, callback) {
173 //
174 }
175 },
176 ////////////////////////////////////////////////////////////////////
177 //
178 fileDelete: {
179 enumerable: false,
180 value: function (file, callback) {
181 //
182 }
183 },
184 ////////////////////////////////////////////////////////////////////
185 //
186 parseHtmlToNinjaTemplate: {
187 enumerable: false,
188 value: function (html) {
189 //Creating temp object to mimic HTML
190 var doc = window.document.implementation.createHTMLDocument(), template;
191 //Setting content to temp
192 doc.getElementsByTagName('html')[0].innerHTML = html;
193 //Creating return object
194 return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc};
195 }
196 },
197 ////////////////////////////////////////////////////////////////////
198 //TODO: Expand to allow more templates
199 parseNinjaTemplateToHtml: {
200 enumerable: false,
201 value: function (template) {
202 //
203 template.document.content.document.body.innerHTML = template.body;
204 template.document.content.document.head.innerHTML = template.head;
205 //TODO: Remove temp fix for styles
206 if (template.style) {
207 template.document.content.document.head.getElementsByTagName('style')[0].innerHTML = this.getCssFromRules(template.style.cssRules);
208 }
209 return template.document.content.document.documentElement.outerHTML;
210 }
211 },
212 ////////////////////////////////////////////////////////////////////
213 //Method to return a string from CSS rules (to be saved to a file)
214 getCssFromRules: {
215 enumerable: false,
216 value: function (list) {
217 //Variable to store CSS definitions
218 var i, str, css = '';
219 //Looping through list
220 if (list && list.length > 0) {
221 //Adding each list item to string and also adding breaks
222 for (i = 0; list[i]; i++) {
223 str = list[i].cssText+' ';
224 str = str.replace( new RegExp( "{", "gi" ), "{\n\t" );
225 str = str.replace( new RegExp( "}", "gi" ), "}\n" );