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