diff options
author | Jose Antonio Marquez Russo | 2012-02-26 11:55:39 -0800 |
---|---|---|
committer | Jose Antonio Marquez Russo | 2012-02-26 11:55:39 -0800 |
commit | c65eb4b983e8020346cda4dc7bcb050cb1a410e1 (patch) | |
tree | e9ffbe4e14d687b38369817848d6dace50bda21f /js | |
parent | 97c0efb645b1e0488112c1b7bfd7fa4864eae3dc (diff) | |
parent | e67540d23a1bd5d2480ce47634c0cbe523dd5e4a (diff) | |
download | ninja-c65eb4b983e8020346cda4dc7bcb050cb1a410e1.tar.gz |
Merge pull request #20 from joseeight/FileIO
File IO Updates
Diffstat (limited to 'js')
-rwxr-xr-x | js/document/html-document.js | 41 | ||||
-rw-r--r-- | js/mediators/io-mediator.js | 75 |
2 files changed, 92 insertions, 24 deletions
diff --git a/js/document/html-document.js b/js/document/html-document.js index 1901079d..76436732 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js | |||
@@ -381,7 +381,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
381 | // | 381 | // |
382 | for (var k in this._document.styleSheets) { | 382 | for (var k in this._document.styleSheets) { |
383 | if (this._document.styleSheets[k].ownerNode && this._document.styleSheets[k].ownerNode.setAttribute) { | 383 | if (this._document.styleSheets[k].ownerNode && this._document.styleSheets[k].ownerNode.setAttribute) { |
384 | this._document.styleSheets[k].ownerNode.setAttribute('ninjatemplate', 'true'); | 384 | this._document.styleSheets[k].ownerNode.setAttribute('data-ninja-template', 'true'); |
385 | } | 385 | } |
386 | } | 386 | } |
387 | // | 387 | // |
@@ -449,9 +449,10 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
449 | //Creating tag with file content | 449 | //Creating tag with file content |
450 | tag = this.iframe.contentWindow.document.createElement('style'); | 450 | tag = this.iframe.contentWindow.document.createElement('style'); |
451 | tag.setAttribute('type', 'text/css'); | 451 | tag.setAttribute('type', 'text/css'); |
452 | tag.setAttribute('ninjauri', fileUri); | 452 | tag.setAttribute('data-ninja-uri', fileUri); |
453 | tag.setAttribute('ninjafileurl', cssUrl); | 453 | tag.setAttribute('data-ninja-file-url', cssUrl); |
454 | tag.setAttribute('ninjafilename', cssUrl.split('/')[cssUrl.split('/').length-1]); | 454 | tag.setAttribute('data-ninja-file-read-only', JSON.parse(this.application.ninja.coreIoApi.isFileWritable({uri: fileUri}).content).readOnly); |
455 | tag.setAttribute('data-ninja-file-name', cssUrl.split('/')[cssUrl.split('/').length-1]); | ||
455 | tag.innerHTML = cssData.content; | 456 | tag.innerHTML = cssData.content; |
456 | //Looping through DOM to insert style tag at location of link element | 457 | //Looping through DOM to insert style tag at location of link element |
457 | query = this._templateDocument.html.querySelectorAll(['link']); | 458 | query = this._templateDocument.html.querySelectorAll(['link']); |
@@ -463,6 +464,38 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
463 | this._templateDocument.head.insertBefore(tag, query[j]); | 464 | this._templateDocument.head.insertBefore(tag, query[j]); |
464 | } | 465 | } |
465 | } | 466 | } |
467 | } else { | ||
468 | /* | ||
469 | //None local stylesheet, probably on a CDN (locked) | ||
470 | tag = this.iframe.contentWindow.document.createElement('style'); | ||
471 | tag.setAttribute('type', 'text/css'); | ||
472 | tag.setAttribute('data-ninja-external-url', this._document.styleSheets[i].href); | ||
473 | tag.setAttribute('data-ninja-file-read-only', "true"); | ||
474 | tag.setAttribute('data-ninja-file-name', this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1]); | ||
475 | |||
476 | //TODO: Figure out cross-domain XHR issue, might need cloud to handle | ||
477 | var xhr = new XMLHttpRequest(); | ||
478 | xhr.open("GET", this._document.styleSheets[i].href, true); | ||
479 | xhr.send(); | ||
480 | // | ||
481 | if (xhr.readyState === 4) { | ||
482 | console.log(xhr); | ||
483 | } | ||
484 | //tag.innerHTML = xhr.responseText //xhr.response; | ||
485 | |||
486 | //Currently no external styles will load if unable to load via XHR request | ||
487 | |||
488 | //Disabling external style sheets | ||
489 | query = this._templateDocument.html.querySelectorAll(['link']); | ||
490 | for (var j in query) { | ||
491 | if (query[j].href === this._document.styleSheets[i].href) { | ||
492 | //Disabling style sheet to reload via inserting in style tag | ||
493 | query[j].setAttribute('disabled', 'true'); | ||
494 | //Inserting tag | ||
495 | this._templateDocument.head.insertBefore(tag, query[j]); | ||
496 | } | ||
497 | } | ||
498 | */ | ||
466 | } | 499 | } |
467 | } | 500 | } |
468 | } | 501 | } |
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index e5eb1fc6..30180155 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js | |||
@@ -205,11 +205,11 @@ exports.IoMediator = Montage.create(Component, { | |||
205 | //Getting all CSS (style or link) tags | 205 | //Getting all CSS (style or link) tags |
206 | var styletags = template.document.content.document.getElementsByTagName('style'), | 206 | var styletags = template.document.content.document.getElementsByTagName('style'), |
207 | linktags = template.document.content.document.getElementsByTagName('link'), | 207 | linktags = template.document.content.document.getElementsByTagName('link'), |
208 | url = new RegExp(window.location.protocol+'//'+window.location.host+'/js/document/templates/montage-html/', 'gi'); | 208 | url = new RegExp(chrome.extension.getURL('js/document/templates/montage-html/'), 'gi'); |
209 | //Looping through link tags and removing file recreated elements | 209 | //Looping through link tags and removing file recreated elements |
210 | for (var j in styletags) { | 210 | for (var j in styletags) { |
211 | if (styletags[j].getAttribute) { | 211 | if (styletags[j].getAttribute) { |
212 | if(styletags[j].getAttribute('ninjauri') !== null) {//TODO: Use querySelectorAll | 212 | if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll |
213 | try { | 213 | try { |
214 | //Checking head first | 214 | //Checking head first |
215 | template.document.content.document.head.removeChild(styletags[j]); | 215 | template.document.content.document.head.removeChild(styletags[j]); |
@@ -241,7 +241,7 @@ exports.IoMediator = Montage.create(Component, { | |||
241 | if (template.styles[i].ownerNode) { | 241 | if (template.styles[i].ownerNode) { |
242 | if (template.styles[i].ownerNode.getAttribute) { | 242 | if (template.styles[i].ownerNode.getAttribute) { |
243 | //Checking for node not to be loaded from file | 243 | //Checking for node not to be loaded from file |
244 | if (template.styles[i].ownerNode.getAttribute('ninjauri') === null) { | 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 | 245 | //Inseting data from rules array into tag as string |
246 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); | 246 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); |
247 | //Syncing <style> tags count since it might be mixed with <link> | 247 | //Syncing <style> tags count since it might be mixed with <link> |
@@ -258,13 +258,13 @@ exports.IoMediator = Montage.create(Component, { | |||
258 | for(var i in template.css) { | 258 | for(var i in template.css) { |
259 | if (template.css[i].ownerNode) { | 259 | if (template.css[i].ownerNode) { |
260 | if (template.css[i].ownerNode.getAttribute) { | 260 | if (template.css[i].ownerNode.getAttribute) { |
261 | if (template.css[i].ownerNode.getAttribute('ninjauri') === null) {//TODO: Use querySelectorAll | 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 | 262 | //Inseting data from rules array into <style> as string |
263 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); | 263 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); |
264 | styleCounter++; | 264 | styleCounter++; |
265 | } else { | 265 | } else { |
266 | //Saving data from rules array converted to string into <link> file | 266 | //Saving data from rules array converted to string into <link> file |
267 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('ninjauri'), contents: this.getCssFromRules(template.css[i].cssRules)}); | 267 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: this.getCssFromRules(template.css[i].cssRules)}); |
268 | } | 268 | } |
269 | } | 269 | } |
270 | } | 270 | } |
@@ -297,19 +297,23 @@ exports.IoMediator = Montage.create(Component, { | |||
297 | webgltag.setAttribute('data-ninja-webgl', 'true'); | 297 | webgltag.setAttribute('data-ninja-webgl', 'true'); |
298 | template.document.content.document.head.appendChild(webgltag); | 298 | template.document.content.document.head.appendChild(webgltag); |
299 | } | 299 | } |
300 | // | 300 | //TODO: Add version and other data for RDGE |
301 | json = '\n\t\t({\n\t\t\t"version": "X.X.X.X",\n\t\t\t"data": ['; | 301 | json = '\n({\n\t"version": "X.X.X.X",\n\t"data": ['; |
302 | // | 302 | //Looping through data to create escaped array |
303 | for (var j=0; template.webgl[j]; j++) { | 303 | for (var j=0; template.webgl[j]; j++) { |
304 | json += '\n\t\t\t\t"'+escape(template.webgl[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 | } | ||
305 | } | 309 | } |
306 | // | 310 | //Closing array (make-shift JSON string to validate data in <script> tag) |
307 | json += ']\n\t\t})\n\t'; | 311 | json += '\n\t\t]\n})\n'; |
308 | // | 312 | //Setting string in tag |
309 | webgltag.innerHTML = json; | 313 | webgltag.innerHTML = json; |
310 | } | 314 | } |
311 | // | 315 | // |
312 | return template.document.content.document.documentElement.outerHTML.replace(url, ''); | 316 | return this.getPretyHtml(template.document.content.document.documentElement.outerHTML.replace(url, '')); |
313 | } | 317 | } |
314 | }, | 318 | }, |
315 | //////////////////////////////////////////////////////////////////// | 319 | //////////////////////////////////////////////////////////////////// |
@@ -323,18 +327,49 @@ exports.IoMediator = Montage.create(Component, { | |||
323 | if (list && list.length > 0) { | 327 | if (list && list.length > 0) { |
324 | //Adding each list item to string and also adding breaks | 328 | //Adding each list item to string and also adding breaks |
325 | for (i = 0; list[i]; i++) { | 329 | for (i = 0; list[i]; i++) { |
326 | str = list[i].cssText+' '; | 330 | css += list[i].cssText; |
327 | str = str.replace( new RegExp( "{", "gi" ), "{\n\t" ); | ||
328 | str = str.replace( new RegExp( "}", "gi" ), "}\n" ); | ||
329 | str = str.replace( new RegExp( ";", "gi" ), ";\n\t" ); | ||
330 | css += '\n'+str; | ||
331 | } | 331 | } |
332 | } | 332 | } |
333 | //TODO: Add better logic for creating this string | 333 | //TODO: Add better logic for creating this string |
334 | url = new RegExp(window.location.protocol+'//'+window.location.host+'/js/document/templates/montage-html/', 'gi'); | 334 | url = new RegExp(chrome.extension.getURL('js/document/templates/montage-html/'), 'gi'); |
335 | //Returning the CSS string | 335 | //Returning the CSS string |
336 | return css.replace(url, ''); | 336 | return this.getPretyCss(css.replace(url, '')); |
337 | } | 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 | |||