document.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. fetch("admin/chrome", {
  62. method: 'get'
  63. }).then(function(response) {
  64. return response.text();
  65. }).catch(function(err) {
  66. container.remove();
  67. // Resume the queue.
  68. callback( true );
  69. }).then(function(responseText) {
  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. // container.load( "admin/chrome", function( responseText, textStatus ) {
  87. // // If the overlay attached a `createdNode` handler, forward this first call
  88. // // since the overlay will have missed it.
  89. // if ( self.createdNode !== Object.getPrototypeOf( self ).createdNode ) {
  90. // self.createdNode( nodeID, childID, childExtendsID, childImplementsIDs,
  91. // childSource, childType, childURI, childName );
  92. // }
  93. // // Remove the container div if an error occurred or if we received an empty
  94. // // result. The server sends an empty document when the application doesn't
  95. // // provide a chrome file.
  96. // if ( ! ( textStatus == "success" || textStatus == "notmodified" ) || responseText == "" ) {
  97. // container.remove();
  98. // }
  99. // // Resume the queue.
  100. // callback( true );
  101. // } );
  102. }
  103. },
  104. }, function( viewFunctionName ) {
  105. // == View API =============================================================================
  106. } );
  107. } );