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/document/mediators/template.js | 637 +++++++++++++++++++++++++++++++++++++- js/document/views/design.js | 6 +- 2 files changed, 637 insertions(+), 6 deletions(-) (limited to 'js/document') diff --git a/js/document/mediators/template.js b/js/document/mediators/template.js index c5b45ba1..86055a1f 100755 --- a/js/document/mediators/template.js +++ b/js/document/mediators/template.js @@ -6,16 +6,647 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot //////////////////////////////////////////////////////////////////////// // -var Montage = require("montage/core/core").Montage, - Component = require("montage/ui/component").Component; +var Montage = require("montage/core/core").Montage, + Component = require("montage/ui/component").Component, + TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator; //////////////////////////////////////////////////////////////////////// // exports.TemplateDocumentMediator = Montage.create(Component, { //////////////////////////////////////////////////////////////////// // hasTemplate: { - enumerable: false, value: false + }, + //////////////////////////////////////////////////////////////////// + // + getAppTemplatesUrlRegEx: { + value: function () { + var regex = new RegExp(chrome.extension.getURL(this.application.ninja.currentDocument.model.views.design.iframe.src.split(chrome.extension.getURL('/'))[1]).replace(/\//gi, '\\\/'), 'gi'); + return regex; + } + }, + //////////////////////////////////////////////////////////////////// + // + 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: { + 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: { + 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 =