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({
15 password' : 'password',
23 * Ext.apply(_T, _T[lang]);
27 Roo.Login = function(cfg)
30 Roo.each(this.items, function(e) {
33 Roo.Document.on('ready', function() {
37 Roo.Login.superclass.constructor.call(this);
39 Roo.extend(Roo.Login, Roo.LayoutDialog, {
42 * @cfg {String} method
43 * Method used to query for login details.
49 * URL to query login data. - eg. baseURL + '/Login.php'
53 onLoad : function() // called on page load...
57 if (Roo.get('loading')) { // clear any loading indicator..
58 Roo.get('loading').remove();
61 this.switchLang('en'); // set the language to english..
64 success: function(response, opts) { // check successfull...
66 var res = Pman.processResponse(response);
68 if (!res.success) { // error!
70 //console.log('call failure');
71 return this.failure(response,opts);
74 if (!res.data.id) { // id=0 == login failure.
75 return this.show(true);
79 //console.log(success);
80 this.fillAuth(res.data);
82 Roo.XComponent.build();
90 check: function(cfg) // called every so often to refresh cookie etc..
92 if (cfg.again) { // could be undefined..
99 if ( this.checkFails > 4) {
100 Roo.MessageBox.alert("Error",
101 "Error getting authentication status. - try reloading, or wait a while", function() {
102 _this.sending = false;
107 _this.check.defer(10000, _this, [ again: true ]); // check in 10 secs.
118 success: cfg.success || this.success,
119 failure : cfg.failure || this.failure,
134 Pman.Login = new Roo.util.Observable({
147 authUser: { id : false },
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) {
161 Pman.Preview.disable();
162 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
163 "Error getting authentication status. - try reloading");
167 Pman.Login.check.defer(1000, Pman.Login, [ true ]);
172 success : function(response, opts) // check successfull...
174 this.sending = false;
175 var res = Pman.processResponse(response);
177 return this.failure(response, opts);
179 if (!res.data || !res.data.id) {
180 return this.failure(response,opts);
183 this.fillAuth(res.data);
189 fillAuth: function(au) {
190 this.startAuthCheck();
191 this.authUserId = au.id;
193 this.lastChecked = new Date();
194 Pman.fireEvent('authrefreshed', au);
195 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
196 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
198 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
199 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
200 this.switchLang(au.lang);
203 // open system... - -on setyp..
204 if (this.authUserId < 0) {
205 Roo.MessageBox.alert("Warning",
206 "This is an open system - please set up a admin user with a password.");
209 //Pman.onload(); // which should do nothing if it's a re-auth result...
215 intervalID : false, /// the login refresher...
219 startAuthCheck : function() // starter for timeout checking..
221 if (Pman.Login.intervalID) { // timer already in place...
225 Pman.Login.intervalID = window.setInterval(function() {
226 Pman.Login.check(false);
227 }, 120000); // every 120 secs = 2mins..
240 this.dialog = new Roo.LayoutDialog(Roo.get(document.body).createChild({tag:'div'}),
241 { // the real end set is here...
258 // tabPosition: 'top',
261 alwaysShowTabs: false
268 this.dialog.addButton("Forgot Password", function()
271 var n = _this.form.findField('username').getValue();
273 Roo.MessageBox.alert("Error", "Fill in your email address");
277 url: baseURL + '/Login.js',
282 success: function(response, opts) { // check successfull...
284 var res = Pman.processResponse(response);
285 if (!res.success) { // error!
286 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
289 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
291 failure : function() {
292 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
298 this.dialog.addButton("Login", function()
300 Pman.Login.dialog.el.mask("Logging in");
301 Pman.Login.form.doAction('submit', {
302 url: baseURL + '/Login',
306 this.layout = this.dialog.getLayout();
307 this.layout.beginUpdate();
309 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
310 // generate some other tabs
311 this.form = new Roo.form.Form({
315 actionfailed : function(f, act) {
316 // form can return { errors: .... }
318 //act.result.errors // invalid form element list...
319 //act.result.errorMsg// invalid form element list...
321 Pman.Login.dialog.el.unmask();
322 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
323 "Login failed - communication error - try again.");
326 actioncomplete: function(re, act) {
328 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
329 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
330 Pman.Login.fillAuth(act.result.data);
332 Pman.Login.dialog.hide();
333 if (Roo.get('loading-mask')) {
334 //Roo.get('loading').show();
335 Roo.get('loading-mask').show();
354 new Roo.form.TextField({
355 fieldLabel: "Email Address",
358 autoCreate : {tag: "input", type: "text", size: "20"}
361 new Roo.form.TextField({
362 fieldLabel: "Password",
363 inputType: 'password',
366 autoCreate : {tag: "input", type: "text", size: "20"},
368 specialkey : function(e,ev) {
369 if (ev.keyCode == 13) {
370 Pman.Login.dialog.el.mask("Logging in");
371 Pman.Login.form.doAction('submit', {
372 url: baseURL + '/Login.json',
379 new Roo.form.ComboBox({
380 fieldLabel: "Language",
383 xtype : 'SimpleStore',
384 fields: ['lang', 'ldisp'],
387 [ 'zh_HK' , '\u7E41\u4E2D' ],
388 [ 'zh_CN', '\u7C21\u4E2D' ]
395 displayField:'ldisp',
399 triggerAction: 'all',
400 emptyText:'Select a Language...',
403 select : function(cb, rec, ix) {
406 Pman.Login.switchLang(rec.data.lang);
416 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
417 ef.dom.style.margin = 10;
419 this.form.render(ef.dom);
420 // logoprefix comes from base config.
421 Pman.Login.form.el.createChild({
423 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
424 style: 'margin-bottom: 10px;'
426 Pman.Login.form.el.dom.firstChild
429 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
431 //title: 'Org Details',
438 this.layout.endUpdate();
440 this.fireEvent('render', this);
447 resizeToLogo : function()
449 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
451 this.resizeToLogo.defer(1000,this);
454 var w = Ext.lib.Dom.getViewWidth() - 100;
455 var h = Ext.lib.Dom.getViewHeight() - 100;
456 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
457 Pman.Login.dialog.center();
462 show: function (modal)
467 modal = modal || false;
468 if (Pman.Login.authUserId < 0) { // logout!?
472 if (Pman.Login.intervalID) {
474 window.clearInterval(Pman.Login.intervalID);
475 Pman.Login.intervalID = false;
482 if (Roo.get('loading')) {
483 Roo.get('loading').remove();
485 if (Roo.get('loading-mask')) {
486 Roo.get('loading-mask').hide();
489 //incomming._node = tnode;
491 this.dialog.modal = !modal;
493 this.dialog.el.unmask();
494 this.resizeToLogo.defer(1000,this);
497 this.form.setValues({
498 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
499 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
501 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
502 if (this.form.findField('username').getValue().length > 0 ){
503 this.form.findField('password').focus();
505 this.form.findField('username').focus();
515 window.onbeforeunload = function() { }; // false does not work for IE..
516 Pman.Login.authUserId = -1;
518 url: baseURL + '/Login.html',
523 failure : function() {
524 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
525 document.location = baseURL + '?ts=' + Math.random();
529 success : function() {
530 Pman.Login.authUserId = -1;
531 Pman.Login.checkFails =0;
533 document.location = baseURL + '?ts=' + Math.random();
539 switchLang : function (lang) {
543 if (typeof(_T.en) == 'undefined') {
545 Roo.apply(_T.en, _T);
548 if (typeof(_T[lang]) == 'undefined') {
549 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
554 Roo.apply(_T, _T[lang]);
555 // just need to set the text values for everything...
559 function formLabel(name, val) {
561 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
562 if (lbl.getElementsByTagName('label').length) {
563 lbl = lbl.getElementsByTagName('label')[0];
565 lbl = lbl.parentNode.getElementsByTagName('label')[0];
571 formLabel('password', "Password"+':');
572 formLabel('username', "Email Address"+':');
573 formLabel('lang', "Language"+':');
574 this.dialog.setTitle("Login");
575 this.dialog.buttons[0].setText("Forgot Password");
576 this.dialog.buttons[1].setText("Login");
582 inGroup : function(g)
584 return this.authUser && this.authUser.groups &&
585 this.authUser.groups.indexOf(g) > -1;
589 return this.authUser && this.authUser.company_id_comptype &&
590 this.authUser.company_id_comptype == 'OWNER';
594 * Depreciated = use Pman.I18n
597 i18nList: function (type, codes)
600 return Pman.I18n.listToNames(type, codes);
602 i18n: function(type, code)
604 return Pman.I18n.toName(type, code);