aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-02-15 16:09:47 -0800
committerValerio Virgillito2012-02-15 16:09:47 -0800
commitd366c0bd1af6471511217ed574083e15059519b5 (patch)
treec8c9f9af761457e3c5f4c6774fb0fbba851df0c4 /js/mediators/io-mediator.js
parent997ce3fb65f27b3d6f331f63b5dc22d3c7fb8f1e (diff)
parentb85bfb54aaca3ccca3c1ef09115de925cd67f4e9 (diff)
downloadninja-d366c0bd1af6471511217ed574083e15059519b5.tar.gz
Merge branch 'refs/heads/integration'
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js209
1 files changed, 209 insertions, 0 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
new file mode 100644
index 00000000..95e775a0
--- /dev/null
+++ b/js/mediators/io-mediator.js
@@ -0,0 +1,209 @@
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 if(save.status === 204){
159 callback();
160 }
161 }
162 },
163 ////////////////////////////////////////////////////////////////////
164 //
165 fileSaveAs: {
166 enumerable: false,
167 value: function (copyTo, copyFrom, callback) {
168 //
169 }
170 },
171 ////////////////////////////////////////////////////////////////////
172 //
173 parseHtmlToNinjaTemplate: {
174 enumerable: false,
175 value: function (html) {
176 //Creating temp object to mimic HTML
177 var doc = window.document.implementation.createHTMLDocument(), template;
178 //Setting content to temp
179 doc.getElementsByTagName('html')[0].innerHTML = html;
180 //Creating return object
181 return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc};
182 }
183 },
184 ////////////////////////////////////////////////////////////////////
185 //Method to return a string from CSS rules (to be saved to a file)
186 getCssFromRules: {
187 enumerable: false,
188 value: function (list) {
189 //Variable to store CSS definitions
190 var i, str, css = '';
191 //Looping through list
192 if (list && list.length > 0) {
193 //Adding each list item to string and also adding breaks
194 for (i = 0; list[i]; i++) {
195 str = list[i].cssText+' ';
196 str = str.replace( new RegExp( "{", "gi" ), "{\n\t" );
197 str = str.replace( new RegExp( "}", "gi" ), "}\n" );
198 str = str.replace( new RegExp( ";", "gi" ), ";\n\t" );
199 css += '\n'+str;
200 }
201 }
202 //Returning the CSS string
203 return css;
204 }
205 }
206 ////////////////////////////////////////////////////////////////////
207});
208////////////////////////////////////////////////////////////////////////
209//////////////////////////////////////////////////////////////////////// \ No newline at end of file