aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-06-01 11:53:57 -0700
committerJose Antonio Marquez2012-06-01 11:53:57 -0700
commit86837d69186bc4d1d6f54fc893c523de0972a0b9 (patch)
tree7121d2178839086a30effefa8d7417de94b13122 /js/mediators/io-mediator.js
parente09e4a8e24e5daf899d24314d0ffc218bbb2a97d (diff)
downloadninja-86837d69186bc4d1d6f54fc893c523de0972a0b9.tar.gz
Creating template mediator class
This is to simplify code, more to be changed and added.
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js658
1 files changed, 9 insertions, 649 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 4dbf32d5..4535cad6 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -10,49 +10,40 @@ var Montage = require("montage/core/core").Montage,
10 Component = require("montage/ui/component").Component, 10 Component = require("montage/ui/component").Component,
11 FileIo = require("js/io/system/fileio").FileIo, 11 FileIo = require("js/io/system/fileio").FileIo,
12 ProjectIo = require("js/io/system/projectio").ProjectIo, 12 ProjectIo = require("js/io/system/projectio").ProjectIo,
13 TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator; 13 TemplateMediator = require("js/document/mediators/template").TemplateDocumentMediator;
14//////////////////////////////////////////////////////////////////////// 14////////////////////////////////////////////////////////////////////////
15// 15//
16exports.IoMediator = Montage.create(Component, { 16exports.IoMediator = Montage.create(Component, {
17 //////////////////////////////////////////////////////////////////// 17 ////////////////////////////////////////////////////////////////////
18 // 18 //
19 hasTemplate: { 19 hasTemplate: {
20 enumerable: false,
21 value: false 20 value: false
22 }, 21 },
23 //////////////////////////////////////////////////////////////////// 22 ////////////////////////////////////////////////////////////////////
24 // 23 //
25 deserializedFromTemplate: { 24 deserializedFromTemplate: {
26 enumerable: false,
27 value: function () { 25 value: function () {
28 // 26 //
29 } 27 }
30 }, 28 },
31 //////////////////////////////////////////////////////////////////// 29 ////////////////////////////////////////////////////////////////////
32 // 30 //
31 tmplt: {
32 value: TemplateMediator
33 },
34 ////////////////////////////////////////////////////////////////////
35 //
33 fio: { 36 fio: {
34 enumerable: false,
35 value: FileIo 37 value: FileIo
36 }, 38 },
37 //////////////////////////////////////////////////////////////////// 39 ////////////////////////////////////////////////////////////////////
38 // 40 //
39 pio: { 41 pio: {
40 enumerable: false,
41 value: ProjectIo 42 value: ProjectIo
42 }, 43 },
43 //////////////////////////////////////////////////////////////////// 44 ////////////////////////////////////////////////////////////////////
44 // 45 //
45 getAppTemplatesUrlRegEx: {
46 enumerable: false,
47 value: function () {
48 var regex = new RegExp(chrome.extension.getURL('js/document/templates/montage-html').replace(/\//gi, '\\\/'), 'gi');
49 return regex;
50 }
51 },
52 ////////////////////////////////////////////////////////////////////
53 //
54 fileNew: { 46 fileNew: {
55 enumerable: false,
56 value: function (file, url, callback, template) { 47 value: function (file, url, callback, template) {
57 //Loading template from template URL 48 //Loading template from template URL
58 var xhr = new XMLHttpRequest(), result; 49 var xhr = new XMLHttpRequest(), result;
@@ -102,7 +93,6 @@ exports.IoMediator = Montage.create(Component, {
102 //////////////////////////////////////////////////////////////////// 93 ////////////////////////////////////////////////////////////////////
103 // 94 //
104 fileOpen: { 95 fileOpen: {
105 enumerable: false,
106 value: function (file, callback) { 96 value: function (file, callback) {
107 //Reading file (Ninja doesn't really open a file, all in browser memory) 97 //Reading file (Ninja doesn't really open a file, all in browser memory)
108 var read = this.fio.readFile({ uri: file }), result; 98 var read = this.fio.readFile({ uri: file }), result;
@@ -118,7 +108,7 @@ exports.IoMediator = Montage.create(Component, {
118 result.content = read.file.content; 108 result.content = read.file.content;
119 } else { 109 } else {
120 //Object to be used by Ninja Template 110 //Object to be used by Ninja Template
121 result.content = this.parseHtmlToNinjaTemplate(read.file.content); 111 result.content = this.tmplt.parseHtmlToNinjaTemplate(read.file.content);
122 } 112 }
123 //Status of call 113 //Status of call
124 result.status = read.status; 114 result.status = read.status;
@@ -161,7 +151,6 @@ exports.IoMediator = Montage.create(Component, {
161 //////////////////////////////////////////////////////////////////// 151 ////////////////////////////////////////////////////////////////////
162 // 152 //
163 fileSave: { 153 fileSave: {
164 enumerable: false,
165 value: function (doc, callback) { 154 value: function (doc, callback) {
166 // 155 //
167 var contents, save; 156 var contents, save;
@@ -170,9 +159,9 @@ exports.IoMediator = Montage.create(Component, {
170 case 'html': 159 case 'html':
171 //Getting content from function to properly handle saving assets (as in external if flagged) 160 //Getting content from function to properly handle saving assets (as in external if flagged)
172 if (doc.template && (doc.template.type === 'banner' || doc.template.type === 'animation')) { 161 if (doc.template && (doc.template.type === 'banner' || doc.template.type === 'animation')) {
173 contents = this.parseNinjaTemplateToHtml(doc, true); 162 contents = this.tmplt.parseNinjaTemplateToHtml(doc, true);
174 } else { 163 } else {
175 contents = this.parseNinjaTemplateToHtml(doc); 164 contents = this.tmplt.parseNinjaTemplateToHtml(doc);
176 } 165 }
177 break; 166 break;
178 default: 167 default:
@@ -200,635 +189,6 @@ exports.IoMediator = Montage.create(Component, {
200 value: function (file, callback) { 189 value: function (file, callback) {
201 //TODO: Implement Delete functionality 190 //TODO: Implement Delete functionality
202 } 191 }
203 },
204 ////////////////////////////////////////////////////////////////////
205 //
206 getDataDirectory: {
207 value: function (path) {
208 //TODO: Implement user overwrite
209 return this._getUserDirectory(path+'data/');
210 }
211 },
212 ////////////////////////////////////////////////////////////////////
213 //
214 getNinjaDirectory: {
215 value: function (path) {
216 //TODO: Implement user overwrite
217 return this._getUserDirectory(this.getDataDirectory(path)+'ninja/');
218 }
219 },
220 ////////////////////////////////////////////////////////////////////
221 //
222 getCanvasDirectory: {
223 value: function (path) {
224 //TODO: Implement user overwrite
225 return this._getUserDirectory(this.getNinjaDirectory(path)+'canvas/');
226 }
227 },
228 ////////////////////////////////////////////////////////////////////
229 //
230 _getUserDirectory: {
231 value: function (path) {
232 //Checking for data directory
233 var check = this.application.ninja.coreIoApi.fileExists({uri: path}), directory;
234 //Creating directory if doesn't exists
235 switch (check.status) {
236 case 204: //Exists
237 directory = path;
238 break;
239 case 404: //Doesn't exists
240 directory = this.application.ninja.coreIoApi.createDirectory({uri: path});
241 //Checking for success
242 if (directory.status === 201) {
243 directory = path;
244 } else {
245 //Error
246 directory = null;
247 }
248 break;
249 default: //Error
250 directory = null;
251 break;
252 }
253 //Returning the path to the directory on disk (null for any error)
254 return directory;
255 }
256 },
257 ////////////////////////////////////////////////////////////////////
258 //
259 parseHtmlToNinjaTemplate: {
260 enumerable: false,
261 value: function (html) {
262 //Creating temp object to mimic HTML
263 var doc = window.document.implementation.createHTMLDocument(), template;
264 //Setting content to temp
265 doc.getElementsByTagName('html')[0].innerHTML = html;
266 //Creating return object
267 return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc};
268 }
269 },
270 ////////////////////////////////////////////////////////////////////
271 //TODO: Expand to allow more templates, clean up variables
272 parseNinjaTemplateToHtml: {
273 enumerable: false,
274 value: function (template, ninjaWrapper) {
275 //TODO: Improve reference for rootUrl
276 var regexRootUrl,
277 rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])),
278 mjsCreator = template.mjsTemplateCreator.create(),
279 mJsSerialization,
280 toremovetags = [],
281 presentNodes,
282 montageTemplate;
283 //Creating instance of template creator
284 montageTemplate = mjsCreator.initWithDocument(template.document);
285 //Setting up expression for parsing URLs
286 regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi');
287 //Injecting head and body into old document
288 if (montageTemplate._ownerSerialization.length > 0) {
289 template.file.content.document.head.innerHTML = montageTemplate._document.head.innerHTML.replace(regexRootUrl, '');
290 template.file.content.document.body.innerHTML = montageTemplate._document.body.innerHTML.replace(regexRootUrl, '');
291 //
292 mJsSerialization = montageTemplate._ownerSerialization;
293 } else {
294 template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, '');
295 template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, '');
296 }
297 //Copying attributes to maintain same properties as the <body>
298 for (var n in template.body.attributes) {
299 if (template.body.attributes[n].value) {
300 //
301 template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value);
302 }
303 }