From f489c10700914b8b07e0807a521279a00ad6f948 Mon Sep 17 00:00:00 2001 From: Brett Swift Date: Fri, 5 Dec 2014 15:39:31 -0500 Subject: [PATCH 1/2] Create event manager driver and component behavior --- support/client/lib/vwf.js | 2 + support/client/lib/vwf/model/eventManager.js | 221 ++++++++++++++++++ support/proxy/vwf.example.com/eventManager.js | 9 + .../vwf.example.com/eventManager.vwf.yaml | 24 ++ 4 files changed, 256 insertions(+) create mode 100644 support/client/lib/vwf/model/eventManager.js create mode 100644 support/proxy/vwf.example.com/eventManager.js create mode 100644 support/proxy/vwf.example.com/eventManager.vwf.yaml diff --git a/support/client/lib/vwf.js b/support/client/lib/vwf.js index 36e3e758a..76dcc8815 100644 --- a/support/client/lib/vwf.js +++ b/support/client/lib/vwf.js @@ -363,6 +363,7 @@ active: false }, { library: "vwf/model/graphtool", active: false }, + { library: "vwf/model/eventManager", active: false }, { library: "vwf/model/sound", active: false }, { library: "vwf/model/object", active: true }, { library: "vwf/model/stage/log", active: true }, @@ -439,6 +440,7 @@ { library: "vwf/model/cesium", active: false }, { library: "vwf/model/blockly", active: false }, { library: "vwf/model/graphtool", active: false }, + { library: "vwf/model/eventManager", active: false }, { library: "vwf/model/sound", active: false }, { library: "vwf/model/kineticjs", active: false }, { library: "vwf/model/mil-sym", active: false }, diff --git a/support/client/lib/vwf/model/eventManager.js b/support/client/lib/vwf/model/eventManager.js new file mode 100644 index 000000000..e9770fbfd --- /dev/null +++ b/support/client/lib/vwf/model/eventManager.js @@ -0,0 +1,221 @@ +"use strict"; + +define( [ "module", "vwf/model", "vwf/utility" ], function( module, model, utility ) { + + + return model.load( module, { + + // == Module Definition ==================================================================== + + // -- initialize --------------------------------------------------------------------------- + + initialize: function() { + this.state.nodes = {}; + this.state.kernel = this.kernel.kernel.kernel; + }, + + // == Model API ============================================================================ + + // -- creatingNode ------------------------------------------------------------------------- + + creatingNode: function( nodeID, childID, childExtendsID, childImplementsIDs, + childSource, childType, childIndex, childName, callback /* ( ready ) */ ) { + + if ( isEventManager( childImplementsIDs ) ) { + this.state.nodes[ childID ] = { + "isBroadcasting": true, + "isListening": true, + "eventMap": {} + }; + } + + }, + + // -- initializingNode --------------------------------------------------------------------- + + initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs, + childSource, childType, childIndex, childName ) { + }, + + // -- deletingNode ------------------------------------------------------------------------- + + deletingNode: function( nodeID ) { + + if ( nodeID ) { + var childNode = this.state.nodes[ nodeID ]; + if ( childNode ) { + var threeObject = childNode.threeObject; + if ( threeObject && threeObject.parent ) { + threeObject.parent.remove( threeObject ); + } + delete this.state.nodes[ childNode ]; + } + } + + }, + + // -- addingChild -------------------------------------------------------------------------- + + addingChild: function( nodeID, childID, childName ) { + }, + + // -- removingChild ------------------------------------------------------------------------ + + removingChild: function( nodeID, childID ) { + }, + + // -- creatingProperty --------------------------------------------------------------------- + + creatingProperty: function( nodeID, propertyName, propertyValue ) { + + return this.initializingProperty( nodeID, propertyName, propertyValue ); + + }, + + // -- initializingProperty ----------------------------------------------------------------- + + initializingProperty: function( nodeID, propertyName, propertyValue ) { + + var value = undefined; + if ( propertyValue !== undefined ) { + var node = this.state.nodes[ nodeID ]; + if ( node !== undefined && propertyName ) { + value = this.settingProperty( nodeID, propertyName, propertyValue ); + } + } + return value; + + }, + + // TODO: deletingProperty + + // -- settingProperty ---------------------------------------------------------------------- + + settingProperty: function( nodeID, propertyName, propertyValue ) { + + var node, value; + if ( this.state.nodes[ nodeID ] ) { + node = this.state.nodes[ nodeID ]; + switch ( propertyName ) { + case "isBroadcasting": + value = propertyValue; + node.isBroadcasting = value; + break; + case "isListening": + value = propertyValue; + node.isListening = value; + break; + case "eventMap": + value = propertyValue; + node.eventMap = value; + break; + } + } + return value; + + }, + + // -- gettingProperty ---------------------------------------------------------------------- + + gettingProperty: function( nodeID, propertyName, propertyValue ) { + + var node = this.state.nodes[ nodeID ]; + var value; + if ( node ) { + switch ( propertyName ) { + case "isBroadcasting": + value = node.isBroadcasting; + break; + case "isListening": + value = node.isListening; + break; + case "eventMap": + value = node.eventMap; + break; + } + } + return value; + + }, + + // -- creatingMethod ----------------------------------------------------------------------- + + creatingMethod: function( nodeID, methodName, methodParameters, methodBody ) { + }, + + // TODO: deletingMethod + + // -- callingMethod ------------------------------------------------------------------------ + + callingMethod: function( nodeID, methodName, methodParameters, methodValue ) { + var node = this.state.nodes[ nodeID ]; + var eventName, callbackName; + + if ( node ) { + switch ( methodName ) { + case "addListener": + eventName = methodParameters[ 0 ]; + callbackName = methodParameters[ 1 ]; + node.eventMap[ eventName ] = callbackName; + break; + case "removeListener": + eventName = methodParameters[ 0 ]; + delete node.eventMap[ eventName ]; + break; + } + } + + }, + + // -- creatingEvent ------------------------------------------------------------------------ + + creatingEvent: function( nodeID, eventName, eventParameters ) { + }, + + // TODO: deletingEvent + + // -- firingEvent -------------------------------------------------------------------------- + + firingEvent: function( nodeID, eventName, eventParameters ) { + + var node = this.state.nodes[ nodeID ]; + var listeners, listenerNode; + if ( node && node.isBroadcasting ) { + listeners = findListeners( this.state.nodes, eventName ); + for ( var i = 0; i < listeners.length; i++ ) { + listenerNode = this.state.nodes[ listeners[ i ] ]; + this.kernel.callMethod( + listeners[ i ], + listenerNode.eventMap[ eventName ], + eventParameters + ); + } + } + + }, + + // -- executing ---------------------------------------------------------------------------- + + executing: function( nodeID, scriptText, scriptType ) { + }, + + } ); + + function isEventManager( implementsIDs ) { + return implementsIDs && implementsIDs.indexOf( "http-vwf-example-com-eventManager-vwf" ) !== -1; + } + + function findListeners( nodes, eventName ) { + var listeners = new Array(); + var keys = Object.keys( nodes ); + var node; + for ( var i = 0; i < keys.length; i++ ) { + node = nodes[ keys[ i ] ] + if ( node.isListening && node.eventMap.hasOwnProperty( eventName ) ) { + listeners.push( keys[ i ] ); + } + } + return listeners; + } + +} ); \ No newline at end of file diff --git a/support/proxy/vwf.example.com/eventManager.js b/support/proxy/vwf.example.com/eventManager.js new file mode 100644 index 000000000..3d1ce989e --- /dev/null +++ b/support/proxy/vwf.example.com/eventManager.js @@ -0,0 +1,9 @@ +this.addListener = function( eventName, callbackMethodName ) { + this.eventMap[ eventName ] = callbackMethodName; +} + +this.removeListener = function( eventName ) { + delete this[ eventName ]; +} + +//@ sourceURL=http://vwf.example.com/eventManager.js \ No newline at end of file diff --git a/support/proxy/vwf.example.com/eventManager.vwf.yaml b/support/proxy/vwf.example.com/eventManager.vwf.yaml new file mode 100644 index 000000000..f66115a74 --- /dev/null +++ b/support/proxy/vwf.example.com/eventManager.vwf.yaml @@ -0,0 +1,24 @@ +# Copyright 2014 Lockheed Martin Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +properties: + isBroadcasting: true + isListening: true + eventMap: {} +methods: + addListener: + removeListener: +scripts: +- source: "http://vwf.example.com/eventManager.js" \ No newline at end of file From 6ed1d42389521b47ce1fbc2799bb58087084d220 Mon Sep 17 00:00:00 2001 From: Brett Swift Date: Sun, 7 Dec 2014 13:55:18 -0500 Subject: [PATCH 2/2] Addressed PR comments --- support/client/lib/vwf/model/eventManager.js | 75 ++++--------------- support/proxy/vwf.example.com/eventManager.js | 9 --- .../vwf.example.com/eventManager.vwf.yaml | 2 - 3 files changed, 16 insertions(+), 70 deletions(-) delete mode 100644 support/proxy/vwf.example.com/eventManager.js diff --git a/support/client/lib/vwf/model/eventManager.js b/support/client/lib/vwf/model/eventManager.js index e9770fbfd..f795463b3 100644 --- a/support/client/lib/vwf/model/eventManager.js +++ b/support/client/lib/vwf/model/eventManager.js @@ -11,7 +11,6 @@ define( [ "module", "vwf/model", "vwf/utility" ], function( module, model, utili initialize: function() { this.state.nodes = {}; - this.state.kernel = this.kernel.kernel.kernel; }, // == Model API ============================================================================ @@ -54,48 +53,25 @@ define( [ "module", "vwf/model", "vwf/utility" ], function( module, model, utili }, - // -- addingChild -------------------------------------------------------------------------- - - addingChild: function( nodeID, childID, childName ) { - }, - - // -- removingChild ------------------------------------------------------------------------ - - removingChild: function( nodeID, childID ) { - }, - // -- creatingProperty --------------------------------------------------------------------- creatingProperty: function( nodeID, propertyName, propertyValue ) { - return this.initializingProperty( nodeID, propertyName, propertyValue ); - }, // -- initializingProperty ----------------------------------------------------------------- initializingProperty: function( nodeID, propertyName, propertyValue ) { - - var value = undefined; - if ( propertyValue !== undefined ) { - var node = this.state.nodes[ nodeID ]; - if ( node !== undefined && propertyName ) { - value = this.settingProperty( nodeID, propertyName, propertyValue ); - } - } - return value; - + return this.settingProperty( nodeID, propertyName, propertyValue ); }, - // TODO: deletingProperty - // -- settingProperty ---------------------------------------------------------------------- settingProperty: function( nodeID, propertyName, propertyValue ) { - var node, value; - if ( this.state.nodes[ nodeID ] ) { - node = this.state.nodes[ nodeID ]; + var node = this.state.nodes[ nodeID ]; + var value; + if ( node ) { switch ( propertyName ) { case "isBroadcasting": value = propertyValue; @@ -138,25 +114,18 @@ define( [ "module", "vwf/model", "vwf/utility" ], function( module, model, utili }, - // -- creatingMethod ----------------------------------------------------------------------- - - creatingMethod: function( nodeID, methodName, methodParameters, methodBody ) { - }, - - // TODO: deletingMethod - // -- callingMethod ------------------------------------------------------------------------ callingMethod: function( nodeID, methodName, methodParameters, methodValue ) { var node = this.state.nodes[ nodeID ]; - var eventName, callbackName; + var eventName, eventHandlerName; if ( node ) { switch ( methodName ) { case "addListener": eventName = methodParameters[ 0 ]; - callbackName = methodParameters[ 1 ]; - node.eventMap[ eventName ] = callbackName; + eventHandlerName = methodParameters[ 1 ]; + node.eventMap[ eventName ] = eventHandlerName; break; case "removeListener": eventName = methodParameters[ 0 ]; @@ -167,25 +136,18 @@ define( [ "module", "vwf/model", "vwf/utility" ], function( module, model, utili }, - // -- creatingEvent ------------------------------------------------------------------------ - - creatingEvent: function( nodeID, eventName, eventParameters ) { - }, - - // TODO: deletingEvent - // -- firingEvent -------------------------------------------------------------------------- firingEvent: function( nodeID, eventName, eventParameters ) { var node = this.state.nodes[ nodeID ]; - var listeners, listenerNode; + var listenerIDs, listenerNode; if ( node && node.isBroadcasting ) { - listeners = findListeners( this.state.nodes, eventName ); - for ( var i = 0; i < listeners.length; i++ ) { - listenerNode = this.state.nodes[ listeners[ i ] ]; + listenerIDs = findListenerIDs( this.state.nodes, eventName ); + for ( var i = 0; i < listenerIDs.length; i++ ) { + listenerNode = this.state.nodes[ listenerIDs[ i ] ]; this.kernel.callMethod( - listeners[ i ], + listenerIDs[ i ], listenerNode.eventMap[ eventName ], eventParameters ); @@ -194,28 +156,23 @@ define( [ "module", "vwf/model", "vwf/utility" ], function( module, model, utili }, - // -- executing ---------------------------------------------------------------------------- - - executing: function( nodeID, scriptText, scriptType ) { - }, - } ); function isEventManager( implementsIDs ) { return implementsIDs && implementsIDs.indexOf( "http-vwf-example-com-eventManager-vwf" ) !== -1; } - function findListeners( nodes, eventName ) { - var listeners = new Array(); + function findListenerIDs( nodes, eventName ) { + var listenerIDs = []; var keys = Object.keys( nodes ); var node; for ( var i = 0; i < keys.length; i++ ) { node = nodes[ keys[ i ] ] if ( node.isListening && node.eventMap.hasOwnProperty( eventName ) ) { - listeners.push( keys[ i ] ); + listenerIDs.push( keys[ i ] ); } } - return listeners; + return listenerIDs; } } ); \ No newline at end of file diff --git a/support/proxy/vwf.example.com/eventManager.js b/support/proxy/vwf.example.com/eventManager.js deleted file mode 100644 index 3d1ce989e..000000000 --- a/support/proxy/vwf.example.com/eventManager.js +++ /dev/null @@ -1,9 +0,0 @@ -this.addListener = function( eventName, callbackMethodName ) { - this.eventMap[ eventName ] = callbackMethodName; -} - -this.removeListener = function( eventName ) { - delete this[ eventName ]; -} - -//@ sourceURL=http://vwf.example.com/eventManager.js \ No newline at end of file diff --git a/support/proxy/vwf.example.com/eventManager.vwf.yaml b/support/proxy/vwf.example.com/eventManager.vwf.yaml index f66115a74..61438fcd0 100644 --- a/support/proxy/vwf.example.com/eventManager.vwf.yaml +++ b/support/proxy/vwf.example.com/eventManager.vwf.yaml @@ -20,5 +20,3 @@ properties: methods: addListener: removeListener: -scripts: -- source: "http://vwf.example.com/eventManager.js" \ No newline at end of file