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