From 3811f72f8cd8caaa2d13fa695b918f25facb85c5 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Wed, 9 May 2012 16:28:13 -0700 Subject: Preliminary Montage Template Cleanup The template creator is currently not returning serializing code, but does clean the document. Need to investigate reason why, currently all components are removed on save. --- js/document/models/html.js | 25 ++--- js/document/views/design.js | 14 +++ js/mediators/io-mediator.js | 40 ++++++-- node_modules/tools/template-creator.js | 117 ------------------------ node_modules/tools/template/template-creator.js | 113 +++++++++++++++++++++++ 5 files changed, 170 insertions(+), 139 deletions(-) delete mode 100644 node_modules/tools/template-creator.js create mode 100755 node_modules/tools/template/template-creator.js diff --git a/js/document/models/html.js b/js/document/models/html.js index 2764a6d6..b57ff832 100755 --- a/js/document/models/html.js +++ b/js/document/models/html.js @@ -22,30 +22,30 @@ exports.HtmlDocumentModel = Montage.create(BaseDocumentModel, { draw3DGrid: { value: false }, + //////////////////////////////////////////////////////////////////// + // + baseHref: { + value: null + }, //////////////////////////////////////////////////////////////////// // webGlHelper: { value: webGlDocumentHelper }, - //////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////// + // userComponents: { value: {} }, - - /** - * Add a reference to a component instance to the userComponents hash using the - * element UUID - */ + //////////////////////////////////////////////////////////////////// + //Add a reference to a component instance to the userComponents hash using the element UUID setComponentInstance: { value: function(instance, el) { this.userComponents[el.uuid] = instance; } }, - - /** - * Returns the component instance obj from the element - */ + //////////////////////////////////////////////////////////////////// + //Returns the component instance obj from the element getComponentFromElement: { value: function(el) { if(el) { @@ -55,7 +55,8 @@ exports.HtmlDocumentModel = Montage.create(BaseDocumentModel, { } } } - + //////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////// }); //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/js/document/views/design.js b/js/document/views/design.js index 9ad088cb..765099e6 100755 --- a/js/document/views/design.js +++ b/js/document/views/design.js @@ -41,6 +41,11 @@ exports.DesignDocumentView = Montage.create(BaseDocumentView, { content: { value: null }, + //////////////////////////////////////////////////////////////////// + //TODO: Remove usage + model: { + value: null + }, //////////////////////////////////////////////////////////////////// // document: { @@ -78,6 +83,8 @@ exports.DesignDocumentView = Montage.create(BaseDocumentView, { // onTemplateLoad: { value: function (e) { + //TODO: Add support to constructing URL with a base HREF + var basetag = this.content.document.getElementsByTagName('base'); //Removing event this.iframe.removeEventListener("load", this.onTemplateLoad.bind(this), false); //TODO: Improve usage of this reference @@ -88,6 +95,13 @@ exports.DesignDocumentView = Montage.create(BaseDocumentView, { this.document.styleSheets[k].ownerNode.setAttribute('data-ninja-template', 'true'); } } + //Checking for a base URL + if (basetag.length) { + if (basetag[basetag.length-1].getAttribute && basetag[basetag.length-1].getAttribute('href')) { + //Setting base HREF in model + this.model.baseHref = basetag[basetag.length-1].getAttribute('href'); + } + } //Creating temp code fragement to load head this._headFragment = this.document.createElement('head'); //Adding event listener to know when head is ready, event only dispatched once when using innerHTML diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index 6d146c97..cf44ab9d 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js @@ -10,7 +10,7 @@ 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-creator").TemplateCreator; + TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator; //////////////////////////////////////////////////////////////////////// // exports.IoMediator = Montage.create(Component, { @@ -203,20 +203,38 @@ exports.IoMediator = Montage.create(Component, { 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, ''); + //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); + + 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]'), - basetags = template.file.content.document.getElementsByTagName('base'); + njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]'); ////////////////////////////////////////////////// - //TODO: Remove, temp hack to avoid montage + //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]); } +*/ ////////////////////////////////////////////////// // @@ -469,9 +487,9 @@ function loadWebGL (e) {\n\ // - var mjsCounter = 0, mjsComponents = [], temp = TemplateCreator.create(); - // - for (var m in template.mjs) { + + /* +for (var m in template.mjs) { mjsComponents.push(template.mjs[m]); mjsCounter++; } @@ -509,6 +527,7 @@ function loadWebGL (e) {\n\ // mjstag.innerHTML = mjscode; } +*/ @@ -570,7 +589,8 @@ function loadWebGL (e) {\n\ // getDocRootUrl: { value: function () { - return this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]).replace(/\/\//gi, '/')); + //TODO: Add support for model.baseHref (base tag) + return this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]).replace(/\/\//gi, '/')); } }, //////////////////////////////////////////////////////////////////// diff --git a/node_modules/tools/template-creator.js b/node_modules/tools/template-creator.js deleted file mode 100644 index e312cd32..00000000 --- a/node_modules/tools/template-creator.js +++ /dev/null @@ -1,117 +0,0 @@ -/* - 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. -
*/ -/** - @module montage/tools/template/template-creator - @requires montage/ui/template -*/ - -exports = typeof exports !== "undefined" ? exports : {}; - -var Montage = require("montage/core/core").Montage; -var Template = require("montage/ui/template").Template; - -/** - @class module:montage/tools/template/template-creator.TemplateCreator - @extends module:montage/ui/template.Template -*/ -var TemplateCreator = exports.TemplateCreator = Montage.create(Template, /** @lends module:montage/tools/template/template-creator.TemplateCreator# */ { - initWithDocument: { - value: function(doc, montageJsPath) { - return this.initWithHeadAndBodyElements(doc.head, doc.body, montageJsPath); - } - }, - - initWithBodyElement: { - value: function(body, montageJsPath) { - return this.initWithHeadAndBodyElements(null, body, montageJsPath); - } - }, - - initWithHeadAndBodyElements: { - value: function(head, body, components, montageJsPath) { - var serializer = this.serializer, - objects = {}, - //components = {}, - doc, - script, - self = this; - - this._objectNamesIndex = {}; - //doc = this._document = document.implementation.createHTMLDocument(""); - - /* -function copyNode(sourceNode, targetNode, isRootNode) { - var childNodes = sourceNode.childNodes, - childNode, - targetChildNode, - label, - script, - component = isRootNode ? null : sourceNode.controller; - - if (component) { - label = self._generateLabelForComponent(component); - targetNode.setAttribute("id", label); - component._element = targetNode; - //components[label] = component; - } else { - for (var i = 0; (childNode = childNodes[i]); i++) { - targetChildNode = targetNode.appendChild(childNode.cloneNode(false)); - copyNode(childNode, targetChildNode); - } - } - } -*/ - - /* -if (head) { - doc.head.innerHTML = head.innerHTML; - } - if (montageJsPath) { - script = doc.createElement("script"); - script.setAttribute("src", montageJsPath); - doc.head.appendChild(script); - doc.head.insertBefore(doc.createTextNode("\n "), script); - } - - // try to make things look nice... - var html = doc.documentElement; - html.insertBefore(doc.createTextNode("\n"), doc.head); - html.insertBefore(doc.createTextNode("\n"), doc.body); - html.appendChild(doc.createTextNode("\n")); - if (!head) { - // the first child is the title - doc.head.insertBefore(doc.createTextNode("\n "), doc.head.firstChild); - } -*/ - - //copyNode(body, this._document.body, true); - this._ownerSerialization = serializer.serialize(components); - this._externalObjects = serializer.getExternalObjects(); - - return this; - } - }, - - _componentNamesIndex: { - value: null - }, - - _generateLabelForComponent: {value: function(component) { - var componentInfo = Montage.getInfoForObject(component), - componentLabel = componentInfo.label, - componentName, - index; - - if (componentLabel) { - return componentLabel; - } else { - componentName = componentInfo.objectName.toLowerCase(); - index = this._componentNamesIndex[componentName] || 1; - this._componentNamesIndex[componentName] = index + 1; - return componentName + index; - } - }}, -}); \ No newline at end of file diff --git a/node_modules/tools/template/template-creator.js b/node_modules/tools/template/template-creator.js new file mode 100755 index 00000000..1cd65ddd --- /dev/null +++ b/node_modules/tools/template/template-creator.js @@ -0,0 +1,113 @@ +/* + 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. +
*/ +/** + @module montage/tools/template/template-creator + @requires montage/ui/template +*/ + +exports = typeof exports !== "undefined" ? exports : {}; + +var Montage = require("montage/core/core").Montage; +var Template = require("montage/ui/template").Template; + +/** + @class module:montage/tools/template/template-creator.TemplateCreator + @extends module:montage/ui/template.Template +*/ +var TemplateCreator = exports.TemplateCreator = Montage.create(Template, /** @lends module:montage/tools/template/template-creator.TemplateCreator# */ { + initWithDocument: { + value: function(doc, montageJsPath) { + return this.initWithHeadAndBodyElements(doc.head, doc.body, montageJsPath); + } + }, + + initWithBodyElement: { + value: function(body, montageJsPath) { + return this.initWithHeadAndBodyElements(null, body, montageJsPath); + } + }, + + initWithHeadAndBodyElements: { + value: function(head, body, montageJsPath) { + var serializer = this.serializer, + objects = {}, + components = {}, + doc, + script, + self = this; + + this._objectNamesIndex = {}; + doc = this._document = document.implementation.createHTMLDocument(""); + + function copyNode(sourceNode, targetNode, isRootNode) { + var childNodes = sourceNode.childNodes, + childNode, + targetChildNode, + label, + script, + component = isRootNode ? null : sourceNode.controller; + + if (component) { + label = self._generateLabelForComponent(component); + targetNode.setAttribute("id", label); + component._element = targetNode; + components[label] = component; + } else { + for (var i = 0; (childNode = childNodes[i]); i++) { + targetChildNode = targetNode.appendChild(childNode.cloneNode(false)); + copyNode(childNode, targetChildNode); + } + } + } + + if (head) { + doc.head.innerHTML = head.innerHTML; + } + if (montageJsPath) { + script = doc.createElement("script"); + script.setAttribute("src", montageJsPath); + doc.head.appendChild(script); + doc.head.insertBefore(doc.createTextNode("\n "), script); + } + + // try to make things look nice... + var html = doc.documentElement; + html.insertBefore(doc.createTextNode("\n"), doc.head); + html.insertBefore(doc.createTextNode("\n"), doc.body); + html.appendChild(doc.createTextNode("\n")); + if (!head) { + // the first child is the title + doc.head.insertBefore(doc.createTextNode("\n "), doc.head.firstChild); + } + + copyNode(body, this._document.body, true); + this._ownerSerialization = serializer.serialize(components); + this._externalObjects = serializer.getExternalObjects(); + + return this; + } + }, + + _componentNamesIndex: { + value: null + }, + + _generateLabelForComponent: {value: function(component) { + var componentInfo = Montage.getInfoForObject(component), + componentLabel = componentInfo.label, + componentName, + index; + + if (componentLabel) { + return componentLabel; + } else { + componentName = componentInfo.objectName.toLowerCase(); + index = this._componentNamesIndex[componentName] || 1; + this._componentNamesIndex[componentName] = index + 1; + return componentName + index; + } + }}, +}); \ No newline at end of file -- cgit v1.2.3 From ec68c665dee1936312842c546e1c7347f05dc555 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Wed, 9 May 2012 16:56:08 -0700 Subject: Temp removing montage clean up --- js/mediators/io-mediator.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index cf44ab9d..dae0f359 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js @@ -203,20 +203,23 @@ exports.IoMediator = Montage.create(Component, { 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, ''); + 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 + /* +//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.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, ''); +*/ @@ -229,12 +232,10 @@ exports.IoMediator = Montage.create(Component, { ////////////////////////////////////////////////// //TODO: Remove, temp hack, this is to be fixed by Montage - /* -var basetags = template.file.content.document.getElementsByTagName('base'); + var basetags = template.file.content.document.getElementsByTagName('base'); for (var g in basetags) { if (basetags[g].getAttribute) toremovetags.push(basetags[g]); } -*/ ////////////////////////////////////////////////// // -- cgit v1.2.3