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