From c6b44313ff8bcc17835ca77793bf425c451a4e75 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Sat, 25 Feb 2012 16:18:27 -0800 Subject: Cleaning up ninja-data attributes Added logic to load cross-domain CSS from CDN, but need to explore how to load without impacting permissions. --- js/document/html-document.js | 46 ++++++++++++++++++++++++++++++++++++++++---- js/mediators/io-mediator.js | 8 ++++---- 2 files changed, 46 insertions(+), 8 deletions(-) (limited to 'js') diff --git a/js/document/html-document.js b/js/document/html-document.js index 1901079d..4059c2a5 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js @@ -381,7 +381,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { // for (var k in this._document.styleSheets) { if (this._document.styleSheets[k].ownerNode && this._document.styleSheets[k].ownerNode.setAttribute) { - this._document.styleSheets[k].ownerNode.setAttribute('ninjatemplate', 'true'); + this._document.styleSheets[k].ownerNode.setAttribute('data-ninja-template', 'true'); } } // @@ -449,9 +449,10 @@ exports.HTMLDocument = Montage.create(TextDocument, { //Creating tag with file content tag = this.iframe.contentWindow.document.createElement('style'); tag.setAttribute('type', 'text/css'); - tag.setAttribute('ninjauri', fileUri); - tag.setAttribute('ninjafileurl', cssUrl); - tag.setAttribute('ninjafilename', cssUrl.split('/')[cssUrl.split('/').length-1]); + tag.setAttribute('data-ninja-uri', fileUri); + tag.setAttribute('data-ninja-file-url', cssUrl); + tag.setAttribute('data-ninja-file-read-only', JSON.parse(this.application.ninja.coreIoApi.isFileWritable({uri: fileUri}).content).readOnly); + tag.setAttribute('data-ninja-file-name', cssUrl.split('/')[cssUrl.split('/').length-1]); tag.innerHTML = cssData.content; //Looping through DOM to insert style tag at location of link element query = this._templateDocument.html.querySelectorAll(['link']); @@ -463,6 +464,43 @@ exports.HTMLDocument = Montage.create(TextDocument, { this._templateDocument.head.insertBefore(tag, query[j]); } } + } else { + //None local stylesheet, probably on a CDN (locked) + //this._document.styleSheets[i].href; + tag = this.iframe.contentWindow.document.createElement('style'); + tag.setAttribute('type', 'text/css'); + tag.setAttribute('data-ninja-external-url', this._document.styleSheets[i].href); + tag.setAttribute('data-ninja-file-read-only', "true"); + tag.setAttribute('data-ninja-file-name', this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1]); + + + + //TODO: Figure out cross-domain XHR issue, might need cloud to handle + /* +var xhr = new XMLHttpRequest(); + xhr.open("GET", this._document.styleSheets[i].href, true); + xhr.send(); + // + if (xhr.readyState === 4) { + console.log(xhr); + } +*/ + + //TODO: Add rules content + //tag.innerHTML = xhr.responseText //xhr.response; + + + query = this._templateDocument.html.querySelectorAll(['link']); + for (var j in query) { + if (query[j].href === this._document.styleSheets[i].href) { + //Disabling style sheet to reload via inserting in style tag + query[j].setAttribute('disabled', 'true'); + //Inserting tag + this._templateDocument.head.insertBefore(tag, query[j]); + } + } + + } } } diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index 16bb98ea..315fb6a7 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js @@ -209,7 +209,7 @@ exports.IoMediator = Montage.create(Component, { //Looping through link tags and removing file recreated elements for (var j in styletags) { if (styletags[j].getAttribute) { - if(styletags[j].getAttribute('ninjauri') !== null) {//TODO: Use querySelectorAll + if(styletags[j].getAttribute('data-ninja-uri') !== null) {//TODO: Use querySelectorAll try { //Checking head first template.document.content.document.head.removeChild(styletags[j]); @@ -241,7 +241,7 @@ exports.IoMediator = Montage.create(Component, { if (template.styles[i].ownerNode) { if (template.styles[i].ownerNode.getAttribute) { //Checking for node not to be loaded from file - if (template.styles[i].ownerNode.getAttribute('ninjauri') === null) { + if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === null) { //Inseting data from rules array into tag as string docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); //Syncing