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                         }
97                         dlg.uploadProgress.defer(2000,dlg);
98                     },
99                     failure: function(data) {
100                       //  console.log('fail');
101                      //   console.log(data);
102                     }
103                 })
104                 
105             },
106             items : [
107                 {
108                     xtype: 'ContentPanel',
109                     xns: Roo,
110                     fitToFrame : true,
111                     region : 'center',
112                     items : [
113                         {
114                             xtype: 'Form',
115                             xns: Roo.form,
116                             listeners : {
117                                 actioncomplete : function(_self,act)
118                                 {
119                                     _this.dialog.uploadComplete = true;
120                                     _this.dialog.haveProgress = 2; 
121                                     Roo.MessageBox.hide(); // force hiding
122                                     //_this.dialog.el.unmask();
123                                      
124                                     if (act.type == 'setdata') { 
125                                         this.url = _this.data._url ? _this.data._url : baseURL + '/Roo/Images.php';
126                                         this.el.dom.action = this.url;
127                                         this.findField('UPLOAD_IDENTIFIER').setValue(
128                                             (new Date() * 1) + '' + Math.random());
129                                             
130                                         return;
131                                     }
132                                      
133                                    
134                                     if (act.type == 'load') {
135                                       // should this happen?  
136                                         _this.data = act.result.data;
137                                        // _this.loaded();
138                                         return;
139                                     }
140                                     
141                                     
142                                     if (act.type == 'submit') { // only submitted here if we are 
143                                         _this.dialog.hide();
144                                         Roo.log("Upload success");
145                                         Roo.log(act);
146                                         //console.log(act);
147                                         if (_this.callback) {
148                                             _this.callback.call(this, act.result.data);
149                                         }
150                                         return; 
151                                     }
152                                     
153                                 
154                                     
155                                 },
156                                 rendered : function (form)
157                                 {
158                                     _this.form= form;
159                                 },
160                                 actionfailed : function (_self, act)
161                                 {
162                                    
163                                    
164                                     _this.dialog.uploadComplete = true;
165                                    // _this.dialog.el.unmask();
166                                     // error msg???
167                                      _this.dialog.haveProgress = 2; 
168                                     if (act.type == 'submit') {
169                                         Roo.log("Upload error");
170                                         Roo.log(act);
171                                         
172                                         try {
173                                             Roo.MessageBox.alert("Error -x", act.result.errorMsg.split(/\n/).join('<BR/>'));
174                                         } catch(e) {
175                                           //  Roo.log(e);
176                                             Roo.MessageBox.alert("Error", "Saving failed = fix errors and try again");        
177                                         }
178                                         return;
179                                     }
180                                     
181                                     // what about load failing..
182                                     Roo.MessageBox.alert("Error", "Error loading details"); 
183                                 }
184                             },
185                             fileUpload : true,
186                             labelWidth : 140,
187                             method : 'POST',
188                             style : 'margin:10px;',
189                             timeout : 300,
190                             url : baseURL + '/Roo/Images.php',
191                             items : [
192                                 {
193                                     xtype: 'Hidden',
194                                     xns: Roo.form,
195                                     name : 'UPLOAD_IDENTIFIER'
196                                 },
197                                 {
198                                     xtype: 'Hidden',
199                                     xns: Roo.form,
200                                     name : 'post_max_size',
201                                     value : "32M"
202                                 },
203                                 {
204                                     xtype: 'Hidden',
205                                     xns: Roo.form,
206                                     name : 'upload_max_filesize',
207                                     value : "32M"
208                                 },
209                                 {
210                                     xtype: 'TextField',
211                                     xns: Roo.form,
212                                     fieldLabel : 'Upload Image or File',
213                                     inputType : 'file',
214                                     name : 'imageUpload',
215                                     width : 200
216                                 },
217                                 {
218                                     xtype: 'Hidden',
219                                     xns: Roo.form,
220                                     name : 'ontable'
221                                 },
222                                 {
223                                     xtype: 'Hidden',
224                                     xns: Roo.form,
225                                     name : 'onid'
226                                 },
227                                 {
228                                     xtype: 'Hidden',
229                                     xns: Roo.form,
230                                     name : 'id'
231                                 },
232                                 {
233                                     xtype: 'Hidden',
234                                     xns: Roo.form,
235                                     name : 'imgtype'
236                                 }
237                             ]
238                         }
239                     ]
240                 }
241             ],
242             center : {
243                 xtype: 'LayoutRegion',
244                 xns: Roo
245             },
246             buttons : [
247                 {
248                     xtype: 'Button',
249                     xns: Roo,
250                     listeners : {
251                         click : function (_self, e)
252                         {
253                             _this.dialog.hide();
254                         }
255                     },
256                     text : "Cancel"
257                 },
258                 {
259                     xtype: 'Button',
260                     xns: Roo,
261                     listeners : {
262                         click : function (_self, e)
263                         {
264                             // do some checks?
265                              
266                             //_this.dialog.el.mask("Sending");
267                             _this.dialog.uploadComplete = false;
268                             _this.form.doAction('submit', {
269                                 params: {
270                                     ts : Math.random()
271                                 } 
272                             });
273                             _this.dialog.haveProgress = 0; // set to show..
274                             _this.dialog.uploadProgress.defer(1000, _this.dialog);
275                         
276                         }
277                     },
278                     text : "Upload"
279                 }
280             ]
281         });
282     }
283 };