aboutsummaryrefslogtreecommitdiff
path: root/js/document/html-document.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/document/html-document.js')
-rwxr-xr-xjs/document/html-document.js92
1 files changed, 84 insertions, 8 deletions
diff --git a/js/document/html-document.js b/js/document/html-document.js
index 9a7755e6..3f417ffc 100755
--- a/js/document/html-document.js
+++ b/js/document/html-document.js
@@ -377,29 +377,35 @@ exports.HTMLDocument = Montage.create(TextDocument, {
377 value: function(event){ 377 value: function(event){
378 //TODO: Clean up, using for prototyping save 378 //TODO: Clean up, using for prototyping save
379 this._templateDocument = {}; 379 this._templateDocument = {};
380 this._templateDocument.html = this.iframe.contentWindow.document;
380 this._templateDocument.head = this.iframe.contentWindow.document.getElementById("userHead"); 381 this._templateDocument.head = this.iframe.contentWindow.document.getElementById("userHead");
381 this._templateDocument.body = this.documentRoot = this.iframe.contentWindow.document.getElementById("UserContent"); 382 this._templateDocument.body = this.documentRoot = this.iframe.contentWindow.document.getElementById("UserContent");
382 //TODO: Remove, also for prototyping 383 //TODO: Remove, also for prototyping
383 this.application.ninja.documentController._hackRootFlag = true; 384 this.application.ninja.documentController._hackRootFlag = true;
384 // 385 //
385 //this.documentRoot = this.iframe.contentWindow.document.getElementById("UserContent");
386 this.stageBG = this.iframe.contentWindow.document.getElementById("stageBG"); 386 this.stageBG = this.iframe.contentWindow.document.getElementById("stageBG");
387 this.stageBG.onclick = null; 387 this.stageBG.onclick = null;
388 this._document = this.iframe.contentWindow.document; 388 this._document = this.iframe.contentWindow.document;
389 this._window = this.iframe.contentWindow; 389 this._window = this.iframe.contentWindow;
390 // 390 //
391 if(!this.documentRoot.Ninja) this.documentRoot.Ninja = {}; 391 for (var k in this._document.styleSheets) {
392 if (this._document.styleSheets[k].ownerNode && this._document.styleSheets[k].ownerNode.setAttribute) {
393 this._document.styleSheets[k].ownerNode.setAttribute('ninjatemplate', 'true');
394 }
395 }
392 // 396 //
397 if(!this.documentRoot.Ninja) this.documentRoot.Ninja = {};
398 //Inserting user's document into template
393 this._templateDocument.head.innerHTML = this._userDocument.content.head; 399 this._templateDocument.head.innerHTML = this._userDocument.content.head;
394 this._templateDocument.body.innerHTML = this._userDocument.content.body; 400 this._templateDocument.body.innerHTML = this._userDocument.content.body;
395 401
396 // Adding a handler for the main user document reel to finish loading. 402 //Adding a handler for the main user document reel to finish loading
397 this._document.body.addEventListener("userTemplateDidLoad", this.userTemplateDidLoad.bind(this), false); 403 this._document.body.addEventListener("userTemplateDidLoad", this.userTemplateDidLoad.bind(this), false);
398 404
399 405
400 /* this.iframe.contentWindow.document.addEventListener('DOMSubtreeModified', function (e) { */ //TODO: Remove events upon loading once 406 /* this.iframe.contentWindow.document.addEventListener('DOMSubtreeModified', function (e) { */ //TODO: Remove events upon loading once
401 407
402 //TODO: When written, the best way to initialize the document is to listen for the DOM tree being modified 408 //TODO: When re-written, the best way to initialize the document is to listen for the DOM tree being modified
403 setTimeout(function () { 409 setTimeout(function () {
404 410
405 411
@@ -408,8 +414,47 @@ exports.HTMLDocument = Montage.create(TextDocument, {
408 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 414 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
409 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 415 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
410 if(this._document.styleSheets.length > 1) { 416 if(this._document.styleSheets.length > 1) {
411 this._styles = this._document.styleSheets[this._document.styleSheets.length - 1]; 417 //Checking all styleSheets in document
412 this._stylesheets = this._document.styleSheets; // Entire stlyesheets array 418 for (var i in this._document.styleSheets) {
419 //If rules are null, assuming cross-origin issue
420 if(this._document.styleSheets[i].rules === null) {
421 //TODO: Revisit URLs and URI creation logic, very hack right now
422 var fileUri, cssUrl, cssData, tag, query;
423 if (this._document.styleSheets[i].href.indexOf('js/document/templates/montage-html') !== -1) {
424 //Getting the url of the CSS file
425 cssUrl = this._document.styleSheets[i].href.split('js/document/templates/montage-html')[1];
426 //Creating the URI of the file (this is wrong should not be splitting cssUrl)
427 fileUri = this.application.ninja.coreIoApi.cloudData.root+this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+cssUrl.split('/')[1];
428 //Loading the data from the file
429 cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri});
430 //Creating tag with file content
431 tag = this.iframe.contentWindow.document.createElement('style');
432 tag.setAttribute('type', 'text/css');
433 tag.setAttribute('ninjauri', fileUri);
434 tag.setAttribute('ninjafileurl', cssUrl);
435 tag.setAttribute('ninjafilename', cssUrl.split('/')[cssUrl.split('/').length-1]);
436 tag.innerHTML = cssData.content;
437 //Looping through DOM to insert style tag at location of link element
438 query = this._templateDocument.html.querySelectorAll(['link']);
439 for (var j in query) {
440 if (query[j].href === this._document.styleSheets[i].href) {
441 //Disabling style sheet to reload via inserting in style tag
442 query[j].setAttribute('disabled', 'true');
443 //Inserting tag
444 this._templateDocument.head.insertBefore(tag, query[j]);
445 }
446 }
447 }
448 }
449 }
450 ////////////////////////////////////////////////////////////////////////////
451 ////////////////////////////////////////////////////////////////////////////
452
453 //TODO: Check if this is needed
454 this._stylesheets = this._document.styleSheets;
455
456 ////////////////////////////////////////////////////////////////////////////
457 ////////////////////////////////////////////////////////////////////////////
413 458
414 //TODO Finish this implementation once we start caching Core Elements 459 //TODO Finish this implementation once we start caching Core Elements
415 // Assign a model to the UserContent and add the ViewPort reference to it. 460 // Assign a model to the UserContent and add the ViewPort reference to it.
@@ -525,7 +570,38 @@ exports.HTMLDocument = Montage.create(TextDocument, {
525 value: function () { 570 value: function () {
526 //TODO: Add code view logic and also styles for HTML 571 //TODO: Add code view logic and also styles for HTML
527 if (this.currentView === 'design') { 572 if (this.currentView === 'design') {
528 return {mode: 'html', document: this._userDocument, webgl: this.glData, style: this._styles, head: this._templateDocument.head.innerHTML, body: this._templateDocument.body.innerHTML}; 573 var styles = [];
574 for (var k in this._document.styleSheets) {
575 if (this._document.styleSheets[k].ownerNode && this._document.styleSheets[k].ownerNode.getAttribute) {
576 if (this._document.styleSheets[k].ownerNode.getAttribute('ninjatemplate') === null) {
577 styles.push(this._document.styleSheets[k]);
578 }
579 }
580 }
581 return {mode: 'html', document: this._userDocument, webgl: this.glData, styles: styles, head: this._templateDocument.head.innerHTML, body: this._templateDocument.body.innerHTML};
582 } else if (this.currentView === "code"){
583 //TODO: Would this get call when we are in code of HTML?
584 } else {
585 //Error
586 }
587 }
588 },
589 ////////////////////////////////////////////////////////////////////
590 //
591 saveAll: {
592 enumerable: false,
593 value: function () {
594 //TODO: Add code view logic and also styles for HTML
595 if (this.currentView === 'design') {
596 var css = [];
597 for (var k in this._document.styleSheets) {
598 if (this._document.styleSheets[k].ownerNode && this._document.styleSheets[k].ownerNode.getAttribute) {
599 if (this._document.styleSheets[k].ownerNode.getAttribute('ninjatemplate') === null) {
600 css.push(this._document.styleSheets[k]);
601 }
602 }
603 }
604 return {mode: 'html', document: this._userDocument, webgl: this.glData, css: css, head: this._templateDocument.head.innerHTML, body: this._templateDocument.body.innerHTML};
529 } else if (this.currentView === "code"){ 605 } else if (this.currentView === "code"){
530 //TODO: Would this get call when we are in code of HTML? 606 //TODO: Would this get call when we are in code of HTML?
531 } else { 607 } else {