Fix #6883 - image loading tidy up
[roojs1] / Roo / bootstrap / Img.js
1 /*
2  * - LGPL
3  *
4  * image
5  * 
6  */
7
8
9 /**
10  * @class Roo.bootstrap.Img
11  * @extends Roo.bootstrap.Component
12  * Bootstrap Img class
13  * @cfg {Boolean} imgResponsive false | true
14  * @cfg {String} border rounded | circle | thumbnail
15  * @cfg {String} src image source
16  * @cfg {String} alt image alternative text
17  * @cfg {String} href a tag href
18  * @cfg {String} target (_self|_blank|_parent|_top)target for a href.
19  * @cfg {String} xsUrl xs image source
20  * @cfg {String} smUrl sm image source
21  * @cfg {String} mdUrl md image source
22  * @cfg {String} lgUrl lg image source
23  * @cfg {Boolean} backgroundContain (use style background and contain image in content)
24  * 
25  * @constructor
26  * Create a new Input
27  * @param {Object} config The config object
28  */
29
30 Roo.bootstrap.Img = function(config){
31     Roo.bootstrap.Img.superclass.constructor.call(this, config);
32     
33     this.addEvents({
34         // img events
35         /**
36          * @event click
37          * The img click event for the img.
38          * @param {Roo.EventObject} e
39          */
40         "click" : true,
41         /**
42          * @event load
43          * The when any image loads
44          * @param {Roo.EventObject} e
45          */
46         "load" : true
47     });
48 };
49
50 Roo.extend(Roo.bootstrap.Img, Roo.bootstrap.Component,  {
51     
52     imgResponsive: true,
53     border: '',
54     src: 'about:blank',
55     href: false,
56     target: false,
57     xsUrl: '',
58     smUrl: '',
59     mdUrl: '',
60     lgUrl: '',
61     backgroundContain : false,
62
63     getAutoCreate : function()
64     {   
65         if(this.src || (!this.xsUrl && !this.smUrl && !this.mdUrl && !this.lgUrl)){
66             return this.createSingleImg();
67         }
68         
69         var cfg = {
70             tag: 'div',
71             cls: 'roo-image-responsive-group',
72             cn: []
73         };
74         var _this = this;
75         
76         Roo.each(['xs', 'sm', 'md', 'lg'], function(size){
77             
78             if(!_this[size + 'Url']){
79                 return;
80             }
81             
82             var img = {
83                 tag: 'img',
84                 cls: (_this.imgResponsive) ? 'img-responsive' : '',
85                 html: _this.html || cfg.html,
86                 src: _this[size + 'Url']
87             };
88             
89             img.cls += ' roo-image-responsive-' + size;
90             
91             var s = ['xs', 'sm', 'md', 'lg'];
92             
93             s.splice(s.indexOf(size), 1);
94             
95             Roo.each(s, function(ss){
96                 img.cls += ' hidden-' + ss;
97             });
98             
99             if (['rounded','circle','thumbnail'].indexOf(_this.border)>-1) {
100                 cfg.cls += ' img-' + _this.border;
101             }
102             
103             if(_this.alt){
104                 cfg.alt = _this.alt;
105             }
106             
107             if(_this.href){
108                 var a = {
109                     tag: 'a',
110                     href: _this.href,
111                     cn: [
112                         img
113                     ]
114                 };
115
116                 if(this.target){
117                     a.target = _this.target;
118                 }
119             }
120             
121             cfg.cn.push((_this.href) ? a : img);
122             
123         });
124         
125         return cfg;
126     },
127     
128     createSingleImg : function()
129     {
130         var cfg = {
131             tag: 'img',
132             cls: (this.imgResponsive) ? 'img-responsive' : '',
133             html : null,
134             src : Roo.BLANK_IMAGE_URL  // just incase src get's set to undefined?!?
135         };
136         
137         if (this.backgroundContain) {
138             cfg.cls += ' background-contain';
139         }
140         
141         cfg.html = this.html || cfg.html;
142         
143         if (this.backgroundContain) {
144             cfg.style="background-image: url(" + this.src + ')';
145         } else {
146             cfg.src = this.src || cfg.src;
147         }
148         
149         if (['rounded','circle','thumbnail'].indexOf(this.border)>-1) {
150             cfg.cls += ' img-' + this.border;
151         }
152         
153         if(this.alt){
154             cfg.alt = this.alt;
155         }
156         
157         if(this.href){
158             var a = {
159                 tag: 'a',
160                 href: this.href,
161                 cn: [
162                     cfg
163                 ]
164             };
165             
166             if(this.target){
167                 a.target = this.target;
168             }
169             
170         }
171         
172         return (this.href) ? a : cfg;
173     },
174     
175     initEvents: function() 
176     {
177         if(!this.href){
178             this.el.on('click', this.onClick, this);
179         }
180         if(this.src || (!this.xsUrl && !this.smUrl && !this.mdUrl && !this.lgUrl)){
181             this.el.on('load', this.onImageLoad, this);
182         } else {
183             // not sure if this works.. not tested
184             this.el.select('img', true).on('load', this.onImageLoad, this);
185         }
186         
187     },
188     
189     onClick : function(e)
190     {
191         Roo.log('img onclick');
192         this.fireEvent('click', this, e);
193     },
194     onImageLoad: function(e)
195     {
196         Roo.log('img load');
197         this.fireEvent('load', this, e);
198     },
199     
200     /**
201      * Sets the url of the image - used to update it
202      * @param {String} url the url of the image
203      */
204     
205     setSrc : function(url)
206     {
207         this.src =  url;
208         
209         if(this.src || (!this.xsUrl && !this.smUrl && !this.mdUrl && !this.lgUrl)){
210             if (this.backgroundContain) {
211                 this.el.dom.style.backgroundImage =  'url(' + url + ')';
212             } else {
213                 this.el.dom.src =  url;
214             }
215             return;
216         }
217         
218         this.el.select('img', true).first().dom.src =  url;
219     }
220     
221     
222    
223 });
224
225