/*
This file contains proprietary software owned by Motorola Mobility, Inc.
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
*/
////////////////////////////////////////////////////////////////////////
//
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component,
FileIo = require("js/io/system/fileio").FileIo,
ProjectIo = require("js/io/system/projectio").ProjectIo,
TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator;
////////////////////////////////////////////////////////////////////////
//
exports.IoMediator = Montage.create(Component, {
////////////////////////////////////////////////////////////////////
//
hasTemplate: {
enumerable: false,
value: false
},
////////////////////////////////////////////////////////////////////
//
deserializedFromTemplate: {
enumerable: false,
value: function () {
//
}
},
////////////////////////////////////////////////////////////////////
//
fio: {
enumerable: false,
value: FileIo
},
////////////////////////////////////////////////////////////////////
//
pio: {
enumerable: false,
value: ProjectIo
},
////////////////////////////////////////////////////////////////////
//
getAppTemplatesUrlRegEx: {
enumerable: false,
value: function () {
var regex = new RegExp(chrome.extension.getURL('js/document/templates/montage-html').replace(/\//gi, '\\\/'), 'gi');
return regex;
}
},
////////////////////////////////////////////////////////////////////
//
fileNew: {
enumerable: false,
value: function (file, url, callback, template) {
//Loading template from template URL
var xhr = new XMLHttpRequest(), result;
xhr.open("GET", url, false);
xhr.send();
if (xhr.readyState === 4) {
//Making call to create file, checking for return code
switch (this.fio.newFile({ uri: file, contents: parseTemplate(xhr.response, template) })) {
case 201:
result = { status: 201, success: true, uri: file };
break;
case 204:
result = { status: 204, success: false, uri: file };
break;
case 400:
result = { status: 400, success: false, uri: file };
break;
default:
result = { status: 500, success: false, uri: file };
break;
}
//TODO: Improve template data injection
function parseTemplate (content, template) {
//
if (template.name.toLowerCase() === 'banner') {
//Getting dimensions of banner
var dimensions = template.id.split('x');
dimensions = {width: String(dimensions[0])+'px', height: String(dimensions[1])+'px'};
//
content = content.replace(/Dimensions@@@/gi, "Dimensions@@@"+template.id);
content = content.replace(/ninja-banner {}/gi, "ninja-banner {overflow: visible; width: "+dimensions.width+"; height: "+dimensions.height+"}");
content = content.replace(/ninja-content-wrapper {}/gi, "ninja-content-wrapper {overflow: hidden; width: "+dimensions.width+"; height: "+dimensions.height+"}");
}
//
return content;
}
} else {
result = { status: 500, success: false, uri: file };
}
//Sending result to callback if requested for handling
if (callback) callback(result);
//Codes
// 204: File exists | 400: File exists
// 201: File succesfully created | 500: Unknown (Probably cloud API not running)
}
},
////////////////////////////////////////////////////////////////////
//
fileOpen: {
enumerable: false,
value: function (file, callback) {
//Reading file (Ninja doesn't really open a file, all in browser memory)
var read = this.fio.readFile({ uri: file }), result;
//Checking for status
switch (read.status) {
case 204:
//Creating and formatting result object for callbak
result = read.file.details;
result.root = read.file.details.uri.replace(read.file.details.name, "");
//Checking for type of content to returns
if (result.extension !== 'html' && result.extension !== 'htm') {
//Simple string
result.content = read.file.content;
} else {
//Object to be used by Ninja Template
result.content = this.parseHtmlToNinjaTemplate(read.file.content);
}
//Status of call
result.status = read.status;
//Calling back with result
if (callback) callback(result);
break;
case 404:
//File does not exists
if (callback) callback({ status: read.status });
break;
default:
//Unknown
if (callback) callback({ status: 500 });
break;
}
/*
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//Return Object Description
Object.status (Always presents for handling)
204: File exists (Success)
404: File does not exists (Failure)
500: Unknown (Probably cloud API not running)
(Below only present if succesfull 204)
Object.content
Object.extension
Object.name
Object.uri
Object.creationDate
Object.modifiedDate
Object.readOnly
Object.size
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
*/
}
},
////////////////////////////////////////////////////////////////////
//
fileSave: {
enumerable: false,
value: function (doc, callback) {
//
var contents, save;
//
switch (doc.mode) {
case 'html':
//Getting content from function to properly handle saving assets (as in external if flagged)
contents = this.parseNinjaTemplateToHtml(doc);
break;
default:
contents = doc.content;
break;
}
//Making call to save file
save = this.fio.saveFile({ uri: doc.file.uri, contents: contents });
//Checking for callback
if (callback) callback(save);
}
},
////////////////////////////////////////////////////////////////////
//
fileSaveAs: {
enumerable: false,
value: function (copyTo, copyFrom, callback) {
//TODO: Implement Save As functionality
}
},
////////////////////////////////////////////////////////////////////
//
fileDelete: {
enumerable: false,
value: function (file, callback) {
//TODO: Implement Delete functionality
}
},
////////////////////////////////////////////////////////////////////
//
parseHtmlToNinjaTemplate: {
enumerable: false,
value: function (html) {
//Creating temp object to mimic HTML
var doc = window.document.implementation.createHTMLDocument(), template;
//Setting content to temp
doc.getElementsByTagName('html')[0].innerHTML = html;
//Creating return object
return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc};
}
},
////////////////////////////////////////////////////////////////////
//TODO: Expand to allow more templates, clean up variables
parseNinjaTemplateToHtml: {
enumerable: false,
value: function (template) {
var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]));
regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi');
//Injecting head and body into old document
template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, '');
template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, '');
/*
//Testing using montage clean up method
var mjscode, mjsTemp = TemplateCreator.create();
//mjscode = mjsTemp.initWithHeadAndBodyElements(template.head, template.body);
//mjscode = mjsTemp.initWithDocument(template.file.content.document);
mjscode = mjsTemp.initWithDocument(template.document);
console.log(template.head, mjscode._document.head);
template.file.content.document.head.innerHTML = mjscode._document.head.innerHTML.replace(regexRootUrl, '');
template.file.content.document.body.innerHTML = mjscode._document.body.innerHTML.replace(regexRootUrl, '');
*/
//Getting all CSS (style or link) tags
var styletags = template.file.content.document.getElementsByTagName('style'),
linktags = template.file.content.document.getElementsByTagName('link'),
toremovetags = [],
njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]');
//////////////////////////////////////////////////
//TODO: Remove, temp hack, this is to be fixed by Montage
var basetags = template.file.content.document.getElementsByTagName('base');
for (var g in basetags) {
if (basetags[g].getAttribute) toremovetags.push(basetags[g]);
}
//////////////////////////////////////////////////
//
for (var f in njtemplatetags) {
if (njtemplatetags[f].getAttribute) toremovetags.push(njtemplatetags[f]);
}
//Getting styles tags to be removed from document
if (styletags.length) {
for (var j = 0; j < styletags.length; j++) {
if (styletags[j].getAttribute) {
if (styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {
toremovetags.push(styletags[j]);
} else if (styletags[j].getAttribute('data-ninja-external-url')) {
toremovetags.push(styletags[j]);
}
}
}
}
//Removing styles tags from document
for (var h = 0; toremovetags[h]; h++) {
try {
//Checking head first
template.file.content.document.head.removeChild(toremovetags[h]);
} catch (e) {
}
try {
//Checking body if not in head
template.file.content.document.body.removeChild(toremovetags[h]);
} catch (e) {
//Error, not found!
}
}
//Removing disabled tags from tags that were not originally disabled by user (Ninja enables all)
for (var l in linktags) {
if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll
for (var p = 0; toremovetags[p]; p++) {
if (toremovetags[p].getAttribute('href') === linktags[l].getAttribute('href')) {
if (!toremovetags[p].getAttribute('data-ninja-disabled')) {
linktags[l].removeAttribute('disabled');
}
}
}
}
}
//Checking for type of save: styles =