map.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/model/stage/map.js translates between kernel-side nodeIDs and model-side objects or ids.
  15. ///
  16. /// @module vwf/model/stage/map
  17. /// @requires vwf/model/stage
  18. define( [ "module", "vwf/model/stage" ], function( module, stage ) {
  19. return stage.load( module, {
  20. // == Module Definition ====================================================================
  21. // -- initialize ---------------------------------------------------------------------------
  22. initialize: function( object_id ) {
  23. this.kernel_to_model = {}; // maps nodeID => model object
  24. this.model_to_kernel = {}; // maps model object => nodeID
  25. if ( typeof object_id == "function" || object_id instanceof Function ) {
  26. this.object_id = object_id;
  27. } else if ( typeof object_id == "string" || object_id instanceof String ) {
  28. this.object_id = function( node ) { return node[object_id] };
  29. } else {
  30. this.object_id = function( node ) { return node }; // will use node's toString()
  31. }
  32. },
  33. // == Kernel API ===========================================================================
  34. // TODO: setState
  35. // TODO: getState
  36. // TODO: hashState
  37. // -- createNode ---------------------------------------------------------------------------
  38. createNode: function( nodeComponent, nodeAnnotation, baseURI, callback /* ( nodeID ) */ ) {
  39. return this.kernel.createNode( nodeComponent, nodeAnnotation, baseURI, callback ); // TODO remap callback parameter (nodeAnnotation and baseURI are optional and callback may be second or third argument)
  40. },
  41. // -- deleteNode ---------------------------------------------------------------------------
  42. deleteNode: function( node ) {
  43. return this.kernel.deleteNode( this.model_to_kernel[this.object_id(node)] || node );
  44. },
  45. // TODO: setNode
  46. // TODO: getNode
  47. // TODO: hashNode
  48. // -- createChild --------------------------------------------------------------------------
  49. createChild: function( node, childName, childComponent, childURI, callback /* ( childID ) */ ) {
  50. return this.kernel.createChild( this.model_to_kernel[this.object_id(node)] || node,
  51. childName, childComponent, childURI, callback ); // TODO: remap callback parameter
  52. },
  53. // -- deleteChild --------------------------------------------------------------------------
  54. deleteChild: function( node, childName ) {
  55. return this.kernel.deleteChild( this.model_to_kernel[this.object_id(node)] || node,
  56. childName );
  57. },
  58. // -- addChild -----------------------------------------------------------------------------
  59. addChild: function( node, child, childName ) {
  60. return this.kernel.addChild( this.model_to_kernel[this.object_id(node)] || node,
  61. this.model_to_kernel[this.object_id(child)] || child, childName );
  62. },
  63. // -- removeChild --------------------------------------------------------------------------
  64. removeChild: function( node, child ) {
  65. return this.kernel.removeChild( this.model_to_kernel[this.object_id(node)] || node,
  66. this.model_to_kernel[this.object_id(child)] || child );
  67. },
  68. // TODO: setProperties
  69. // TODO: getProperties
  70. // -- createProperty -----------------------------------------------------------------------
  71. createProperty: function( node, propertyName, propertyValue ) {
  72. return this.kernel.createProperty( this.model_to_kernel[this.object_id(node)] || node,
  73. propertyName, propertyValue );
  74. },
  75. // TODO: deleteProperty
  76. // -- setProperty --------------------------------------------------------------------------
  77. setProperty: function( node, propertyName, propertyValue ) {
  78. return this.kernel.setProperty( this.model_to_kernel[this.object_id(node)] || node,
  79. propertyName, propertyValue );
  80. },
  81. // -- getProperty --------------------------------------------------------------------------
  82. getProperty: function( node, propertyName, propertyValue ) {
  83. return this.kernel.getProperty( this.model_to_kernel[this.object_id(node)] || node,
  84. propertyName, propertyValue );
  85. },
  86. // -- createMethod -------------------------------------------------------------------------
  87. createMethod: function( node, methodName, methodParameters, methodBody ) {
  88. return this.kernel.createMethod( this.model_to_kernel[this.object_id(node)] || node,
  89. methodName, methodParameters, methodBody );
  90. },
  91. // TODO: deleteMethod
  92. // -- callMethod ---------------------------------------------------------------------------
  93. callMethod: function( node, methodName, methodParameters ) {
  94. return this.kernel.callMethod( this.model_to_kernel[this.object_id(node)] || node,
  95. methodName, methodParameters );
  96. },
  97. // -- createEvent --------------------------------------------------------------------------
  98. createEvent: function( node, eventName, eventParameters ) {
  99. return this.kernel.createEvent( this.model_to_kernel[this.object_id(node)] || node,
  100. eventName, eventParameters );
  101. },
  102. // TODO: deleteEvent
  103. // -- fireEvent ----------------------------------------------------------------------------
  104. fireEvent: function( node, eventName, eventParameters ) {
  105. return this.kernel.fireEvent( this.model_to_kernel[this.object_id(node)] || node,
  106. eventName, eventParameters );
  107. },
  108. // -- dispatchEvent ------------------------------------------------------------------------
  109. dispatchEvent: function( node, eventName, eventParameters, eventNodeParameters ) {
  110. return this.kernel.dispatchEvent( this.model_to_kernel[this.object_id(node)] || node,
  111. eventName, eventParameters, eventNodeParameters ); // TODO: remap any node references in eventParameters and eventNodeParameters (possible to do without knowing specific events?)
  112. },
  113. // -- execute ------------------------------------------------------------------------------
  114. execute: function( node, scriptText, scriptType ) {
  115. return this.kernel.execute( this.model_to_kernel[this.object_id(node)] || node,
  116. scriptText, scriptType );
  117. },
  118. // -- random -------------------------------------------------------------------------------
  119. random: function( node ) {
  120. return this.kernel.random( this.model_to_kernel[this.object_id(node)] || node );
  121. },
  122. // -- seed ---------------------------------------------------------------------------------
  123. seed: function( node, seed ) {
  124. return this.kernel.seed( this.model_to_kernel[this.object_id(node)] || node, seed );
  125. },
  126. // -- time ---------------------------------------------------------------------------------
  127. time: function() {
  128. return this.kernel.time();
  129. },
  130. // -- client -------------------------------------------------------------------------------
  131. client: function() {
  132. return this.kernel.client();
  133. },
  134. // -- moniker ------------------------------------------------------------------------------
  135. moniker: function() {
  136. return this.kernel.moniker();
  137. },
  138. // -- intrinsics ---------------------------------------------------------------------------
  139. intrinsics: function( node, result ) {
  140. return this.kernel.intrinsics( this.model_to_kernel[this.object_id(node)] || node, result );
  141. },
  142. // -- uri ----------------------------------------------------------------------------------
  143. uri: function( node, searchAncestors, initializedOnly ) {
  144. return this.kernel.uri( this.model_to_kernel[this.object_id(node)] || node, searchAncestors, initializedOnly );
  145. },
  146. // -- name ---------------------------------------------------------------------------------
  147. name: function( node ) {
  148. return this.kernel.name( this.model_to_kernel[this.object_id(node)] || node );
  149. },
  150. // -- prototype ----------------------------------------------------------------------------
  151. prototype: function( node ) {
  152. return this.kernel.prototype( this.model_to_kernel[this.object_id(node)] || node ); // TODO remap return value
  153. },
  154. // -- prototypes ---------------------------------------------------------------------------
  155. prototypes: function( node, includeBehaviors ) {
  156. return this.kernel.prototypes( this.model_to_kernel[this.object_id(node)] || node, includeBehaviors ); // TODO remap return value
  157. },
  158. // -- behaviors ----------------------------------------------------------------------------
  159. behaviors: function( node ) {
  160. return this.kernel.behaviors( this.model_to_kernel[this.object_id(node)] || node ); // TODO remap return value
  161. },
  162. // -- ancestors ----------------------------------------------------------------------------
  163. ancestors: function( node, initializedOnly ) {
  164. return this.kernel.ancestors( this.model_to_kernel[this.object_id(node)] || node, initializedOnly ); // TODO remap return value
  165. },
  166. // -- parent -------------------------------------------------------------------------------
  167. parent: function( node, initializedOnly ) {
  168. return this.kernel.parent( this.model_to_kernel[this.object_id(node)] || node, initializedOnly ); // TODO remap return value
  169. },
  170. // -- children -----------------------------------------------------------------------------
  171. children: function( node ) {
  172. return this.kernel.children( this.model_to_kernel[this.object_id(node)] || node ); // TODO remap return value
  173. },
  174. // -- descendants --------------------------------------------------------------------------
  175. descendants: function( node ) {
  176. return this.kernel.descendants( this.model_to_kernel[this.object_id(node)] || node ); // TODO remap return value
  177. },
  178. // -- find ---------------------------------------------------------------------------------
  179. find: function( node, matchPattern, initializedOnly, callback /* ( matchID ) */ ) { // TODO remap return value and callback parameter (initializedOnly is optional and callback may be third argument)
  180. return this.kernel.find( this.model_to_kernel[this.object_id(node)] || node,
  181. matchPattern, initializedOnly, callback );
  182. },
  183. // -- test ---------------------------------------------------------------------------------
  184. test: function( node, matchPattern, test, initializedOnly ) {
  185. return this.kernel.test( this.model_to_kernel[this.object_id(node)] || node,
  186. matchPattern, this.model_to_kernel[this.object_id(test)] || test, initializedOnly );
  187. },
  188. // == Model API ============================================================================
  189. // -- creatingNode -------------------------------------------------------------------------
  190. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  191. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  192. var child = this.model.creatingNode && this.model.creatingNode(
  193. this.kernel_to_model[nodeID] || nodeID,
  194. childID,
  195. this.kernel_to_model[childExtendsID] || childExtendsID,
  196. childImplementsIDs, // TODO: remap childImplementsIDs array values
  197. childSource, childType, childIndex, childName, callback );
  198. if ( child !== undefined ) {
  199. this.kernel_to_model[childID] = child;
  200. this.model_to_kernel[this.object_id(child)] = childID;
  201. }
  202. return undefined; // creatingNode doesn't return anything to the kernel
  203. },
  204. // -- initializingNode ---------------------------------------------------------------------
  205. initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  206. childSource, childType, childIndex, childName ) {
  207. return this.model.initializingNode && this.model.initializingNode(
  208. this.kernel_to_model[nodeID] || nodeID,
  209. this.kernel_to_model[childID] || childID,
  210. this.kernel_to_model[childExtendsID] || childExtendsID,
  211. childImplementsIDs, // TODO: remap childImplementsIDs array values
  212. childSource, childType, childIndex, childName );
  213. },
  214. // -- deletingNode -------------------------------------------------------------------------
  215. deletingNode: function( nodeID ) {
  216. return this.model.deletingNode && this.model.deletingNode( this.kernel_to_model[nodeID] || nodeID );
  217. },
  218. // -- addingChild --------------------------------------------------------------------------
  219. addingChild: function( nodeID, childID, childName ) {
  220. return this.model.addingChild && this.model.addingChild( this.kernel_to_model[nodeID] || nodeID,
  221. this.kernel_to_model[childID] || childID, childName );
  222. },
  223. // -- removingChild ------------------------------------------------------------------------
  224. removingChild: function( nodeID, childID ) {
  225. return this.model.removingChild && this.model.removingChild(
  226. this.kernel_to_model[nodeID] || nodeID, this.kernel_to_model[childID] || childID );
  227. },
  228. // -- creatingProperty ---------------------------------------------------------------------
  229. creatingProperty: function( nodeID, propertyName, propertyValue ) {
  230. return this.model.creatingProperty && this.model.creatingProperty( this.kernel_to_model[nodeID] || nodeID,
  231. propertyName, propertyValue );
  232. },
  233. // -- initializingProperty -----------------------------------------------------------------
  234. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  235. return this.model.initializingProperty && this.model.initializingProperty( this.kernel_to_model[nodeID] || nodeID,
  236. propertyName, propertyValue );
  237. },
  238. // TODO: deletingProperty
  239. // -- settingProperty ----------------------------------------------------------------------
  240. settingProperty: function( nodeID, propertyName, propertyValue ) {
  241. return this.model.settingProperty && this.model.settingProperty( this.kernel_to_model[nodeID] || nodeID,
  242. propertyName, propertyValue );
  243. },
  244. // -- gettingProperty ----------------------------------------------------------------------
  245. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  246. return this.model.gettingProperty && this.model.gettingProperty( this.kernel_to_model[nodeID] || nodeID,
  247. propertyName, propertyValue );
  248. },
  249. // -- creatingMethod -----------------------------------------------------------------------
  250. creatingMethod: function( nodeID, methodName, methodParameters, methodBody ) {
  251. return this.model.creatingMethod && this.model.creatingMethod( this.kernel_to_model[nodeID] || nodeID,
  252. methodName, methodParameters, methodBody );
  253. },
  254. // TODO: deletingMethod
  255. // -- settingMethod ------------------------------------------------------------------------
  256. settingMethod: function( nodeID, methodName, methodHandler ) {
  257. return this.model.settingMethod && this.model.settingMethod( this.kernel_to_model[nodeID] || nodeID,
  258. methodName, methodHandler );
  259. },
  260. // -- gettingMethod ------------------------------------------------------------------------
  261. gettingMethod: function( nodeID, methodName ) {
  262. return this.model.gettingMethod && this.model.gettingMethod( this.kernel_to_model[nodeID] || nodeID,
  263. methodName );
  264. },
  265. // -- callingMethod ------------------------------------------------------------------------
  266. callingMethod: function( nodeID, methodName, methodParameters ) {
  267. return this.model.callingMethod && this.model.callingMethod( this.kernel_to_model[nodeID] || nodeID,
  268. methodName, methodParameters );
  269. },
  270. // -- creatingEvent ------------------------------------------------------------------------
  271. creatingEvent: function( nodeID, eventName, eventParameters ) {
  272. return this.model.creatingEvent && this.model.creatingEvent( this.kernel_to_model[nodeID] || nodeID,
  273. eventName, eventParameters );
  274. },
  275. // TODO: deletingEvent
  276. // -- firingEvent --------------------------------------------------------------------------
  277. firingEvent: function( nodeID, eventName, eventParameters ) {
  278. return this.model.firingEvent && this.model.firingEvent( this.kernel_to_model[nodeID] || nodeID,
  279. eventName, eventParameters );
  280. },
  281. // -- executing ----------------------------------------------------------------------------
  282. executing: function( nodeID, scriptText, scriptType ) {
  283. return this.model.executing && this.model.executing( this.kernel_to_model[nodeID] || nodeID,
  284. scriptText, scriptType );
  285. },
  286. // -- ticking ------------------------------------------------------------------------------
  287. ticking: function( time ) {
  288. return this.model.ticking && this.model.ticking( time );
  289. },
  290. } );
  291. } );