document.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use strict";
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  5. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
  6. */
  7. define( [ "module", "vwf/view", "vwf/utility"], function( module, view, utility) {
  8. return view.load( module, {
  9. // == Module Definition ====================================================================
  10. initialize: function() {
  11. window.vwf_view = this;
  12. },
  13. // == Model API ============================================================================
  14. createdNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  15. childSource, childType, childURI, childName, callback /* ( ready ) */ ) {
  16. var self = this;
  17. // At the root node of the application, load the UI chrome if available.
  18. if ( childID == this.kernel.application() &&
  19. ( window.location.protocol == "http:" || window.location.protocol == "https:" ) ) {
  20. // Suspend the queue.
  21. callback( false );
  22. // Load the file and insert it into the main page.
  23. // var container = jQuery( "body" ).append( "<div />" ).children( ":last" );
  24. let container = document.createElement("div");
  25. document.querySelector("body").appendChild(container);
  26. //var container = document.querySelector("body").append( "<div />" ).children( ":last" );
  27. let path = JSON.parse(localStorage.getItem('lcs_app')).path.public_path;
  28. let appName = JSON.parse(localStorage.getItem('lcs_app')).path.application.split(".").join("_");
  29. let dbPath = appName + '_html';
  30. let worldName = path.slice(1);
  31. let userDB = _LCSDB.user(_LCS_WORLD_USER.pub);
  32. function loadDoc(doc){
  33. var responseText = doc;
  34. // If the overlay attached a `createdNode` handler, forward this first call
  35. // since the overlay will have missed it.
  36. setInnerHtml(container, responseText);
  37. if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  38. self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  39. childSource, childType, childURI, childName );
  40. }
  41. // Remove the container div if an error occurred or if we received an empty
  42. // result. The server sends an empty document when the application doesn't
  43. // provide a chrome file.
  44. if ( responseText == "" ) {
  45. container.remove();
  46. }
  47. // Resume the queue.
  48. //callback( true );
  49. }
  50. userDB.get('worlds').get(worldName).load(all=>{
  51. if(all){
  52. if(Object.keys(all).includes(dbPath)){
  53. userDB.get('worlds').get(worldName).get(dbPath).get('file').load(function(res) {
  54. //let res = all[dbPath].file;
  55. loadDoc(res);
  56. callback( true );
  57. },{wait:300})
  58. } else {
  59. //NEED TO FIXED!!! Error: Callback was already called.
  60. // userDB.get('worlds').get('empty').get(dbPath).get('file').once(function(res) {
  61. // loadDoc(res);
  62. // callback( true );
  63. // },{wait:300})
  64. var emptyDoc = '<!DOCTYPE html><html><head><script type=\"text\/javascript\">\r\n\r\n vwf_view.satProperty = function (nodeID, propertyName, propertyValue) {\r\n if (propertyValue === undefined || propertyValue == null) {\r\n return;\r\n }\r\n }\r\n\r\n\r\n <\/script>\r\n<\/head>\r\n\r\n<body>\r\n<\/body>\r\n\r\n<\/html>';
  65. loadDoc(emptyDoc);
  66. callback( true );
  67. }
  68. }
  69. },{wait:300})
  70. // fetch("admin/chrome", {
  71. // method: 'get'
  72. // }).then(function(response) {
  73. // return response.text();
  74. // }).catch(function(err) {
  75. // container.remove();
  76. // // Resume the queue.
  77. // callback( true );
  78. // }).then(function(responseText) {
  79. // // If the overlay attached a `createdNode` handler, forward this first call
  80. // // since the overlay will have missed it.
  81. // // setInnerHtml(container, responseText);
  82. // if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  83. // self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  84. // childSource, childType, childURI, childName );
  85. // }
  86. // // Remove the container div if an error occurred or if we received an empty
  87. // // result. The server sends an empty document when the application doesn't
  88. // // provide a chrome file.
  89. // if ( responseText == "" ) {
  90. // container.remove();
  91. // }
  92. // // Resume the queue.
  93. // callback( true );
  94. // });
  95. // container.load( "admin/chrome", function( responseText, textStatus ) {
  96. // // If the overlay attached a `createdNode` handler, forward this first call
  97. // // since the overlay will have missed it.
  98. // if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  99. // self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  100. // childSource, childType, childURI, childName );
  101. // }
  102. // // Remove the container div if an error occurred or if we received an empty
  103. // // result. The server sends an empty document when the application doesn't
  104. // // provide a chrome file.
  105. // if ( ! ( textStatus == "success" || textStatus == "notmodified" ) || responseText == "" ) {
  106. // container.remove();
  107. // }
  108. // // Resume the queue.
  109. // callback( true );
  110. // } );
  111. }
  112. },
  113. }, function( viewFunctionName ) {
  114. // == View API =============================================================================
  115. } );
  116. function setInnerHtml (elm, html) {
  117. elm.innerHTML = html;
  118. Array.from(elm.querySelectorAll("script")).forEach(function(el) {
  119. let newEl = document.createElement("script");
  120. Array.from(el.attributes).forEach(function(el) {
  121. newEl.setAttribute(el.name, el.value)
  122. });
  123. newEl.appendChild(document.createTextNode(el.innerHTML));
  124. el.parentNode.replaceChild(newEl, el);
  125. })
  126. }
  127. } );