JsCompile.php
[Pman.Core] / Pman.js
1 //<script type="text/javascript">
2
3 /**
4  * 
5  * >>> Pman.layout.getRegion('center').tabs.stripWrap
6  * ==> tab.???
7  * var tbh = Pman.layout.getRegion('center').tabs.stripWrap.child('div').createChild(
8  * 
9  * {tag: 'div', style: 'display:block;position:absolute;top:2;left:300;width:100%;height:25px'});
10  * 
11  * 
12  *  CHANGES
13  *  - gtranslate moved to Pman.GoogleTranslate
14  * 
15  * 
16  */
17  
18 if (typeof(_T) == 'undefined') { _T={};}
19  
20
21
22   
23
24 Pman = new Roo.Document(
25 {
26    /// appVersion: '1.7', // fixme = needs to be removed - use Global AppVersion
27     subMenuItems : [],
28     topMenuItems : [],
29     rightNames: { }, /// register right names here - so they can be translated and rendered.
30     buildCompleted : false, // flag to say if we are building interface..
31     events : {
32         'beforeload' : true, // fired after page ready, before module building.
33         'load' : true, // fired after module building
34         'authrefreshed' : true // fire on auth updated?? - should be on Login?!?!?
35     },
36     
37     listeners : {
38         'ready' : function()
39         {
40             // kludge to fix firebug debugger
41             if (typeof(console) == 'undefined') {
42                 console = { log : function() {  } };
43             }
44             
45             // remove loader..
46             if (Roo.get('loading')) {
47                 Roo.get('loading').remove();
48             }
49             
50             Roo.state.Manager.setProvider(new Roo.state.CookieProvider());
51             
52             // link errors...
53             
54             if (AppLinkError.length) {
55                 Roo.MessageBox.alert("Error", AppLinkError, function() {
56                     Pman.Login.onLoad();
57                 });
58                 return;
59             }
60             
61             
62             // reset password!!!!
63             if (showNewPass.length) {
64                 Pman.PasswordChange.show(  { passwordReset : showNewPass },
65                     function(data) {
66                         // fail and success we do  a load...
67                         Pman.Login.onLoad();
68                     }
69                 );
70                 return;
71             }
72              
73             Pman.Login.onLoad();
74             
75         },
76         'load' : function()
77         {
78             if (Roo.get('loading-logo-tile')) {
79                 Roo.get('loading-logo-tile').remove();
80             }
81             if (Roo.get('loading-logo-tile-top')) {
82                 Roo.get('loading-logo-tile-top').remove();
83             }
84             if (Roo.get('loading-logo-bottom')) {
85                 Roo.get('loading-logo-bottom').remove();
86             }
87             if (Roo.get('loading-logo-center')) {
88                 Roo.get('loading-logo-center').remove();
89             }
90         }   
91         
92     },
93    
94
95     
96     layout: false,
97     
98     onload: function() {
99         //this.fireEvent('beforeload',this);
100         
101         
102         
103         if (this.layout) {
104             return; // already loaded
105         } 
106         if (Roo.get('loading')) {
107             Roo.get('loading').remove();
108         }
109         if (Roo.get('loading-mask')) {
110             Roo.get('loading-mask').show();
111         }
112         
113         
114        
115         
116         /*
117         Roo.MessageBox.show({
118            title: "Please wait...",
119            msg: "Building Interface...",
120            width:340,
121            progress:true,
122            closable:false
123           
124         });
125         */
126         //Pman.onLoadBuild();
127         //Roo.get(document.body).mask("Building Interface");
128         //Pman.onLoadBuild.defer(100, Pman);
129        //Pman.onLoadBuild();
130                     
131    // },
132     //onLoadBuild : function() {
133         
134         var _this = this;
135         this.stime = new Date();
136         this.layout = new Roo.BorderLayout(document.body, {
137             north: {
138                 split:false,
139                 initialSize: 25,
140                 titlebar: false
141             },
142          
143              
144             center: {
145                 titlebar: false,
146                 autoScroll:false,
147                 closeOnTab: true,
148                 tabPosition: 'top',
149                 //resizeTabs: true,
150                 alwaysShowTabs: true,
151                 minTabWidth: 140
152             } ,
153             south: {
154                 collapsible : true,
155                 collapsed : true,
156                 split:false,
157                 height: 120,
158                 titlebar: false 
159             }
160             
161         });
162         
163         this.fireEvent('beforeload',this);
164         
165         
166         
167         this.layout.beginUpdate();
168         this.layout.add('north', new Roo.ContentPanel('title', 'North'));
169         var au = Pman.Login.authUser;
170         if (au.id > 0 && au.company_id_background_color && au.company_id_background_color.length) {
171             Roo.get('title').dom.style.backgroundColor = '#' + au.company_id_background_color;
172             Roo.get('headerInformation').dom.style.color = this.invertColor('#' + au.company_id_background_color);
173         }
174         if (au.id > 0 && au.company_id_logo_id * 1 > 0) {
175             Roo.get('headerInformation-company-logo').dom.src =  baseURL + 
176                 '/Images/' + au.company_id_logo_id + '/' + au.company_id_logo_id_filename;
177         } else {
178             Roo.get('headerInformation-company-logo').dom.src = Roo.BLANK_IMAGE_URL;
179         }
180         
181         Roo.get('headerInformation').dom.innerHTML = String.format(
182                 "You are Logged in as <b>{0} ({1})</b>", // to {4} v{3}", // for <b>{2}</b>",
183                 au.name, au.email, au.company_id_name, 
184                 AppVersion , appNameShort
185         );
186         
187         
188         document.title = appName + ' v' + AppVersion + ' - ' + au.company_id_name;
189         Roo.QuickTips.init(); 
190         if (Roo.isGecko) {
191            Roo.useShims = true;
192         }
193        
194         //this.mainLayout.beginUpdate();
195         //var maskDom = Roo.get(document.body)._maskMsg.dom
196         this.layout.beginUpdate();
197         
198         Pman.building = true;
199         
200         this.buildModules(this, 
201             function() {
202                 
203                 _this.layout.getRegion('center').showPanel(0);
204                 _this.layout.endUpdate(); 
205                 _this.addTopToolbar();  
206                 _this.finalize();
207                 _this.fireEvent('load',this);
208                 
209                 if (!_this.layout.getRegion('south').panels.length) {
210                     _this.layout.getRegion('south').hide();
211                 }
212                 
213                 
214             }
215         );
216         
217         
218      
219     },
220     
221     addTopToolbar : function()
222     {
223           //console.log( "t6:" + ((new Date())-stime));
224         //this.mainLayout.endUpdate();
225         // make a new tab to hold administration stuff...
226         
227        
228         //console.log( "t7:" + ((new Date())-stime));
229         var se = Pman.layout.getRegion('center').tabs.stripEl;
230         var tbh = se.createChild( 
231                 { tag: 'td', style: 'width:100%;'  });
232         
233         var lotb = new Roo.Toolbar(tbh);
234         
235         if (Roo.isSafari) {
236             var tbl = se.child('table', true);
237             tbl.setAttribute('width', '100%');
238         }
239         lotb.add(
240             new Roo.Toolbar.Fill(), 
241      
242             {
243                 text: "Change Password",
244                 cls: 'x-btn-text-icon',
245                 icon: rootURL + '/Pman/templates/images/change-password.gif',
246                 handler : function(){
247                     Pman.PasswordChange.show({});
248                 }
249             }, '-'
250         );
251          
252         
253         if (this.topMenuItems.length) {
254             
255             Roo.each(this.topMenuItems, function (mi) {
256                 lotb.add(mi);
257             });
258             lotb.add('-');
259         }
260         
261         
262         
263         if (this.subMenuItems.length) {
264             
265             this.subMenuItems.sort(function (a,b) {
266                 return a.seqid > b.seqid ? 1 : -1;
267             });
268             // chop off last seperator.
269             // since we always add it.. just chop of last item
270             this.subMenuItems.pop(); 
271             
272             lotb.add(
273                 {
274                      
275                     text: "Add New Item",
276                     cls: 'x-btn-text-icon',
277                     icon: Roo.rootURL + 'images/default/dd/drop-add.gif',
278                     menu : {
279                         items : this.subMenuItems
280                     }     
281                 } ,'-'
282             );
283         }
284        
285         lotb.add(
286             {
287                 text: "Logout",
288                 cls: 'x-btn-text-icon',
289                 icon: rootURL + '/Pman/templates/images/logout.gif',
290                 handler: function() {
291                     Pman.Login.logout();
292                 }
293                  
294             }
295         );
296       
297        // this.layout.endUpdate();
298     },
299     
300     
301     finalize : function() {
302         
303       
304        
305         window.onbeforeunload = function(e) { 
306             e = e || window.event;
307             var r = "Closing this window will loose changes, are you sure you want to do that?";
308
309             // For IE and Firefox
310             if (e) {
311                 e.returnValue = r;
312             }
313
314             // For Safari
315             return r;
316             
317         };
318         
319         Roo.MessageBox.hide();
320         if (Roo.get('loading-mask')) {
321            Roo.get('loading-mask').remove();
322         }
323         
324         
325         this.buildCompleted = true; // now we can force refreshes on everything..
326         
327         
328         // does the URL indicate we want to see a system..
329         if (AppTrackOnLoad * 1 > 0) {
330             this.onLoadTrack(AppTrackOnLoad,false);
331         }
332         
333         // Open system..
334         
335         var forceAdmin = function(data)
336         {
337             if (!data || !data.id) {
338                 //Roo.log("Force Admin");
339                 Pman.Dialog.PersonStaff.show( 
340                     { 
341                         id : 0, 
342                         company_id : Pman.Login.authUser.company_id_id * 1, 
343                         company_id_name : Pman.Login.authUser.company_id_name
344                     }, function(data) {
345                         forceAdmin(data);
346                     }
347                 );
348                 return;
349             }
350             Roo.state.Manager.set('Pman.Login.username', data.email),
351             window.onbeforeunload = false;
352             document.location = baseURL + '?ts=' + Math.random();
353         }
354         
355         var forceCompany = function(data) {
356             if (Pman.Login.authUser.company_id * 1 > 0) {
357                 forceAdmin();
358                 return;
359             }
360             if (!data || !data.id) {
361                 Pman.Dialog.Companies.show( { id : 0, isOwner : 1, comptype: 'OWNER' }, function(data) {
362                     forceCompany(data);
363                 });
364                 return;
365             }
366             Pman.Login.authUser.company_id_id  = data.id;
367             Pman.Login.authUser.company_id  = data.id;
368             Pman.Login.authUser.company_id_name  = data.name;
369             forceAdmin();
370         }
371         
372         if (Pman.Login.authUser.id < 0) {
373             // admin company has been created - create the user..
374             if (Pman.Login.authUser.company_id_id* 1 > 0) {
375                 forceAdmin();
376                 return;
377             }
378             
379             forceCompany();
380             /// create account..
381             
382             
383         }
384         
385
386     },
387     
388     
389     // REMOVE THESE 
390     
391      
392     onLoadTrack : function(id,cb) {
393         this.onLoadTrackCall(id, cb, 'DocumentsCirc_');
394     },
395     onLoadTrackEdit : function(id,cb) {
396         this.onLoadTrackCall(id, cb, 'Documents_');
397     },
398     
399     
400     /// ----------- FIXME -----
401     
402     
403     onLoadTrackCall : function(id,cb, cls) {
404         Roo.get(document.body).mask("Loading Document details");
405
406         Pman.request({
407             url: baseURL + '/Roo/Documents.html',  
408             params: {
409                 _id: id
410             },  
411             method: 'GET',  
412             success : function(data) {
413                 Roo.get(document.body).unmask();
414              
415                 
416                 switch(data.in_out) {
417                     case 'IN' : cls+='In';break;
418                     case 'OUT' : cls+='Out';break;
419                     case 'WIP' : cls+='Wip';break;
420                     default: 
421                         Roo.MessageBox.alert("Error", "invalid in_out");
422                         return;
423                 }
424                 Pman.Dialog[cls].show(data, cb ? cb : Pman.refreshActivePanel);
425             }, 
426             
427             failure: function() {
428                 Roo.get(document.body).unmask();
429                 //if (cb) {
430                 //    cb.call(false);
431                 //}
432                  
433            }
434         });
435           
436     },
437     
438     refreshActivePanel : function() {
439         var actpan = this.layout.getRegion('center').getActivePanel();
440         if (actpan.controller && actpan.controller.paging) {
441             actpan.controller.paging.onClick('refresh');
442             return;
443         }
444         
445         var agid = Pman.layout.getRegion('center').getActivePanel().id;
446         if (!agid) {
447             return;
448         }
449         Pman.Tab[agid].paging.onClick('refresh');
450     },
451     toCidV : function(data) {
452         return 'C' + data.in_out.substring(0,1) + data.cid;
453     },
454     
455     
456     /**
457      * eg. has Pman.hasPerm('Admin.Admin_Tab', 'S') == showlist..
458      * 
459      */
460     hasPerm: function(name, lvl) {
461         if (
462             (typeof(Pman.Login.authUser) != 'object')
463             ||
464             (typeof(Pman.Login.authUser.perms) != 'object')
465             ||
466             (typeof(Pman.Login.authUser.perms[name]) != 'string')
467             ) {
468                 return false;
469         }
470         
471         return Pman.Login.authUser.perms[name].indexOf(lvl) > -1;
472         
473     },
474     
475     
476     
477     
478     
479     
480     
481     
482     Readers : {},
483     ColModels : {},
484     Forms : {},
485     Tab : {},
486     Dialog : {},
487     
488     processResponse : function (response)
489     {
490         var res = '';
491         try {
492             res = Roo.decode(response.responseText);
493             // oops...
494             if (typeof(res) != 'object') {
495                 res = { success : false, errorMsg : res, errors : true };
496             }
497             if (typeof(res.success) == 'undefined') {
498                 res.success = false;
499             }
500             
501         } catch(e) {
502             res = { success : false,  errorMsg : response.responseText, errors : true };
503         }
504         return res;
505     },
506     genericDelete : function(tab,tbl) {
507         
508         var r = [];
509         
510             
511         var s = tab.grid.getSelectionModel().getSelections();
512         if (!s.length)  {
513             Roo.MessageBox.alert("Error", "Select at least one Row to delete" );
514             return '';
515         }
516         
517         for(var i = 0; i < s.length; i++) {
518             r.push(s[i].data.id);
519         }
520     
521         Roo.MessageBox.confirm("Confirm", "Are you sure you want to delete that?",
522             function(btn) {
523                 if (btn != 'yes') {
524                     return;
525                 }
526                 // what about the toolbar??
527                 tab.grid.getView().mainWrap.mask("Deleting");
528                 Pman.request({
529                     url: baseURL + '/Roo/'+tbl+'.php',
530                     method: 'GET',
531                     params: {
532                         _delete : r.join(',')
533                     },
534                     success: function(response) {
535                         tab.grid.getView().mainWrap.unmask();
536                         if ( tab.paging ) {
537                             tab.paging.onClick('refresh');   
538                         } else if (tab.refresh) {
539                             tab.refresh();
540                         } else if (tab.grid.footer && tab.grid.footer.onClick) {
541                             // new xtype built grids
542                             tab.grid.footer.onClick('refresh');   
543                         } else {
544                             tab.grid.getDataSource().load();
545                         }
546                         
547                         
548                         
549                     },
550                     failure: function(act) {
551                         tab.grid.getView().mainWrap.unmask();
552                         Roo.MessageBox.alert("Error", "Error Deleting");
553                     }
554                     
555                 });
556             }
557             
558         );
559         return '';
560     },
561     
562     
563     standardActionFailed :  function(f, act, cb) {
564     
565         if (act.failureType == 'client') {
566             Roo.MessageBox.alert("Error", "Please Correct all the errors in red", cb);
567             return;
568         }
569         if (act.failureType == 'connect') {
570             Roo.MessageBox.alert("Error", "Problem Connecting to Server - please try again.", cb);
571             return;
572         }
573         
574         if (act.type == 'submit') {
575             
576             Roo.MessageBox.alert("Error", typeof(act.result.errorMsg) == 'string' ?
577                 String.format('{0}', act.result.errorMsg) : 
578                 "Saving failed = fix errors and try again", cb);
579             return;
580         }
581         
582         // what about load failing..
583         Roo.MessageBox.alert("Error", "Error loading details",cb); 
584     },
585     /**
586      * Depreciated - USE new Pman.Request
587     * 
588      * 
589      */
590     request : function(c) {
591         var r= new Roo.data.Connection({
592             timeout : typeof(c.timeout) == 'undefined' ?  30000 : c.timeout
593         });
594         r.request({
595             url: c.url,
596             method : c.method,
597             params: c.params,
598             xmlData : c.xmlData,
599             success:  function(response, opts)  {  // check successfull...
600                
601                 var res = Pman.processResponse(response);
602                 
603                 if (!res.success) { // error!
604                     if (c.failure) {
605                         if (true === c.failure.call(this,response, opts)) {
606                             return;
607                         }
608                     }
609                     Roo.MessageBox.hide();
610                     Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : "Error Sending");
611                     return;
612                 }
613                 
614                 c.success.call(this, res.data);
615                 
616                 return; 
617             },
618             failure :  function(response, opts)  {  // check successfull...
619                 
620                 if (c.failure) {
621                     if (true === c.failure.call(this,response, opts)) {
622                         return;
623                     }
624                 }
625                 Roo.MessageBox.hide();
626                 Roo.MessageBox.alert("Error", "Connection timed out sending");
627                 Roo.log(response);
628                 
629             },
630             scope: this
631             
632         });
633     },
634     
635     
636     // depreciated - use Pman.Download()
637     
638     download : function(c) {
639         
640         return new Pman.Download(c);
641     },
642     
643     // fixme - move to document manager...
644     downloadRevision : function(doc, rev)
645     {
646         this.download({
647             url: baseURL + '/Documents/Doc/DownloadRev/'+ doc.id + '/' + rev + '/' +
648                 doc.project_id_code + '-' + doc.cidV + '-' + rev  + '-' +  doc.filename
649         }); 
650                     
651     },
652     
653     
654     exportCSV : function(c) {
655         
656         for(var i=0;i < c.csvFormat.length;i++) {
657             c.params['csvCols['+i+']'] = c.csvFormat[i][0];
658             c.params['csvTitles['+i+']'] = c.csvFormat[i][1];
659         }
660         c.url +=  '?' + Roo.urlEncode(c.params);
661         this.download(c);
662
663     },
664     
665     
666     prettyDate : function (value) 
667     {
668         if (typeof(value) == 'string') {
669             var ds = Date.parseDate(value, 'Y-m-d H:i:s');
670             if (ds) {
671                 return this.prettyDate(ds);
672             }
673             ds = Date.parseDate(value, 'Y-m-d');
674             if (ds) {
675                 return this.prettyDate(ds);
676             }
677             return '';
678         }
679 // last 7 days...
680         if (!value) {
681             return '';
682         }
683         var td = new Date();
684         var daysSince = Math.floor(td.getElapsed(value) / (1000 * 60*60*24));
685         if (daysSince < 7) {
686             return value.dateFormat('D H:i');
687         }
688         
689         // same month
690         if (td.dateFormat('m') == value.dateFormat('m')) {
691             return value.dateFormat('dS D');
692         }
693         // same year?
694         if (td.dateFormat('Y') == value.dateFormat('Y')) {
695             return value.dateFormat('dS M');
696         }
697         return value.dateFormat('d M Y');
698     },
699     loadException : function(a,b,c,d)
700     {
701         if (d && d.authFailure) {
702             Pman.Login.show();
703             return;
704         }
705         Roo.MessageBox.alert("Problem Loading Data", a.message || c.statusText);
706     },
707     
708     
709     /**
710      * 
711      * Routine to flash alerts in the title bar..
712      * 
713      * 
714      */
715     
716     notifyActive : false,
717     
718     notifyTitle: function(msg)
719     {
720         if (this.notifyActive ) {
721             return;
722         }
723         var stop = false;
724         
725         var stopper = function() {
726             stop = true;
727              document.title = oldtitle;
728         };
729         
730         Roo.get(document.body).on('mousemove', stopper, this);
731         var oldtitle = document.title;
732         var s = 1;
733         var _this = this;
734         var ivl = window.setInterval(function() {
735             
736             if (stop) {
737                 Roo.get(document.body).un('mousemove', stopper, this);
738                 _this.notifyActive = false;
739                 document.title = oldtitle;
740                 window.clearInterval(ivl);
741                 return true;
742             }
743             s = !s;
744             document.title = s ? msg : oldtitle;
745             return false;     
746         }, 1000); // every 120 secs = 2mins..
747          document.title =   msg;
748         
749         
750         
751     },
752     
753     modules : false,
754     /**
755      * example:
756      * 
757      * Pman.register({
758           modKey : '00-admin-xxxx',
759           module : Pman.Tab.projectMgr,
760           moduleName : 'Pman.Tab.projectMgr',
761           region : 'center',
762           parent : Pman.layout
763         })
764      * 
765      */
766     register : function(obj)
767     {
768         
769         // ignore registration of objects which are disabled.
770         appDisabled = typeof(appDisabled) == 'undefined' ? [] : appDisabled;
771         
772         if ((typeof(obj.moduleName) != 'undefined')
773                 && appDisabled.indexOf(obj.moduleName) > -1)
774         {
775             return;
776         }
777         
778         if (!obj.parent) {
779             if (obj.parent === false) {
780                 //console.log('skip module (no parent)' + obj.modkey);
781                 return;
782             }
783             // this is an error condition - the parent does not exist..
784             // technically it should not happen..
785             console.log(obj);
786         }
787         if (!obj.parent.modules) {
788             obj.parent.modules = new Roo.util.MixedCollection(false, function(o) { return o.modKey });
789         }
790         
791         obj.parent.modules.add(obj);
792         
793     },
794     
795     buildModules : function(parent, onComplete) 
796     {
797         
798         var _this = this;
799         var cmp = function(a,b) {   
800             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
801             
802         };
803         if (!parent.modules) {
804             return;
805         }
806         parent.modules.keySort('ASC',  cmp );
807         var mods = [];
808         
809         
810         // add modules to their parents..
811         var addMod = function(m) {
812            // console.log(m.modKey);
813             
814             mods.push(m);
815             if (m.module.modules) {
816                 m.module.modules.keySort('ASC',  cmp );
817                 m.module.modules.each(addMod);
818             }
819             if (m.finalize) {
820                 m.finalize.name = m.name + " (clean up) ";
821                 mods.push(m.finalize);
822             }
823             
824         }
825  
826         parent.modules.each(addMod);
827         //this.allmods = mods;
828         //console.log(mods);
829         //return;
830         if (!mods.length) {
831             if (onComplete) onComplete();
832             return;
833         }
834         // flash it up as modal - so we store the mask!?
835         Roo.MessageBox.show({ title: 'loading' });
836         Roo.MessageBox.show({
837            title: "Please wait...",
838            msg: "Building Interface...",
839            width:450,
840            progress:true,
841            closable:false,
842            modal: false
843           
844         });
845         var n = 0;
846         var progressRun = function() {
847             
848             var mod = mods[n];
849             
850             
851             Roo.MessageBox.updateProgress(
852                 (n+1)/mods.length,  "Building Interface " + (n+1) + 
853                     " of " + mods.length + 
854                     (mod.name ? (' - ' + mod.name) : '')
855                     );
856             
857             
858             
859             if (typeof(mod) == 'function') {
860                 mod();
861                 
862             } else  if (mod.parent.layout && !mod.module.disabled) {
863                 // honour permname setings..
864                 if (mod.permname && mod.permname.length) {
865                     if (Pman.hasPerm(mod.permname, 'S')) {
866                         mod.module.add(mod.parent.layout, mod.region);    
867                     }
868                 } else {
869                     mod.module.add(mod.parent.layout, mod.region);    
870                 }
871                     
872                  
873             }
874             
875             
876             n++;
877             if (n >= mods.length) {
878                 onComplete();  
879                 return;
880             }
881                 
882             
883             progressRun.defer(10, Pman);    
884         }
885         progressRun.defer(1, Pman);
886      
887         
888         
889     },
890     
891     invertColor : function(c)
892     {
893         // read..
894         var ca = [];
895         for(var i = 0; i < 3; i++){
896             ca[i] = parseInt(c.charAt((i*2)+1) + c.charAt((i*2)+2), 16);
897         }
898             
899         // invert..
900         var col = '';
901         Roo.each(ca, function(hi) {
902             var h = parseInt(255-hi).toString(16);
903             if(h < 16){
904                 h = '0' + h;
905             }
906             col += h;
907         });
908         return '#' + col;
909         
910     }
911     
912     
913     
914     
915     
916     
917     
918 });
919