aboutsummaryrefslogtreecommitdiff
path: root/js/document/mediators/template.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/document/mediators/template.js')
-rwxr-xr-xjs/document/mediators/template.js100
1 files changed, 70 insertions, 30 deletions
diff --git a/js/document/mediators/template.js b/js/document/mediators/template.js
index 068a1f48..50fad1c3 100755
--- a/js/document/mediators/template.js
+++ b/js/document/mediators/template.js
@@ -8,7 +8,8 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
8// 8//
9var Montage = require("montage/core/core").Montage, 9var Montage = require("montage/core/core").Montage,
10 Component = require("montage/ui/component").Component, 10 Component = require("montage/ui/component").Component,
11 TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator; 11 TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator,
12 ClassUuid = require("js/components/core/class-uuid").ClassUuid;
12//////////////////////////////////////////////////////////////////////// 13////////////////////////////////////////////////////////////////////////
13// 14//
14exports.TemplateDocumentMediator = Montage.create(Component, { 15exports.TemplateDocumentMediator = Montage.create(Component, {
@@ -83,9 +84,24 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
83 parseHtmlToNinjaTemplate: { 84 parseHtmlToNinjaTemplate: {
84 value: function (html) { 85 value: function (html) {
85 //Creating temp object to mimic HTML 86 //Creating temp object to mimic HTML
86 var doc = window.document.implementation.createHTMLDocument(), template; 87 var doc = window.document.implementation.createHTMLDocument(), template, docHtmlTag,
88 hackHtml = document.createElement('html'), hackTag;
87 //Setting content to temp 89 //Setting content to temp
88 doc.getElementsByTagName('html')[0].innerHTML = html; 90 doc.getElementsByTagName('html')[0].innerHTML = html;
91 //TODO: Improve this, very bad way of copying attributes (in a pinch to get it working)
92 hackHtml.innerHTML = html.replace(/html/gi, 'ninjahtmlhack');
93 hackTag = hackHtml.getElementsByTagName('ninjahtmlhack')[0];
94 docHtmlTag = doc.getElementsByTagName('html')[0];
95 //Looping through the attributes to copy them
96 if (hackTag) {
97 for (var m in hackTag.attributes) {
98 if (hackTag.attributes[m].value) {
99 docHtmlTag.setAttribute(hackTag.attributes[m].name.replace(/ninjahtmlhack/gi, 'html'), hackTag.attributes[m].value.replace(/ninjahtmlhack/gi, 'html'));
100 }
101 }
102 }
103 //Garbage collection
104 hackHtml = hackTag = null;
89 //Creating return object 105 //Creating return object
90 return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc}; 106 return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc};
91 } 107 }
@@ -93,7 +109,7 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
93 //////////////////////////////////////////////////////////////////// 109 ////////////////////////////////////////////////////////////////////
94 //TODO: Expand to allow more templates, clean up variables 110 //TODO: Expand to allow more templates, clean up variables
95 parseNinjaTemplateToHtml: { 111 parseNinjaTemplateToHtml: {
96 value: function (template, ninjaWrapper, libCopyCallback) { 112 value: function (saveExternalData, template, ninjaWrapper, libCopyCallback) {
97 //TODO: Improve reference for rootUrl 113 //TODO: Improve reference for rootUrl
98 var regexRootUrl, 114 var regexRootUrl,
99 rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])), 115 rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])),
@@ -116,20 +132,36 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
116 template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, ''); 132 template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, '');
117 template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, ''); 133 template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, '');
118 } 134 }
135 //Removes all attributes from node
136 function wipeAttributes (node) {
137 for (var f in node.attributes) {
138 node.removeAttribute(node.attributes[f].name);
139 }
140 }
119 //Copying attributes to maintain same properties as the <body> 141 //Copying attributes to maintain same properties as the <body>
142 wipeAttributes(template.file.content.document.body);
120 for (var n in template.body.attributes) { 143 for (var n in template.body.attributes) {
121 if (template.body.attributes[n].value) { 144 if (template.body.attributes[n].value) {
122 //
123 template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value); 145 template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value);
124 } 146 }
125 } 147 }
126 148 wipeAttributes(template.file.content.document.head);
127 149 //Copying attributes to maintain same properties as the <head>
128 150 for (var m in template.document.head.attributes) {
129 //TODO: Add attribute copying for <HEAD> and <HTML> 151 if (template.document.head.attributes[m].value) {
130 152 template.file.content.document.head.setAttribute(template.document.head.attributes[m].name, template.document.head.attributes[m].value);
131 153 }
132 154 }
155 //Copying attributes to maintain same properties as the <html>
156 var htmlTagMem = template.document.getElementsByTagName('html')[0], htmlTagDoc = template.file.content.document.getElementsByTagName('html')[0];
157 wipeAttributes(htmlTagDoc);
158 for (var m in htmlTagMem.attributes) {
159 if (htmlTagMem.attributes[m].value) {
160 if (htmlTagMem.attributes[m].value.replace(/montage-app-bootstrapping/gi, '').length>0) {
161 htmlTagDoc.setAttribute(htmlTagMem.attributes[m].name, htmlTagMem.attributes[m].value.replace(/ montage-app-bootstrapping/gi, ''));
162 }
163 }
164 }
133 //Getting list of current nodes (Ninja DOM) 165 //Getting list of current nodes (Ninja DOM)
134 presentNodes = template.file.content.document.getElementsByTagName('*'); 166 presentNodes = template.file.content.document.getElementsByTagName('*');
135 //Looping through nodes to determine origin and removing if not inserted by Ninja 167 //Looping through nodes to determine origin and removing if not inserted by Ninja
@@ -328,7 +360,7 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
328 360
329 361
330 362
331 //TODO: Make proper webGL/Canvas method 363
332 364
333 // 365 //
334 var webgltag, webgllibtag, webglrdgetag, mjstag, mjslibtag, matchingtags = [], 366 var webgltag, webgllibtag, webglrdgetag, mjstag, mjslibtag, matchingtags = [],
@@ -357,19 +389,33 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
357 } 389 }
358 } 390 }
359 } 391 }
392
393
394
395
396 //TODO: Make proper webGL/Canvas method
397
398
360 //Checking for webGL elements in document 399 //Checking for webGL elements in document
361 if (template.webgl && template.webgl.length > 1) {//TODO: Should be length 0, hack for a temp fix 400 if (template.webgl && template.webgl.length > 1) {//TODO: Should be length 0, hack for a temp fix
362 var rdgeDirName, rdgeVersion, cvsDataDir = this.getCanvasDirectory(template.file.root), fileCvsDir, fileCvsDirAppend, cvsDirCounter = 1, fileOrgDataSrc; 401 var rdgeDirName, rdgeVersion, cvsDataDir = this.getCanvasDirectory(template.file.root), fileCvsDir, fileCvsDirAppend, cvsDirCounter = 1, fileOrgDataSrc;
363 // 402 //
364 if (cvsDataDir && !matchingtags.length && !webgllibtag) { 403 if (cvsDataDir && !matchingtags.length && !webgllibtag) {
365 fileCvsDir = cvsDataDir+template.file.name.split('.'+template.file.extension)[0]; 404
405 if (template.libs.canvasId) {
406 libsobserver.canvasId = template.libs.canvasId;
407 } else {
408 libsobserver.canvasId = ClassUuid.generate();
409 }
410
411 //Creating data directory, will include materials at a later time
412 fileCvsDir = cvsDataDir+template.file.name.split('.'+template.file.extension)[0]+'_'+libsobserver.canvasId;
413
366 if (!this._getUserDirectory(fileCvsDir)) { 414 if (!this._getUserDirectory(fileCvsDir)) {
367 fileCvsDirAppend = fileCvsDir+cvsDirCounter; 415 //TODO: create proper logic not to overwrite files
368 while (!this._getUserDirectory(fileCvsDirAppend)) { 416 console.log('error');
369 fileCvsDirAppend = fileCvsDir+(cvsDirCounter++);
370 }
371 } 417 }
372 //TODO: Allow user overwrite 418
373 fileCvsDir += '/'; 419 fileCvsDir += '/';
374 } else if (webgllibtag && webgllibtag.getAttribute && webgllibtag.getAttribute('data-ninja-canvas-json') !== null) { 420 } else if (webgllibtag && webgllibtag.getAttribute && webgllibtag.getAttribute('data-ninja-canvas-json') !== null) {
375 fileOrgDataSrc = template.file.root+webgllibtag.getAttribute('data-ninja-canvas-json'); 421 fileOrgDataSrc = template.file.root+webgllibtag.getAttribute('data-ninja-canvas-json');
@@ -437,17 +483,9 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
437 //Looping through data to create escaped array 483 //Looping through data to create escaped array
438 for (var j = 0; template.webgl[j]; j++) { 484 for (var j = 0; template.webgl[j]; j++) {
439 if (j === 0) { 485 if (j === 0) {
440 //if (fileCvsDir) { 486 json += '\n\t\t\t"' + escape(template.webgl[j]) + '"';
441 // json += '\n\t\t\t"' + template.webgl[j] + '"';
442 //} else {
443 json += '\n\t\t\t"' + escape(template.webgl[j]) + '"';
444 //}
445 } else { 487 } else {
446 //if (fileCvsDir) { 488 json += ',\n\t\t\t"' + escape(template.webgl[j]) + '"';
447 // json += ',\n\t\t\t"' + template.webgl[j] + '"';
448 //} else {
449 json += ',\n\t\t\t"' + escape(template.webgl[j]) + '"';
450 //}
451 } 489 }
452 } 490 }
453 //Closing array (make-shift JSON string to validate data in <script> tag) 491 //Closing array (make-shift JSON string to validate data in <script> tag)
@@ -575,6 +613,8 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
575 } else { 613 } else {
576 cleanHTML = template.file.content.document.documentElement.outerHTML.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaRootUrl.bind(this)); 614 cleanHTML = template.file.content.document.documentElement.outerHTML.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaRootUrl.bind(this));
577 } 615 }
616 //TODO: Remove, this is a temp hack to maintain a doc type on HTML files
617 cleanHTML = '<!DOCTYPE html>'+cleanHTML;
578 // 618 //
579 function parseNinjaRootUrl(url) { 619 function parseNinjaRootUrl(url) {
580 if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) { 620 if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) {
@@ -590,9 +630,9 @@ exports.TemplateDocumentMediator = Montage.create(Component, {
590 } 630 }
591 // 631 //
592 if (libsobserver.montage || libsobserver.canvas) { 632 if (libsobserver.montage || libsobserver.canvas) {
593 return {content: this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), '')), libs: true}; 633 return {content: this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), '')), libs: true, montageId: libsobserver.montageId, canvasId: libsobserver.canvasId};
594 } else { 634 } else {
595 return {content: this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), '')), libs: false}; 635 return {content: this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), '')), libs: false, montageId: libsobserver.montageId, canvasId: libsobserver.canvasId};
596 } 636 }
597 } 637 }
598 }, 638 },