Img.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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-Img'>/**
  19. </span> * Simple helper class for easily creating image components. This renders an image tag to
  20. * the DOM with the configured src.
  21. *
  22. * {@img Ext.Img/Ext.Img.png Ext.Img component}
  23. *
  24. * ## Example usage:
  25. *
  26. * var changingImage = Ext.create('Ext.Img', {
  27. * src: 'http://www.sencha.com/img/20110215-feat-html5.png',
  28. * renderTo: Ext.getBody()
  29. * });
  30. *
  31. * // change the src of the image programmatically
  32. * changingImage.setSrc('http://www.sencha.com/img/20110215-feat-perf.png');
  33. *
  34. * By default, only an img element is rendered and that is this component's primary
  35. * {@link Ext.AbstractComponent#getEl element}. If the {@link Ext.AbstractComponent#autoEl} property
  36. * is other than 'img' (the default), the a child img element will be added to the primary
  37. * element. This can be used to create a wrapper element around the img.
  38. *
  39. * ## Wrapping the img in a div:
  40. *
  41. * var wrappedImage = Ext.create('Ext.Img', {
  42. * src: 'http://www.sencha.com/img/20110215-feat-html5.png',
  43. * autoEl: 'div', // wrap in a div
  44. * renderTo: Ext.getBody()
  45. * });
  46. */
  47. Ext.define('Ext.Img', {
  48. extend: 'Ext.Component',
  49. alias: ['widget.image', 'widget.imagecomponent'],
  50. autoEl: 'img',
  51. <span id='Ext-Img-cfg-src'> /**
  52. </span> * @cfg {String} src
  53. * The image src.
  54. */
  55. src: '',
  56. <span id='Ext-Img-cfg-alt'> /**
  57. </span> * @cfg {String} alt
  58. * The descriptive text for non-visual UI description.
  59. */
  60. alt: '',
  61. <span id='Ext-Img-cfg-imgCls'> /**
  62. </span> * @cfg {String} imgCls
  63. * Optional CSS classes to add to the img element.
  64. */
  65. imgCls: '',
  66. getElConfig: function() {
  67. var me = this,
  68. config = me.callParent(),
  69. img;
  70. // It is sometimes helpful (like in a panel header icon) to have the img wrapped
  71. // by a div. If our autoEl is not 'img' then we just add an img child to the el.
  72. if (me.autoEl == 'img') {
  73. img = config;
  74. } else {
  75. config.cn = [img = {
  76. tag: 'img',
  77. id: me.id + '-img'
  78. }];
  79. }
  80. if (me.imgCls) {
  81. img.cls = (img.cls ? img.cls + ' ' : '') + me.imgCls;
  82. }
  83. img.src = me.src || Ext.BLANK_IMAGE_URL;
  84. if (me.alt) {
  85. img.alt = me.alt;
  86. }
  87. return config;
  88. },
  89. onRender: function () {
  90. var me = this,
  91. el;
  92. me.callParent(arguments);
  93. el = me.el;
  94. me.imgEl = (me.autoEl == 'img') ? el : el.getById(me.id + '-img');
  95. },
  96. onDestroy: function () {
  97. Ext.destroy(this.imgEl);
  98. this.imgEl = null;
  99. this.callParent();
  100. },
  101. <span id='Ext-Img-method-setSrc'> /**
  102. </span> * Updates the {@link #src} of the image.
  103. * @param {String} src
  104. */
  105. setSrc: function(src) {
  106. var me = this,
  107. imgEl = me.imgEl;
  108. me.src = src;
  109. if (imgEl) {
  110. imgEl.dom.src = src || Ext.BLANK_IMAGE_URL;
  111. }
  112. }
  113. });</pre>
  114. </body>
  115. </html>