Component4.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-draw-Component'>/**
  19. </span> * The Draw Component is a surface in which sprites can be rendered. The Draw Component
  20. * manages and holds an {@link Ext.draw.Surface} instance where
  21. * {@link Ext.draw.Sprite Sprites} can be appended.
  22. *
  23. * One way to create a draw component is:
  24. *
  25. * @example
  26. * var drawComponent = Ext.create('Ext.draw.Component', {
  27. * viewBox: false,
  28. * items: [{
  29. * type: 'circle',
  30. * fill: '#79BB3F',
  31. * radius: 100,
  32. * x: 100,
  33. * y: 100
  34. * }]
  35. * });
  36. *
  37. * Ext.create('Ext.Window', {
  38. * width: 215,
  39. * height: 235,
  40. * layout: 'fit',
  41. * items: [drawComponent]
  42. * }).show();
  43. *
  44. * In this case we created a draw component and added a {@link Ext.draw.Sprite sprite} to it.
  45. * The {@link Ext.draw.Sprite#type type} of the sprite is `circle` so if you run this code you'll see a yellow-ish
  46. * circle in a Window. When setting `viewBox` to `false` we are responsible for setting the object's position and
  47. * dimensions accordingly.
  48. *
  49. * You can also add sprites by using the surface's add method:
  50. *
  51. * drawComponent.surface.add({
  52. * type: 'circle',
  53. * fill: '#79BB3F',
  54. * radius: 100,
  55. * x: 100,
  56. * y: 100
  57. * });
  58. *
  59. * ## Larger example
  60. *
  61. * @example
  62. * var drawComponent = Ext.create('Ext.draw.Component', {
  63. * width: 800,
  64. * height: 600,
  65. * renderTo: document.body
  66. * }), surface = drawComponent.surface;
  67. *
  68. * surface.add([{
  69. * type: 'circle',
  70. * radius: 10,
  71. * fill: '#f00',
  72. * x: 10,
  73. * y: 10,
  74. * group: 'circles'
  75. * }, {
  76. * type: 'circle',
  77. * radius: 10,
  78. * fill: '#0f0',
  79. * x: 50,
  80. * y: 50,
  81. * group: 'circles'
  82. * }, {
  83. * type: 'circle',
  84. * radius: 10,
  85. * fill: '#00f',
  86. * x: 100,
  87. * y: 100,
  88. * group: 'circles'
  89. * }, {
  90. * type: 'rect',
  91. * width: 20,
  92. * height: 20,
  93. * fill: '#f00',
  94. * x: 10,
  95. * y: 10,
  96. * group: 'rectangles'
  97. * }, {
  98. * type: 'rect',
  99. * width: 20,
  100. * height: 20,
  101. * fill: '#0f0',
  102. * x: 50,
  103. * y: 50,
  104. * group: 'rectangles'
  105. * }, {
  106. * type: 'rect',
  107. * width: 20,
  108. * height: 20,
  109. * fill: '#00f',
  110. * x: 100,
  111. * y: 100,
  112. * group: 'rectangles'
  113. * }]);
  114. *
  115. * // Get references to my groups
  116. * circles = surface.getGroup('circles');
  117. * rectangles = surface.getGroup('rectangles');
  118. *
  119. * // Animate the circles down
  120. * circles.animate({
  121. * duration: 1000,
  122. * to: {
  123. * translate: {
  124. * y: 200
  125. * }
  126. * }
  127. * });
  128. *
  129. * // Animate the rectangles across
  130. * rectangles.animate({
  131. * duration: 1000,
  132. * to: {
  133. * translate: {
  134. * x: 200
  135. * }
  136. * }
  137. * });
  138. *
  139. * For more information on Sprites, the core elements added to a draw component's surface,
  140. * refer to the {@link Ext.draw.Sprite} documentation.
  141. */
  142. Ext.define('Ext.draw.Component', {
  143. /* Begin Definitions */
  144. alias: 'widget.draw',
  145. extend: 'Ext.Component',
  146. requires: [
  147. 'Ext.draw.Surface',
  148. 'Ext.layout.component.Draw'
  149. ],
  150. /* End Definitions */
  151. <span id='Ext-draw-Component-cfg-enginePriority'> /**
  152. </span> * @cfg {String[]} enginePriority
  153. * Defines the priority order for which Surface implementation to use. The first
  154. * one supported by the current environment will be used.
  155. */
  156. enginePriority: ['Svg', 'Vml'],
  157. baseCls: Ext.baseCSSPrefix + 'surface',
  158. componentLayout: 'draw',
  159. <span id='Ext-draw-Component-cfg-viewBox'> /**
  160. </span> * @cfg {Boolean} viewBox
  161. * Turn on view box support which will scale and position items in the draw component to fit to the component while
  162. * maintaining aspect ratio. Note that this scaling can override other sizing settings on your items.
  163. */
  164. viewBox: true,
  165. shrinkWrap: 3,
  166. <span id='Ext-draw-Component-cfg-autoSize'> /**
  167. </span> * @cfg {Boolean} autoSize
  168. * Turn on autoSize support which will set the bounding div's size to the natural size of the contents.
  169. */
  170. autoSize: false,
  171. <span id='Ext-draw-Component-cfg-gradients'> /**
  172. </span> * @cfg {Object[]} gradients (optional) Define a set of gradients that can be used as `fill` property in sprites.
  173. * The gradients array is an array of objects with the following properties:
  174. *
  175. * - `id` - string - The unique name of the gradient.
  176. * - `angle` - number, optional - The angle of the gradient in degrees.
  177. * - `stops` - object - An object with numbers as keys (from 0 to 100) and style objects as values
  178. *
  179. * ## Example
  180. *
  181. * gradients: [{
  182. * id: 'gradientId',
  183. * angle: 45,
  184. * stops: {
  185. * 0: {
  186. * color: '#555'
  187. * },
  188. * 100: {
  189. * color: '#ddd'
  190. * }
  191. * }
  192. * }, {
  193. * id: 'gradientId2',
  194. * angle: 0,
  195. * stops: {
  196. * 0: {
  197. * color: '#590'
  198. * },
  199. * 20: {
  200. * color: '#599'
  201. * },
  202. * 100: {
  203. * color: '#ddd'
  204. * }
  205. * }
  206. * }]
  207. *
  208. * Then the sprites can use `gradientId` and `gradientId2` by setting the fill attributes to those ids, for example:
  209. *
  210. * sprite.setAttributes({
  211. * fill: 'url(#gradientId)'
  212. * }, true);
  213. */
  214. <span id='Ext-draw-Component-cfg-items'> /**
  215. </span> * @cfg {Ext.draw.Sprite[]} items
  216. * Array of sprites or sprite config objects to add initially to the surface.
  217. */
  218. <span id='Ext-draw-Component-property-surface'> /**
  219. </span> * @property {Ext.draw.Surface} surface
  220. * The Surface instance managed by this component.
  221. */
  222. initComponent: function() {
  223. this.callParent(arguments);
  224. this.addEvents(
  225. <span id='Ext-draw-Component-event-mousedown'> /**
  226. </span> * @event
  227. * Event forwarded from {@link Ext.draw.Surface surface}.
  228. * @inheritdoc Ext.draw.Surface#mousedown
  229. */
  230. 'mousedown',
  231. <span id='Ext-draw-Component-event-mouseup'> /**
  232. </span> * @event
  233. * Event forwarded from {@link Ext.draw.Surface surface}.
  234. * @inheritdoc Ext.draw.Surface#mouseup
  235. */
  236. 'mouseup',
  237. <span id='Ext-draw-Component-event-mousemove'> /**
  238. </span> * @event
  239. * Event forwarded from {@link Ext.draw.Surface surface}.
  240. * @inheritdoc Ext.draw.Surface#mousemove
  241. */
  242. 'mousemove',
  243. <span id='Ext-draw-Component-event-mouseenter'> /**
  244. </span> * @event
  245. * Event forwarded from {@link Ext.draw.Surface surface}.
  246. * @inheritdoc Ext.draw.Surface#mouseenter
  247. */
  248. 'mouseenter',
  249. <span id='Ext-draw-Component-event-mouseleave'> /**
  250. </span> * @event
  251. * Event forwarded from {@link Ext.draw.Surface surface}.
  252. * @inheritdoc Ext.draw.Surface#mouseleave
  253. */
  254. 'mouseleave',
  255. <span id='Ext-draw-Component-event-click'> /**
  256. </span> * @event
  257. * Event forwarded from {@link Ext.draw.Surface surface}.
  258. * @inheritdoc Ext.draw.Surface#click
  259. */
  260. 'click',
  261. <span id='Ext-draw-Component-event-dblclick'> /**
  262. </span> * @event
  263. * Event forwarded from {@link Ext.draw.Surface surface}.
  264. * @inheritdoc Ext.draw.Surface#dblclick
  265. */
  266. 'dblclick'
  267. );
  268. },
  269. <span id='Ext-draw-Component-method-onRender'> /**
  270. </span> * @private
  271. *
  272. * Create the Surface on initial render
  273. */
  274. onRender: function() {
  275. var me = this,
  276. viewBox = me.viewBox,
  277. autoSize = me.autoSize,
  278. bbox, items, width, height, x, y;
  279. me.callParent(arguments);
  280. if (me.createSurface() !== false) {
  281. items = me.surface.items;
  282. if (viewBox || autoSize) {
  283. bbox = items.getBBox();
  284. width = bbox.width;
  285. height = bbox.height;
  286. x = bbox.x;
  287. y = bbox.y;
  288. if (me.viewBox) {
  289. me.surface.setViewBox(x, y, width, height);
  290. } else {
  291. me.autoSizeSurface();
  292. }
  293. }
  294. }
  295. },
  296. // @private
  297. autoSizeSurface: function() {
  298. var bbox = this.surface.items.getBBox();
  299. this.setSurfaceSize(bbox.width, bbox.height);
  300. },
  301. setSurfaceSize: function (width, height) {
  302. this.surface.setSize(width, height);
  303. if (this.autoSize) {
  304. var bbox = this.surface.items.getBBox();
  305. this.surface.setViewBox(bbox.x, bbox.y - (+Ext.isOpera), width, height);
  306. }
  307. },
  308. <span id='Ext-draw-Component-method-createSurface'> /**
  309. </span> * Create the Surface instance. Resolves the correct Surface implementation to
  310. * instantiate based on the 'enginePriority' config. Once the Surface instance is
  311. * created you can use the handle to that instance to add sprites. For example:
  312. *
  313. * drawComponent.surface.add(sprite);
  314. *
  315. * @private
  316. */
  317. createSurface: function() {
  318. var me = this,
  319. cfg = Ext.applyIf({
  320. renderTo: me.el,
  321. height: me.height,
  322. width: me.width,
  323. items: me.items
  324. }, me.initialConfig), surface;
  325. // ensure we remove any listeners to prevent duplicate events since we refire them below
  326. delete cfg.listeners;
  327. surface = Ext.draw.Surface.create(cfg);
  328. if (!surface) {
  329. // In case we cannot create a surface, return false so we can stop
  330. return false;
  331. }
  332. me.surface = surface;
  333. function refire(eventName) {
  334. return function(e) {
  335. me.fireEvent(eventName, e);
  336. };
  337. }
  338. surface.on({
  339. scope: me,
  340. mouseup: refire('mouseup'),
  341. mousedown: refire('mousedown'),
  342. mousemove: refire('mousemove'),
  343. mouseenter: refire('mouseenter'),
  344. mouseleave: refire('mouseleave'),
  345. click: refire('click'),
  346. dblclick: refire('dblclick')
  347. });
  348. },
  349. <span id='Ext-draw-Component-method-onDestroy'> /**
  350. </span> * @private
  351. *
  352. * Clean up the Surface instance on component destruction
  353. */
  354. onDestroy: function() {
  355. Ext.destroy(this.surface);
  356. this.callParent(arguments);
  357. }
  358. });
  359. </pre>
  360. </body>
  361. </html>