Pman.Dialog.Companies.js
[Pman.Core] / Pman.Dialog.Companies.js
1 //<script type="text/javascript">
2
3
4 Pman.Dialog.Companies =   new Roo.util.Observable({
5     events : {
6         'beforerender' : true, // trigger so we can add modules later..
7         'show' : true, // trigger on showing form.. - to load additiona data..
8         'beforesave' : true
9     },
10      
11     dialog : false,
12     form : false,
13     callback: false,
14     create: function()
15     {
16         if (this.dialog) {
17             return;
18         }
19         
20         this.dialog = new Ext.LayoutDialog(Ext.get(document.body).createChild({tag:'div'}),  { 
21             autoCreated: true,
22             title: "Edit Companies",
23             modal: true,
24             width:  750,
25             height: 400,
26             shadow:true,
27             minWidth:200,
28             minHeight:180,
29             //proxyDrag: true,
30             collapsible : false,
31             closable: false,
32             draggable: false,
33             center: {
34                 autoScroll:false,
35                 titlebar: false,
36                // tabPosition: 'top',
37                 hideTabs: true,
38                 closeOnTab: true,
39                 alwaysShowTabs: false
40             }
41         });
42         this.dialog.addKeyListener(27, this.dialog.hide, this.dialog);
43         this.dialog.addButton("Cancel", this.dialog.hide, this.dialog);
44        
45         this.dialog.addButton("Save", this.save, this);
46         this.layout = this.dialog.getLayout();
47         this.layout.beginUpdate();
48         
49         var _this = this;
50         
51         this.form = new Ext.form.Form({
52             labelWidth: 150 ,
53               
54             fileUpload : true,
55             listeners : {
56                 actionfailed : function(f, act) {
57                     _this.dialog.el.unmask();
58                     // error msg???
59                     Pman.standardActionFailed(f,act);
60                               
61                 },
62                 actioncomplete: function(f, act) {
63                     _this.dialog.el.unmask();
64                     //console.log('load completed'); 
65                     // error messages?????
66                     
67                    
68                     if (act.type == 'load') {
69                         
70                         _this.data = act.result.data;
71                         var meth = _this.data.isOwner || !Pman.Login.isOwner() ? 'disable' : 'enable';
72                      
73                             
74                         if (_this.form.findField('comptype')) {
75                             _this.form.findField('comptype')[meth]();
76                         }
77                          
78                        // _this.loaded();
79                         return;
80                     }
81                     
82                     
83                     if (act.type == 'submit') { // only submitted here if we are 
84                         _this.dialog.hide();
85                        
86                         if (_this.callback) {
87                             _this.callback.call(this, act.result.data);
88                         }
89                         return; 
90                     }
91                     // unmask?? 
92                 }
93             }
94         
95             
96             
97              
98         });
99         //?? will this work...
100         
101         this.form.addxtype.apply(this.form, this.getFormFields());
102          this.fireEvent('beforeRender', this );
103         
104         var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
105         ef.dom.style.margin = 10;
106          
107         this.form.render(ef.dom);
108
109         var vp = this.dialog.getLayout().add('center', new Ext.ContentPanel(ef, {
110             autoCreate : true,
111             //title: 'Org Details',
112             //toolbar: this.tb,
113             width: 250,
114             maxWidth: 250,
115             fitToFrame:true
116         }));
117           
118         
119         
120         
121         this.layout.endUpdate();
122     },
123     show : function (data, callback)
124     {
125         this.callback = callback;
126         this._id = data.id ? data.id : 0;  // modify if you do not use ID !!!!
127         this.create();
128         this.data = data;
129         this.form.reset();
130         if (data._fetch) {
131             this.dialog.show();
132             this.dialog.el.mask("Loading");
133             this.form.doAction('load', {
134                 url: baseURL + '/Roo/Companies.html',
135                 method: 'GET',
136                 params: {
137                     _id: this._id ,
138                     _ts : Math.random()
139                 } 
140             });
141             this.fireEvent('show');
142             return;
143         } else {
144             this.form.setValues(data);
145         }
146         
147         
148         
149         this.dialog.show();
150         
151         if (data.isOwner || !Pman.Login.isOwner()) {
152             this.dialog.setTitle("Your Company Details");
153             if (this.form.findField('comptype')) {
154                 this.form.findField('comptype').disable();
155             }
156             
157             
158             
159             
160         } else {
161             this.dialog.setTitle(data.id ? "Edit Company" : "Add Company");
162             if (this.form.findField('comptype')) {
163                 this.form.findField('comptype').enable();
164             }
165         }
166         this.fireEvent('show');
167
168     },
169     
170     
171     save : function()
172     {
173         this.form.fileUpload = this.form.findField('imageUpload') ? true : false;
174         this.fireEvent('beforesave'); 
175         this.form.doAction('submit', {
176             url: baseURL + '/Roo/Companies.html',
177             method: 'POST',
178             params: {
179                 _id: this._id ,
180                 ts : Math.random()
181             } 
182         });
183     },
184     
185     comptypeList : function()
186     {
187         // should probably be system configurable..
188         return [
189             
190             [ 'CONSULTANT', "Consultant" ],
191             [ 'CLIENT'    ,  "Client" ],
192             [ 'CONTRACTOR' , "Contractor" ]
193           //  [ 'OWNER', "System Owner" ]
194          ];
195     },
196     comptypeListToString: function(v) {
197         if (!v.length) {
198             return '';
199         }
200         if (v== "OWNER") {
201             return "System Owner";
202         }
203         var a = this.comptypeList();
204         var ret = '';
205         Roo.each(a, function( ar) {
206             if (ar[0] == v) {
207                 ret = ar[1];
208                 return false;
209             }
210         });
211         return ret;
212         
213         
214         
215     },
216     
217     getFormFields : function() {
218         return [
219             {   
220                 xtype : 'Column',
221                 width: 500,
222                 items: [
223                     this.c_code(),
224                     this.c_comptype_name(),
225                     this.c_name(),
226                     this.c_tel(),
227                     this.c_fax(),
228                     this.c_email(),
229                     
230                     
231                     this.c_address(),
232                     this.c_remarks()
233                 ]
234             },
235             {   
236                 xtype : 'Column',
237                 width: 200,
238                 labelAlign: 'top',
239                 items : [
240                     this.c_background_color(),
241                     this.c_image_edit()
242                     //this.c_image_view(),
243                     //this.c_image_change(),
244                 ]
245             },
246             this.c_isOwner(),
247             this.c_id()
248         ];
249     },
250     
251     
252     c_code : function() {
253         return {
254                 name : 'code',
255                 fieldLabel : "Company ID (for filing Ref.)",
256                 value : '',
257                 allowBlank : false,
258                 qtip : "Enter code",
259                 xtype : 'TextField',
260                 width : 100
261             }
262     },
263     c_comptype_name : function() {
264         return {
265                         
266                                 fieldLabel : 'Type',
267                                 disabled : Pman.Login.isOwner() ? false : true,
268                 name : 'comptype_name',
269                 xtype : 'ComboBox',
270                 allowBlank : false,
271                                 qtip : 'Select Company type',
272                 
273                 width: 200,
274                 xns : Roo.form,
275                 
276                 listWidth : 250,
277                 
278                
279                 store: {
280                     xtype : 'SimpleStore',
281                     fields: ['val', 'desc'],
282                     data : this.comptypeList()
283                 },
284                 displayField:'desc',
285                 valueField: 'val',
286                 hiddenName : 'comptype',
287                 
288                 typeAhead: false,
289                 editable: false,
290                 //mode: 'local',
291                 triggerAction: 'all',
292                 emptyText: "Select Type",
293                 selectOnFocus: true
294                 
295                 
296            }
297     },
298     c_name : function() {
299         return {
300     
301                 name : 'name',
302                 fieldLabel : "Company Name",
303                 value : '',
304                 allowBlank : true,
305                 qtip : "Enter Company Name",
306                 xtype : 'TextField',
307                 width : 300
308                     }
309     },
310     c_tel : function() {
311         return {
312     
313                 name : 'tel',
314                 fieldLabel : "Phone",
315                 value : '',
316                 allowBlank : true,
317                 qtip : "Enter Phone Number",
318                 xtype : 'TextField',
319                 width : 300
320                     }
321     },
322     c_fax : function() {
323         return {
324     
325                 name : 'fax',
326                 fieldLabel : "fax",
327                 value : '',
328                 allowBlank : true,
329                 qtip : "Enter fax Number",
330                 xtype : 'TextField',
331                 width : 300
332                     }
333     },
334     c_email : function() {
335         return {
336     
337                 name : 'email',
338                 fieldLabel : "Email",
339                 value : '',
340                 allowBlank : true,
341                 qtip : "Enter Email Address",
342                 xtype : 'TextField',
343                 width : 300
344                     }
345     },
346     c_address : function() {
347         return {
348     
349                 name : 'address',
350                 fieldLabel : "Address",
351                 value : '',
352                 allowBlank : true,
353                 qtip : "Enter Address",
354                 xtype : 'TextArea',
355                 height : 70,
356                 width : 300
357         }
358     },
359     c_remarks : function() {
360         return {
361     
362                 name : 'remarks',
363                 fieldLabel : "Remarks",
364                 value : '',
365                 allowBlank : true,
366                 qtip : "Enter remarks",
367                 xtype : 'TextArea',
368                 height : 40,
369                 width : 300
370         }
371     },
372     c_background_color : function() {
373         return {
374                     xtype: 'ColorField',
375                 name : 'background_color',
376                 fieldLabel: "Background Colour"
377         }
378     },
379     c_image_view : function() {
380         var _this = this;
381         return {
382                 xtype :  'FieldSetEx',
383                 name : 'image-view',
384                 collapseGroup : 'companies-image',
385                 value: 0,
386                 labelWidth: 100,
387                 expanded: true,
388                 style: 'width:420px;',
389                 legend : "Logo Image",
390                 items: [
391                     {
392                         xtype :  'DisplayImage', // image preview...
393                         name : 'logo_id',
394                         fieldLabel : 'Logo Image',
395                         width: 300,
396                         height: 50,
397                         renderer : function(v) {
398                             return v ?  String.format('<img src="{0}" height="{1}">', 
399                                 baseURL + '/Images/' + v + '/' + _this.data.logo_id_filename, 
400                                 Math.min(this.height, _this.data.logo_id_height)) : "No Image Attached";
401                             
402                         }
403                     }
404                 ]
405                 
406         }
407     },
408     c_image_edit : function() {
409         var _this = this;
410         return {
411                     name : 'logo_id',
412                     fieldLabel : "Logo Image",
413                     value : '',
414                     allowBlank : true,
415                     style: 'border: 1px solid #ccc;',
416                     xtype : 'DisplayImage',
417                     width : 170,
418                     height: 170,
419                     addTitle : "Change / Add Image",
420                     icon: Roo.rootURL + 'images/default/dd/drop-add.gif',
421                     handler : function() {
422                         var _t = this;
423                          
424                         Pman.Dialog.Image.show({
425                             onid :_this.data.id,
426                             ontable : 'Companies',
427                             imgtype : 'LOGO'
428                         }, function(data) {
429                             if  (data) {
430                                 _t.setValue(data.id);
431                             }
432                             
433                         });
434                     }, 
435                     renderer : function(v) {
436                         //var vp = v ? v : 'Companies:' + _this.data.id + ':-LOGO';
437                         if (!v) {
438                             return "No Image Available" + '<BR/>';
439                         }
440                         return String.format('<img src="{0}" width="150">', 
441                                 baseURL + '/Images/Thumb/150x150/' + v + '/logo.jpg'
442                         );
443                     }
444             
445             }  ;
446         
447     },
448     c_image_change: function() {
449         return { 
450                 xtype :  'FieldSetEx',
451                 collapseGroup : 'companies-image',
452                 name : 'image-change',
453                 value: 0,
454                 labelWidth: 100,
455                 expanded: false,
456                 style: 'width:420px;',
457                 legend : "Add / Change Image",
458                 items : [ 
459                     {   
460                         xtype :  'TextField',
461                         name : 'imageUpload',
462                         fieldLabel : "Upload Image",
463                         inputType : 'file'
464                     }
465                 ]
466         }
467     },
468     c_isOwner : function() {
469         return {                 
470                 name : 'isOwner',
471                 value : '',
472                 xtype : 'Hidden'
473             }
474     },
475     c_id : function() {
476         return { 
477                 name : 'id',
478                 value : '',
479                 xtype : 'Hidden'
480             }
481     }
482          
483 });