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