mil-sym.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  2. // Secretary of Defense (Personnel & Readiness).
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. // in compliance with the License. You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software distributed under the License
  10. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. // or implied. See the License for the specific language governing permissions and limitations under
  12. // the License.
  13. /// @module vwf/view/test
  14. /// @requires vwf/view
  15. define( [ "module", "vwf/view", "mil-sym/cws" ], function( module, view, cws ) {
  16. var self;
  17. return view.load( module, {
  18. // == Module Definition ====================================================================
  19. // -- initialize ---------------------------------------------------------------------------
  20. initialize: function( options ) {
  21. self = this;
  22. this.arguments = Array.prototype.slice.call( arguments );
  23. if ( options === undefined ) { options = {}; }
  24. if ( this.state === undefined ) {
  25. this.state = {};
  26. }
  27. if ( this.state.nodes === undefined ) {
  28. this.state.nodes = {};
  29. }
  30. var rs = armyc2.c2sd.renderer.utilities.RendererSettings;
  31. //rs.setSymbologyStandard( rs.Symbology_2525Bch2_USAS_13_14 );
  32. rs.setSymbologyStandard( rs.Symbology_2525C );
  33. rs.setTextOutlineWidth( 1 );
  34. },
  35. // createdNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
  36. // childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  37. // },
  38. // initializedNode: function( nodeID, childID, childExtendsID, childImplementsIDs, childSource, childType, childIndex, childName ) {
  39. // },
  40. // -- deletedNode ------------------------------------------------------------------------------
  41. // deletedNode: function( childID ) { },
  42. // -- addedChild -------------------------------------------------------------------------------
  43. //addedChild: function( nodeID, childID, childName ) { },
  44. // -- removedChild -----------------------------------------------------------------------------
  45. //removedChild: function( nodeID, childID ) { },
  46. // -- createdProperty --------------------------------------------------------------------------
  47. // createdProperty: function (nodeID, propertyName, propertyValue) {
  48. // this.satProperty(nodeID, propertyName, propertyValue);
  49. // },
  50. // -- initializedProperty ----------------------------------------------------------------------
  51. // initializedProperty: function ( nodeID, propertyName, propertyValue ) {
  52. // this.satProperty(nodeID, propertyName, propertyValue);
  53. // },
  54. // TODO: deletedProperty
  55. // -- satProperty ------------------------------------------------------------------------------
  56. // satProperty: function ( nodeID, propertyName, propertyValue ) {
  57. // },
  58. // -- gotProperty ------------------------------------------------------------------------------
  59. // gotProperty: function ( nodeID, propertyName, propertyValue ) {
  60. // },
  61. // -- calledMethod -----------------------------------------------------------------------------
  62. calledMethod: function( nodeID, methodName, methodParameters, methodValue ) {
  63. //console.info( nodeID + " " + methodName );
  64. if ( nodeID === this.kernel.application() ) {
  65. var clientThatCalledMethod = this.kernel.client();
  66. var me = this.kernel.moniker();
  67. switch ( methodName ) {
  68. case "insertUnits":
  69. if ( clientThatCalledMethod === me ) {
  70. addInsertableUnits( methodParameters[ 0 ] );
  71. }
  72. break;
  73. case "getUnitSymbol":
  74. if ( clientThatCalledMethod === me ) {
  75. getUnitSymbol( methodParameters[ 0 ], methodParameters[ 1 ], methodParameters[ 2 ], methodParameters[ 3 ], methodParameters[ 4 ], methodParameters[ 5 ] );
  76. }
  77. break;
  78. }
  79. }
  80. },
  81. // -- firedEvent -----------------------------------------------------------------------------
  82. // firedEvent: function( nodeID, eventName, eventParameters ) {
  83. // },
  84. // -- ticked -----------------------------------------------------------------------------------
  85. // ticked: function() {
  86. // },
  87. } );
  88. function addInsertableUnits( units ) {
  89. var foundUnits = undefined;
  90. var unit = undefined;
  91. var fullName = undefined;
  92. var actualName = undefined;
  93. var searchAcronym = undefined;
  94. var searchName = undefined;
  95. var unitsToAdd = undefined;
  96. var image = undefined;
  97. var appID = self.kernel.application();
  98. var description = undefined;
  99. var unitDef;
  100. if ( cws ) {
  101. // units must be an object with battleDivision members that are defined in cws
  102. // ex. ground, sea, air, subsurface, space
  103. for ( var battleDivision in units ) {
  104. unitsToAdd = units[ battleDivision ];
  105. if ( ! ( unitsToAdd instanceof Array ) ) {
  106. unitsToAdd = [ unitsToAdd ];
  107. }
  108. // unitsToAdd is now an array of acronyms from cws.defs
  109. // a series of acronyms make up the 'fullNames' of the units separated by '.'
  110. // those fullNames also kind of describe the hierarchy of the objects definition
  111. for ( var i = 0; i < unitsToAdd.length; i++ ) {
  112. searchAcronym = unitsToAdd[ i ];
  113. searchName = cws.decode( searchAcronym );
  114. // searchAcronym is a single acronym defined in CWS
  115. // findAll will search through all of the fullNames
  116. // for this 'battleDivision' and return an array of those units
  117. foundUnits = cws.findAll( battleDivision, searchAcronym );
  118. if ( foundUnits ) {
  119. // loop through the array and send out an event
  120. // so the application can present the user
  121. // with options to add these units to the application instance
  122. for ( fullName in foundUnits ) {
  123. unit = foundUnits[ fullName ];
  124. // render all of the affiliations, so that the UI doesn't
  125. // have to request them on an as needed basis
  126. image = {
  127. "unknown": getUnitImage( cws.unknown( unit.symbolID ) ),
  128. "friendly": getUnitImage( cws.friendly( unit.symbolID ) ),
  129. "neutral": getUnitImage( cws.neutral( unit.symbolID ) ),
  130. "hostile": getUnitImage( cws.hostile( unit.symbolID ) )
  131. };
  132. description = cws.description( fullName, unit.tag );
  133. actualName = cws.decode( cws.postTag( fullName, unit.tag ) ).replace( ".", " " );
  134. unitDef = {
  135. "fullName": fullName,
  136. "actualName": actualName,
  137. "searchAcronym": searchAcronym,
  138. "searchName": searchName,
  139. "description": description,
  140. "tag": unit.tag,
  141. "symbolID": unit.symbolID,
  142. "image": image
  143. };
  144. self.kernel.fireEvent( appID, "insertableUnitAdded", [ unitDef ] );
  145. }
  146. } else {
  147. self.logger.warnx( "Unable to find: " + unitsToAdd[ i ] + " in " + battleDivision );
  148. }
  149. }
  150. }
  151. self.kernel.fireEvent( appID, "unitLoadingComplete", [ true ] );
  152. }
  153. }
  154. function getUnitSymbol( symbolID, affiliation, echelonID, modifierList, unit, options ) {
  155. var updatedUnit = {};
  156. var appID = self.kernel.application();
  157. var renderer = armyc2.c2sd.renderer;
  158. var msa = renderer.utilities.MilStdAttributes;
  159. var rs = renderer.utilities.RendererSettings;
  160. var symUtil = renderer.utilities.SymbolUtilities;
  161. var modifiers = {};
  162. self.logger.info(" Mil-SymJS SymbolID before echelon and affiliation: " + symbolID );
  163. if ( cws ) {
  164. updatedUnit = unit;
  165. // Set affiliation in unit symbol id
  166. updatedUnit.symbolID = cws.addAffiliationToSymbolId( symbolID, affiliation );
  167. // Add echelon
  168. if ( echelonID != undefined ) {
  169. self.logger.info(" Mil-SymJS Adding Echelon: " + echelonID );
  170. updatedUnit.symbolID = cws.addEchelonToSymbolId( updatedUnit.symbolID, echelonID );
  171. self.logger.info(" Mil-SymJS SymbolID after echelon and affiliation: " + updatedUnit.symbolID );
  172. }
  173. // Add modifiers
  174. modifiers[ msa.PixelSize ] = "60";
  175. for ( var prop in modifierList ) {
  176. if (modifierList[prop] != undefined) {
  177. switch ( prop ) {
  178. case "pixelSize":
  179. case "PixelSize":
  180. modifiers[ msa.PixelSize ] = modifierList[ prop ];
  181. break;
  182. case "icon":
  183. case "Icon":
  184. modifiers[ msa.Icon ] = modifierList[ prop ];
  185. break;
  186. default:
  187. modifiers[ prop ] = modifierList[ prop ];
  188. break;
  189. }
  190. }
  191. }
  192. // Define the list of valid modifiers
  193. updatedUnit.validModifiers = [];
  194. updatedUnit.validModifiers.push( "pixelSize" );
  195. var aliases = Object.keys( cws.aliasModifiers );
  196. for ( var i = 0; i < aliases.length; i++ ) {
  197. var alias = aliases[ i ];
  198. var modObj = cws.aliasModifiers[ alias ];
  199. var modifier = renderer.utilities.ModifiersUnits[ modObj.modifier ];
  200. if ( symUtil.hasModifier( updatedUnit.symbolID,
  201. modifier,
  202. rs.getSymbologyStandard() ) ) {
  203. // Add to the array of valid modifiers
  204. updatedUnit.validModifiers.push( alias );
  205. }
  206. }
  207. // Render the unit image
  208. // if icon == true then you'll get no modifiers
  209. modifiers[ msa.Icon ] = false;
  210. modifiers[ msa.SymbologyStandard ] = rs.Symbology_2525C;
  211. var img = renderer.MilStdIconRenderer.Render( updatedUnit.symbolID, modifiers );
  212. if ( img ) {
  213. var imgBounds = img.getImageBounds();
  214. updatedUnit.image["selected"] = {
  215. "url": img.toDataUrl(),
  216. "width": imgBounds.width,
  217. "height": imgBounds.height
  218. }
  219. }
  220. }
  221. var unitEvent = "selectedUnitSymbolRendered";
  222. if ( (options.request) && (options.unitID) ) {
  223. switch ( options.request ) {
  224. case "addQuickUnit":
  225. unitEvent = "quickUnitAdded";
  226. if ( options.role ) {
  227. self.kernel.fireEvent( appID, unitEvent, [ options.role, options.unitID, updatedUnit ] );
  228. } else {
  229. self.kernel.fireEvent( appID, unitEvent, [ '', options.unitID, updatedUnit ] );
  230. }
  231. break;
  232. case "addFavoriteUnit":
  233. unitEvent = "favoriteUnitAdded";
  234. self.kernel.fireEvent( appID, unitEvent, [ options.unitID, updatedUnit ] );
  235. break;
  236. case "addRecentUnit":
  237. var unitEvent = "recentUnitAdded";
  238. self.kernel.fireEvent( appID, unitEvent, [ options.unitID, updatedUnit ] );
  239. break;
  240. case "renderSelectedUnit":
  241. default:
  242. // If nothing else, make this the selected unit
  243. self.kernel.fireEvent( appID, unitEvent, [ updatedUnit ] );
  244. break;
  245. }
  246. }
  247. else {
  248. self.kernel.fireEvent( appID, unitEvent, [ updatedUnit ] );
  249. }
  250. }
  251. function getUnitImage( symbolID ) {
  252. var renderer = armyc2.c2sd.renderer;
  253. var msa = renderer.utilities.MilStdAttributes;
  254. var rs = renderer.utilities.RendererSettings;
  255. var modifiers = {};
  256. modifiers[ msa.PixelSize ] = 32;
  257. modifiers[ msa.Icon ] = true;
  258. modifiers[ msa.SymbologyStandard ] = rs.Symbology_2525C;
  259. var img = renderer.MilStdIconRenderer.Render( symbolID, modifiers );
  260. if ( img ) {
  261. return img.toDataUrl();
  262. } else {
  263. return "";
  264. }
  265. }
  266. } );