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