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.js166
1 files changed, 153 insertions, 13 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 56869839..30180155 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -195,18 +195,125 @@ exports.IoMediator = Montage.create(Component, {
195 } 195 }
196 }, 196 },
197 //////////////////////////////////////////////////////////////////// 197 ////////////////////////////////////////////////////////////////////
198 //TODO: Expand to allow more templates 198 //TODO: Expand to allow more templates, clean up variables
199 parseNinjaTemplateToHtml: { 199 parseNinjaTemplateToHtml: {
200 enumerable: false, 200 enumerable: false,
201 value: function (template) { 201 value: function (template) {
202 // 202 //Injecting head and body into old document
203 template.document.content.document.body.innerHTML = template.body; 203 template.document.content.document.body.innerHTML = template.body;
204 template.document.content.document.head.innerHTML = template.head; 204 template.document.content.document.head.innerHTML = template.head;
205 //TODO: Remove temp fix for styles 205 //Getting all CSS (style or link) tags
206 if (template.style) { 206 var styletags = template.document.content.document.getElementsByTagName('style'),
207 template.document.content.document.head.getElementsByTagName('style')[0].innerHTML = this.getCssFromRules(template.style.cssRules); 207 linktags = template.document.content.document.getElementsByTagName('link'),
208 url = new RegExp(chrome.extension.getURL('js/document/templates/montage-html/'), 'gi');
209 //Looping through link tags and removing file recreated elements
210 for (var j in styletags) {
211 if (styletags[j].getAttribute) {
212 if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll
213 try {
214 //Checking head first
215 template.document.content.document.head.removeChild(styletags[j]);
216 } catch (e) {
217 try {
218 //Checking body if not in head
219 template.document.content.document.body.removeChild(styletags[j]);
220 } catch (e) {
221 //Error, not found!
222 }
223 }
224
225 }
226 }
208 } 227 }
209 return template.document.content.document.documentElement.outerHTML; 228 //TODO: Add logic to only enble tags we disabled
229 for (var l in linktags) {
230 if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll
231 linktags[l].removeAttribute('disabled');
232 }
233 }
234 //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS)
235 if (template.styles) {
236 //Getting all style tags
237 var styleCounter = 0,
238 docStyles = template.document.content.document.getElementsByTagName('style');
239 //Looping through all style tags
240 for(var i in template.styles) {
241 if (template.styles[i].ownerNode) {
242 if (template.styles[i].ownerNode.getAttribute) {
243 //Checking for node not to be loaded from file
244 if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.styles[i].ownerNode.getAttribute('data-ninja-template')) {
245 //Inseting data from rules array into tag as string
246 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules);
247 //Syncing <style> tags count since it might be mixed with <link>
248 styleCounter++;
249 }
250 }
251 }
252 }
253 } else if (template.css) {
254 //Getting all style and link tags
255 var styleCounter = 0,
256 docStyles = template.document.content.document.getElementsByTagName('style'),
257 docLinks = template.document.content.document.getElementsByTagName('link');
258 for(var i in template.css) {
259 if (template.css[i].ownerNode) {
260 if (template.css[i].ownerNode.getAttribute) {
261 if (template.css[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.css[i].ownerNode.getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll
262 //Inseting data from rules array into <style> as string
263 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules);
264 styleCounter++;
265 } else {
266 //Saving data from rules array converted to string into <link> file
267 var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: this.getCssFromRules(template.css[i].cssRules)});
268 }
269 }
270 }
271 }
272 }
273 //Checking for webGL elements in document
274 if (template.webgl.length) {
275 //
276 var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script');
277 //
278 for (var i in scripts) {
279 if (scripts[i].getAttribute) {
280 if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll
281 matchingtags.push(scripts[i]);
282 }
283 }
284 }
285 //
286 if (matchingtags.length) {
287 if (matchingtags.length === 1) {
288 webgltag = matchingtags[0];
289 } else {
290 //TODO: Add logic to handle multiple tags, perhaps combine to one
291 webgltag = matchingtags[matchingtags.length-1]; //Saving all data to last one...
292 }
293 }
294 //
295 if (!webgltag) {
296 webgltag = template.document.content.document.createElement('script');
297 webgltag.setAttribute('data-ninja-webgl', 'true');
298 template.document.content.document.head.appendChild(webgltag);
299 }
300 //TODO: Add version and other data for RDGE
301 json = '\n({\n\t"version": "X.X.X.X",\n\t"data": [';
302 //Looping through data to create escaped array
303 for (var j=0; template.webgl[j]; j++) {
304 if (j === 0) {
305 json += '\n\t\t\t"'+escape(template.webgl[j])+'"';
306 } else {
307 json += ',\n\t\t\t"'+escape(template.webgl[j])+'"';
308 }
309 }
310 //Closing array (make-shift JSON string to validate data in <script> tag)
311 json += '\n\t\t]\n})\n';
312 //Setting string in tag
313 webgltag.innerHTML = json;
314 }
315 //
316 return this.getPretyHtml(template.document.content.document.documentElement.outerHTML.replace(url, ''));
210 } 317 }
211 }, 318 },
212 //////////////////////////////////////////////////////////////////// 319 ////////////////////////////////////////////////////////////////////
@@ -215,21 +322,54 @@ exports.IoMediator = Montage.create(Component, {
215 enumerable: false, 322 enumerable: false,
216 value: function (list) { 323 value: function (list) {
217 //Variable to store CSS definitions 324 //Variable to store CSS definitions
218 var i, str, css = ''; 325 var i, str, url, css = '';
219 //Looping through list 326 //Looping through list
220 if (list && list.length > 0) { 327 if (list && list.length > 0) {
221 //Adding each list item to string and also adding breaks 328 //Adding each list item to string and also adding breaks
222 for (i = 0; list[i]; i++) { 329 for (i = 0; list[i]; i++) {
223 str = list[i].cssText+' '; 330 css += list[i].cssText;
224 str = str.replace( new RegExp( "{", "gi" ), "{\n\t" );
225 str = str.replace( new RegExp( "}", "gi" ), "}\n" );
226 str = str.replace( new RegExp( ";", "gi" ), ";\n\t" );
227 css += '\n'+str;
228 } 331 }
229 } 332 }
333 //TODO: Add better logic for creating this string
334 url = new RegExp(chrome.extension.getURL('js/document/templates/montage-html/'), 'gi');
230 //Returning the CSS string 335 //Returning the CSS string
231 return css; 336 return this.getPretyCss(css.replace(url, ''));
232 } 337 }
338 },
339 ////////////////////////////////////////////////////////////////////
340 //Using prettification code from http://jsbeautifier.org
341 /*
342 Copyright (c) 2009 - 2011, Einar Lielmanis
343
344 Permission is hereby granted, free of charge, to any person
345 obtaining a copy of this software and associated documentation
346 files (the "Software"), to deal in the Software without
347 restriction, including without limitation the rights to use,
348 copy, modify, merge, publish, distribute, sublicense, and/or sell
349 copies of the Software, and to permit persons to whom the
350 Software is furnished to do so, subject to the following
351 conditions:
352
353 The above copyright notice and this permission notice shall be
354 included in all copies or substantial portions of the Software.
355
356 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
357 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
358 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
359 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
360 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
361 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
362 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
363 OTHER DEALINGS IN THE SOFTWARE.
364 */
365 getPretyHtml: {
366 enumerable: false,
367 value: function style_html(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}