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',
30 * Ext.apply(_T, _T[lang]);
34 Roo.Login = function(cfg)
37 Roo.each(this.items, function(e) {
40 Roo.Document.on('ready', function() {
44 Roo.Login.superclass.constructor.call(this);
48 Roo.extend(Roo.Login, Roo.LayoutDialog, {
51 * @cfg {String} method
52 * Method used to query for login details.
58 * URL to query login data. - eg. baseURL + '/Login.php'
62 onLoad : function() // called on page load...
66 if (Roo.get('loading')) { // clear any loading indicator..
67 Roo.get('loading').remove();
70 this.switchLang('en'); // set the language to english..
73 success: function(response, opts) { // check successfull...
75 var res = Pman.processResponse(response);
77 if (!res.success) { // error!
79 //console.log('call failure');
80 return this.failure(response,opts);
83 if (!res.data.id) { // id=0 == login failure.
84 return this.show(true);
88 //console.log(success);
89 this.fillAuth(res.data);
91 Roo.XComponent.build();
99 check: function(cfg) // called every so often to refresh cookie etc..
101 if (cfg.again) { // could be undefined..
108 if ( this.checkFails > 4) {
109 Roo.MessageBox.alert("Error",
110 "Error getting authentication status. - try reloading, or wait a while", function() {
111 _this.sending = false;
116 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
127 success: cfg.success || this.success,
128 failure : cfg.failure || this.failure,
136 success : function(response, opts) // check successfull...
138 this.sending = false;
139 var res = Pman.processResponse(response);
141 return this.failure(response, opts);
143 if (!res.data || !res.data.id) {
144 return this.failure(response,opts);
147 this.fillAuth(res.data);
154 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
157 this.sending = false;
158 var res = Pman.processResponse(response);
160 if ( Pman.Login.checkFails > 2) {
162 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
163 "Error getting authentication status. - try reloading");
166 opts.callCfg.again = true;
167 this.check.defer(1000, this, [ opts.callCfg ]);
173 fillAuth: function(au) {
174 this.startAuthCheck();
175 this.authUserId = au.id;
177 this.lastChecked = new Date();
178 Pman.fireEvent('authrefreshed', au);
179 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
180 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
182 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
183 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
184 this.switchLang(au.lang);
187 // open system... - -on setyp..
188 if (this.authUserId < 0) {
189 Roo.MessageBox.alert("Warning",
190 "This is an open system - please set up a admin user with a password.");
193 //Pman.onload(); // which should do nothing if it's a re-auth result...
198 startAuthCheck : function() // starter for timeout checking..
200 if (Pman.Login.intervalID) { // timer already in place...
204 Pman.Login.intervalID = window.setInterval(function() {
205 Pman.Login.check(false);
206 }, 120000); // every 120 secs = 2mins..
226 // tabPosition: 'top',
229 alwaysShowTabs: false
232 rendered : function(dlg) {
233 this.items[0].dialog = dlg;
243 actionfailed : function(f, act) {
244 // form can return { errors: .... }
246 //act.result.errors // invalid form element list...
247 //act.result.errorMsg// invalid form element list...
249 this.dialog.el.unmask();
250 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
251 "Login failed - communication error - try again.");
254 actioncomplete: function(re, act) {
256 Roo.state.Manager.set(
257 this.dialog.realm + '.' + this.dialog.params.username,
258 this.form.findField(this.dialog.params.username).getValue()
260 Roo.state.Manager.set(
261 this.dialog.realm + '.lang',
262 Pman.Login.form.findField('lang').getValue()
265 this.dialog.fillAuth(act.result.data);
269 if (Roo.get('loading-mask')) {
270 Roo.get('loading-mask').show();
272 Roo.XComponent.build();
287 Pman.Login = new Roo.util.Observable({
300 authUser: { id : false },
310 intervalID : false, /// the login refresher...
323 this.dialog = new Roo.LayoutDialog(Roo.get(document.body).createChild({tag:'div'}),
324 { // the real end set is here...
341 // tabPosition: 'top',
344 alwaysShowTabs: false
351 this.dialog.addButton("Forgot Password", function()
354 var n = _this.form.findField('username').getValue();
356 Roo.MessageBox.alert("Error", "Fill in your email address");
360 url: baseURL + '/Login.js',
365 success: function(response, opts) { // check successfull...
367 var res = Pman.processResponse(response);
368 if (!res.success) { // error!
369 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
372 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
374 failure : function() {
375 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
381 this.dialog.addButton("Login", function()
383 Pman.Login.dialog.el.mask("Logging in");
384 Pman.Login.form.doAction('submit', {
385 url: baseURL + '/Login',
389 this.layout = this.dialog.getLayout();
390 this.layout.beginUpdate();
392 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
393 // generate some other tabs
394 this.form = new Roo.form.Form({
398 actionfailed : function(f, act) {
399 // form can return { errors: .... }
401 //act.result.errors // invalid form element list...
402 //act.result.errorMsg// invalid form element list...
404 Pman.Login.dialog.el.unmask();
405 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
406 "Login failed - communication error - try again.");
409 actioncomplete: function(re, act) {
411 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
412 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
413 Pman.Login.fillAuth(act.result.data);
415 Pman.Login.dialog.hide();
416 if (Roo.get('loading-mask')) {
417 //Roo.get('loading').show();
418 Roo.get('loading-mask').show();
437 new Roo.form.TextField({
438 fieldLabel: "Email Address",
441 autoCreate : {tag: "input", type: "text", size: "20"}
444 new Roo.form.TextField({
445 fieldLabel: "Password",
446 inputType: 'password',
449 autoCreate : {tag: "input", type: "text", size: "20"},
451 specialkey : function(e,ev) {
452 if (ev.keyCode == 13) {
453 Pman.Login.dialog.el.mask("Logging in");
454 Pman.Login.form.doAction('submit', {
455 url: baseURL + '/Login.json',
462 new Roo.form.ComboBox({
463 fieldLabel: "Language",
466 xtype : 'SimpleStore',
467 fields: ['lang', 'ldisp'],
470 [ 'zh_HK' , '\u7E41\u4E2D' ],
471 [ 'zh_CN', '\u7C21\u4E2D' ]
478 displayField:'ldisp',
482 triggerAction: 'all',
483 emptyText:'Select a Language...',
486 select : function(cb, rec, ix) {
489 Pman.Login.switchLang(rec.data.lang);
499 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
500 ef.dom.style.margin = 10;
502 this.form.render(ef.dom);
503 // logoprefix comes from base config.
504 Pman.Login.form.el.createChild({
506 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
507 style: 'margin-bottom: 10px;'
509 Pman.Login.form.el.dom.firstChild
512 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
514 //title: 'Org Details',
521 this.layout.endUpdate();
523 this.fireEvent('render', this);
530 resizeToLogo : function()
532 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
534 this.resizeToLogo.defer(1000,this);
537 var w = Ext.lib.Dom.getViewWidth() - 100;
538 var h = Ext.lib.Dom.getViewHeight() - 100;
539 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
540 Pman.Login.dialog.center();
545 show: function (modal)
550 modal = modal || false;
551 if (Pman.Login.authUserId < 0) { // logout!?
555 if (Pman.Login.intervalID) {
557 window.clearInterval(Pman.Login.intervalID);
558 Pman.Login.intervalID = false;
565 if (Roo.get('loading')) {
566 Roo.get('loading').remove();
568 if (Roo.get('loading-mask')) {
569 Roo.get('loading-mask').hide();
572 //incomming._node = tnode;
574 this.dialog.modal = !modal;
576 this.dialog.el.unmask();
577 this.resizeToLogo.defer(1000,this);
580 this.form.setValues({
581 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
582 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
584 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
585 if (this.form.findField('username').getValue().length > 0 ){
586 this.form.findField('password').focus();
588 this.form.findField('username').focus();
598 window.onbeforeunload = function() { }; // false does not work for IE..
599 Pman.Login.authUserId = -1;
601 url: baseURL + '/Login.html',
606 failure : function() {
607 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
608 document.location = baseURL + '?ts=' + Math.random();
612 success : function() {
613 Pman.Login.authUserId = -1;
614 Pman.Login.checkFails =0;
616 document.location = baseURL + '?ts=' + Math.random();
622 switchLang : function (lang) {
626 if (typeof(_T.en) == 'undefined') {
628 Roo.apply(_T.en, _T);
631 if (typeof(_T[lang]) == 'undefined') {
632 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
637 Roo.apply(_T, _T[lang]);
638 // just need to set the text values for everything...
642 function formLabel(name, val) {
644 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
645 if (lbl.getElementsByTagName('label').length) {
646 lbl = lbl.getElementsByTagName('label')[0];
648 lbl = lbl.parentNode.getElementsByTagName('label')[0];
654 formLabel('password', "Password"+':');
655 formLabel('username', "Email Address"+':');
656 formLabel('lang', "Language"+':');
657 this.dialog.setTitle("Login");
658 this.dialog.buttons[0].setText("Forgot Password");
659 this.dialog.buttons[1].setText("Login");
665 inGroup : function(g)
667 return this.authUser && this.authUser.groups &&
668 this.authUser.groups.indexOf(g) > -1;
672 return this.authUser && this.authUser.company_id_comptype &&
673 this.authUser.company_id_comptype == 'OWNER';
677 * Depreciated = use Pman.I18n
680 i18nList: function (type, codes)
683 return Pman.I18n.listToNames(type, codes);
685 i18n: function(type, code)
687 return Pman.I18n.toName(type, code);