Merge branch 'master' of http://git.roojs.com:8081/Pman.Core
[Pman.Core] / Pman.js
diff --git a/Pman.js b/Pman.js
index 082b826..41bad06 100644 (file)
--- a/Pman.js
+++ b/Pman.js
@@ -8,11 +8,37 @@
  * 
  * {tag: 'div', style: 'display:block;position:absolute;top:2;left:300;width:100%;height:25px'});
  * 
+ * 
+ *  CHANGES
+ *  - gtranslate moved to Pman.GoogleTranslate
+ * 
+ * 
  */
  
 if (typeof(_T) == 'undefined') { _T={};}
  
 
+
+Roo.XComponent.on('register', function(e) { if (typeof(Pman) != 'undefined') { return Pman.xregister(e); } return true;  });
+Roo.XComponent.on('beforebuild', function(e) { if (typeof(Pman) != 'undefined') { return Pman.xbeforebuild(e); } return true; });
+
+Roo.XComponent.on('buildComplete',  
+     function() {
+                    
+        Pman.layout.getRegion('center').showPanel(0);
+        Pman.layout.endUpdate(); 
+        Pman.addTopToolbar();  
+        Pman.finalize();
+        Pman.fireEvent('load',this);
+        
+        if (!Pman.layout.getRegion('south').panels.length) {
+            Pman.layout.getRegion('south').hide();
+        }
+    
+    
+} );
+
+//Roo.debug = 1;
   
 
 Pman = new Roo.Document(
@@ -21,6 +47,13 @@ Pman = new Roo.Document(
     subMenuItems : [],
     topMenuItems : [],
     rightNames: { }, /// register right names here - so they can be translated and rendered.
+    /**
+     * @property {Roo.menu.Menu} pulldownMenu - the 'add menu pulldown, you can use it to add items..
+     *
+     */
+    pulldownMenu : false, 
+    
+    
     buildCompleted : false, // flag to say if we are building interface..
     events : {
         'beforeload' : true, // fired after page ready, before module building.
@@ -85,7 +118,17 @@ Pman = new Roo.Document(
         
     },
    
-
+    fakeRoot :  new Roo.XComponent( {
+        modKey : '000',
+        module : 'Pman',
+        region : 'center',
+        parent : false,
+        isTop : true,
+        name : "Pman Base",
+        disabled : false, 
+        permname: '' ,
+        render : function (el) { this.el = this.layout; }
+    }),
     
     layout: false,
     
@@ -104,27 +147,7 @@ Pman = new Roo.Document(
             Roo.get('loading-mask').show();
         }
         
-        
-       
-        
-        /*
-        Roo.MessageBox.show({
-           title: "Please wait...",
-           msg: "Building Interface...",
-           width:340,
-           progress:true,
-           closable:false
-          
-        });
-        */
-        //Pman.onLoadBuild();
-        //Roo.get(document.body).mask("Building Interface");
-        //Pman.onLoadBuild.defer(100, Pman);
-       //Pman.onLoadBuild();
-                    
-   // },
-    //onLoadBuild : function() {
-        
+     
         var _this = this;
         this.stime = new Date();
         this.layout = new Roo.BorderLayout(document.body, {
@@ -143,14 +166,26 @@ Pman = new Roo.Document(
                 //resizeTabs: true,
                 alwaysShowTabs: true,
                 minTabWidth: 140
-            } /*,
+            } ,
             south: {
+                collapsible : true,
+                collapsed : true,
                 split:false,
-                initialSize: 25,
-                titlebar: false
+                height: 120,
+                titlebar: false 
             }
-            */
+            
         });
+        this.fakeRoot.layout = this.layout;
+        /*
+        Pman.register( Roo.apply(this.fakeRoot, {
+            layout : this.layout      
+                
+                                 
+        } ) );
+        */
+        
+        // creates all the modules ready to load..
         
         this.fireEvent('beforeload',this);
         
@@ -159,7 +194,7 @@ Pman = new Roo.Document(
         this.layout.beginUpdate();
         this.layout.add('north', new Roo.ContentPanel('title', 'North'));
         var au = Pman.Login.authUser;
-        if (au.id > 0 && au.company_id_background_color.length) {
+        if (au.id > 0 && au.company_id_background_color && au.company_id_background_color.length) {
             Roo.get('title').dom.style.backgroundColor = '#' + au.company_id_background_color;
             Roo.get('headerInformation').dom.style.color = this.invertColor('#' + au.company_id_background_color);
         }
@@ -188,17 +223,8 @@ Pman = new Roo.Document(
         this.layout.beginUpdate();
         
         Pman.building = true;
-        
-        this.buildModules(this, 
-            function() {
-                
-                _this.layout.getRegion('center').showPanel(0);
-                _this.layout.endUpdate(); 
-                _this.addTopToolbar();  
-                _this.finalize();
-                _this.fireEvent('load',this);
-            }
-        );
+        Roo.XComponent.build();
+         
         
         
      
@@ -255,17 +281,19 @@ Pman = new Roo.Document(
             // since we always add it.. just chop of last item
             this.subMenuItems.pop(); 
             
-            lotb.add(
+            var btn = new Roo.Toolbar.Button( 
                 {
-                     
                     text: "Add New Item",
                     cls: 'x-btn-text-icon',
                     icon: Roo.rootURL + 'images/default/dd/drop-add.gif',
                     menu : {
                         items : this.subMenuItems
                     }     
-                } ,'-'
+                }
             );
+            this.pulldownMenu = btn.menu;
+            lotb.add(btn, '-');
+            
         }
        
         lotb.add(
@@ -289,7 +317,7 @@ Pman = new Roo.Document(
       
        
         window.onbeforeunload = function(e) { 
-            var e = e || window.event;
+            e = e || window.event;
             var r = "Closing this window will loose changes, are you sure you want to do that?";
 
             // For IE and Firefox
@@ -321,10 +349,11 @@ Pman = new Roo.Document(
         var forceAdmin = function(data)
         {
             if (!data || !data.id) {
+                //Roo.log("Force Admin");
                 Pman.Dialog.PersonStaff.show( 
                     { 
                         id : 0, 
-                        company_id : Pman.Login.authUser.company_id * 1, 
+                        company_id : Pman.Login.authUser.company_id_id * 1, 
                         company_id_name : Pman.Login.authUser.company_id_name
                     }, function(data) {
                         forceAdmin(data);
@@ -348,12 +377,19 @@ Pman = new Roo.Document(
                 });
                 return;
             }
+            Pman.Login.authUser.company_id_id  = data.id;
             Pman.Login.authUser.company_id  = data.id;
             Pman.Login.authUser.company_id_name  = data.name;
             forceAdmin();
         }
         
         if (Pman.Login.authUser.id < 0) {
+            // admin company has been created - create the user..
+            if (Pman.Login.authUser.company_id_id* 1 > 0) {
+                forceAdmin();
+                return;
+            }
+            
             forceCompany();
             /// create account..
             
@@ -364,7 +400,7 @@ Pman = new Roo.Document(
     },
     
     
-    
+    // REMOVE THESE 
     
      
     onLoadTrack : function(id,cb) {
@@ -381,13 +417,14 @@ Pman = new Roo.Document(
     onLoadTrackCall : function(id,cb, cls) {
         Roo.get(document.body).mask("Loading Document details");
 
-        Pman.request({
+        new Pman.Request({
             url: baseURL + '/Roo/Documents.html',  
             params: {
                 _id: id
             },  
             method: 'GET',  
-            success : function(data) {
+            success : function(res) {
+                var data = res.data;
                 Roo.get(document.body).unmask();
              
                 
@@ -413,22 +450,65 @@ Pman = new Roo.Document(
           
     },
     
+    refreshActivePanel : function() {
+        var actpan = this.layout.getRegion('center').getActivePanel();
+        if (actpan.controller && actpan.controller.paging) {
+            actpan.controller.paging.onClick('refresh');
+            return;
+        }
+        
+        var agid = Pman.layout.getRegion('center').getActivePanel().id;
+        if (!agid) {
+            return;
+        }
+        Pman.Tab[agid].paging.onClick('refresh');
+    },
+    toCidV : function(data) {
+        return 'C' + data.in_out.substring(0,1) + data.cid;
+    },
+    
+    
     /**
-     * eg. has Pman.hasPerm('Admin.Admin_Tab', 'S') == showlist..
+     * hasPerm:
+     * Does the authenticated user have permission to see this.
      * 
+     * @param {String} name the [Module].[permission] to check for
+     * @param {Char} lvl  - which type of permission to use (eg. S=show...)
+     * @returns {Boolean} tue indicates permission allowed
      */
     hasPerm: function(name, lvl) {
-        if (typeof(Pman.Login.authUser) != 'object') {
-            return false;
-        }
-        if (typeof(Pman.Login.authUser.perms[name]) != 'string') {
-            return false;
+        if (
+            (typeof(Pman.Login.authUser) != 'object')
+            ||
+            (typeof(Pman.Login.authUser.perms) != 'object')
+            ||
+            (typeof(Pman.Login.authUser.perms[name]) != 'string')
+            ) {
+                return false;
         }
+        
         return Pman.Login.authUser.perms[name].indexOf(lvl) > -1;
         
     },
-    
-    
+    /**
+     * hasPermExists:
+     * Is there a permission defined for this (used by module registration.)
+     * 
+     * @param {String} name the [Module].[permission] to check for
+     * @returns {Boolean} tue indicates permission exists.
+     */
+    hasPermExists: function(name) {
+        if (
+            (typeof(Pman.Login.authUser) != 'object')
+            ||
+            (typeof(Pman.Login.authUser.perms) != 'object')
+            ||
+            (typeof(Pman.Login.authUser.perms[name]) != 'string')
+            ) {
+                return false;
+        }
+        return true;
+    },
     
     
     
@@ -481,9 +561,9 @@ Pman = new Roo.Document(
                 }
                 // what about the toolbar??
                 tab.grid.getView().mainWrap.mask("Deleting");
-                Pman.request({
+                new Pman.Request({
                     url: baseURL + '/Roo/'+tbl+'.php',
-                    method: 'GET',
+                    method: 'POST',
                     params: {
                         _delete : r.join(',')
                     },
@@ -510,25 +590,12 @@ Pman = new Roo.Document(
                     
                 });
             }
+            
         );
-    },
-    refreshActivePanel : function() {
-        var actpan = this.layout.getRegion('center').getActivePanel();
-        if (actpan.controller && actpan.controller.paging) {
-            actpan.controller.paging.onClick('refresh');
-            return;
-        }
-        
-        var agid = Pman.layout.getRegion('center').getActivePanel().id;
-        if (!agid) {
-            return;
-        }
-        Pman.Tab[agid].paging.onClick('refresh');
-    },
-    toCidV : function(data) {
-        return 'C' + data.in_out.substring(0,1) + data.cid;
+        return '';
     },
     
+    
     standardActionFailed :  function(f, act, cb) {
     
         if (act.failureType == 'client') {
@@ -552,17 +619,14 @@ Pman = new Roo.Document(
         Roo.MessageBox.alert("Error", "Error loading details",cb); 
     },
     /**
-     * 
-     * similar to Roo.Ajax, but handles our responses better...
-     * c.url
-     * c.method
-     * c.params
-     * c.failure() == failure function..
-     * c.success(data) == success function..
-     * 
+     * Depreciated - USE new Pman.Request
+    *  We need to replace all the uses with this, however the api is slightly different,
+    *  the success argument is res.data, not res..
      * 
      */
     request : function(c) {
+        //return new Pman.Request(c);
+         
         var r= new Roo.data.Connection({
             timeout : typeof(c.timeout) == 'undefined' ?  30000 : c.timeout
         });
@@ -605,6 +669,7 @@ Pman = new Roo.Document(
             scope: this
             
         });
+         
     },
     
     
@@ -717,220 +782,206 @@ Pman = new Roo.Document(
             }
             s = !s;
             document.title = s ? msg : oldtitle;
-                  
+            return false;     
         }, 1000); // every 120 secs = 2mins..
          document.title =   msg;
         
         
         
     },
-    
-    modules : false,
     /**
-     * example:
-     * 
-     * Pman.register({
-          modKey : '00-admin-xxxx',
-          module : Pman.Tab.projectMgr,
-          region : 'center',
-          parent : Pman.layout
-        })
-     * 
+     * @property {Array} appModules  - array based on AppModules global
      */
-    register : function(obj) {
-        if (!obj.parent) {
-            if (obj.parent === false) {
-                //console.log('skip module (no parent)' + obj.modkey);
+    appModules : false,
+    
+    modules : false,
+    
+    
+    xregister : function(obj)
+    {
+        
+        // work out owner..
+        if (!Pman.appModules === false) {
+            Pman.appModules = typeof(AppModules ) == 'undefined'? [] :
+                AppModules.split(',');
+        }
+        
+        
+        
+        // ignore registration of objects which are disabled.
+        // global supplied by master.html
+        appDisabled = typeof(appDisabled) == 'undefined' ? [] : appDisabled;
+        
+        
+        /// design flaw
+        // previously we did not a good naming policy for module and parts
+        // most things that are called module here, really are 'parts'
+        // new versions should have 'part' as [ module : part ]
+         if (typeof(obj.part) != 'undefined')  {
+           
+            var permname = obj.part.join('.');
+                // we now have permission...
+                // obj.moduleOwner '.' lname
+           
+           
+            if (appDisabled.indexOf(permname) > -1)  {
+                Roo.log(permname + " is Disabled for this site");
+                obj.disabled = true;
                 return;
             }
             
-            console.log(obj);
-        }
-        if (!obj.parent.modules) {
-            obj.parent.modules = new Roo.util.MixedCollection(false, function(o) { return o.modKey });
+            
         }
         
-        obj.parent.modules.add(obj);
-        
-    },
-    
-    buildModules : function(parent, onComplete) 
-    {
+       
         
-        var _this = this;
-        var cmp = function(a,b) {   
-            return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
-            
-        };
-        if (!parent.modules) {
+        if ( obj.isTop) {
+            // false parent... use it..
             return;
         }
-        parent.modules.keySort('ASC',  cmp );
-        var mods = [];
         
         
-        // add modules to their parents..
-        var addMod = function(m) {
-           // console.log(m.modKey);
-            
-            mods.push(m);
-            if (m.module.modules) {
-                m.module.modules.keySort('ASC',  cmp );
-                m.module.modules.each(addMod);
-            }
-            if (m.finalize) {
-                m.finalize.name = m.name + " (clean up) ";
-                mods.push(m.finalize);
-            }
-            
+        if (obj.parent === Pman || obj.parent  == 'Pman') {
+            Roo.log("PARENT OF : " + obj.name + " replacing with fake");
+            obj.parent = Pman.fakeRoot;
         }
-        parent.modules.each(addMod);
-        //this.allmods = mods;
-        //console.log(mods);
-        //return;
-        if (!mods.length) {
-            if (onComplete) onComplete();
+        
+        if (typeof(obj.parent) == 'undefined') {
+            console.log("Parent is undefined");
+            console.log(obj);
+            obj.disabled = true;
             return;
         }
-        // flash it up as modal - so we store the mask!?
-        Roo.MessageBox.show({ title: 'loading' });
-        Roo.MessageBox.show({
-           title: "Please wait...",
-           msg: "Building Interface...",
-           width:450,
-           progress:true,
-           closable:false,
-           modal: false
-          
-        });
-        var n = 0;
-        var progressRun = function() {
-            
-            var mod = mods[n];
-            
-            
-            Roo.MessageBox.updateProgress(
-                (n+1)/mods.length,  "Building Interface " + (n+1) + 
-                    " of " + mods.length + 
-                    (mod.name ? (' - ' + mod.name) : '')
-                    );
             
             
+        if (obj.parent === false) {
+            obj.disabled = true;
+            console.log('ignoring top level object (as parent===false found)');
+            console.log(obj);
+            return;
+        }
+        // this is an error condition - the parent does not exist..
+            // technically it should not happen..
+          
+        // hack for Pman parent == Pman..
+        if (obj.parent == obj.module) {
+            obj.parent = false;
             
-            if (typeof(mod) == 'function') {
-                mod();
-                
-            } else  if (mod.parent.layout && !mod.module.disabled) {
-                // honour permname setings..
-                if (mod.permname && mod.permname.length) {
-                    if (Pman.hasPerm(mod.permname, 'S')) {
-                        mod.module.add(mod.parent.layout, mod.region);    
-                    }
-                } else {
-                    mod.module.add(mod.parent.layout, mod.region);    
-                }
-                    
-                 
+        }
+       
+        
+    },
+    /**
+     * fired before building on each compoenent
+     * used to apply permissions.
+     */
+    
+    xbeforebuild : function(obj)
+    {
+        if (typeof(obj.part) != 'undefined')  {
+           
+            if (!obj.part[1].length) {
+                obj.part[1] = obj.part[0];
             }
+            var permname = obj.part.join('.');
             
+            Roo.log("CHECKING: "+ permname);
             
-            n++;
-            if (n >= mods.length) {
-                onComplete();  
+                // we now have permission...
+                // obj.moduleOwner '.' lname
+           
+            if (Pman.hasPermExists(permname) && !Pman.hasPerm(permname,'S')) {
+                // it's a turned off permission...
+                Roo.log(permname + " is Disabled for this user");
+                obj.disabled = true;
                 return;
             }
-                
+        
+            
             
-            progressRun.defer(10, Pman);    
         }
-        progressRun.defer(1, Pman);
-     
         
         
     },
     
-    gtranslate : function(str, src, dest, cb) {
-        // load script: 
-        var cur = 0;
-        var sbits = [];
-        var complete = '';
+    /**
+     * DEPRICATED : use Roo.XComponents now..
+     * 
+     * Pman.register({
+          modKey : '00-admin-xxxx',
+          module : Pman.Tab.projectMgr, << really a components..
+          part : [ 'Admin', 'ProjectManager' ]
+          moduleOwner : 
+          region : 'center',
+          parent : Pman.layout
+        })
+     * 
+     */
+    register : function(obj)
+    {
+        
+        //this.xregister(obj);
         
-        function transbits()
+        
+        // old style calls go in here..
+        // we need to convert the object so that it looks a bit like an XCompoenent..
+         
+        obj.render = function()
         {
-            if ((cur +1) > sbits.length) {
-                cb.call(complete);
+            if (!this.parent) {
+                Roo.log("Skip module, as parent does not exist");
+                Roo.log(this);
+                return;
+            }
+            //if (typeof(mod) == 'function') {
+            //    mod();
+                
+            if (typeof(this.region) == 'undefined') {
+                Roo.log("Module does not have region defined, skipping");
+                Roo.log(this);
+                return;
+            }
+            if (this.module.disabled) {
+                Roo.log("Module disabled, should not rendering")
+                Roo.log(this);
+                return;
+            }
+            
+            if (!this.parent.layout) {
+                Roo.log("Module parent does not have property layout.")
+                Roo.log(this);
                 return;
             }
-            //console.log("SEND : " + sbits[cur]);
-            Pman.gtranslate( sbits[cur], src, dest, function(result) {
-                if (typeof(result) == 'object') {
-                    cb.call(result);
-                    return;
-                }
-                complete += complete.length ? ' ' : ''; 
-                complete += result;
-                cur++;
-                transbits();
-            });
-        }
-        
         
-        if (str.length > 500) {
-            var bits = str.split(/\s+/);
-            sbits[0]  = '';
-            for (var i =0; i < bits.length; i++) {
-                if (sbits[cur].length + bits[i].length > 500) {
-                    cur++;
-                    sbits[cur] = bits[i];
-                    continue;
+           // honour DEPRICATED permname setings..
+           // new code should use PART name, and matching permissions.
+            if (this.permname && this.permname.length) {
+                if (!Pman.hasPerm(this.permname, 'S')) {
+                    return;
                 }
-                sbits[cur] += sbits[cur].length  ? ' ' : '';
-                sbits[cur] += bits[i] + ' '
                 
             }
-            cur = 0; // reset cursor.
+            this.add(this.parent.layout, this.region);
+            this.el = this.layout;
             
-            transbits();
-            return;
-        }
-                
-                
-                
-                
-                
+            
+              
+        };
+        // map some of the standard properties..
+        obj.order = obj.modKey;
         
+        // a bit risky...
         
         
         
-        var x = new Roo.data.ScriptTagProxy({ 
-            url:  'http://ajax.googleapis.com/ajax/services/language/translate', 
-            callbackParam : 'callback' 
-            
-        });
-        x.load(
-            {
-                v: '1.0',
-                q : str,
-                langpair : src + '|' +dest
-            }, // end params.
-            { // reader
-                readRecords : function (o) {
-                    if (!o.responseData) {
-                        return o;
-                    }
-                    return o.responseData.translatedText;
-                }
-            }, 
-            function (result) {
-                cb(result);
-            },
-            this,
-            []
-        );
+        // the other issue we have is that
+         
         
-            
+        // Roo.log("CALLING XComponent register with : " + obj.name);
         
+        // this will call xregister as it's the on.register handler..
+        Roo.XComponent.register(obj.isTop ? obj : Roo.apply(obj.module, obj));
+         
     } ,
     invertColor : function(c)
     {