Pman.Dialog.Image.bjs
[Pman.Core] / Pman.Dialog.Image.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.Image = {
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                 
37                     // this does not really work - escape on the borders works..
38                     // resize to fit.. if we have styled stuff...
39                     
40                     
41                     
42                     
43                     var d = this;
44                     
45                     var pad =     d.el.getSize().height - (d.header.getSize().height +
46                         d.footer.getSize().height +        
47                         d.layout.getRegion('center').getPanel(0).el.getSize().height
48                         )
49                     
50                     var height = (
51                         pad + 
52                         d.header.getSize().height +
53                         d.footer.getSize().height +        
54                         d.layout.getRegion('center').getPanel(0).el.child('div').getSize().height
55                     );
56                     this.resizeTo(d.el.getSize().width, height);
57                     
58                     if (this.keylistener) {
59                         return;
60                     }
61                     this.keylistener = this.addKeyListener(27, this.hide, this);
62                     
63                 }
64             },
65             closable : false,
66             collapsible : false,
67             haveProgress : false,
68             height : 140,
69             modal : true,
70             resizable : false,
71             title : "Upload an Image or File",
72             uploadComplete : false,
73             width : 500,
74             shadow : true,
75             uploadProgress : function()
76             {
77                 var dlg = this;
78                if (!dlg.haveProgress) {
79                     Roo.MessageBox.progress("Uploading", "Uploading");
80                 }
81                 
82                 if (dlg.haveProgress == 2) {
83                     // it's been closed elsewhere..
84                     return;
85                 }
86                 if (dlg.uploadComplete) {
87                     Roo.MessageBox.hide();
88                     return;
89                 }
90                 
91                 dlg.haveProgress = 1;
92             
93                 var uid = _this.form.findField('UPLOAD_IDENTIFIER').getValue();
94                 new Pman.Request({
95                     url : baseURL + '/Core/UploadProgress.php',
96                     params: {
97                         id : uid
98                     },
99                     method: 'GET',
100                     success : function(res){
101                         //console.log(data);
102                         var data = res.data;
103                         if (dlg.haveProgress == 2) {
104                             // it's been closed elsewhere..
105                             return;
106                         }
107                         
108                         if (dlg.uploadComplete) {
109                             Roo.MessageBox.hide();
110                             return;
111                         }
112                             
113                         if (data){
114                             Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
115                                 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
116                             );
117                         } else {
118                             Roo.MessageBox.updateProgress(1,
119                                 "Upload Complete - processing"
120                             );
121                             return;
122                         }
123                         dlg.uploadProgress.defer(2000,dlg);
124                     },
125                     failure: function(data) {
126                       //  console.log('fail');
127                      //   console.log(data);
128                     }
129                 })
130                 
131             },
132             items : [
133                 {
134                     xtype: 'ContentPanel',
135                     xns: Roo,
136                     fitToFrame : true,
137                     region : 'center',
138                     items : [
139                         {
140                             xtype: 'Form',
141                             xns: Roo.form,
142                             listeners : {
143                                 actioncomplete : function(_self,act)
144                                 {
145                                     _this.dialog.uploadComplete = true;
146                                     _this.dialog.haveProgress = 2; 
147                                     Roo.MessageBox.hide(); // force hiding
148                                     //_this.dialog.el.unmask();
149                                      
150                                     if (act.type == 'setdata') { 
151                                         this.url = _this.data._url ? _this.data._url : baseURL + '/Roo/Images.php';
152                                         this.el.dom.action = this.url;
153                                         if (typeof(_this.data.timeout) != 'undefined') {
154                                             this.timeout = _this.data.timeout;
155                                         }
156                                         
157                                         this.findField('UPLOAD_IDENTIFIER').setValue(
158                                             (new Date() * 1) + '' + Math.random());
159                                             
160                                         return;
161                                     }
162                                      
163                                    
164                                     if (act.type == 'load') {
165                                       // should this happen?  
166                                         _this.data = act.result.data;
167                                        // _this.loaded();
168                                         return;
169                                     }
170                                     
171                                     
172                                     if (act.type == 'submit') { // only submitted here if we are 
173                                         _this.dialog.hide();
174                                         Roo.log("Upload success");
175                                         Roo.log(act);
176                                         //console.log(act);
177                                         if (_this.callback) {
178                                             _this.callback.call(this, act.result.data, act.result.extra);
179                                         }
180                                         return; 
181                                     }
182                                     
183                                 
184                                     
185                                 },
186                                 rendered : function (form)
187                                 {
188                                     _this.form= form;
189                                 },
190                                 actionfailed : function (_self, act)
191                                 {
192                                    
193                                    
194                                     _this.dialog.uploadComplete = true;
195                                    // _this.dialog.el.unmask();
196                                     // error msg???
197                                      _this.dialog.haveProgress = 2; 
198                                     if (act.type == 'submit') {
199                                         Roo.log("Upload error");
200                                         Roo.log(act);
201                                         
202                                         try {
203                                             Roo.MessageBox.alert("Error", act.result.errorMsg.split(/\n/).join('<BR/>'));
204                                         } catch(e) {
205                                           //  Roo.log(e);
206                                             Roo.MessageBox.alert("Error", "Saving failed = fix errors and try again");        
207                                         }
208                                         return;
209                                     }
210                                     
211                                     // what about load failing..
212                                     Roo.MessageBox.alert("Error", "Error loading details"); 
213                                 }
214                             },
215                             fileUpload : true,
216                             labelWidth : 140,
217                             method : 'POST',
218                             style : 'margin:10px;',
219                             timeout : 300,
220                             url : baseURL + '/Roo/Images.php',
221                             items : [
222                                 {
223                                     xtype: 'Hidden',
224                                     xns: Roo.form,
225                                     name : 'UPLOAD_IDENTIFIER'
226                                 },
227                                 {
228                                     xtype: 'Hidden',
229                                     xns: Roo.form,
230                                     name : 'post_max_size',
231                                     value : "32M"
232                                 },
233                                 {
234                                     xtype: 'Hidden',
235                                     xns: Roo.form,
236                                     name : 'upload_max_filesize',
237                                     value : "32M"
238                                 },
239                                 {
240                                     xtype: 'TextField',
241                                     xns: Roo.form,
242                                     fieldLabel : 'Upload Image or File',
243                                     inputType : 'file',
244                                     name : 'imageUpload',
245                                     width : 200
246                                 },
247                                 {
248                                     xtype: 'Hidden',
249                                     xns: Roo.form,
250                                     name : 'ontable'
251                                 },
252                                 {
253                                     xtype: 'Hidden',
254                                     xns: Roo.form,
255                                     name : 'onid'
256                                 },
257                                 {
258                                     xtype: 'Hidden',
259                                     xns: Roo.form,
260                                     name : 'id'
261                                 },
262                                 {
263                                     xtype: 'Hidden',
264                                     xns: Roo.form,
265                                     name : 'imgtype'
266                                 }
267                             ]
268                         }
269                     ]
270                 }
271             ],
272             center : {
273                 xtype: 'LayoutRegion',
274                 xns: Roo
275             },
276             buttons : [
277                 {
278                     xtype: 'Button',
279                     xns: Roo,
280                     listeners : {
281                         click : function (_self, e)
282                         {
283                             _this.dialog.hide();
284                         }
285                     },
286                     text : "Cancel"
287                 },
288                 {
289                     xtype: 'Button',
290                     xns: Roo,
291                     listeners : {
292                         click : function (_self, e)
293                         {
294                             // do some checks?
295                              
296                             //_this.dialog.el.mask("Sending");
297                             _this.dialog.uploadComplete = false;
298                             _this.form.doAction('submit', {
299                                 params: {
300                                     ts : Math.random()
301                                 } 
302                             });
303                             _this.dialog.haveProgress = 0; // set to show..
304                             _this.dialog.uploadProgress.defer(1000, _this.dialog);
305                         
306                         }
307                     },
308                     text : "Upload"
309                 }
310             ]
311         });
312     }
313 };