From 648ee61ae84216d0236e0dbc211addc13b2cfa3a Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Fri, 6 Jul 2012 11:52:06 -0700 Subject: Expand tabs --- js/document/mediators/io.js | 18 +- js/document/mediators/template.js | 486 +++++++++++++++++++------------------- 2 files changed, 252 insertions(+), 252 deletions(-) (limited to 'js/document/mediators') diff --git a/js/document/mediators/io.js b/js/document/mediators/io.js index 85d805ad..194eafa3 100755 --- a/js/document/mediators/io.js +++ b/js/document/mediators/io.js @@ -30,19 +30,19 @@ POSSIBILITY OF SUCH DAMAGE. //////////////////////////////////////////////////////////////////////// // -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; //////////////////////////////////////////////////////////////////////// -// +// exports.IoDocumentMediator = Montage.create(Component, { - //////////////////////////////////////////////////////////////////// - // - hasTemplate: { - enumerable: false, + //////////////////////////////////////////////////////////////////// + // + hasTemplate: { + enumerable: false, value: false } - //////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////// }); //////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// diff --git a/js/document/mediators/template.js b/js/document/mediators/template.js index 982b410f..6ab58e32 100755 --- a/js/document/mediators/template.js +++ b/js/document/mediators/template.js @@ -30,16 +30,16 @@ POSSIBILITY OF SUCH DAMAGE. //////////////////////////////////////////////////////////////////////// // -var Montage = require("montage/core/core").Montage, - Component = require("montage/ui/component").Component, - TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator, - ClassUuid = require("js/components/core/class-uuid").ClassUuid; +var Montage = require("montage/core/core").Montage, + Component = require("montage/ui/component").Component, + TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator, + ClassUuid = require("js/components/core/class-uuid").ClassUuid; //////////////////////////////////////////////////////////////////////// -// +// exports.TemplateDocumentMediator = Montage.create(Component, { - //////////////////////////////////////////////////////////////////// - // - hasTemplate: { + //////////////////////////////////////////////////////////////////// + // + hasTemplate: { value: false }, //////////////////////////////////////////////////////////////////// @@ -53,55 +53,55 @@ exports.TemplateDocumentMediator = Montage.create(Component, { //////////////////////////////////////////////////////////////////// // getDataDirectory: { - value: function (path) { - //TODO: Implement user overwrite - return this._getUserDirectory(path+'data/'); - } + 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/'); - } + 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/'); - } + value: function (path) { + //TODO: Implement user overwrite + return this._getUserDirectory(this.getNinjaDirectory(path)+'canvas/'); + } }, //////////////////////////////////////////////////////////////////// // _getUserDirectory: { - value: function (path) { - //Checking for data directory + 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; + 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; - } + } }, //////////////////////////////////////////////////////////////////// // @@ -109,7 +109,7 @@ exports.TemplateDocumentMediator = Montage.create(Component, { value: function (html) { //Creating temp object to mimic HTML var doc = window.document.implementation.createHTMLDocument(), template, docHtmlTag, - hackHtml = document.createElement('html'), hackTag; + hackHtml = document.createElement('html'), hackTag; //Setting content to temp doc.getElementsByTagName('html')[0].innerHTML = html; //TODO: Improve this, very bad way of copying attributes (in a pinch to get it working) @@ -118,13 +118,13 @@ exports.TemplateDocumentMediator = Montage.create(Component, { docHtmlTag = doc.getElementsByTagName('html')[0]; //Looping through the attributes to copy them if (hackTag) { - for (var m in hackTag.attributes) { - if (hackTag.attributes[m].value) { - docHtmlTag.setAttribute(hackTag.attributes[m].name.replace(/ninjahtmlhack/gi, 'html'), hackTag.attributes[m].value.replace(/ninjahtmlhack/gi, 'html')); - } - } - } - //Garbage collection + for (var m in hackTag.attributes) { + if (hackTag.attributes[m].value) { + docHtmlTag.setAttribute(hackTag.attributes[m].name.replace(/ninjahtmlhack/gi, 'html'), hackTag.attributes[m].value.replace(/ninjahtmlhack/gi, 'html')); + } + } + } + //Garbage collection hackHtml = hackTag = null; //Creating return object return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc}; @@ -134,42 +134,42 @@ exports.TemplateDocumentMediator = Montage.create(Component, { //TODO: Expand to allow more templates, clean up variables parseNinjaTemplateToHtml: { value: function (saveExternalData, template, ninjaWrapper, libCopyCallback) { - //TODO: Improve reference for rootUrl + //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; + 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; + 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, ''); + template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, ''); + template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, ''); } //Removes all attributes from node function wipeAttributes (node) { - for (var f in node.attributes) { - node.removeAttribute(node.attributes[f].name); - } + for (var f in node.attributes) { + node.removeAttribute(node.attributes[f].name); + } } //Copying attributes to maintain same properties as the wipeAttributes(template.file.content.document.body); - 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); - } - } - // + 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); + } + } + // if(template.template) { // // TODO - Need to handle banner and animation templates. @@ -201,53 +201,53 @@ exports.TemplateDocumentMediator = Montage.create(Component, { } } - wipeAttributes(template.file.content.document.head); + wipeAttributes(template.file.content.document.head); //Copying attributes to maintain same properties as the - for (var m in template.document.head.attributes) { - if (template.document.head.attributes[m].value) { - template.file.content.document.head.setAttribute(template.document.head.attributes[m].name, template.document.head.attributes[m].value); - } - } + for (var m in template.document.head.attributes) { + if (template.document.head.attributes[m].value) { + template.file.content.document.head.setAttribute(template.document.head.attributes[m].name, template.document.head.attributes[m].value); + } + } //Copying attributes to maintain same properties as the - var htmlTagMem = template.document.getElementsByTagName('html')[0], htmlTagDoc = template.file.content.document.getElementsByTagName('html')[0]; - wipeAttributes(htmlTagDoc); - // - for (var m in htmlTagMem.attributes) { - if (htmlTagMem.attributes[m].value) { - if (htmlTagMem.attributes[m].value.replace(/montage-app-bootstrapping/gi, '').length>0) { - htmlTagDoc.setAttribute(htmlTagMem.attributes[m].name, htmlTagMem.attributes[m].value.replace(/ montage-app-bootstrapping/gi, '')); - } - } - } - // - if (htmlTagMem && htmlTagMem.getAttribute('data-ninja-style') !== null) { - htmlTagDoc.setAttribute('style', htmlTagMem.getAttribute('data-ninja-style')); - htmlTagDoc.removeAttribute('data-ninja-style'); - } else if (htmlTagMem && htmlTagMem.getAttribute('data-ninja-style') === null) { - htmlTagDoc.removeAttribute('style'); - htmlTagDoc.removeAttribute('data-ninja-style'); - } + var htmlTagMem = template.document.getElementsByTagName('html')[0], htmlTagDoc = template.file.content.document.getElementsByTagName('html')[0]; + wipeAttributes(htmlTagDoc); + // + for (var m in htmlTagMem.attributes) { + if (htmlTagMem.attributes[m].value) { + if (htmlTagMem.attributes[m].value.replace(/montage-app-bootstrapping/gi, '').length>0) { + htmlTagDoc.setAttribute(htmlTagMem.attributes[m].name, htmlTagMem.attributes[m].value.replace(/ montage-app-bootstrapping/gi, '')); + } + } + } + // + if (htmlTagMem && htmlTagMem.getAttribute('data-ninja-style') !== null) { + htmlTagDoc.setAttribute('style', htmlTagMem.getAttribute('data-ninja-style')); + htmlTagDoc.removeAttribute('data-ninja-style'); + } else if (htmlTagMem && htmlTagMem.getAttribute('data-ninja-style') === null) { + htmlTagDoc.removeAttribute('style'); + htmlTagDoc.removeAttribute('data-ninja-style'); + } //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'); - } - } + // + 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]'); - - //Adding to tags to be removed form template - for (var f in njtemplatetags) { - if (njtemplatetags[f].getAttribute) toremovetags.push(njtemplatetags[f]); - } + linktags = template.file.content.document.getElementsByTagName('link'), + njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]'); + + //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++) { @@ -308,7 +308,7 @@ exports.TemplateDocumentMediator = Montage.create(Component, { if (template.styles) { //Getting all style tags var styleCounter = 0, - docStyles = template.file.content.document.getElementsByTagName('style'); + docStyles = template.file.content.document.getElementsByTagName('style'); //Looping through all style tags for (var i in template.styles) { if (template.styles[i].ownerNode) { @@ -328,8 +328,8 @@ exports.TemplateDocumentMediator = Montage.create(Component, { } else if (template.css) { //Getting all style and link tags var styleCounter = 0, - docStyles = template.file.content.document.getElementsByTagName('style'), - docLinks = template.file.content.document.getElementsByTagName('link'); + docStyles = template.file.content.document.getElementsByTagName('style'), + docLinks = template.file.content.document.getElementsByTagName('link'); //Removing Ninja Data Attributes for (var n in docLinks) { if (docLinks[n].attributes) { @@ -373,10 +373,10 @@ exports.TemplateDocumentMediator = Montage.create(Component, { var cleanedCss, - dirtyCss = this.getCssFromRules(template.css[i].cssRules), - fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'), - fileRootUrl = this.application.ninja.coreIoApi.rootUrl + fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length - 1])[0], - cleanedCss = dirtyCss.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaUrl.bind(this)); + dirtyCss = this.getCssFromRules(template.css[i].cssRules), + fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'), + fileRootUrl = this.application.ninja.coreIoApi.rootUrl + fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length - 1])[0], + cleanedCss = dirtyCss.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaUrl.bind(this)); function parseNinjaUrl(url) { if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) { @@ -420,28 +420,28 @@ exports.TemplateDocumentMediator = Montage.create(Component, { // var webgltag, webgllibtag, webglrdgetag, mjstag, mjslibtag, matchingtags = [], - scripts = template.file.content.document.getElementsByTagName('script'), - libsobserver = {montage: false, canvas: false, montageCopied: null, canvasCopied: null, callback: libCopyCallback, dispatched: false}; + scripts = template.file.content.document.getElementsByTagName('script'), + libsobserver = {montage: false, canvas: false, montageCopied: null, canvasCopied: null, callback: libCopyCallback, dispatched: false}; //TODO: Clean up, this is messy if (mJsSerialization) libsobserver.montage = true; if (template.webgl && template.webgl.length > 1) libsobserver.canvas = true; // for (var i in scripts) { - if (scripts[i].getAttribute) { - if (scripts[i].getAttribute('data-ninja-canvas') !== null) {//TODO: Use querySelectorAll - matchingtags.push(scripts[i]); + if (scripts[i].getAttribute) { + if (scripts[i].getAttribute('data-ninja-canvas') !== null) {//TODO: Use querySelectorAll + matchingtags.push(scripts[i]); } if (scripts[i].getAttribute('data-ninja-canvas-lib') !== null) { webgllibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags } if (scripts[i].getAttribute('data-ninja-canvas-rdge') !== null) { - webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags + webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags } if (scripts[i].getAttribute('type') === 'text/montage-serialization') { mjstag = scripts[i]; // TODO: Add logic to delete unneccesary tags } if (scripts[i].getAttribute('data-mjs-lib') !== null) { - mjslibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags + mjslibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags } } } @@ -458,23 +458,23 @@ exports.TemplateDocumentMediator = Montage.create(Component, { // if (cvsDataDir && !matchingtags.length && !webgllibtag) { - if (template.libs.canvasId) { - libsobserver.canvasId = template.libs.canvasId; - } else { - libsobserver.canvasId = ClassUuid.generate(); - } - - //Creating data directory, will include materials at a later time - fileCvsDir = cvsDataDir+template.file.name.split('.'+template.file.extension)[0]+'_'+libsobserver.canvasId; - - if (!this._getUserDirectory(fileCvsDir)) { - //TODO: create proper logic not to overwrite files - console.log('error'); - } - - fileCvsDir += '/'; + if (template.libs.canvasId) { + libsobserver.canvasId = template.libs.canvasId; + } else { + libsobserver.canvasId = ClassUuid.generate(); + } + + //Creating data directory, will include materials at a later time + fileCvsDir = cvsDataDir+template.file.name.split('.'+template.file.extension)[0]+'_'+libsobserver.canvasId; + + if (!this._getUserDirectory(fileCvsDir)) { + //TODO: create proper logic not to overwrite files + console.log('error'); + } + + fileCvsDir += '/'; } else if (webgllibtag && webgllibtag.getAttribute && webgllibtag.getAttribute('data-ninja-canvas-json') !== null) { - fileOrgDataSrc = template.file.root+webgllibtag.getAttribute('data-ninja-canvas-json'); + fileOrgDataSrc = template.file.root+webgllibtag.getAttribute('data-ninja-canvas-json'); } //Copy webGL library if needed for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { @@ -503,9 +503,9 @@ exports.TemplateDocumentMediator = Montage.create(Component, { webglrdgetag.setAttribute('src', rdgeDirName + '/rdge-compiled.js'); webglrdgetag.setAttribute('data-ninja-canvas-rdge', 'true'); if (ninjaWrapper) { - template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webglrdgetag); + template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webglrdgetag); } else { - template.file.content.document.head.appendChild(webglrdgetag); + template.file.content.document.head.appendChild(webglrdgetag); } } // @@ -515,9 +515,9 @@ exports.TemplateDocumentMediator = Montage.create(Component, { webgllibtag.setAttribute('src', rdgeDirName + '/canvas-runtime.js'); webgllibtag.setAttribute('data-ninja-canvas-lib', 'true'); if (ninjaWrapper) { - template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgllibtag); + template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgllibtag); } else { - template.file.content.document.head.appendChild(webgllibtag); + template.file.content.document.head.appendChild(webgllibtag); } } // @@ -525,10 +525,10 @@ exports.TemplateDocumentMediator = Montage.create(Component, { webgltag = template.file.content.document.createElement('script'); webgltag.setAttribute('data-ninja-canvas', 'true'); if (ninjaWrapper) { - template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgltag); + template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgltag); } else { - template.file.content.document.head.appendChild(webgltag); - } + template.file.content.document.head.appendChild(webgltag); + } } //TODO: Decide if this should be over-writter or only written on creation @@ -539,41 +539,41 @@ exports.TemplateDocumentMediator = Montage.create(Component, { //Looping through data to create escaped array for (var j = 0; template.webgl[j]; j++) { if (j === 0) { - json += '\n\t\t\t"' + escape(template.webgl[j]) + '"'; + json += '\n\t\t\t"' + escape(template.webgl[j]) + '"'; } else { - json += ',\n\t\t\t"' + escape(template.webgl[j]) + '"'; + json += ',\n\t\t\t"' + escape(template.webgl[j]) + '"'; } } //Closing array (make-shift JSON string to validate data in