aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js216
1 files changed, 216 insertions, 0 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
new file mode 100644
index 00000000..599b7d84
--- /dev/null
+++ b/js/mediators/io-mediator.js
@@ -0,0 +1,216 @@
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 //Checking for type of content to returns
90 if (result.extension !== 'html' && result.extension !== 'htm') {
91 //Simple string
92 result.content = read.file.content;
93 } else {
94 //Object to be used by Ninja Template
95 result.content = this.parseHtmlToNinjaTemplate(read.file.content);
96 }
97 //Status of call
98 result.status = read.status;
99 //Calling back with result
100 if (callback) callback(result);
101 break;
102 case 404:
103 //File does not exists
104 if (callback) callback({status: read.status});
105 break;
106 default:
107 //Unknown
108 if (callback) callback({status: 500});
109 break;
110 }
111 /*
112 ////////////////////////////////////////////////////////////
113 ////////////////////////////////////////////////////////////
114 //Return Object Description
115 Object.status (Always presents for handling)
116 204: File exists (Success)
117 404: File does not exists (Failure)
118 500: Unknown (Probably cloud API not running)
119
120 (Below only present if succesfull 204)
121
122 Object.content
123 Object.extension
124 Object.name
125 Object.uri
126 Object.creationDate
127 Object.modifiedDate
128 Object.readOnly
129 Object.size
130 ////////////////////////////////////////////////////////////
131 ////////////////////////////////////////////////////////////
132 */
133 }
134 },
135 ////////////////////////////////////////////////////////////////////
136 //
137 fileSave: {
138 enumerable: false,
139 value: function (file, callback) {
140 //
141 var contents, save;
142 //
143 switch (file.mode) {
144 case 'html':
145 file.document.content.document.body.innerHTML = file.body;
146 file.document.content.document.head.innerHTML = file.head;
147 if (file.style) {
148 file.document.content.document.head.getElementsByTagName('style')[0].innerHTML = this.getCssFromRules(file.style.cssRules);
149 }
150 contents = file.document.content.document.documentElement.outerHTML;
151 break;
152 default:
153 contents = file.content;
154 break;
155 }
156 //
157 save = this.fio.saveFile({uri: file.document.uri, contents: contents});
158 //
159 if (callback) callback(save);
160 }
161 },
162 ////////////////////////////////////////////////////////////////////
163 //
164 fileSaveAs: {
165 enumerable: false,
166 value: function (copyTo, copyFrom, callback) {
167 //
168 }
169 },
170 ////////////////////////////////////////////////////////////////////
171 //
172 fileDelete: {
173 enumerable: false,
174 value: function (file, callback) {
175 //
176 }
177 },
178 ////////////////////////////////////////////////////////////////////
179 //
180 parseHtmlToNinjaTemplate: {
181 enumerable: false,
182 value: function (html) {
183 //Creating temp object to mimic HTML
184 var doc = window.document.implementation.createHTMLDocument(), template;
185 //Setting content to temp
186 doc.getElementsByTagName('html')[0].innerHTML = html;
187 //Creating return object
188 return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc};
189 }
190 },
191 ////////////////////////////////////////////////////////////////////
192 //Method to return a string from CSS rules (to be saved to a file)
193 getCssFromRules: {
194 enumerable: false,
195 value: function (list) {
196 //Variable to store CSS definitions
197 var i, str, css = '';
198 //Looping through list
199 if (list && list.length > 0) {
200 //Adding each list item to string and also adding breaks
201 for (i = 0; list[i]; i++) {
202 str = list[i].cssText+' ';
203 str = str.replace( new RegExp( "{", "gi" ), "{\n\t" );
204 str = str.replace( new RegExp( "}", "gi" ), "}\n" );
205 str = str.replace( new RegExp( ";", "gi" ), ";\n\t" );
206 css += '\n'+str;
207 }
208 }
209 //Returning the CSS string
210 return css;
211 }
212 }
213 ////////////////////////////////////////////////////////////////////
214});
215////////////////////////////////////////////////////////////////////////
216//////////////////////////////////////////////////////////////////////// \ No newline at end of file