document.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  3. // Secretary of Defense (Personnel & Readiness).
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the License
  11. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  12. // or implied. See the License for the specific language governing permissions and limitations under
  13. // the License.
  14. /// vwf/view/document extends a view interface up to the browser document. When vwf/view/document
  15. /// is active, scripts on the main page may make (reflected) kernel calls:
  16. ///
  17. /// window.vwf_view.kernel.createChild( nodeID, childName, childComponent, childURI, function( childID ) {
  18. /// ...
  19. /// } );
  20. ///
  21. /// And receive view calls:
  22. ///
  23. /// window.vwf_view.createdNode = function( nodeID, childID, childExtendsID, childImplementsIDs,
  24. /// childSource, childType, childIndex, childName, callback /- ( ready ) -/ ) {
  25. /// ...
  26. /// }
  27. ///
  28. /// @module vwf/view/document
  29. /// @requires vwf/view
  30. define( [ "module", "vwf/view", "vwf/utility"], function( module, view, utility) {
  31. return view.load( module, {
  32. // == Module Definition ====================================================================
  33. initialize: function() {
  34. window.vwf_view = this;
  35. },
  36. // == Model API ============================================================================
  37. createdNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  38. childSource, childType, childURI, childName, callback /* ( ready ) */ ) {
  39. var self = this;
  40. // At the root node of the application, load the UI chrome if available.
  41. let setInnerHtml = function(elm, html) {
  42. elm.innerHTML = html;
  43. Array.from(elm.querySelectorAll("script")).forEach(function(el) {
  44. let newEl = document.createElement("script");
  45. Array.from(el.attributes).forEach(function(el) {
  46. newEl.setAttribute(el.name, el.value)
  47. });
  48. newEl.appendChild(document.createTextNode(el.innerHTML));
  49. el.parentNode.replaceChild(newEl, el);
  50. })
  51. }
  52. if ( childID == this.kernel.application() &&
  53. ( window.location.protocol == "http:" || window.location.protocol == "https:" ) ) {
  54. // Suspend the queue.
  55. callback( false );
  56. // Load the file and insert it into the main page.
  57. // var container = jQuery( "body" ).append( "<div />" ).children( ":last" );
  58. let container = document.createElement("div");
  59. document.querySelector("body").appendChild(container);
  60. //var container = document.querySelector("body").append( "<div />" ).children( ":last" );
  61. let path = JSON.parse(localStorage.getItem('lcs_app')).path.public_path;
  62. let appName = JSON.parse(localStorage.getItem('lcs_app')).path.application.split(".").join("_");
  63. let dbPath = appName + '_html';
  64. let worldName = path.slice(1);
  65. _LCS_WORLD_USER.get('worlds').get(worldName).get(dbPath).once().then(res => {
  66. var responseText = "";
  67. if (res) {
  68. responseText = res.file;
  69. }
  70. // If the overlay attached a `createdNode` handler, forward this first call
  71. // since the overlay will have missed it.
  72. setInnerHtml(container, responseText);
  73. if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  74. self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  75. childSource, childType, childURI, childName );
  76. }
  77. // Remove the container div if an error occurred or if we received an empty
  78. // result. The server sends an empty document when the application doesn't
  79. // provide a chrome file.
  80. if ( responseText == "" ) {
  81. container.remove();
  82. }
  83. // Resume the queue.
  84. callback( true );
  85. })
  86. // fetch("admin/chrome", {
  87. // method: 'get'
  88. // }).then(function(response) {
  89. // return response.text();
  90. // }).catch(function(err) {
  91. // container.remove();
  92. // // Resume the queue.
  93. // callback( true );
  94. // }).then(function(responseText) {
  95. // // If the overlay attached a `createdNode` handler, forward this first call
  96. // // since the overlay will have missed it.
  97. // // setInnerHtml(container, responseText);
  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 ( responseText == "" ) {
  106. // container.remove();
  107. // }
  108. // // Resume the queue.
  109. // callback( true );
  110. // });
  111. // container.load( "admin/chrome", function( responseText, textStatus ) {
  112. // // If the overlay attached a `createdNode` handler, forward this first call
  113. // // since the overlay will have missed it.
  114. // if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  115. // self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  116. // childSource, childType, childURI, childName );
  117. // }
  118. // // Remove the container div if an error occurred or if we received an empty
  119. // // result. The server sends an empty document when the application doesn't
  120. // // provide a chrome file.
  121. // if ( ! ( textStatus == "success" || textStatus == "notmodified" ) || responseText == "" ) {
  122. // container.remove();
  123. // }
  124. // // Resume the queue.
  125. // callback( true );
  126. // } );
  127. }
  128. },
  129. }, function( viewFunctionName ) {
  130. // == View API =============================================================================
  131. } );
  132. } );