a76c31be69dfef6792ed8cf26f02ad50362cabf2
[roojs1] / Roo / bootstrap / ButtonUploader.js
1
2 /*
3 * Licence: LGPL
4 */
5
6 /**
7  * @class Roo.bootstrap.ButtonUploader
8  * @extends Roo.bootstrap.Button
9  * Bootstrap Button Uploader class - it's a button which when you add files to it
10  *
11  * 
12  * @cfg {Number} errorTimeout default 3000
13  * @cfg {Array}  images  an array of ?? Img objects ??? when loading existing files..
14  * @cfg {Array}  html The button text.
15
16  *
17  * @constructor
18  * Create a new CardUploader
19  * @param {Object} config The config object
20  */
21
22 Roo.bootstrap.ButtonUploader = function(config){
23     
24  
25     
26     Roo.bootstrap.ButtonUploader.superclass.constructor.call(this, config);
27     
28      
29      this.addEvents({
30          // raw events
31         /**
32          * @event beforeselect
33          * When button is pressed, before show upload files dialog is shown
34          * @param {Roo.bootstrap.UploaderButton} this
35          *
36          */
37         'beforeselect' : true,
38          /**
39          * @event fired when files have been selected, 
40          * When a the download link is clicked
41          * @param {Roo.bootstrap.UploaderButton} this
42          * @param {Array} Array of files that have been uploaded
43          */
44         'uploaded' : true
45         
46     });
47 };
48  
49 Roo.extend(Roo.bootstrap.ButtonUploader, Roo.bootstrap.Button,  {
50     
51      
52     errorTimeout : 3000,
53      
54     images : false,
55    
56     fileCollection : false,
57     allowBlank : true,
58     
59     getAutoCreate : function()
60     {
61         
62         
63         return  {
64             cls :'div' ,
65             cn : [
66                 Roo.bootstrap.Button.prototype.getAutoCreate.call(this),
67                 {
68                     tag: 'input',
69                     multiple : 'multiple',
70                     type : 'file',
71                     cls : 'd-none  roo-card-upload-selector' 
72                   
73                 }
74                  
75
76             ]
77         };
78            
79          
80     },
81      
82    
83     initEvents : function()
84     {
85         
86         Roo.bootstrap.Button.prototype.initEvents.call(this);
87         
88         
89         
90         
91         
92         this.urlAPI = (window.createObjectURL && window) || 
93                                 (window.URL && URL.revokeObjectURL && URL) || 
94                                 (window.webkitURL && webkitURL);
95                         
96          
97          
98          
99         this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
100         
101         this.selectorEl.on('change', this.onFileSelected, this);
102          
103          
104        
105     },
106     
107    
108     onClick : function(e)
109     {
110         e.preventDefault();
111         
112         if ( this.fireEvent('beforeselect', this) === false) {
113             return;
114         }
115          
116         this.selectorEl.dom.click();
117          
118     },
119     
120     onFileSelected : function(e)
121     {
122         e.preventDefault();
123         
124         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
125             return;
126         }
127         this.fireEvent('uploaded', this,  this.selectorEl.dom.files );
128         
129     },
130     
131        
132    
133     
134     /**
135      * addCard - add an Attachment to the uploader
136      * @param data - the data about the image to upload
137      *
138      * {
139           id : 123
140           title : "Title of file",
141           is_uploaded : false,
142           src : "http://.....",
143           srcfile : { the File upload object },
144           mimetype : file.type,
145           preview : false,
146           is_deleted : 0
147           .. any other data...
148         }
149      *
150      * 
151     */
152      
153     reset: function()
154     {
155          
156          this.selectorEl
157     } 
158     
159     
160     
161     
162 });
163