From 86837d69186bc4d1d6f54fc893c523de0972a0b9 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 1 Jun 2012 11:53:57 -0700 Subject: Creating template mediator class This is to simplify code, more to be changed and added. --- js/mediators/io-mediator.js | 658 +------------------------------------------- 1 file changed, 9 insertions(+), 649 deletions(-) (limited to 'js/mediators/io-mediator.js') 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, 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; + TemplateMediator = require("js/document/mediators/template").TemplateDocumentMediator; //////////////////////////////////////////////////////////////////////// // exports.IoMediator = Montage.create(Component, { //////////////////////////////////////////////////////////////////// // hasTemplate: { - enumerable: false, value: false }, //////////////////////////////////////////////////////////////////// // deserializedFromTemplate: { - enumerable: false, value: function () { // } }, //////////////////////////////////////////////////////////////////// // + tmplt: { + value: TemplateMediator + }, + //////////////////////////////////////////////////////////////////// + // 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; @@ -102,7 +93,6 @@ exports.IoMediator = Montage.create(Component, { //////////////////////////////////////////////////////////////////// // 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; @@ -118,7 +108,7 @@ exports.IoMediator = Montage.create(Component, { result.content = read.file.content; } else { //Object to be used by Ninja Template - result.content = this.parseHtmlToNinjaTemplate(read.file.content); + result.content = this.tmplt.parseHtmlToNinjaTemplate(read.file.content); } //Status of call result.status = read.status; @@ -161,7 +151,6 @@ exports.IoMediator = Montage.create(Component, { //////////////////////////////////////////////////////////////////// // fileSave: { - enumerable: false, value: function (doc, callback) { // var contents, save; @@ -170,9 +159,9 @@ exports.IoMediator = Montage.create(Component, { case 'html': //Getting content from function to properly handle saving assets (as in external if flagged) if (doc.template && (doc.template.type === 'banner' || doc.template.type === 'animation')) { - contents = this.parseNinjaTemplateToHtml(doc, true); + contents = this.tmplt.parseNinjaTemplateToHtml(doc, true); } else { - contents = this.parseNinjaTemplateToHtml(doc); + contents = this.tmplt.parseNinjaTemplateToHtml(doc); } break; default: @@ -200,635 +189,6 @@ exports.IoMediator = Montage.create(Component, { value: function (file, callback) { //TODO: Implement Delete functionality } - }, - //////////////////////////////////////////////////////////////////// - // - getDataDirectory: { - value: function (path) { - //TODO: Implement user overwrite - return this._getUserDirectory(path+'data/'); - } - }, - //////////////////////////////////////////////////////////////////// - // - getNinjaDirectory: { - value: function (path) { - //TODO: Implement user overwrite - return this._getUserDirectory(this.getDataDirectory(path)+'ninja/'); - } - }, - //////////////////////////////////////////////////////////////////// - // - getCanvasDirectory: { - value: function (path) { - //TODO: Implement user overwrite - return this._getUserDirectory(this.getNinjaDirectory(path)+'canvas/'); - } - }, - //////////////////////////////////////////////////////////////////// - // - _getUserDirectory: { - value: function (path) { - //Checking for data directory - var check = this.application.ninja.coreIoApi.fileExists({uri: path}), directory; - //Creating directory if doesn't exists - switch (check.status) { - case 204: //Exists - directory = path; - break; - case 404: //Doesn't exists - directory = this.application.ninja.coreIoApi.createDirectory({uri: path}); - //Checking for success - if (directory.status === 201) { - directory = path; - } else { - //Error - directory = null; - } - break; - default: //Error - directory = null; - break; - } - //Returning the path to the directory on disk (null for any error) - return directory; - } - }, - //////////////////////////////////////////////////////////////////// - // - 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, ninjaWrapper) { - //TODO: Improve reference for rootUrl - var regexRootUrl, - rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])), - mjsCreator = template.mjsTemplateCreator.create(), - mJsSerialization, - toremovetags = [], - presentNodes, - montageTemplate; - //Creating instance of template creator - montageTemplate = mjsCreator.initWithDocument(template.document); - //Setting up expression for parsing URLs - regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); - //Injecting head and body into old document - if (montageTemplate._ownerSerialization.length > 0) { - template.file.content.document.head.innerHTML = montageTemplate._document.head.innerHTML.replace(regexRootUrl, ''); - template.file.content.document.body.innerHTML = montageTemplate._document.body.innerHTML.replace(regexRootUrl, ''); - // - mJsSerialization = montageTemplate._ownerSerialization; - } else { - template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, ''); - template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, ''); - } - //Copying attributes to maintain same properties as the - for (var n in template.body.attributes) { - if (template.body.attributes[n].value) { - // - template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value); - } - } - - - - //TODO: Add attribute copying for and - - - - //Getting list of current nodes (Ninja DOM) - presentNodes = template.file.content.document.getElementsByTagName('*'); - //Looping through nodes to determine origin and removing if not inserted by Ninja - for (var n in presentNodes) { - if (presentNodes[n].getAttribute && presentNodes[n].getAttribute('data-ninja-node') === null) { - toremovetags.push(presentNodes[n]); - } else if (presentNodes[n].getAttribute && presentNodes[n].getAttribute('data-ninja-node') !== null) { - //Removing attribute - presentNodes[n].removeAttribute('data-ninja-node'); - } - } - //Getting all CSS (style or link) tags - var styletags = template.file.content.document.getElementsByTagName('style'), - linktags = template.file.content.document.getElementsByTagName('link'), - 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 && basetags[g].href && basetags[g].href.indexOf('chrome-extension://') !== -1) toremovetags.push(basetags[g]); - } - ////////////////////////////////////////////////// - - //Adding to tags to be removed form template - 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 =