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,
141 Pman.Login = new Roo.util.Observable({
154 authUser: { id : false },
161 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
164 this.sending = false;
165 var res = Pman.processResponse(response);
167 if ( Pman.Login.checkFails > 2) {
168 Pman.Preview.disable();
169 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
170 "Error getting authentication status. - try reloading");
174 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
179 success : function(response, opts) // check successfull...
181 this.sending = false;
182 var res = Pman.processResponse(response);
184 return this.failure(response, opts);
186 if (!res.data || !res.data.id) {
187 return this.failure(response,opts);
190 this.fillAuth(res.data);
196 fillAuth: function(au) {
197 this.startAuthCheck();
198 this.authUserId = au.id;
200 this.lastChecked = new Date();
201 Pman.fireEvent('authrefreshed', au);
202 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
203 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
205 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
206 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
207 this.switchLang(au.lang);
210 // open system... - -on setyp..
211 if (this.authUserId < 0) {
212 Roo.MessageBox.alert("Warning",
213 "This is an open system - please set up a admin user with a password.");
216 //Pman.onload(); // which should do nothing if it's a re-auth result...
222 intervalID : false, /// the login refresher...
226 startAuthCheck : function() // starter for timeout checking..
228 if (Pman.Login.intervalID) { // timer already in place...
232 Pman.Login.intervalID = window.setInterval(function() {
233 Pman.Login.check(false);
234 }, 120000); // every 120 secs = 2mins..
247 this.dialog = new Roo.LayoutDialog(Roo.get(document.body).createChild({tag:'div'}),
248 { // the real end set is here...
265 // tabPosition: 'top',
268 alwaysShowTabs: false
275 this.dialog.addButton("Forgot Password", function()
278 var n = _this.form.findField('username').getValue();
280 Roo.MessageBox.alert("Error", "Fill in your email address");
284 url: baseURL + '/Login.js',
289 success: function(response, opts) { // check successfull...
291 var res = Pman.processResponse(response);
292 if (!res.success) { // error!
293 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
296 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
298 failure : function() {
299 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
305 this.dialog.addButton("Login", function()
307 Pman.Login.dialog.el.mask("Logging in");
308 Pman.Login.form.doAction('submit', {
309 url: baseURL + '/Login',
313 this.layout = this.dialog.getLayout();
314 this.layout.beginUpdate();
316 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
317 // generate some other tabs
318 this.form = new Roo.form.Form({
322 actionfailed : function(f, act) {
323 // form can return { errors: .... }
325 //act.result.errors // invalid form element list...
326 //act.result.errorMsg// invalid form element list...
328 Pman.Login.dialog.el.unmask();
329 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
330 "Login failed - communication error - try again.");
333 actioncomplete: function(re, act) {
335 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
336 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
337 Pman.Login.fillAuth(act.result.data);
339 Pman.Login.dialog.hide();
340 if (Roo.get('loading-mask')) {
341 //Roo.get('loading').show();
342 Roo.get('loading-mask').show();
361 new Roo.form.TextField({
362 fieldLabel: "Email Address",
365 autoCreate : {tag: "input", type: "text", size: "20"}
368 new Roo.form.TextField({
369 fieldLabel: "Password",
370 inputType: 'password',
373 autoCreate : {tag: "input", type: "text", size: "20"},
375 specialkey : function(e,ev) {
376 if (ev.keyCode == 13) {
377 Pman.Login.dialog.el.mask("Logging in");
378 Pman.Login.form.doAction('submit', {
379 url: baseURL + '/Login.json',
386 new Roo.form.ComboBox({
387 fieldLabel: "Language",
390 xtype : 'SimpleStore',
391 fields: ['lang', 'ldisp'],
394 [ 'zh_HK' , '\u7E41\u4E2D' ],
395 [ 'zh_CN', '\u7C21\u4E2D' ]
402 displayField:'ldisp',
406 triggerAction: 'all',
407 emptyText:'Select a Language...',
410 select : function(cb, rec, ix) {
413 Pman.Login.switchLang(rec.data.lang);
423 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
424 ef.dom.style.margin = 10;
426 this.form.render(ef.dom);
427 // logoprefix comes from base config.
428 Pman.Login.form.el.createChild({
430 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
431 style: 'margin-bottom: 10px;'
433 Pman.Login.form.el.dom.firstChild
436 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
438 //title: 'Org Details',
445 this.layout.endUpdate();
447 this.fireEvent('render', this);
454 resizeToLogo : function()
456 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
458 this.resizeToLogo.defer(1000,this);
461 var w = Ext.lib.Dom.getViewWidth() - 100;
462 var h = Ext.lib.Dom.getViewHeight() - 100;
463 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
464 Pman.Login.dialog.center();
469 show: function (modal)
474 modal = modal || false;
475 if (Pman.Login.authUserId < 0) { // logout!?
479 if (Pman.Login.intervalID) {
481 window.clearInterval(Pman.Login.intervalID);
482 Pman.Login.intervalID = false;
489 if (Roo.get('loading')) {
490 Roo.get('loading').remove();
492 if (Roo.get('loading-mask')) {
493 Roo.get('loading-mask').hide();
496 //incomming._node = tnode;
498 this.dialog.modal = !modal;
500 this.dialog.el.unmask();
501 this.resizeToLogo.defer(1000,this);
504 this.form.setValues({
505 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
506 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
508 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
509 if (this.form.findField('username').getValue().length > 0 ){
510 this.form.findField('password').focus();
512 this.form.findField('username').focus();
522 window.onbeforeunload = function() { }; // false does not work for IE..
523 Pman.Login.authUserId = -1;
525 url: baseURL + '/Login.html',
530 failure : function() {
531 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
532 document.location = baseURL + '?ts=' + Math.random();
536 success : function() {
537 Pman.Login.authUserId = -1;
538 Pman.Login.checkFails =0;
540 document.location = baseURL + '?ts=' + Math.random();
546 switchLang : function (lang) {
550 if (typeof(_T.en) == 'undefined') {
552 Roo.apply(_T.en, _T);
555 if (typeof(_T[lang]) == 'undefined') {
556 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
561 Roo.apply(_T, _T[lang]);
562 // just need to set the text values for everything...
566 function formLabel(name, val) {
568 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
569 if (lbl.getElementsByTagName('label').length) {
570 lbl = lbl.getElementsByTagName('label')[0];
572 lbl = lbl.parentNode.getElementsByTagName('label')[0];
578 formLabel('password', "Password"+':');
579 formLabel('username', "Email Address"+':');
580 formLabel('lang', "Language"+':');
581 this.dialog.setTitle("Login");
582 this.dialog.buttons[0].setText("Forgot Password");
583 this.dialog.buttons[1].setText("Login");
589 inGroup : function(g)
591 return this.authUser && this.authUser.groups &&
592 this.authUser.groups.indexOf(g) > -1;
596 return this.authUser && this.authUser.company_id_comptype &&
597 this.authUser.company_id_comptype == 'OWNER';
601 * Depreciated = use Pman.I18n
604 i18nList: function (type, codes)
607 return Pman.I18n.listToNames(type, codes);
609 i18n: function(type, code)
611 return Pman.I18n.toName(type, code);