API Documentation for: 0.2.8
Show:

File:FlashArt.js

  1. /**
  2. * @module SpringRoll Plugin
  3. * @namespace pixiflash
  4. * @requires Pixi Flash
  5. */
  6. (function(undefined)
  7. {
  8. var Debug;
  9.  
  10. /**
  11. * Handles the Asset loading for Flash Art takes care of unloading
  12. * @class FlashArt
  13. * @constructor
  14. * @private
  15. * @param {String} id The asset id
  16. * @param {NodeElement} dom The `<script>` element added to the document
  17. * @param {String} [libName='lib'] The window parameter name
  18. * @param {Boolean} suppressWarnings Should we hide 'flash asset collision' warnings (default false)
  19. */
  20. var FlashArt = function(id, dom, libName, suppressWarnings)
  21. {
  22. if (DEBUG && Debug === undefined)
  23. {
  24. Debug = include('springroll.Debug', false);
  25. }
  26.  
  27. /**
  28. * Reference to the node element
  29. * @property {NodeElement} dom
  30. */
  31. this.dom = dom;
  32.  
  33. /**
  34. * The collection of loaded symbols by name
  35. * @property {Array} symbols
  36. */
  37. this.symbols = [];
  38.  
  39. /**
  40. * The name of the output lib name
  41. * @property {String} libName
  42. * @default 'lib'
  43. */
  44. this.libName = libName || 'lib';
  45.  
  46. /**
  47. * The name of the output lib name
  48. * @property {String} id
  49. */
  50. this.id = id;
  51.  
  52. // Parse the dom object
  53. this.parseSymbols(dom.text, suppressWarnings);
  54. };
  55.  
  56. // Reference to the prototype
  57. var p = FlashArt.prototype;
  58.  
  59. /**
  60. * The collection of all symbols and assets
  61. * @property {Object} globalSymbols
  62. * @static
  63. * @private
  64. */
  65. FlashArt.globalSymbols = {};
  66.  
  67. /**
  68. * Get the name of all the library elements of the dom text
  69. * @method parseSymbols
  70. * @param {String} text The DOM text contents
  71. * @param {Boolean} suppressWarnings Should we hide 'flash asset collision' warnings (default false)
  72. */
  73. p.parseSymbols = function(text, suppressWarnings)
  74. {
  75. // split into the initialization functions, that take 'lib' as a parameter
  76. var textArray = text.split(/[\(!]function\s*\(/);
  77. var globalSymbols = FlashArt.globalSymbols;
  78. // go through each initialization function
  79. for (var i = 0; i < textArray.length; ++i)
  80. {
  81. text = textArray[i];
  82. if (!text) continue;
  83.  
  84. // determine what the 'lib' parameter has been minified into
  85. var libName = text.substring(0, text.indexOf(","));
  86. if (!libName) continue;
  87.  
  88. // get all the things that are 'lib.X = <stuff>'
  89. var varFinder = new RegExp("\\(" + libName + ".(\\w+)\\s*=", "g");
  90. var foundName = varFinder.exec(text);
  91. var assetId;
  92.  
  93. while (foundName)
  94. {
  95. assetId = foundName[1];
  96.  
  97. // Warn about collisions with assets that already exist
  98. if (DEBUG && Debug && globalSymbols[assetId] && !suppressWarnings)
  99. {
  100. Debug.warn(
  101. "Flash Asset Collision: asset '" + this.id +
  102. "' wants to create 'lib." + assetId +
  103. "' which is already created by asset '" +
  104. FlashArt.globalSymbols[assetId] + "'"
  105. );
  106. }
  107.  
  108. // keep track of the asset id responsible
  109. this.symbols.push(assetId);
  110. globalSymbols[assetId] = this.id;
  111. foundName = varFinder.exec(text);
  112. }
  113. }
  114. };
  115.  
  116. /**
  117. * Cleanup the Flash library that's been loaded
  118. * @method destroy
  119. */
  120. p.destroy = function()
  121. {
  122. // remove the <script> element from the stage
  123. this.dom.parentNode.removeChild(this.dom);
  124. this.dom = null;
  125.  
  126. // Delete the elements
  127. var globalSymbols = FlashArt.globalSymbols;
  128. var lib = window[this.libName];
  129. this.symbols.forEach(function(id)
  130. {
  131. delete globalSymbols[id];
  132. delete lib[id];
  133. });
  134. this.symbols = null;
  135. };
  136. // Assign to namespace
  137. namespace('pixiflash').FlashArt = FlashArt;
  138.  
  139. }());