roojs-core.js
[roojs1] / Roo / Login.js
index 6b34c00..b5a2f89 100644 (file)
@@ -2,46 +2,46 @@
 
 
 /**
-* A generic Login Dialog..... - only one needed in theory!?!?
-*
-* Fires XComponent builder on success...
-* 
-* Sends 
-*    username,password, lang = for login actions.
-*    check = 1 for periodic checking that sesion is valid.
-*    passwordRequest = email request password
-*    logout = 1 = to logout
-* 
-* Affects: (this id="????" elements)
-*   loading  (removed) (used to indicate application is loading)
-*   loading-mask (hides) (used to hide application when it's building loading)
-*   
-* 
-* Usage: 
-*    
-* 
-* Myapp.login = Roo.Login({
-    url: xxxx,
-  
-    realm : 'Myapp', 
-    
-    
-    method : 'POST',
-    
-    
-    * 
-})
-* 
-* 
-* 
-* Ext.apply(_T, _T[lang]);
-* 
-**/
+ * @class Roo.Login
+ * @extends Roo.LayoutDialog
+ * A generic Login Dialog..... - only one needed in theory!?!?
+ *
+ * Fires XComponent builder on success...
+ * 
+ * Sends 
+ *    username,password, lang = for login actions.
+ *    check = 1 for periodic checking that sesion is valid.
+ *    passwordRequest = email request password
+ *    logout = 1 = to logout
+ * 
+ * Affects: (this id="????" elements)
+ *   loading  (removed) (used to indicate application is loading)
+ *   loading-mask (hides) (used to hide application when it's building loading)
+ *   
+ * 
+ * Usage: 
+ *    
+ * 
+ * Myapp.login = Roo.Login({
+     url: xxxx,
+   
+     realm : 'Myapp', 
+     
+     
+     method : 'POST',
+     
+     
+     
+ })
+ 
+ * 
+ 
+ **/
  
 Roo.Login = function(cfg)
 {
     this.addEvents({
-        'refreshed' : true,
+        'refreshed' : true
     });
     
     Roo.apply(this,cfg);
@@ -50,8 +50,10 @@ Roo.Login = function(cfg)
         this.onLoad();
     }, this);
     // call parent..
-    Roo.Login.superclass.constructor.call(this, {});
-     
+    
+   
+    Roo.Login.superclass.constructor.call(this, this);
+    //this.addxtype(this.items[0]);
     
     
 }
@@ -77,6 +79,18 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
      * @type {Object} 
      */
     user : false,
+    /**
+     * @property checkFails
+     * Number of times we have attempted to get authentication check, and failed.
+     * @type {Number} 
+     */
+    checkFails : 0,
+      /**
+     * @property intervalID
+     * The window interval that does the constant login checking.
+     * @type {Number} 
+     */
+    intervalID : 0,
     
     
     onLoad : function() // called on page load...
@@ -92,7 +106,7 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
         this.check({
             success:  function(response, opts)  {  // check successfull...
             
-                var res = Pman.processResponse(response);
+                var res = this.processResponse(response);
                 this.checkFails =0;
                 if (!res.success) { // error!
                     this.checkFails = 5;
@@ -101,7 +115,7 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
                 }
                 
                 if (!res.data.id) { // id=0 == login failure.
-                    return this.show(true);
+                    return this.show();
                 }
                 
                               
@@ -182,12 +196,29 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
         }); 
     },
     
-    
+    processResponse : function (response)
+    {
+        var res = '';
+        try {
+            res = Roo.decode(response.responseText);
+            // oops...
+            if (typeof(res) != 'object') {
+                res = { success : false, errorMsg : res, errors : true };
+            }
+            if (typeof(res.success) == 'undefined') {
+                res.success = false;
+            }
+            
+        } catch(e) {
+            res = { success : false,  errorMsg : response.responseText, errors : true };
+        }
+        return res;
+    },
     
     success : function(response, opts)  // check successfull...
     {  
         this.sending = false;
-        var res = Pman.processResponse(response);
+        var res = this.processResponse(response);
         if (!res.success) {
             return this.failure(response, opts);
         }
@@ -198,7 +229,7 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
         this.fillAuth(res.data);
         
         this.checkFails =0;
-        Pman.onload();
+        
     },
     
     
