fabric.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. ///
  7. /// @module vwf/view
  8. /// @requires logger
  9. /// @requires vwf/api/kernel
  10. /// @requires vwf/api/view
  11. import { Utility } from '/core/vwf/utility/utility.js';
  12. import { Helpers } from '/core/helpers.js';
  13. import { Logger } from '/core/vwf/utility/logger.js';
  14. import { Kernel } from '/core/vwf/api/kernel.js';
  15. import { ViewApi } from '/core/vwf/api/view.js';
  16. import { ModelApi } from '/core/vwf/api/model.js';
  17. class Fabric {
  18. constructor(module, api) {
  19. //console.log("Fabric constructor");
  20. // this.view_api = new ViewApi;
  21. this.api = (api == 'View') ? new ViewApi : new ModelApi
  22. this.kernel_api = new Kernel;
  23. this.loggerFactory = new Logger;
  24. this.utility = new Utility;
  25. this.helpers = new Helpers;
  26. this.module = module;
  27. this.label = module.id.replace( /\//g, "." );
  28. this.loggerFactory.for( this.label ).debug( "loading" );
  29. this.logger = this.loggerFactory.for( this.label );
  30. }
  31. load ( module, initializer, fabricGenerator, kernelGenerator ) {
  32. var instance = Object.create( this );
  33. instance.driver = this;
  34. instance.module = module;
  35. instance.logger = this.logger.for( instance.module.id.replace( /\//g, "." ), instance );
  36. instance.logger.debug( "loading" );
  37. if ( typeof initializer == "function" || initializer instanceof Function ) {
  38. initializer = initializer();
  39. }
  40. for ( var key in initializer ) {
  41. instance[key] = initializer[key];
  42. }
  43. fabricGenerator && Object.keys( this.api ).forEach( function( fabricFunctionName ) {
  44. if ( ! instance.hasOwnProperty( fabricFunctionName ) ) {
  45. instance[fabricFunctionName] = fabricGenerator.call( instance, fabricFunctionName );
  46. instance[fabricFunctionName] || delete instance[fabricFunctionName];
  47. }
  48. } );
  49. kernelGenerator && Object.keys( this.kernel_api ).forEach( function( kernelFunctionName ) {
  50. if ( ! instance.hasOwnProperty( kernelFunctionName ) ) {
  51. instance[kernelFunctionName] = kernelGenerator.call( instance, kernelFunctionName );
  52. instance[kernelFunctionName] || delete instance[kernelFunctionName];
  53. }
  54. } );
  55. return instance;
  56. }
  57. create ( kernel, fabric, stages, state, parameters ) {
  58. let self = this;
  59. // let view_api = this.view_api;
  60. // let kernel_api = this.kernel_api;
  61. this.logger.debug( "creating" );
  62. // Interpret create( kernel, stages, ... ) as create( kernel, undefined, stages, ... )
  63. if ( fabric && fabric.length !== undefined ) { // is an array?
  64. parameters = state;
  65. state = stages;
  66. stages = fabric;
  67. fabric = undefined;
  68. }
  69. // Append this driver's stages to the pipeline to be placed in front of this driver.
  70. if ( ! fabric ) {
  71. stages = Array.prototype.concat.apply( [], ( this.pipeline || [] ).map( function( stage ) {
  72. return ( stages || [] ).concat( stage );
  73. } ) ).concat( stages || [] );
  74. } else {
  75. stages = ( stages || [] ).concat( this.pipeline || [] );
  76. }
  77. // Create the driver stage using its module as its prototype.
  78. var instance = Object.create( this );
  79. // Attach the reference to the stage to the right through the view API.
  80. fabricize.call( instance, fabric, self.api );
  81. // Create the pipeline to the left and attach the reference to the stage to the left
  82. // through the kernel API.
  83. kernelize.call( instance,
  84. stages.length ?
  85. stages.pop().create( kernel, instance, stages ) :
  86. kernel,
  87. self.kernel_api );
  88. // Attach the shared state object.
  89. instance.state = state || {};
  90. // Call the driver's initialize().
  91. initialize.apply( instance, parameters );
  92. // Call viewize() on the driver.
  93. function fabricize( fabric_type, fabric_api ) {
  94. Object.getPrototypeOf( this ) && fabricize.call( Object.getPrototypeOf( this ), fabric_type, fabric_api ); // depth-first recursion through the prototypes
  95. this.hasOwnProperty( "fabricize" ) && this.fabricize.call( instance, fabric_type, fabric_api ); // viewize() from the bottom up
  96. }
  97. // Call kernelize() on the driver.
  98. function kernelize( kernel, kernel_api ) {
  99. Object.getPrototypeOf( this ) && kernelize.call( Object.getPrototypeOf( this ), kernel, kernel_api ); // depth-first recursion through the prototypes
  100. this.hasOwnProperty( "kernelize" ) && this.kernelize.call( instance, kernel, kernel_api ); // kernelize() from the bottom up
  101. }
  102. // Call initialize() on the driver.
  103. function initialize( /* parameters */ ) {
  104. Object.getPrototypeOf( this ) && initialize.apply( Object.getPrototypeOf( this ), arguments ); // depth-first recursion through the prototypes
  105. this.hasOwnProperty( "initialize" ) && this.initialize.apply( instance, arguments ); // initialize() from the bottom up
  106. }
  107. // Return the driver stage. For the actual driver, return the leftmost stage in the
  108. // pipeline.
  109. if ( ! fabric ) {
  110. while ( instance.kernel !== kernel ) {
  111. instance = instance.kernel;
  112. }
  113. }
  114. this.driver.instance = instance;
  115. return instance;
  116. }
  117. kernelize ( kernel, kernel_api ) {
  118. this.kernel = kernel;
  119. }
  120. static getPrototypes(kernel, extendsID) {
  121. let prototypes = [];
  122. let id = extendsID;
  123. while (id !== undefined) {
  124. prototypes.push(id);
  125. id = kernel.prototype(id);
  126. }
  127. return prototypes;
  128. }
  129. }
  130. export {
  131. Fabric
  132. }