123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- "use strict";
- define( [ "module", "vwf/model", "vwf/utility", "vwf/configuration" ],
- function( module, model, utility, configuration ) {
- return model.load( module, {
-
-
- initialize: function() {
- this.objects = {};
- this.creatingNode( undefined, 0 );
- },
-
-
- creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback /* ( ready ) */ ) {
-
-
-
-
- var parent = nodeID != 0 && this.objects[nodeID];
- var child = this.objects[childID];
- if ( ! child ) {
-
- child = this.objects[childID] = {
- id: childID,
- prototype: undefined,
- behaviors: undefined,
- source: childSource,
- type: childType,
- uri: parent ? undefined : childIndex,
- name: childName,
- properties: {},
- methods: {},
- events: {},
- parent: undefined,
- children: [],
- sequence: 0,
- prng: parent ?
- new Alea( JSON.stringify( parent.prng.state ), childID ) :
- new Alea( configuration.active["random-seed"], childID ),
-
-
-
-
-
-
-
-
-
-
-
-
-
- initialized: false,
- };
-
- if ( parent ) {
- child.parent = parent;
- parent.children[childIndex] = child;
- }
-
-
- if ( child.uri ) {
- child.patches = { };
- } else if ( parent && ! parent.initialized && parent.patches ) {
- child.patches = { };
- }
- } else if ( ! child.prototype ) {
-
- child.prototype = childExtendsID && this.objects[childExtendsID];
- child.behaviors = ( childImplementsIDs || [] ).map( function( childImplementsID ) {
- return this.objects[childImplementsID];
- }, this );
- } else {
-
- }
- },
-
- initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName ) {
- this.objects[childID].initialized = true;
- },
-
- deletingNode: function( nodeID ) {
- var child = this.objects[nodeID];
- var object = child.parent;
- if ( object ) {
- var index = object.children.indexOf( child );
- if ( index >= 0 ) {
- object.children.splice( index, 1 );
- }
- child.parent = undefined;
- }
- delete this.objects[nodeID];
- },
-
- addingChild: function( nodeID, childID, childName ) {
- var object = this.objects[nodeID];
- var child = this.objects[childID];
- child.parent = object;
- object.children.push( child );
- },
-
- removingChild: function( nodeID, childID ) {
- var object = this.objects[nodeID];
- var child = this.objects[childID];
- child.parent = undefined;
- object.children.splice( object.children.indexOf( child ), 1 );
- },
-
-
- settingProperties: function( nodeID, properties ) {
- var object = this.objects[nodeID];
- if ( ! object ) return;
- var node_properties = object.properties;
- for ( var propertyName in properties ) {
- if ( ! node_properties.hasOwnProperty( propertyName ) ) {
- this.kernel.setProperty( nodeID, propertyName, properties[propertyName] );
- }
- node_properties[propertyName] = properties[propertyName];
- }
- return node_properties;
- },
-
- gettingProperties: function( nodeID, properties ) {
- return this.objects[nodeID].properties;
- },
-
- creatingProperty: function( nodeID, propertyName, propertyValue ) {
- return this.initializingProperty( nodeID, propertyName, propertyValue );
- },
-
- initializingProperty: function( nodeID, propertyName, propertyValue ) {
- return this.settingProperty( nodeID, propertyName, propertyValue );
- },
-
-
- settingProperty: function( nodeID, propertyName, propertyValue ) {
- var object = this.objects[nodeID];
- return object.properties[propertyName] = propertyValue;
- },
-
- gettingProperty: function( nodeID, propertyName, propertyValue ) {
- return this.objects[nodeID].properties[propertyName];
- },
-
- creatingMethod: function( nodeID, methodName, methodParameters, methodBody ) {
- return this.settingMethod( nodeID, methodName,
- { parameters: methodParameters, body: methodBody } );
- },
-
- settingMethod: function( nodeID, methodName, methodHandler ) {
- return this.objects[nodeID].methods[methodName] = methodHandler;
- },
-
- gettingMethod: function( nodeID, methodName ) {
- return this.objects[nodeID].methods[methodName];
- },
-
- addingEventListener: function( nodeID, eventName, eventListenerID, eventHandler, eventContextID, eventPhases ) {
- if ( ! this.objects[ nodeID ].events[ eventName ] ) {
- this.objects[ nodeID ].events[ eventName ] = {};
- }
- return this.settingEventListener( nodeID, eventName, eventListenerID,
- utility.merge( eventHandler, { context: eventContextID, phases: eventPhases } ) ) ?
- true : undefined;
- },
-
- removingEventListener: function( nodeID, eventName, eventListenerID ) {
- var listeners = this.objects[ nodeID ].events[ eventName ];
- if ( listeners && listeners[ eventListenerID ] ) {
- delete listeners[ eventListenerID ];
- return true;
- }
- return undefined;
- },
-
- settingEventListener: function( nodeID, eventName, eventListenerID, eventListener ) {
- return this.objects[ nodeID ].events[ eventName ][ eventListenerID ] = eventListener;
- },
-
- gettingEventListener: function( nodeID, eventName, eventListenerID ) {
- return this.objects[ nodeID ].events[ eventName ][ eventListenerID ];
- },
-
- flushingEventListeners: function( nodeID, eventName, eventContextID ) {
- var listeners = this.objects[ nodeID ].events[ eventName ];
- Object.keys( listeners ).forEach( function( eventListenerID ) {
- if ( listeners[ eventListenerID ].context === eventContextID ) {
- delete listeners[ eventListenerID ];
- }
- } );
- },
-
-
-
-
- random: function( nodeID ) {
- var object = this.objects[nodeID];
- object.initialized && object.patches && ( object.patches.internals = true );
- return object.prng();
- },
-
- seed: function( nodeID, seed ) {
- var object = this.objects[nodeID];
- object.initialized && object.patches && ( object.patches.internals = true );
- object.prng = new Alea( seed );
- },
-
- intrinsics: function( nodeID, result ) {
- var object = this.objects[nodeID];
- result = result || {};
-
- result.source = object.source;
- result.type = object.type;
- return result;
- },
-
- uri: function( nodeID ) {
- var node = this.objects[ nodeID ];
- if ( node ) {
- return node.uri;
- } else {
- this.logger.warnx( "Could not find uri of nonexistent node: '" + nodeID + "'" );
- }
- },
-
- name: function( nodeID ) {
- return this.objects[nodeID].name || "";
- },
-
- prototype: function( nodeID ) {
- var object = this.objects[nodeID];
- return object.prototype && object.prototype.id;
- },
-
- behaviors: function( nodeID ) {
- var behaviors = this.objects[nodeID].behaviors;
- if ( behaviors ) {
- return behaviors.map( function( behavior ) {
- return behavior.id;
- } );
- } else {
- this.logger.warnx( "Node '" + nodeID + "' does not have a valid behaviors array" );
- }
- },
-
- parent: function( nodeID, initializedOnly ) {
- var object = this.objects[ nodeID ];
- if ( object ) {
- return ( ! initializedOnly || object.initialized ) ?
- ( object.parent && object.parent.id || 0 ) : undefined;
- } else {
- this.logger.error( "Cannot find node: '" + nodeID + "'" );
- }
- },
-
- children: function( nodeID, initializedOnly ) {
- if ( nodeID === undefined ) {
- this.logger.errorx( "children", "cannot retrieve children of nonexistent node");
- return;
- }
- var node = this.objects[ nodeID ];
- if ( node ) {
- return node.children.map( function( child ) {
- return ( ! initializedOnly || child.initialized ) ?
- child.id : undefined;
- } );
- } else {
- this.logger.error( "Cannot find node: " + nodeID );
- }
- },
-
-
-
-
- properties: function( nodeID ) {
- return this.objects[nodeID].properties;
- },
-
- internals: function( nodeID, internals ) {
- var object = this.objects[nodeID];
- if ( !object ) {
- this.logger.errorx( "internals: object does not exist with id = '" + nodeID + "'" );
- return;
- }
- if ( internals ) {
- if ( internals.sequence !== undefined ) {
- object.sequence = internals.sequence;
- object.initialized && object.patches && ( object.patches.internals = true );
- }
- if ( internals.random !== undefined ) {
- merge( object.prng.state, internals.random );
- object.initialized && object.patches && ( object.patches.internals = true );
- }
- } else {
- internals = {};
- internals.sequence = object.sequence;
- internals.random = object.prng.state;
- }
- return internals;
- },
-
- sequence: function( nodeID ) {
- var object = this.objects[nodeID];
- object.initialized && object.patches && ( object.patches.internals = true );
- return object && ++object.sequence;
- },
-
- patches: function( nodeID ) {
- return this.objects[nodeID].patches;
- },
-
- exists: function( nodeID ) {
- return !! this.objects[nodeID];
- },
-
- initialized: function( nodeID ) {
- return this.objects[nodeID].initialized;
- },
- } );
-
- function merge( target /* [, source1 [, source2 ... ] ] */ ) {
- for ( var index = 1; index < arguments.length; index++ ) {
- var source = arguments[index];
- Object.keys( source ).forEach( function( key ) {
- target[key] = source[key];
- } );
- }
- return target;
- }
- } );
|