Pman.Dialog.Image.bjs
[Pman.Core] / Pman.Dialog.ImageUpload.js
1 //<script type="text/javascript">
2
3 // Auto generated file - created by app.Builder.js- do not edit directly (at present!)
4
5 Pman.Dialog.ImageUpload = {
6
7     dialog : false,
8     callback:  false,
9
10     show : function(data, cb)
11     {
12         if (!this.dialog) {
13             this.create();
14         }
15
16         this.callback = cb;
17         this.data = data;
18         this.dialog.show(this.data._el);
19         if (this.form) {
20            this.form.reset();
21            this.form.setValues(data);
22            this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });
23         }
24
25     },
26
27     create : function()
28     {
29         var _this = this;
30         this.dialog = Roo.factory({
31             xtype: 'LayoutDialog',
32             xns: Roo,
33             listeners : {
34                 show : function (_self)
35                 {
36                     if (this.keylistener) {
37                         return;
38                     }
39                     this.keylistener = this.addKeyListener(27, this.hide, this);
40                 }
41             },
42             closable : false,
43             collapsible : false,
44             haveProgress : false,
45             height : 140,
46             modal : true,
47             resizable : true,
48             title : "Upload an Image or File",
49             uploadProgress : false,
50             width : 500,
51             shadow : true,
52             uploadProgres : function()
53             {
54                 var dlg = this;
55                if (!dlg.haveProgress) {
56                     Roo.MessageBox.progress("Uploading", "Uploading");
57                 }
58                 if (dlg.uploadComplete) {
59                     Roo.MessageBox.hide();
60                     return;
61                 }
62                 dlg.haveProgress = true;
63             
64                 var uid = _this.form.findField('UPLOAD_IDENTIFIER').getValue();
65                 Pman.request({
66                     url : baseURL + '/Core/UploadProgress.php',
67                     params: {
68                         id : uid
69                     },
70                     method: 'GET',
71                     success : function(data){
72                         //console.log(data);
73                         if (dlg.uploadComplete) {
74                             Roo.MessageBox.hide();
75                             return;
76                         }
77                             
78                         if (data){
79                             Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
80                                 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
81                             );
82                         }
83                         dlg.uploadProgress.defer(2000,dlg);
84                     },
85                     failure: function(data) {
86                       //  console.log('fail');
87                      //   console.log(data);
88                     }
89                 })
90                 
91             },
92             items : [
93                 {
94                     xtype: 'ContentPanel',
95                     xns: Roo,
96                     fitToFrame : true,
97                     region : 'center',
98                     items : [
99                         {
100                             xtype: 'Form',
101                             xns: Roo.form,
102                             listeners : {
103                                 actioncomplete : function(_self,act)
104                                 {
105                                        _this.dialog.uploadComplete = true;
106                                         _this.dialog.el.unmask();
107                                          
108                                           if (act.type == 'setdata') { 
109                                          
110                                               this.findField('UPLOAD_IDENTIFIER').setValue(
111                                                 (new Date() * 1) + '' + Math.random());
112                                             return;
113                                          }
114                                          
115                                        
116                                         if (act.type == 'load') {
117                                           // should this happen?  
118                                             _this.data = act.result.data;
119                                            // _this.loaded();
120                                             return;
121                                         }
122                                         
123                                         
124                                         if (act.type == 'submit') { // only submitted here if we are 
125                                             _this.dialog.hide();
126                                             //console.log(act);
127                                             if (_this.callback) {
128                                                 _this.callback.call(this, act.result.data);
129                                             }
130                                             return; 
131                                         }
132                                 },
133                                 rendered : function (form)
134                                 {
135                                     _this.form= form;
136                                 },
137                                 actionfailed : function (_self, act)
138                                 {
139                                     _this.dialog.uploadComplete = true;
140                                     _this.dialog.el.unmask();
141                                     // error msg???
142                                     
143                                     if (act.type == 'submit') {
144                                         Ext.MessageBox.alert("Error", "Saving failed = fix errors and try again");
145                                         return;
146                                     }
147                                     
148                                     // what about load failing..
149                                     Ext.MessageBox.alert("Error", "Error loading details"); 
150                                 }
151                             },
152                             labelWidth : 140,
153                             method : 'POST',
154                             style : 'margin:10px;',
155                             url : baseURL + '/Roo/Images.php',
156                             items : [
157                                 {
158                                     xtype: 'Hidden',
159                                     xns: Roo.form,
160                                     name : 'UPLOAD_IDENTIFIER'
161                                 },
162                                 {
163                                     xtype: 'TextField',
164                                     xns: Roo.form,
165                                     fieldLabel : 'Upload Image or File',
166                                     inputType : 'file',
167                                     name : 'imageUpload',
168                                     width : 200
169                                 },
170                                 {
171                                     xtype: 'Hidden',
172                                     xns: Roo.form,
173                                     name : 'ontable'
174                                 },
175                                 {
176                                     xtype: 'Hidden',
177                                     xns: Roo.form,
178                                     name : 'onid'
179                                 },
180                                 {
181                                     xtype: 'Hidden',
182                                     xns: Roo.form,
183                                     name : 'imgtype'
184                                 },
185                                 {
186                                     xtype: 'Hidden',
187                                     xns: Roo.form,
188                                     name : 'post_max_size',
189                                     value : "32M"
190                                 },
191                                 {
192                                     xtype: 'Hidden',
193                                     xns: Roo.form,
194                                     name : 'upload_max_filesize',
195                                     value : "32M"
196                                 }
197                             ]
198                         }
199                     ]
200                 }
201             ],
202             center : {
203                 xtype: 'LayoutRegion',
204                 xns: Roo
205             },
206             buttons : [
207                 {
208                     xtype: 'Button',
209                     xns: Roo,
210                     listeners : {
211                         click : function (_self, e)
212                         {
213                             _this.dialog.hide();
214                         }
215                     },
216                     text : "Cancel"
217                 },
218                 {
219                     xtype: 'Button',
220                     xns: Roo,
221                     listeners : {
222                         click : function (_self, e)
223                         {
224                             // do some checks?
225                              
226                             _this.dialog.el.mask("Sending");
227                             _this.dialog.uploadComplete = false;
228                             _this.form.doAction('submit', {
229                                 url: baseURL + '/Roo/Images.html',
230                                 method: 'POST',
231                                 params: {
232                                  //   _id: 0 ,
233                                     ts : Math.random()
234                                 } 
235                             });
236                             _this.dialog.haveProgress = false,
237                             _this.dialog.uploadProgress.defer(1000, _this.dialog);
238                         
239                         }
240                     },
241                     text : "Upload"
242                 }
243             ]
244         });
245     }
246 };