1 //<script type="text/javascript">
5 * A generic Login Dialog..... - only one needed in theory!?!?
7 * Fires XComponent builder on success...
12 * Myapp.login = Roo.Login({
16 password' : 'password',
28 * Ext.apply(_T, _T[lang]);
32 Roo.Login = function(cfg)
35 Roo.each(this.items, function(e) {
38 Roo.Document.on('ready', function() {
42 Roo.Login.superclass.constructor.call(this);
44 Roo.extend(Roo.Login, Roo.LayoutDialog, {
47 * @cfg {String} method
48 * Method used to query for login details.
54 * URL to query login data. - eg. baseURL + '/Login.php'
58 onLoad : function() // called on page load...
62 if (Roo.get('loading')) { // clear any loading indicator..
63 Roo.get('loading').remove();
66 this.switchLang('en'); // set the language to english..
69 success: function(response, opts) { // check successfull...
71 var res = Pman.processResponse(response);
73 if (!res.success) { // error!
75 //console.log('call failure');
76 return this.failure(response,opts);
79 if (!res.data.id) { // id=0 == login failure.
80 return this.show(true);
84 //console.log(success);
85 this.fillAuth(res.data);
87 Roo.XComponent.build();
95 check: function(cfg) // called every so often to refresh cookie etc..
97 if (cfg.again) { // could be undefined..
104 if ( this.checkFails > 4) {
105 Roo.MessageBox.alert("Error",
106 "Error getting authentication status. - try reloading, or wait a while", function() {
107 _this.sending = false;
112 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
123 success: cfg.success || this.success,
124 failure : cfg.failure || this.failure,
139 Pman.Login = new Roo.util.Observable({
152 authUser: { id : false },
159 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
162 this.sending = false;
163 var res = Pman.processResponse(response);
165 if ( Pman.Login.checkFails > 2) {
166 Pman.Preview.disable();
167 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
168 "Error getting authentication status. - try reloading");
172 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
177 success : function(response, opts) // check successfull...
179 this.sending = false;
180 var res = Pman.processResponse(response);
182 return this.failure(response, opts);
184 if (!res.data || !res.data.id) {
185 return this.failure(response,opts);
188 this.fillAuth(res.data);
194 fillAuth: function(au) {
195 this.startAuthCheck();
196 this.authUserId = au.id;
198 this.lastChecked = new Date();
199 Pman.fireEvent('authrefreshed', au);
200 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
201 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
203 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
204 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
205 this.switchLang(au.lang);
208 // open system... - -on setyp..
209 if (this.authUserId < 0) {
210 Roo.MessageBox.alert("Warning",
211 "This is an open system - please set up a admin user with a password.");
214 //Pman.onload(); // which should do nothing if it's a re-auth result...
220 intervalID : false, /// the login refresher...
224 startAuthCheck : function() // starter for timeout checking..
226 if (Pman.Login.intervalID) { // timer already in place...
230 Pman.Login.intervalID = window.setInterval(function() {
231 Pman.Login.check(false);
232 }, 120000); // every 120 secs = 2mins..
245 this.dialog = new Roo.LayoutDialog(Roo.get(document.body).createChild({tag:'div'}),
246 { // the real end set is here...
263 // tabPosition: 'top',
266 alwaysShowTabs: false
273 this.dialog.addButton("Forgot Password", function()
276 var n = _this.form.findField('username').getValue();
278 Roo.MessageBox.alert("Error", "Fill in your email address");
282 url: baseURL + '/Login.js',
287 success: function(response, opts) { // check successfull...
289 var res = Pman.processResponse(response);
290 if (!res.success) { // error!
291 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
294 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
296 failure : function() {
297 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
303 this.dialog.addButton("Login", function()
305 Pman.Login.dialog.el.mask("Logging in");
306 Pman.Login.form.doAction('submit', {
307 url: baseURL + '/Login',
311 this.layout = this.dialog.getLayout();
312 this.layout.beginUpdate();
314 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
315 // generate some other tabs
316 this.form = new Roo.form.Form({
320 actionfailed : function(f, act) {
321 // form can return { errors: .... }
323 //act.result.errors // invalid form element list...
324 //act.result.errorMsg// invalid form element list...
326 Pman.Login.dialog.el.unmask();
327 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
328 "Login failed - communication error - try again.");
331 actioncomplete: function(re, act) {
333 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
334 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
335 Pman.Login.fillAuth(act.result.data);
337 Pman.Login.dialog.hide();
338 if (Roo.get('loading-mask')) {
339 //Roo.get('loading').show();
340 Roo.get('loading-mask').show();
359 new Roo.form.TextField({
360 fieldLabel: "Email Address",
363 autoCreate : {tag: "input", type: "text", size: "20"}
366 new Roo.form.TextField({
367 fieldLabel: "Password",
368 inputType: 'password',
371 autoCreate : {tag: "input", type: "text", size: "20"},
373 specialkey : function(e,ev) {
374 if (ev.keyCode == 13) {
375 Pman.Login.dialog.el.mask("Logging in");
376 Pman.Login.form.doAction('submit', {
377 url: baseURL + '/Login.json',
384 new Roo.form.ComboBox({
385 fieldLabel: "Language",
388 xtype : 'SimpleStore',
389 fields: ['lang', 'ldisp'],
392 [ 'zh_HK' , '\u7E41\u4E2D' ],
393 [ 'zh_CN', '\u7C21\u4E2D' ]
400 displayField:'ldisp',
404 triggerAction: 'all',
405 emptyText:'Select a Language...',
408 select : function(cb, rec, ix) {
411 Pman.Login.switchLang(rec.data.lang);
421 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
422 ef.dom.style.margin = 10;
424 this.form.render(ef.dom);
425 // logoprefix comes from base config.
426 Pman.Login.form.el.createChild({
428 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
429 style: 'margin-bottom: 10px;'
431 Pman.Login.form.el.dom.firstChild
434 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
436 //title: 'Org Details',
443 this.layout.endUpdate();
445 this.fireEvent('render', this);
452 resizeToLogo : function()
454 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
456 this.resizeToLogo.defer(1000,this);
459 var w = Ext.lib.Dom.getViewWidth() - 100;
460 var h = Ext.lib.Dom.getViewHeight() - 100;
461 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
462 Pman.Login.dialog.center();
467 show: function (modal)
472 modal = modal || false;
473 if (Pman.Login.authUserId < 0) { // logout!?
477 if (Pman.Login.intervalID) {
479 window.clearInterval(Pman.Login.intervalID);
480 Pman.Login.intervalID = false;
487 if (Roo.get('loading')) {
488 Roo.get('loading').remove();
490 if (Roo.get('loading-mask')) {
491 Roo.get('loading-mask').hide();
494 //incomming._node = tnode;
496 this.dialog.modal = !modal;
498 this.dialog.el.unmask();
499 this.resizeToLogo.defer(1000,this);
502 this.form.setValues({
503 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
504 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
506 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
507 if (this.form.findField('username').getValue().length > 0 ){
508 this.form.findField('password').focus();
510 this.form.findField('username').focus();
520 window.onbeforeunload = function() { }; // false does not work for IE..
521 Pman.Login.authUserId = -1;
523 url: baseURL + '/Login.html',
528 failure : function() {
529 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
530 document.location = baseURL + '?ts=' + Math.random();
534 success : function() {
535 Pman.Login.authUserId = -1;
536 Pman.Login.checkFails =0;
538 document.location = baseURL + '?ts=' + Math.random();
544 switchLang : function (lang) {
548 if (typeof(_T.en) == 'undefined') {
550 Roo.apply(_T.en, _T);
553 if (typeof(_T[lang]) == 'undefined') {
554 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
559 Roo.apply(_T, _T[lang]);
560 // just need to set the text values for everything...
564 function formLabel(name, val) {
566 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
567 if (lbl.getElementsByTagName('label').length) {
568 lbl = lbl.getElementsByTagName('label')[0];
570 lbl = lbl.parentNode.getElementsByTagName('label')[0];
576 formLabel('password', "Password"+':');
577 formLabel('username', "Email Address"+':');
578 formLabel('lang', "Language"+':');
579 this.dialog.setTitle("Login");
580 this.dialog.buttons[0].setText("Forgot Password");
581 this.dialog.buttons[1].setText("Login");
587 inGroup : function(g)
589 return this.authUser && this.authUser.groups &&
590 this.authUser.groups.indexOf(g) > -1;
594 return this.authUser && this.authUser.company_id_comptype &&
595 this.authUser.company_id_comptype == 'OWNER';
599 * Depreciated = use Pman.I18n
602 i18nList: function (type, codes)
605 return Pman.I18n.listToNames(type, codes);
607 i18n: function(type, code)
609 return Pman.I18n.toName(type, code);