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         
67         Roo.each(['xsUrl', 'smUrl', 'mdUrl', 'lgUrl'], function(size){
68             if(!this[size]){
69                 return;
70             }
71             
72             var img = {
73                 tag: 'img',
74                 cls: (this.imgResponsive) ? 'img-responsive' : '',
75                 html: this.html || cfg.html,
76                 src: this[size]
77             }
78             
79             if (['rounded','circle','thumbnail'].indexOf(this.border)>-1) {
80                 cfg.cls += ' img-' + this.border;
81             }
82             
83             if(this.alt){
84                 cfg.alt = this.alt;
85             }
86             
87             if(this.href){
88                 var a = {
89                     tag: 'a',
90                     href: this.href,
91                     cn: [
92                         img
93                     ]
94                 }
95
96                 if(this.target){
97                     a.target = this.target;
98                 }
99             }
100             
101             cfg.cn.push((this.href) ? a : img);
102             
103         });
104         
105         return cfg;
106     },
107     
108     createSingleImg : function()
109     {
110         var cfg = {
111             tag: 'img',
112             cls: (this.imgResponsive) ? 'img-responsive' : '',
113             html : null
114         }
115         
116         cfg.html = this.html || cfg.html;
117         
118         cfg.src = this.src || cfg.src;
119         
120         if (['rounded','circle','thumbnail'].indexOf(this.border)>-1) {
121             cfg.cls += ' img-' + this.border;
122         }
123         
124         if(this.alt){
125             cfg.alt = this.alt;
126         }
127         
128         if(this.href){
129             var a = {
130                 tag: 'a',
131                 href: this.href,
132                 cn: [
133                     cfg
134                 ]
135             }
136             
137             if(this.target){
138                 a.target = this.target;
139             }
140             
141         }
142         
143         return (this.href) ? a : cfg;
144     },
145     
146     initEvents: function() {
147         
148         if(!this.href){
149             this.el.on('click', this.onClick, this);
150         }
151     },
152     
153     onClick : function(e)
154     {
155         Roo.log('img onclick');
156         this.fireEvent('click', this, e);
157     }
158    
159 });
160
161