aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js101
1 files changed, 78 insertions, 23 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 097f5975..d81f4543 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -152,17 +152,7 @@ exports.IoMediator = Montage.create(Component, {
152 // 152 //
153 switch (file.mode) { 153 switch (file.mode) {
154 case 'html': 154 case 'html':
155 //Copy webGL library if needed 155
156 if (file.webgl && file.webgl.length > 0) {
157 for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) {
158 //Checking for RDGE library to be available
159 if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') {
160 this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(file.document.root, (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase());
161 } else {
162 //TODO: Error handle no available library to copy
163 }
164 }
165 }
166 156
167 //TODO: Add check for Monatage library to copy 157 //TODO: Add check for Monatage library to copy
168 158
@@ -335,7 +325,11 @@ exports.IoMediator = Montage.create(Component, {
335 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)); 325 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));
336 326
337 function parseNinjaUrl (url) { 327 function parseNinjaUrl (url) {
338 return this.getUrlfromNinjaUrl(url, fileRootUrl, fileUrl); 328 if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) {
329 return this.getUrlfromNinjaUrl(url, fileRootUrl, fileUrl);
330 } else {
331 return url;
332 }
339 } 333 }
340 334
341 /////////////////////////////////////////////////////////////////////////////////////////// 335 ///////////////////////////////////////////////////////////////////////////////////////////
@@ -353,15 +347,44 @@ exports.IoMediator = Montage.create(Component, {
353 } 347 }
354 } 348 }
355 //Checking for webGL elements in document 349 //Checking for webGL elements in document
356 if (template.webgl && template.webgl.length) { 350 if (template.webgl && template.webgl.length > 0) {
351 var rdgeDirName, rdgeVersion;
352 //Copy webGL library if needed
353 for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) {
354 //Checking for RDGE library to be available
355 if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') {
356 rdgeDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase();
357 rdgeVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version;
358 this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.document.root, rdgeDirName, hackRename.bind(this));
359 //TODO: Remove, this is copying the library into a static name
360 function hackRename (status) {
361 if (status) {
362 setTimeout(function () {
363 this.application.ninja.coreIoApi.copyDirectory({sourceUri: template.document.root+rdgeDirName, destUri: template.document.root+'assets'});
364 }.bind(this), 3000);
365 }
366 }
367 } else {
368 //TODO: Error handle no available library to copy
369 }
370 }
357 // 371 //
358 var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'); 372 var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'), webgljstag, webgllibtag, webglrdgetag;
359 // 373 //
360 for (var i in scripts) { 374 for (var i in scripts) {
361 if (scripts[i].getAttribute) { 375 if (scripts[i].getAttribute) {
362 if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll 376 if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll
363 matchingtags.push(scripts[i]); 377 matchingtags.push(scripts[i]);
364 } 378 }
379 if (scripts[i].getAttribute('data-ninja-webgl-js') !== null) {
380 webgljstag = scripts[i]; // TODO: Add logic to delete unneccesary tags
381 }
382 if (scripts[i].getAttribute('data-ninja-webgl-lib') !== null) {
383 webgllibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags
384 }
385 if (scripts[i].getAttribute('data-ninja-webgl-rdge') !== null) {
386 webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags
387 }
365 } 388 }
366 } 389 }
367 // 390 //
@@ -374,13 +397,47 @@ exports.IoMediator = Montage.create(Component, {
374 } 397 }
375 } 398 }
376 // 399 //
400 if (!webglrdgetag) {
401 webglrdgetag = template.document.content.document.createElement('script');
402 webglrdgetag.setAttribute('type', 'text/javascript');
403 webglrdgetag.setAttribute('src', rdgeDirName+'/rdge-compiled.js');
404 webglrdgetag.setAttribute('data-ninja-webgl-rdge', 'true');
405 template.document.content.document.head.appendChild(webglrdgetag);
406 }
407 //
408 if (!webgllibtag) {
409 webgllibtag = template.document.content.document.createElement('script');
410 webgllibtag.setAttribute('type', 'text/javascript');
411 webgllibtag.setAttribute('src', rdgeDirName+'/canvas-runtime.js');
412 webgllibtag.setAttribute('data-ninja-webgl-lib', 'true');
413 template.document.content.document.head.appendChild(webgllibtag);
414 }
415 //
377 if (!webgltag) { 416 if (!webgltag) {
378 webgltag = template.document.content.document.createElement('script'); 417 webgltag = template.document.content.document.createElement('script');
379 webgltag.setAttribute('data-ninja-webgl', 'true'); 418 webgltag.setAttribute('data-ninja-webgl', 'true');
380 template.document.content.document.head.appendChild(webgltag); 419 template.document.content.document.head.appendChild(webgltag);
381 } 420 }
382 //TODO: Add version and other data for RDGE 421 //TODO: Remove this tag and place inside JS file
383 json = '\n({\n\t"version": "X.X.X.X",\n\t"data": ['; 422 if (!webgljstag) {
423 webgljstag = template.document.content.document.createElement('script');
424 webgljstag.setAttribute('type', 'text/javascript');
425 webgljstag.setAttribute('data-ninja-webgl-js', 'true');
426 template.document.content.document.head.appendChild(webgljstag);
427 }
428 //TODO: Decide if this should be over-writter or only written on creation
429 var rootElement = 'document.body'; //TODO: Set actual root element
430 webgljstag.innerHTML = "\
431//Loading webGL/canvas data on window load\n\
432window.addEventListener('load', loadWebGL, false);\n\
433function loadWebGL (e) {\n\
434 window.removeEventListener('load', loadWebGL, false);\n\
435 //Calling method to initialize all webGL/canvas(es)\n\
436 initWebGl("+rootElement+", '"+rdgeDirName+"/');\n\
437}\
438 ";
439 //TODO: This data should be saved to a JSON file eventually
440 json = '\n({\n\t"version": "'+rdgeVersion+'",\n\t"directory": "'+rdgeDirName+'/",\n\t"data": [';
384 //Looping through data to create escaped array 441 //Looping through data to create escaped array
385 for (var j=0; template.webgl[j]; j++) { 442 for (var j=0; template.webgl[j]; j++) {
386 if (j === 0) { 443 if (j === 0) {
@@ -394,18 +451,16 @@ exports.IoMediator = Montage.create(Component, {
394 //Setting string in tag 451 //Setting string in tag
395 webgltag.innerHTML = json; 452 webgltag.innerHTML = json;
396 } 453 }
454 //Cleaning URLs from HTML
397 var cleanHTML = template.document.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)); 455 var cleanHTML = template.document.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));
398 //console.log(this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), ''))); 456 //
399 function parseNinjaRootUrl (url) { 457 function parseNinjaRootUrl (url) {
400 if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) { 458 if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) {
401 return this.getUrlfromNinjaUrl(url, rootUrl, rootUrl.replace(new RegExp((this.application.ninja.coreIoApi.rootUrl).replace(/\//gi, '\\\/'), 'gi'), '')+'file.ext');//Wrong parameters 459 return this.getUrlfromNinjaUrl(url, rootUrl, rootUrl.replace(new RegExp((this.application.ninja.coreIoApi.rootUrl).replace(/\//gi, '\\\/'), 'gi'), '')+'file.ext');
402 } else { 460 } else {
403 return url; 461 return url;
404 } 462 }
405 } 463 }
406 //console.log(rootUrl, this.application.ninja.coreIoApi.rootUrl, this.application.ninja.documentController.documentHackReference.root, this.application.ninja.coreIoApi.cloudData.root);
407 //console.log(this.getPrettyHtml(template.document.content.document.documentElement.outerHTML));
408 //return;
409 // 464 //
410 return this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), '')); 465 return this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), ''));
411 } 466 }
@@ -491,7 +546,7 @@ exports.IoMediator = Montage.create(Component, {
491 //For HTML, including any JS or CSS (single string/file) 546 //For HTML, including any JS or CSS (single string/file)
492 getPrettyHtml: { 547 getPrettyHtml: {
493 enumerable: false, 548 enumerable: false,
494 value: function (a,b){function h(){this.pos=0;this.token="";this.current_mode="CONTENT";this.tags={parent:"parent1",parentcount:1,parent1:""};this.tag_type="";this.token_text=this.last_token=this.last_text=this.token_type="";this.Utils={whitespace:"\n\r\t ".split(""),single_token:"br,input,link,meta,!doctype,basefont,base,area,hr,wbr,param,img,isindex,?xml,embed".split(","),extra_liners:"head,body,/html".split(","),in_array:function(a,b){for(var c=0;c<b.length;c++){if(a===b[c]){return true}}return false}};this.get_content=function(){var a="";var b=[];var c=false;while(this.input.charAt(this.pos)!=="<"){if(this.pos>=this.input.length){return b.length?b.join(""):["","TK_EOF"]}a=this.input.charAt(this.pos);this.pos++;this.line_char_count++;if(this.Utils.in_array(a,this.Utils.whitespace)){if(b.length){c=true}this.line_char_count--;continue}else if(c){if(this.line_char_count>=this.max_char){b.push("\n");for(var d=0;d<this.indent_level;d++){b.push(this.indent_string)}this.line_char_count=0}else{b.push(" ");this.line_char_count++}c=false}b.push(a)}return b.length?b.join(""):""};this.get_contents_to=function(a){if(this.pos==this.input.length){return["","TK_EOF"]}var b="";var c="";var d=new RegExp("</"+a+"\\s*>","igm");d.lastIndex=this.pos;var e=d.exec(this.input);var f=e?e.index:this.input.length;if(this.pos<f){c=this.input.substring(this.pos,f);this.pos=f}return c};this.record_tag=function(a){if(this.tags[a+"count"]){this.tags[a+"count"]++;this.tags[a+this.tags[a+"count"]]=this.indent_level}else{this.tags[a+"count"]=1;this.tags[a+this.tags[a+"count"]]=this.indent_level}this.tags[a+this.tags[a+"count"]+"parent"]=this.tags.parent;this.tags.parent=a+this.tags[a+"count"]};this.retrieve_tag=function(a){if(this.tags[a+"count"]){var b=this.tags.parent;while(b){if(a+this.tags[a+"count"]===b){break}b=this.tags[b+"parent"]}if(b){this.indent_level=this.tags[a+this.tags[a+"count"]];this.tags.parent=this.tags[b+"parent"]}delete this.tags[a+this.tags[a+"count"]+"parent"];delete this.tags[a+this.tags[a+"count"]];if(this.tags[a+"count"]==1){delete this.tags[a+"count"]}else{this.tags[a+"count"]--}}};this.get_tag=function(){var a="";var b=[];var c=false;do{if(this.pos>=this.input.length){return b.length?b.join(""):["","TK_EOF"]}a=this.input.charAt(this.pos);this.pos++;this.line_char_count++;if(this.Utils.in_array(a,this.Utils.whitespace)){c=true;this.line_char_count--;continue}if(a==="'"||a==='"'){if(!b[1]||b[1]!=="!"){a+=this.get_unformatted(a);c=true}}if(a==="="){c=false}if(b.length&&b[b.length-1]!=="="&&a!==">"&&c){if(this.line_char_count>=this.max_char){this.print_newline(false,b);this.line_char_count=0}else{b.push(" ");this.line_char_count++}c=false}b.push(a)}while(a!==">");var d=b.join("");var e;if(d.indexOf(" ")!=-1){e=d.indexOf(" ")}else{e=d.indexOf(">")}var f=d.substring(1,e).toLowerCase();if(d.charAt(d.length-2)==="/"||this.Utils.in_array(f,this.Utils.single_token)){this.tag_type="SINGLE"}else if(f==="script"){this.record_tag(f);this.tag_type="SCRIPT"}else if(f==="style"){this.record_tag(f);this.tag_type="STYLE"}else if(this.Utils.in_array(f,unformatted)){var g=this.get_unformatted("</"+f+">",d);b.push(g);this.tag_type="SINGLE"}else if(f.charAt(0)==="!"){if(f.indexOf("[if")!=-1){if(d.indexOf("!IE")!=-1){var g=this.get_unformatted("-->",d);b.push(g)}this.tag_type="START"}else if(f.indexOf("[endif")!=-1){this.tag_type="END";this.unindent()}else if(f.indexOf("[cdata[")!=-1){var g=this.get_unformatted("]]>",d);b.push(g);this.tag_type="SINGLE"}else{var g=this.get_unformatted("-->",d);b.push(g);this.tag_type="SINGLE"}}else{if(f.charAt(0)==="/"){this.retrieve_tag(f.substring(1));this.tag_type="END"}else{this.record_tag(f);this.tag_type="START"}if(this.Utils.in_array(f,this.Utils.extra_liners)){this.print_newline(true,this.output)}}return b.join("")};this.get_unformatted=function(a,b){if(b&&b.indexOf(a)!=-1){return""}var c="";var d="";var e=true;do{if(this.pos>=this.input.length){return d}c=this.input.charAt(this.pos);this.pos++;if(this.Utils.in_array(c,this.Utils.whitespace)){if(!e){this.line_char_count--;continue}if(c==="\n"||c==="\r"){d+="\n";this.line_char_count=0;continue}}d+=c;this.line_char_count++;e=true}while(d.indexOf(a)==-1);return d};this.get_token=function(){var a;if(this.last_token==="TK_TAG_SCRIPT"||this.last_token==="TK_TAG_STYLE"){var b=this.last_token.substr(7);a=this.get_contents_to(b);if(typeof a!=="string"){return a}return[a,"TK_"+b]}if(this.current_mode==="CONTENT"){a=this.get_content();if(typeof a!=="string"){return a}else{return[a,"TK_CONTENT"]}}if(this.current_mode==="TAG"){a=this.get_tag();if(typeof a!=="string"){return a}else{var c="TK_TAG_"+this.tag_type;return[a,c]}}};this.get_full_indent=function(a){a=this.indent_level+a||0;if(a<1)return"";return Array(a+1).join(this.indent_string)};this.printer=function(a,b,c,d,e){this.input=a||"";this.output=[];this.indent_character=b;this.indent_string="";this.indent_size=c;this.brace_style=e;this.indent_level=0;this.max_char=d;this.line_char_count=0;for(var f=0;f<this.indent_size;f++){this.indent_string+=this.indent_character}this.print_newline=function(a,b){this.line_char_count=0;if(!b||!b.length){return}if(!a){while(this.Utils.in_array(b[b.length-1],this.Utils.whitespace)){b.pop()}}b.push("\n");for(var c=0;c<this.indent_level;c++){b.push(this.indent_string)}};this.print_token=function(a){this.output.push(a)};this.indent=function(){this.indent_level++};this.unindent=function(){if(this.indent_level>0){this.indent_level--}}};return this}var c,d,e,f,g;b=b||{};d=b.indent_size||4;e=b.indent_char||" ";g=b.brace_style||"collapse";f=b.max_char||"500";unformatted=b.unformatted||["a"];c=new h;c.printer(a,e,d,f,g);while(true){var i=c.get_token();c.token_text=i[0];c.token_type=i[1];if(c.token_type==="TK_EOF"){break}switch(c.token_type){case"TK_TAG_START":c.print_newline(false,c.output);c.print_token(c.token_text);c.indent();c.current_mode="CONTENT";break;case"TK_TAG_STYLE":case"TK_TAG_SCRIPT":c.print_newline(false,c.output);c.print_token(c.token_text);c.current_mode="CONTENT";break;case"TK_TAG_END":if(c.last_token==="TK_CONTENT"&&c.last_text===""){var j=c.token_text.match(/\w+/)[0];var k=c.output[c.output.length-1].match(/<\s*(\w+)/);if(k===null||k[1]!==j)c.print_newline(true,c.output)}c.print_token(c.token_text);c.current_mode="CONTENT";break;case"TK_TAG_SINGLE":c.print_newline(false,c.output);c.print_token(c.token_text);c.current_mode="CONTENT";break;case"TK_CONTENT":if(c.token_text!==""){c.print_token(c.token_text)}c.current_mode="TAG";break;case"TK_STYLE":case"TK_SCRIPT":if(c.token_text!==""){c.output.push("\n");var l=c.token_text;if(c.token_type=="TK_SCRIPT"){var m=typeof js_beautify=="function"&&js_beautify}else if(c.token_type=="TK_STYLE"){var m=typeof this.getPrettyCss=="function"&&this.getPrettyCss}if(b.indent_scripts=="keep"){var n=0}else if(b.indent_scripts=="separate"){var n=-c.indent_level}else{var n=1}var o=c.get_full_indent(n);if(m){l=m(l.replace(/^\s*/,o),b)}else{var p=l.match(/^\s*/)[0];var q=p.match(/[^\n\r]*$/)[0].split(c.indent_string).length-1;var r=c.get_full_indent(n-q);l=l.replace(/^\s*/,o).replace(/\r\n|\r|\n/g,"\n"+r).replace(/\s*$/,"")}if(l){c.print_token(l);c.print_newline(true,c.output)}}c.current_mode="TAG";break}c.last_token=c.token_type;c.last_text=c.token_text}return c.output.join("")}