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);
46 Roo.extend(Roo.Login, Roo.LayoutDialog, {
49 * @cfg {String} method
50 * Method used to query for login details.
56 * URL to query login data. - eg. baseURL + '/Login.php'
60 onLoad : function() // called on page load...
64 if (Roo.get('loading')) { // clear any loading indicator..
65 Roo.get('loading').remove();
68 this.switchLang('en'); // set the language to english..
71 success: function(response, opts) { // check successfull...
73 var res = Pman.processResponse(response);
75 if (!res.success) { // error!
77 //console.log('call failure');
78 return this.failure(response,opts);
81 if (!res.data.id) { // id=0 == login failure.
82 return this.show(true);
86 //console.log(success);
87 this.fillAuth(res.data);
89 Roo.XComponent.build();
97 check: function(cfg) // called every so often to refresh cookie etc..
99 if (cfg.again) { // could be undefined..
106 if ( this.checkFails > 4) {
107 Roo.MessageBox.alert("Error",
108 "Error getting authentication status. - try reloading, or wait a while", function() {
109 _this.sending = false;
114 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
125 success: cfg.success || this.success,
126 failure : cfg.failure || this.failure,
133 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
136 this.sending = false;
137 var res = Pman.processResponse(response);
139 if ( Pman.Login.checkFails > 2) {
140 Pman.Preview.disable();
141 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
142 "Error getting authentication status. - try reloading");
146 this.check.defer(1000, this, [ opts.callCfg ]);
159 Pman.Login = new Roo.util.Observable({
172 authUser: { id : false },
180 success : function(response, opts) // check successfull...
182 this.sending = false;
183 var res = Pman.processResponse(response);
185 return this.failure(response, opts);
187 if (!res.data || !res.data.id) {
188 return this.failure(response,opts);
191 this.fillAuth(res.data);
197 fillAuth: function(au) {
198 this.startAuthCheck();
199 this.authUserId = au.id;
201 this.lastChecked = new Date();
202 Pman.fireEvent('authrefreshed', au);
203 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
204 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
206 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
207 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
208 this.switchLang(au.lang);
211 // open system... - -on setyp..
212 if (this.authUserId < 0) {
213 Roo.MessageBox.alert("Warning",
214 "This is an open system - please set up a admin user with a password.");
217 //Pman.onload(); // which should do nothing if it's a re-auth result...
223 intervalID : false, /// the login refresher...
227 startAuthCheck : function() // starter for timeout checking..
229 if (Pman.Login.intervalID) { // timer already in place...
233 Pman.Login.intervalID = window.setInterval(function() {
234 Pman.Login.check(false);
235 }, 120000); // every 120 secs = 2mins..
248 this.dialog = new Roo.LayoutDialog(Roo.get(document.body).createChild({tag:'div'}),
249 { // the real end set is here...
266 // tabPosition: 'top',
269 alwaysShowTabs: false
276 this.dialog.addButton("Forgot Password", function()
279 var n = _this.form.findField('username').getValue();
281 Roo.MessageBox.alert("Error", "Fill in your email address");
285 url: baseURL + '/Login.js',
290 success: function(response, opts) { // check successfull...
292 var res = Pman.processResponse(response);
293 if (!res.success) { // error!
294 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
297 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
299 failure : function() {
300 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
306 this.dialog.addButton("Login", function()
308 Pman.Login.dialog.el.mask("Logging in");
309 Pman.Login.form.doAction('submit', {
310 url: baseURL + '/Login',
314 this.layout = this.dialog.getLayout();
315 this.layout.beginUpdate();
317 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
318 // generate some other tabs
319 this.form = new Roo.form.Form({
323 actionfailed : function(f, act) {
324 // form can return { errors: .... }
326 //act.result.errors // invalid form element list...
327 //act.result.errorMsg// invalid form element list...
329 Pman.Login.dialog.el.unmask();
330 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
331 "Login failed - communication error - try again.");
334 actioncomplete: function(re, act) {
336 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
337 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
338 Pman.Login.fillAuth(act.result.data);
340 Pman.Login.dialog.hide();
341 if (Roo.get('loading-mask')) {
342 //Roo.get('loading').show();
343 Roo.get('loading-mask').show();
362 new Roo.form.TextField({
363 fieldLabel: "Email Address",
366 autoCreate : {tag: "input", type: "text", size: "20"}
369 new Roo.form.TextField({
370 fieldLabel: "Password",
371 inputType: 'password',
374 autoCreate : {tag: "input", type: "text", size: "20"},
376 specialkey : function(e,ev) {
377 if (ev.keyCode == 13) {
378 Pman.Login.dialog.el.mask("Logging in");
379 Pman.Login.form.doAction('submit', {
380 url: baseURL + '/Login.json',
387 new Roo.form.ComboBox({
388 fieldLabel: "Language",
391 xtype : 'SimpleStore',
392 fields: ['lang', 'ldisp'],
395 [ 'zh_HK' , '\u7E41\u4E2D' ],
396 [ 'zh_CN', '\u7C21\u4E2D' ]
403 displayField:'ldisp',
407 triggerAction: 'all',
408 emptyText:'Select a Language...',
411 select : function(cb, rec, ix) {
414 Pman.Login.switchLang(rec.data.lang);
424 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
425 ef.dom.style.margin = 10;
427 this.form.render(ef.dom);
428 // logoprefix comes from base config.
429 Pman.Login.form.el.createChild({
431 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
432 style: 'margin-bottom: 10px;'
434 Pman.Login.form.el.dom.firstChild
437 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
439 //title: 'Org Details',
446 this.layout.endUpdate();
448 this.fireEvent('render', this);
455 resizeToLogo : function()
457 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
459 this.resizeToLogo.defer(1000,this);
462 var w = Ext.lib.Dom.getViewWidth() - 100;
463 var h = Ext.lib.Dom.getViewHeight() - 100;
464 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
465 Pman.Login.dialog.center();
470 show: function (modal)
475 modal = modal || false;
476 if (Pman.Login.authUserId < 0) { // logout!?
480 if (Pman.Login.intervalID) {
482 window.clearInterval(Pman.Login.intervalID);
483 Pman.Login.intervalID = false;
490 if (Roo.get('loading')) {
491 Roo.get('loading').remove();
493 if (Roo.get('loading-mask')) {
494 Roo.get('loading-mask').hide();
497 //incomming._node = tnode;
499 this.dialog.modal = !modal;
501 this.dialog.el.unmask();
502 this.resizeToLogo.defer(1000,this);
505 this.form.setValues({
506 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
507 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
509 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
510 if (this.form.findField('username').getValue().length > 0 ){
511 this.form.findField('password').focus();
513 this.form.findField('username').focus();
523 window.onbeforeunload = function() { }; // false does not work for IE..
524 Pman.Login.authUserId = -1;
526 url: baseURL + '/Login.html',
531 failure : function() {
532 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
533 document.location = baseURL + '?ts=' + Math.random();
537 success : function() {
538 Pman.Login.authUserId = -1;
539 Pman.Login.checkFails =0;
541 document.location = baseURL + '?ts=' + Math.random();
547 switchLang : function (lang) {
551 if (typeof(_T.en) == 'undefined') {
553 Roo.apply(_T.en, _T);
556 if (typeof(_T[lang]) == 'undefined') {
557 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
562 Roo.apply(_T, _T[lang]);
563 // just need to set the text values for everything...
567 function formLabel(name, val) {
569 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
570 if (lbl.getElementsByTagName('label').length) {
571 lbl = lbl.getElementsByTagName('label')[0];
573 lbl = lbl.parentNode.getElementsByTagName('label')[0];
579 formLabel('password', "Password"+':');
580 formLabel('username', "Email Address"+':');
581 formLabel('lang', "Language"+':');
582 this.dialog.setTitle("Login");
583 this.dialog.buttons[0].setText("Forgot Password");
584 this.dialog.buttons[1].setText("Login");
590 inGroup : function(g)
592 return this.authUser && this.authUser.groups &&
593 this.authUser.groups.indexOf(g) > -1;
597 return this.authUser && this.authUser.company_id_comptype &&
598 this.authUser.company_id_comptype == 'OWNER';
602 * Depreciated = use Pman.I18n
605 i18nList: function (type, codes)
608 return Pman.I18n.listToNames(type, codes);
610 i18n: function(type, code)
612 return Pman.I18n.toName(type, code);