document.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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).once(res=>{
  51. if(res){
  52. if(Object.keys(res).includes(dbPath)){
  53. userDB.get('worlds').get(worldName).get(dbPath).get('file').once(function(res) {
  54. loadDoc(res);
  55. callback( true );
  56. })
  57. } else {
  58. //NEED TO FIXED!!! Error: Callback was already called.
  59. userDB.get('worlds').get('empty').get(dbPath).get('file').once(function(res) {
  60. loadDoc(res);
  61. callback( true );
  62. })
  63. // 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>';
  64. // loadDoc(emptyDoc);
  65. // callback( true );
  66. }
  67. }
  68. })
  69. // fetch("admin/chrome", {
  70. // method: 'get'
  71. // }).then(function(response) {
  72. // return response.text();
  73. // }).catch(function(err) {
  74. // container.remove();
  75. // // Resume the queue.
  76. // callback( true );
  77. // }).then(function(responseText) {
  78. // // If the overlay attached a `createdNode` handler, forward this first call
  79. // // since the overlay will have missed it.
  80. // // setInnerHtml(container, responseText);
  81. // if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  82. // self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  83. // childSource, childType, childURI, childName );
  84. // }
  85. // // Remove the container div if an error occurred or if we received an empty
  86. // // result. The server sends an empty document when the application doesn't
  87. // // provide a chrome file.
  88. // if ( responseText == "" ) {
  89. // container.remove();
  90. // }
  91. // // Resume the queue.
  92. // callback( true );
  93. // });
  94. // container.load( "admin/chrome", function( responseText, textStatus ) {
  95. // // If the overlay attached a `createdNode` handler, forward this first call
  96. // // since the overlay will have missed it.
  97. // if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  98. // self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  99. // childSource, childType, childURI, childName );
  100. // }
  101. // // Remove the container div if an error occurred or if we received an empty
  102. // // result. The server sends an empty document when the application doesn't
  103. // // provide a chrome file.
  104. // if ( ! ( textStatus == "success" || textStatus == "notmodified" ) || responseText == "" ) {
  105. // container.remove();
  106. // }
  107. // // Resume the queue.
  108. // callback( true );
  109. // } );
  110. }
  111. },
  112. }, function( viewFunctionName ) {
  113. // == View API =============================================================================
  114. } );
  115. function setInnerHtml (elm, html) {
  116. elm.innerHTML = html;
  117. Array.from(elm.querySelectorAll("script")).forEach(function(el) {
  118. let newEl = document.createElement("script");
  119. Array.from(el.attributes).forEach(function(el) {
  120. newEl.setAttribute(el.name, el.value)
  121. });
  122. newEl.appendChild(document.createTextNode(el.innerHTML));
  123. el.parentNode.replaceChild(newEl, el);
  124. })
  125. }
  126. } );