]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * Simple helper class for easily creating image components. This renders an image tag to\r | |
3 | * the DOM with the configured src.\r | |
4 | *\r | |
5 | * {@img Ext.Img/Ext.Img.png Ext.Img component}\r | |
6 | *\r | |
7 | * ## Example usage:\r | |
8 | *\r | |
9 | * var changingImage = Ext.create('Ext.Img', {\r | |
10 | * src: 'http://www.sencha.com/img/20110215-feat-html5.png',\r | |
11 | * width: 184,\r | |
12 | * height: 90,\r | |
13 | * renderTo: Ext.getBody()\r | |
14 | * });\r | |
15 | *\r | |
16 | * // change the src of the image programmatically\r | |
17 | * changingImage.setSrc('http://www.sencha.com/img/20110215-feat-perf.png');\r | |
18 | *\r | |
19 | * By default, only an img element is rendered and that is this component's primary\r | |
20 | * {@link Ext.Component#getEl element}. If the {@link Ext.Component#autoEl} property\r | |
21 | * is other than 'img' (the default), the a child img element will be added to the primary\r | |
22 | * element. This can be used to create a wrapper element around the img.\r | |
23 | *\r | |
24 | * ## Wrapping the img in a div:\r | |
25 | *\r | |
26 | * var wrappedImage = Ext.create('Ext.Img', {\r | |
27 | * src: 'http://www.sencha.com/img/20110215-feat-html5.png',\r | |
28 | * autoEl: 'div', // wrap in a div\r | |
29 | * renderTo: Ext.getBody()\r | |
30 | * });\r | |
31 | *\r | |
32 | * ## Image Dimensions\r | |
33 | *\r | |
34 | * You should include height and width dimensions for any image owned by a parent \r | |
35 | * container. By omitting dimensions, an owning container will not know how to \r | |
36 | * size and position the image in the initial layout.\r | |
37 | */\r | |
38 | Ext.define('Ext.Img', {\r | |
39 | extend: 'Ext.Component',\r | |
40 | alias: ['widget.image', 'widget.imagecomponent'],\r | |
41 | \r | |
42 | autoEl: 'img',\r | |
43 | \r | |
44 | baseCls: Ext.baseCSSPrefix + 'img',\r | |
45 | \r | |
46 | config: {\r | |
47 | /**\r | |
48 | * @cfg {String} src The source of this image. See {@link Ext#resolveResource} for\r | |
49 | * details on locating application resources.\r | |
50 | * @accessor\r | |
51 | */\r | |
52 | src: null\r | |
53 | },\r | |
54 | \r | |
55 | /**\r | |
56 | * @cfg {String} alt\r | |
57 | * The descriptive text for non-visual UI description.\r | |
58 | */\r | |
59 | alt: '',\r | |
60 | \r | |
61 | /**\r | |
62 | * @cfg {String} title\r | |
63 | * Specifies addtional information about the image.\r | |
64 | */\r | |
65 | title: '',\r | |
66 | \r | |
67 | /**\r | |
68 | * @cfg {String} imgCls\r | |
69 | * Optional CSS classes to add to the img element.\r | |
70 | */\r | |
71 | imgCls: '',\r | |
72 | \r | |
73 | /**\r | |
74 | * @cfg {Number/String} glyph\r | |
75 | * A numeric unicode character code to serve as the image. If this option is used\r | |
76 | * The image will be rendered using a div with innerHTML set to the html entity\r | |
77 | * for the given character code. The default font-family for glyphs can be set\r | |
78 | * globally using {@link Ext#setGlyphFontFamily Ext.setGlyphFontFamily()}. Alternatively,\r | |
79 | * this config option accepts a string with the charCode and font-family separated by\r | |
80 | * the `@` symbol. For example '65@My Font Family'.\r | |
81 | */\r | |
82 | \r | |
83 | maskOnDisable: false,\r | |
84 | \r | |
85 | initComponent: function() {\r | |
86 | if (this.glyph) {\r | |
87 | this.autoEl = 'div';\r | |
88 | }\r | |
89 | \r | |
90 | this.callParent();\r | |
91 | },\r | |
92 | \r | |
93 | applySrc: function (src) {\r | |
94 | return src && Ext.resolveResource(src);\r | |
95 | },\r | |
96 | \r | |
97 | getElConfig: function() {\r | |
98 | var me = this,\r | |
99 | autoEl = me.autoEl,\r | |
100 | config = me.callParent(),\r | |
101 | glyphFontFamily = Ext._glyphFontFamily,\r | |
102 | glyph = me.glyph,\r | |
103 | img, glyphParts;\r | |
104 | \r | |
105 | // It is sometimes helpful (like in a panel header icon) to have the img wrapped\r | |
106 | // by a div. If our autoEl is not 'img' then we just add an img child to the el.\r | |
107 | if (autoEl === 'img' || (Ext.isObject(autoEl) && autoEl.tag === 'img')) {\r | |
108 | img = config;\r | |
109 | }\r | |
110 | else if (me.glyph) {\r | |
111 | if (typeof glyph === 'string') {\r | |
112 | glyphParts = glyph.split('@');\r | |
113 | glyph = glyphParts[0];\r | |
114 | glyphFontFamily = glyphParts[1] || glyphFontFamily;\r | |
115 | }\r | |
116 | \r | |
117 | config.html = '&#' + glyph + ';';\r | |
118 | \r | |
119 | if (glyphFontFamily) {\r | |
120 | config.style = config.style || {};\r | |
121 | config.style.fontFamily = glyphFontFamily;\r | |
122 | }\r | |
123 | \r | |
124 | // A glyph is a graphic which is not an <img> tag so it should have\r | |
125 | // the corresponding role for Accessibility interface to recognize\r | |
126 | config.role = 'img';\r | |
127 | }\r | |
128 | else {\r | |
129 | config.cn = [img = {\r | |
130 | tag: 'img',\r | |
131 | id: me.id + '-img'\r | |
132 | }];\r | |
133 | }\r | |
134 | \r | |
135 | if (img) {\r | |
136 | if (me.imgCls) {\r | |
137 | img.cls = (img.cls ? img.cls + ' ' : '') + me.imgCls;\r | |
138 | }\r | |
139 | \r | |
140 | img.src = me.src || Ext.BLANK_IMAGE_URL;\r | |
141 | }\r | |
142 | \r | |
143 | if (me.alt) {\r | |
144 | (img || config).alt = me.alt;\r | |
145 | }\r | |
146 | else {\r | |
147 | // Images that do not have alt attribute can't be properly announced\r | |
148 | // by screen readers. In best case they will be silently skipped;\r | |
149 | // in worst case screen reader will announce data url. Yes, that very long\r | |
150 | // base-64 encoded string. :/\r | |
151 | // That will make the application totally unusable for blind people.\r | |
152 | (img || config).alt = '';\r | |
153 | \r | |
154 | //<debug>\r | |
155 | Ext.log.warn('For WAI-ARIA compliance, IMG elements SHOULD have an alt attribute.');\r | |
156 | //</debug>\r | |
157 | }\r | |
158 | \r | |
159 | if (me.title) {\r | |
160 | (img || config).title = me.title;\r | |
161 | }\r | |
162 | \r | |
163 | return config;\r | |
164 | },\r | |
165 | \r | |
166 | onRender: function () {\r | |
167 | var me = this,\r | |
168 | autoEl = me.autoEl,\r | |
169 | el;\r | |
170 | \r | |
171 | me.callParent(arguments);\r | |
172 | \r | |
173 | el = me.el;\r | |
174 | \r | |
175 | if (autoEl === 'img' || (Ext.isObject(autoEl) && autoEl.tag === 'img')) {\r | |
176 | me.imgEl = el;\r | |
177 | } else {\r | |
178 | me.imgEl = el.getById(me.id + '-img');\r | |
179 | }\r | |
180 | },\r | |
181 | \r | |
182 | onDestroy: function () {\r | |
183 | var me = this,\r | |
184 | imgEl = me.imgEl;\r | |
185 | \r | |
186 | // Only clean up when the img is a child, otherwise it will get handled\r | |
187 | // by the element destruction in the parent\r | |
188 | if (imgEl && me.el !== imgEl) {\r | |
189 | imgEl.destroy();\r | |
190 | }\r | |
191 | this.imgEl = null;\r | |
192 | this.callParent();\r | |
193 | },\r | |
194 | \r | |
195 | getTitle: function () {\r | |
196 | return this.title;\r | |
197 | },\r | |
198 | \r | |
199 | /**\r | |
200 | * Updates the {@link #title} of the image.\r | |
201 | * @param {String} title\r | |
202 | */\r | |
203 | setTitle: function (title) {\r | |
204 | var me = this,\r | |
205 | imgEl = me.imgEl;\r | |
206 | \r | |
207 | me.title = title || '';\r | |
208 | \r | |
209 | if (imgEl) {\r | |
210 | imgEl.dom.title = title || '';\r | |
211 | }\r | |
212 | },\r | |
213 | \r | |
214 | getAlt: function () {\r | |
215 | return this.alt;\r | |
216 | },\r | |
217 | \r | |
218 | /**\r | |
219 | * Updates the {@link #alt} of the image.\r | |
220 | * @param {String} alt\r | |
221 | */\r | |
222 | setAlt: function (alt) {\r | |
223 | var me = this,\r | |
224 | imgEl = me.imgEl;\r | |
225 | \r | |
226 | me.alt = alt || '';\r | |
227 | \r | |
228 | if (imgEl) {\r | |
229 | imgEl.dom.alt = alt || '';\r | |
230 | }\r | |
231 | },\r | |
232 | \r | |
233 | updateSrc: function (src) {\r | |
234 | var imgEl = this.imgEl;\r | |
235 | \r | |
236 | if (imgEl) {\r | |
237 | imgEl.dom.src = src || Ext.BLANK_IMAGE_URL;\r | |
238 | }\r | |
239 | },\r | |
240 | \r | |
241 | /**\r | |
242 | * Updates the {@link #glyph} of the image.\r | |
243 | * @param {Number/String} glyph\r | |
244 | */\r | |
245 | setGlyph: function (glyph) {\r | |
246 | var me = this,\r | |
247 | glyphFontFamily = Ext._glyphFontFamily,\r | |
248 | old = me.glyph,\r | |
249 | el = me.el,\r | |
250 | glyphParts;\r | |
251 | \r | |
252 | me.glyph = glyph;\r | |
253 | if (me.rendered && glyph !== old) {\r | |
254 | if (typeof glyph === 'string') {\r | |
255 | glyphParts = glyph.split('@');\r | |
256 | glyph = glyphParts[0];\r | |
257 | glyphFontFamily = glyphParts[1] || glyphFontFamily;\r | |
258 | }\r | |
259 | \r | |
260 | el.dom.innerHTML = '&#' + glyph + ';';\r | |
261 | if (glyphFontFamily) {\r | |
262 | el.setStyle('font-family', glyphFontFamily);\r | |
263 | }\r | |
264 | }\r | |
265 | }\r | |
266 | }); |