Roo/bootstrap/Img.js
[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  * 
24  * @constructor
25  * Create a new Input
26  * @param {Object} config The config object
27  */
28
29 Roo.bootstrap.Img = function(config){
30     Roo.bootstrap.Img.superclass.constructor.call(this, config);
31     
32     this.addEvents({
33         // img events
34         /**
35          * @event click
36          * The img click event for the img.
37          * @param {Roo.EventObject} e
38          */
39         "click" : true
40     });
41 };
42
43 Roo.extend(Roo.bootstrap.Img, Roo.bootstrap.Component,  {
44     
45     imgResponsive: true,
46     border: '',
47     src: '',
48     href: false,
49     target: false,
50     xsUrl: '',
51     smUrl: '',
52     mdUrl: '',
53     lgUrl: '',
54
55     getAutoCreate : function()
56     {   
57         if(this.src || (!this.xsUrl && !this.smUrl && !this.mdUrl && !this.lgUrl)){
58             return this.createSingleImg();
59         }
60         
61         var cfg = {
62             tag: 'div',
63             cls: 'roo-image-responsive-group',
64             cn: []
65         }
66         var _this = this;
67         
68         Roo.each(['xs', 'sm', 'md', 'lg'], function(size){
69             Roo.log(size);
70             Roo.log(_this[size + 'Url']);
71             if(!_this[size + 'Url']){
72                 return;
73             }
74             
75             var img = {
76                 tag: 'img',
77                 cls: (_this.imgResponsive) ? 'img-responsive' : '',
78                 html: _this.html || cfg.html,
79                 src: _this[size]
80             }
81             
82             img.cls += ' roo-image-responsive-' + size;
83             
84             if (['rounded','circle','thumbnail'].indexOf(_this.border)>-1) {
85                 cfg.cls += ' img-' + _this.border;
86             }
87             
88             if(_this.alt){
89                 cfg.alt = _this.alt;
90             }
91             
92             if(_this.href){
93                 var a = {
94                     tag: 'a',
95                     href: _this.href,
96                     cn: [
97                         img
98                     ]
99                 }
100
101                 if(this.target){
102                     a.target = _this.target;
103                 }
104             }
105             
106             cfg.cn.push((_this.href) ? a : img);
107             
108         });
109         
110         return cfg;
111     },
112     
113     createSingleImg : function()
114     {
115         var cfg = {
116             tag: 'img',
117             cls: (this.imgResponsive) ? 'img-responsive' : '',
118             html : null
119         }
120         
121         cfg.html = this.html || cfg.html;
122         
123         cfg.src = this.src || cfg.src;
124         
125         if (['rounded','circle','thumbnail'].indexOf(this.border)>-1) {
126             cfg.cls += ' img-' + this.border;
127         }
128         
129         if(this.alt){
130             cfg.alt = this.alt;
131         }
132         
133         if(this.href){
134             var a = {
135                 tag: 'a',
136                 href: this.href,
137                 cn: [
138                     cfg
139                 ]
140             }
141             
142             if(this.target){
143                 a.target = this.target;
144             }
145             
146         }
147         
148         return (this.href) ? a : cfg;
149     },
150     
151     initEvents: function() 
152     {
153         if(!this.href){
154             this.el.on('click', this.onClick, this);
155         }
156         
157     },
158     
159     onClick : function(e)
160     {
161         Roo.log('img onclick');
162         this.fireEvent('click', this, e);
163     }
164    
165 });
166
167