@@ -206,9 +237,9 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
     {
         this.authUser = -1;
         this.sending = false;
-        var res = Pman.processResponse(response);
+        var res = this.processResponse(response);
         //console.log(res);
-        if ( Pman.Login.checkFails > 2) {
+        if ( this.checkFails > 2) {
         
             Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : 
                 "Error getting authentication status. - try reloading"); 
@@ -229,10 +260,10 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
         this.fireEvent('refreshed', au);
         //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
         //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
-        
+        au.lang = au.lang || 'en';
         //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
         Roo.state.Manager.set( this.realm + 'lang' , au.lang);
-        this.switchLang(au.lang);
+        this.switchLang(au.lang );
         
      
         // open system... - -on setyp..
@@ -248,13 +279,13 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
     
     startAuthCheck : function() // starter for timeout checking..
     {
-        if (Pman.Login.intervalID) { // timer already in place...
+        if (this.intervalID) { // timer already in place...
             return false;
         }
-        
-        Pman.Login.intervalID =  window.setInterval(function() {
-                  Pman.Login.check(false);
-                }, 120000); // every 120 secs = 2mins..
+        var _this = this;
+        this.intervalID =  window.setInterval(function() {
+              _this.check(false);
+            }, 120000); // every 120 secs = 2mins..
         
         
     },
@@ -262,11 +293,13 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
     
     switchLang : function (lang) 
     {
-        if (!_T && lang != 'en') {
-            Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
+        _T = typeof(_T) == 'undefined' ? false : _T;
+          if (!_T || !lang.length) {
             return;
         }
-        if (!_T || !lang.length) {
+        
+        if (!_T && lang != 'en') {
+            Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
             return;
         }
         
@@ -308,7 +341,8 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
     title: "Login",
     modal: true,
     width:  350,
-    height: 230,
+    //height: 230,
+    height: 180,
     shadow: true,
     minWidth:200,
     minHeight:180,
@@ -327,37 +361,27 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
     } ,
     listeners : {
         
-        rendered : function(dlg) {
-            this.form = this.items[0];
+        show  : function(dlg)
+        {
+            //console.log(this);
+            this.form = this.layout.getRegion('center').activePanel.form;
             this.form.dialog = dlg;
             this.buttons[0].form = this.form;
-            this.buttons[0].dialog = dlg
+            this.buttons[0].dialog = dlg;
             this.buttons[1].form = this.form;
             this.buttons[1].dialog = dlg;
-            
-             // logoprefix comes from base config.
-            //Pman.Login.form.el.createChild({
-            //    tag: 'img', 
-            //    src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
-            //    style: 'margin-bottom: 10px;'
-            //},
-            
-    
-        
-            
-        },
-        show  : function()
-        {
-            //this.resizeToLogo.defer(1000,this);
+           
+           //this.resizeToLogo.defer(1000,this);
+            // this is all related to resizing for logos..
             //var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
            //// if (!sz) {
              //   this.resizeToLogo.defer(1000,this);
              //   return;
            // }
-            var w = Ext.lib.Dom.getViewWidth() - 100;
-            var h = Ext.lib.Dom.getViewHeight() - 100;
-            this.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
-            this.center();
+            //var w = Ext.lib.Dom.getViewWidth() - 100;
+            //var h = Ext.lib.Dom.getViewHeight() - 100;
+            //this.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
+            //this.center();
             if (this.disabled) {
                 this.hide();
                 return;
@@ -385,7 +409,7 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
             this.form.reset();
             //this.dialog.modal = !modal;
             //this.dialog.show();
-            this.dialog.el.unmask(); 
+            this.el.unmask(); 
             
             
             this.form.setValues({
@@ -393,7 +417,7 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
                 'lang' : Roo.state.Manager.get(this.realm + '.lang', 'en')
             });
             
-            Pman.Login.switchLang(Roo.state.Manager.get(this.realm + '.lang', ''));
+            this.switchLang(Roo.state.Manager.get(this.realm + '.lang', 'en'));
             if (this.form.findField('username').getValue().length > 0 ){
                 this.form.findField('password').focus();
             } else {
@@ -409,6 +433,7 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
             xns : Roo,
             region: 'center',
             fitToFrame : true,
+            
             items : [
     
                 {
@@ -416,7 +441,8 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
                     xtype : 'Form',
                     xns : Roo.form,
                     labelWidth: 100,
-                    style : 'margin : 10px;',
+                    style : 'margin: 10px;',
+                    
                     listeners : {
                         actionfailed : function(f, act) {
                             // form can return { errors: .... }
@@ -433,11 +459,11 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
                              
                             Roo.state.Manager.set(
                                 this.dialog.realm + '.username',  
-                                    this.form.findField('username').getValue()
+                                    this.findField('username').getValue()
                             );
                             Roo.state.Manager.set(
                                 this.dialog.realm + '.lang',  
-                                Pman.Login.form.findField('lang').getValue() 
+                                this.findField('lang').getValue() 
                             );
                             
                             this.dialog.fillAuth(act.result.data);
@@ -476,7 +502,7 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
                                         this.form.dialog.el.mask("Logging in");
                                         this.form.doAction('submit', {
                                             url: this.form.dialog.url,
-                                            method: this.form.dialog.method,
+                                            method: this.form.dialog.method
                                         });
                                     }
                                 }
@@ -515,67 +541,69 @@ Roo.extend(Roo.Login, Roo.LayoutDialog, {
                         
                         }
                     ]
-                },
+                }
                   
                 
-            ],
-            buttons : [
-                {
-                    xtype : 'Button',
-                    xns : 'Roo',
-                    text : "Forgot Password",
-                    listeners : {
-                        click : function() {
-                    
-                            var n = this.form.findField('username').getValue();
-                            if (!n.length) {
-                                Roo.MessageBox.alert("Error", "Fill in your email address");
-                                return;
-                            }
-                            Roo.Ajax.request({
-                                url: this.dialog.url,
-                                params: {
-                                    passwordRequest: n
-                                },
-                                method: this.dialog.method,
-                                success:  function(response, opts)  {  // check successfull...
-                                
-                                    var res = Pman.processResponse(response);
-                                    if (!res.success) { // error!
-                                       Roo.MessageBox.alert("Error" ,
-                                            res.errorMsg ? res.errorMsg  : "Problem Requesting Password Reset");
-                                       return;
-                                    }
-                                    Roo.MessageBox.alert("Notice" ,
-                                        "Please check you email for the Password Reset message");
-                                },
-                                failure : function() {
-                                    Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
-                                }
-                                
-                            });
-                        }
+            ]
+        }
+    ],
+    buttons : [
+        {
+            xtype : 'Button',
+            xns : 'Roo',
+            text : "Forgot Password",
+            listeners : {
+                click : function() {
+                    //console.log(this);
+                    var n = this.form.findField('username').getValue();
+                    if (!n.length) {
+                        Roo.MessageBox.alert("Error", "Fill in your email address");
+                        return;
                     }
-                },
-                {
-                    xtype : 'Button',
-                    xns : 'Roo',
-                    text : "Login",
-                    listeners : {
+                    Roo.Ajax.request({
+                        url: this.dialog.url,
+                        params: {
+                            passwordRequest: n
+                        },
+                        method: this.dialog.method,
+                        success:  function(response, opts)  {  // check successfull...
                         
-                        click : function () {
-                                
-                            this.dialog.el.mask("Logging in");
-                            this.form.doAction('submit', {
-                                    url: this.dialog.url,
-                                    method: this.dialog.method
-                            });
+                            var res = this.dialog.processResponse(response);
+                            if (!res.success) { // error!
+                               Roo.MessageBox.alert("Error" ,
+                                    res.errorMsg ? res.errorMsg  : "Problem Requesting Password Reset");
+                               return;
+                            }
+                            Roo.MessageBox.alert("Notice" ,
+                                "Please check you email for the Password Reset message");
+                        },
+                        failure : function() {
+                            Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
                         }
-                    }
+                        
+                    });
                 }
-            ]
+            }
+        },
+        {
+            xtype : 'Button',
+            xns : 'Roo',
+            text : "Login",
+            listeners : {
+                
+                click : function () {
+                        
+                    this.dialog.el.mask("Logging in");
+                    this.form.doAction('submit', {
+                            url: this.dialog.url,
+                            method: this.dialog.method
+                    });
+                }
+            }
         }
     ]
+  
+  
 })