ohm.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. */
  5. // VWF & Ohm model driver
  6. import { Fabric } from '/core/vwf/fabric.js';
  7. class OhmModel extends Fabric {
  8. constructor(module) {
  9. console.log("OhmModel constructor");
  10. super(module, "Model");
  11. }
  12. factory() {
  13. let _self_ = this;
  14. return this.load( this.module,
  15. {
  16. // == Module Definition ====================================================================
  17. // -- pipeline -----------------------------------------------------------------------------
  18. // pipeline: [ log ], // vwf <=> log <=> scene
  19. // -- initialize ---------------------------------------------------------------------------
  20. initialize: function() {
  21. var self = this;
  22. this.state = {
  23. nodes: {},
  24. scenes: {},
  25. prototypes: {},
  26. createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  27. childSource, childType, childIndex, childName, callback) {
  28. return {
  29. "parentID": nodeID,
  30. "ID": childID,
  31. "extendsID": childExtendsID,
  32. "implementsIDs": childImplementsIDs,
  33. "source": childSource,
  34. "type": childType,
  35. "name": childName,
  36. "prototypes": undefined,
  37. "lang": {
  38. "grammar": undefined,
  39. "semantics": undefined,
  40. "source": undefined
  41. }
  42. };
  43. },
  44. isOhmNodeComponent: function (prototypes) {
  45. var found = false;
  46. if (prototypes) {
  47. for (var i = 0; i < prototypes.length && !found; i++) {
  48. found = (prototypes[i] === "proxy/ohm/node.vwf");
  49. }
  50. }
  51. return found;
  52. }
  53. };
  54. this.state.kernel = this.kernel.kernel.kernel;
  55. this.ohm = ohm;
  56. window._ohm = this.ohm;
  57. //this.state.kernel = this.kernel.kernel.kernel;
  58. },
  59. // == Model API ============================================================================
  60. // -- creatingNode -------------------------------------------------------------------------
  61. creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  62. childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
  63. // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
  64. // the scene or a prototype. In either of those cases, save the uri of the new node
  65. var childURI = (nodeID === 0 ? childIndex : undefined);
  66. var appID = this.kernel.application();
  67. // If the node being created is a prototype, construct it and add it to the array of prototypes,
  68. // and then return
  69. var prototypeID = _self_.utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
  70. if (prototypeID !== undefined) {
  71. this.state.prototypes[prototypeID] = {
  72. parentID: nodeID,
  73. ID: childID,
  74. extendsID: childExtendsID,
  75. implementsID: childImplementsIDs,
  76. source: childSource,
  77. type: childType,
  78. name: childName
  79. };
  80. return;
  81. }
  82. var protos = _self_.getPrototypes(this.kernel, childExtendsID);
  83. //var kernel = this.kernel.kernel.kernel;
  84. var node;
  85. if (this.state.isOhmNodeComponent(protos)) {
  86. // Create the local copy of the node properties
  87. if (this.state.nodes[childID] === undefined) {
  88. this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
  89. childSource, childType, childIndex, childName, callback);
  90. }
  91. node = this.state.nodes[childID];
  92. node.prototypes = protos;
  93. //node.aframeObj = createAFrameObject(node);
  94. //addNodeToHierarchy(node);
  95. //notifyDriverOfPrototypeAndBehaviorProps();
  96. }
  97. },
  98. // -- initializingNode -------------------------------------------------------------------------
  99. // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  100. // childSource, childType, childIndex, childName ) {
  101. // },
  102. // -- deletingNode -------------------------------------------------------------------------
  103. //deletingNode: function( nodeID ) {
  104. //},
  105. // -- initializingProperty -----------------------------------------------------------------
  106. initializingProperty: function( nodeID, propertyName, propertyValue ) {
  107. var value = undefined;
  108. var node = this.state.nodes[nodeID];
  109. if (node !== undefined) {
  110. value = this.settingProperty(nodeID, propertyName, propertyValue);
  111. }
  112. return value;
  113. },
  114. // -- creatingProperty ---------------------------------------------------------------------
  115. creatingProperty: function (nodeID, propertyName, propertyValue) {
  116. return this.initializingProperty(nodeID, propertyName, propertyValue);
  117. },
  118. // -- settingProperty ----------------------------------------------------------------------
  119. settingProperty: function( nodeID, propertyName, propertyValue ) {
  120. var node = this.state.nodes[nodeID];
  121. var value = undefined;
  122. if (node && _self_.utility.validObject(propertyValue)) {
  123. switch ( propertyName ) {
  124. case "ohmLang":
  125. node.lang.source = propertyValue;
  126. node.lang.grammar = ohm.grammar(propertyValue);
  127. node.lang.semantics = node.lang.grammar.createSemantics();
  128. break;
  129. default:
  130. value = undefined;
  131. break;
  132. }
  133. }
  134. return value;
  135. },
  136. // -- gettingProperty ----------------------------------------------------------------------
  137. gettingProperty: function( nodeID, propertyName, propertyValue ) {
  138. var node = this.state.nodes[nodeID];
  139. var value = undefined;
  140. if (node) {
  141. switch ( propertyName ) {
  142. case "grammar":
  143. value = node.lang.grammar;
  144. break;
  145. case "semantics":
  146. value = node.lang.semantics;
  147. break;
  148. }
  149. }
  150. if ( value !== undefined ) {
  151. propertyValue = value;
  152. }
  153. return value;
  154. }
  155. } );
  156. }
  157. getPrototypes(kernel, extendsID) {
  158. var prototypes = [];
  159. var id = extendsID;
  160. while (id !== undefined) {
  161. prototypes.push(id);
  162. id = kernel.prototype(id);
  163. }
  164. return prototypes;
  165. }
  166. }
  167. export {
  168. OhmModel as default
  169. }