view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
  5. */
  6. /// FROM FABRIC.JS
  7. /// vwf/view.js is the common implementation of all Virtual World Framework views. Views
  8. /// interpret information from the simulation, present it to the user, and accept user input
  9. /// influencing the simulation.
  10. ///
  11. /// Views are outside of the simulation. Unlike models, they may accept external input--such as
  12. /// pointer and key events from a user--but may only affect the simulation indirectly through the
  13. /// synchronization server.
  14. ///
  15. /// vwf/view and all deriving views are loaded as RequireJS (http://requirejs.org) modules.
  16. /// @module vwf/kernel/view
  17. /// @requires vwf/view
  18. import { Fabric } from '/core/vwf/fabric.js';
  19. class ViewKernel extends Fabric {
  20. constructor(module) {
  21. //console.log("ViewKernel constructor");
  22. super(module, "View")
  23. this.module = module;
  24. //this.view = this;
  25. // new Fabric({
  26. // id:"vwf/view"
  27. // }, 'View');
  28. }
  29. factory(){
  30. return this.load( this.module, {
  31. // == Module Definition ====================================================================
  32. }, function( viewFunctionName ) {
  33. // == View API =============================================================================
  34. // The kernel bypasses vwf/kernel/view and calls directly into the first driver stage.
  35. return undefined;
  36. }, function( kernelFunctionName ) {
  37. // == Kernel API ===========================================================================
  38. switch ( kernelFunctionName ) {
  39. // -- Read-write functions -------------------------------------------------------------
  40. // TODO: setState
  41. // TODO: getState
  42. // TODO: hashState
  43. case "createNode":
  44. return function( nodeComponent, nodeAnnotation, baseURI, when, callback /* nodeID */ ) {
  45. // Interpret `createNode( nodeComponent, when, callback )` as
  46. // `createNode( nodeComponent, undefined, undefined, when, callback )` and
  47. // `createNode( nodeComponent, nodeAnnotation, when, callback )` as
  48. // `createNode( nodeComponent, nodeAnnotation, undefined, when, callback )`.
  49. // `nodeAnnotation` was added in 0.6.12, and `baseURI` was added in 0.6.25.
  50. if ( typeof baseURI == "function" || baseURI instanceof Function ) {
  51. callback = baseURI;
  52. when = nodeAnnotation;
  53. baseURI = undefined;
  54. nodeAnnotation = undefined;
  55. } else if ( typeof when == "function" || when instanceof Function ) {
  56. callback = when;
  57. when = baseURI;
  58. baseURI = undefined;
  59. }
  60. // Make the call.
  61. this.kernel.virtualTime.send( undefined, kernelFunctionName, undefined,
  62. [ nodeComponent, nodeAnnotation ], when || 0, callback /* result */ );
  63. };
  64. case "deleteNode":
  65. return function( nodeID, when, callback ) {
  66. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  67. undefined, when || 0, callback /* result */ );
  68. };
  69. // TODO: setNode
  70. // TODO: getNode
  71. case "createChild":
  72. return function( nodeID, childName, childComponent, childURI, when, callback /* childID */ ) {
  73. this.kernel.virtualTime.send( nodeID, kernelFunctionName, childName,
  74. [ childComponent, childURI ], when || 0, callback /* result */ );
  75. };
  76. case "deleteChild":
  77. return function( nodeID, childName, when, callback ) {
  78. this.kernel.virtualTime.send( nodeID, kernelFunctionName, childName,
  79. undefined, when || 0, callback /* result */ );
  80. };
  81. case "addChild":
  82. return function( nodeID, childID, childName, when, callback ) {
  83. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  84. [ childID, childName ], when || 0, callback /* result */ ); // TODO: swap childID & childName?
  85. };
  86. case "removeChild":
  87. return function( nodeID, childID, when, callback ) {
  88. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  89. [ childID ], when || 0, callback /* result */ ); // TODO: swap childID & childName?
  90. };
  91. case "setProperties":
  92. return function( nodeID, properties, when, callback ) {
  93. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  94. [ properties ], when || 0, callback /* result */ );
  95. };
  96. case "getProperties":
  97. return function( nodeID, when, callback ) {
  98. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  99. undefined, when || 0, callback /* result */ );
  100. };
  101. case "createProperty":
  102. return function( nodeID, propertyName, propertyValue, propertyGet, propertySet, when, callback ) {
  103. this.kernel.virtualTime.send( nodeID, kernelFunctionName, propertyName,
  104. [ propertyValue, propertyGet, propertySet ], when || 0, callback /* result */ ); // TODO: { value: propertyValue, get: propertyGet, set: propertySet } ? -- vwf.receive() needs to parse
  105. };
  106. // TODO: deleteProperty
  107. case "setProperty":
  108. return function( nodeID, propertyName, propertyValue, when, callback ) {
  109. this.kernel.virtualTime.send( nodeID, kernelFunctionName, propertyName,
  110. [ propertyValue ], when || 0, callback /* result */ );
  111. };
  112. case "getProperty":
  113. return function( nodeID, propertyName, when, callback ) {
  114. this.kernel.virtualTime.send( nodeID, kernelFunctionName, propertyName,
  115. undefined, when || 0, callback /* result */ );
  116. };
  117. case "createMethod":
  118. return function( nodeID, methodName, methodParameters, methodBody, when, callback ) {
  119. this.kernel.virtualTime.send( nodeID, kernelFunctionName, methodName,
  120. [ methodParameters, methodBody ], when || 0, callback /* result */ ); // TODO: { parameters: methodParameters, body: methodBody } ? -- vwf.receive() needs to parse
  121. };
  122. // TODO: deleteMethod
  123. case "setMethod":
  124. return function( nodeID, methodName, methodHandler, when, callback ) {
  125. this.kernel.virtualTime.send( nodeID, kernelFunctionName, methodName,
  126. [ methodHandler ], when || 0, callback /* result */ );
  127. };
  128. case "getMethod":
  129. return function( nodeID, methodName, when, callback ) {
  130. this.kernel.virtualTime.send( nodeID, kernelFunctionName, methodName,
  131. undefined, when || 0, callback /* result */ );
  132. };
  133. case "callMethod":
  134. return function( nodeID, methodName, methodParameters, when, callback ) {
  135. this.kernel.virtualTime.send( nodeID, kernelFunctionName, methodName,
  136. [ methodParameters ], when || 0, callback /* result */ );
  137. };
  138. case "createEvent":
  139. return function( nodeID, eventName, eventParameters, when, callback ) {
  140. this.kernel.virtualTime.send( nodeID, kernelFunctionName, eventName,
  141. [ eventParameters ], when || 0, callback /* result */ );
  142. };
  143. // TODO: deleteEvent
  144. case "addEventListener":
  145. return function( nodeID, eventName, eventHandler, eventContextID, eventPhases, when, callback ) {
  146. this.kernel.virtualTime.send( nodeID, kernelFunctionName, eventName,
  147. [ eventHandler, eventContextID, eventPhases ], when || 0, callback /* result */ );
  148. };
  149. case "removeEventListener":
  150. return function( nodeID, eventName, eventListenerID, when, callback ) {
  151. this.kernel.virtualTime.send( nodeID, kernelFunctionName, eventName,
  152. [ eventListenerID ], when || 0, callback /* result */ );
  153. };
  154. case "flushEventListeners":
  155. return function( nodeID, eventName, eventContextID, when, callback ) {
  156. this.kernel.virtualTime.send( nodeID, kernelFunctionName, eventName,
  157. [ eventContextID ], when || 0, callback /* result */ );
  158. };
  159. case "fireEvent":
  160. return function( nodeID, eventName, eventParameters, when, callback ) {
  161. this.kernel.virtualTime.send( nodeID, kernelFunctionName, eventName,
  162. [ eventParameters ], when || 0, callback /* result */ );
  163. };
  164. case "dispatchEvent":
  165. return function( nodeID, eventName, eventParameters, eventNodeParameters, when, callback ) {
  166. this.kernel.virtualTime.send( nodeID, kernelFunctionName, eventName,
  167. [ eventParameters, eventNodeParameters ], when || 0, callback /* result */ );
  168. };
  169. case "execute":
  170. return function( nodeID, scriptText, scriptType, when, callback ) {
  171. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  172. [ scriptText, scriptType ], when || 0, callback /* result */ ); // TODO: { text: scriptText, type: scriptType } ? -- vwf.receive() needs to parse
  173. };
  174. case "random":
  175. return function( nodeID, when, callback ) {
  176. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  177. undefined, when || 0, callback /* result */ );
  178. };
  179. case "seed":
  180. return function( nodeID, seed, when, callback ) {
  181. this.kernel.virtualTime.send( nodeID, kernelFunctionName, undefined,
  182. [ seed ], when || 0, callback /* result */ );
  183. };
  184. // -- Read-only functions --------------------------------------------------------------
  185. case "time":
  186. case "client":
  187. case "moniker":
  188. case "application":
  189. case "intrinsics":
  190. case "uri":
  191. case "name":
  192. case "prototype":
  193. case "prototypes":
  194. case "behaviors":
  195. case "ancestors":
  196. case "parent":
  197. case "children":
  198. case "descendants":
  199. case "find":
  200. case "test":
  201. case "findClients":
  202. return function() {
  203. return this.kernel[kernelFunctionName].apply( this.kernel, arguments );
  204. };
  205. }
  206. } );
  207. }
  208. }
  209. export {
  210. ViewKernel
  211. }