31fdeb626798570866e1eec3dbf811ff409c21e3
[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  * @cfg {Boolean}  multiple (default true) Should the upload allow multiple files to be uploaded.
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         var im = {
62             tag: 'input',
63             
64             type : 'file',
65             cls : 'd-none  roo-card-upload-selector' 
66           
67         }
68         if (this.multiple) {
69             im.multiple = 'multiple';
70         }
71         
72         return  {
73             cls :'div' ,
74             cn : [
75                 Roo.bootstrap.Button.prototype.getAutoCreate.call(this),
76                 im
77
78             ]
79         };
80            
81          
82     },
83      
84    
85     initEvents : function()
86     {
87         
88         Roo.bootstrap.Button.prototype.initEvents.call(this);
89         
90         
91         
92         
93         
94         this.urlAPI = (window.createObjectURL && window) || 
95                                 (window.URL && URL.revokeObjectURL && URL) || 
96                                 (window.webkitURL && webkitURL);
97                         
98          
99          
100          
101         this.selectorEl = this.el.select('.roo-card-upload-selector', true).first();
102         
103         this.selectorEl.on('change', this.onFileSelected, this);
104          
105          
106        
107     },
108     
109    
110     onClick : function(e)
111     {
112         e.preventDefault();
113         
114         if ( this.fireEvent('beforeselect', this) === false) {
115             return;
116         }
117          
118         this.selectorEl.dom.click();
119          
120     },
121     
122     onFileSelected : function(e)
123     {
124         e.preventDefault();
125         
126         if(typeof(this.selectorEl.dom.files) == 'undefined' || !this.selectorEl.dom.files.length){
127             return;
128         }
129         var files = Array.prototype.slice.call(this.selectorEl.dom.files);
130         this.selectorEl.dom.reset();
131         
132         this.fireEvent('uploaded', this,  files );
133         
134     },
135     
136        
137    
138     
139     /**
140      * addCard - add an Attachment to the uploader
141      * @param data - the data about the image to upload
142      *
143      * {
144           id : 123
145           title : "Title of file",
146           is_uploaded : false,
147           src : "http://.....",
148           srcfile : { the File upload object },
149           mimetype : file.type,
150           preview : false,
151           is_deleted : 0
152           .. any other data...
153         }
154      *
155      * 
156     */
157      
158     reset: function()
159     {
160          
161          this.selectorEl
162     } 
163     
164     
165     
166     
167 });
168