changes in app.js

This commit is contained in:
Yuri Kuznetsov
2014-11-19 16:42:20 +02:00
parent 322df7ff68
commit 3cfcfb02de
+59 -53
View File
@@ -17,7 +17,7 @@
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
************************************************************************/
var Espo = Espo || {};
Espo.App = function (options, callback) {
@@ -42,7 +42,7 @@ Espo.App = function (options, callback) {
this._setupAjax();
this.settings = new Espo['Models.Settings'](null, {cache: this.cache});
this.settings = new Espo['Models.Settings'](null, {cache: this.cache});
if (!this.settings.loadFromCache()) {
this.settings.load(true);
}
@@ -130,7 +130,7 @@ _.extend(Espo.App.prototype, {
this.preferences.defs = this.metadata.get('entityDefs.Preferences');
this.loader.addLibsConfig(this.metadata.get('app.jsLibs') || {});
this.loader.addLibsConfig(this.metadata.get('app.jsLibs') || {});
this._initRouter();
}.bind(this));
@@ -139,8 +139,9 @@ _.extend(Espo.App.prototype, {
if (!this.auth) {
this.baseController.login();
} else {
this._initUserData();
onAuth();
this._initUserData(null, function () {
onAuth();
});
}
this.on('auth', onAuth);
@@ -209,7 +210,7 @@ _.extend(Espo.App.prototype, {
callback(this.baseController);
return;
}
if (!(name in this.controllers)) {
if (!(name in this.controllers)) {
try {
var className = this.metadata.get('clientDefs.' + name + '.controller');
if (!className) {
@@ -268,14 +269,14 @@ _.extend(Espo.App.prototype, {
var self = this;
var getResourceInnerPath = function (type, name) {
var getResourceInnerPath = function (type, name) {
switch (type) {
case 'template':
return 'res/templates/' + name.split('.').join('/') + '.tpl';
case 'layoutTemplate':
return 'res/layout-types/' + name + '.tpl';
return 'res/layout-types/' + name + '.tpl';
case 'layout':
return 'res/layouts/' + name + '.json';
return 'res/layouts/' + name + '.json';
}
};
@@ -303,15 +304,15 @@ _.extend(Espo.App.prototype, {
viewLoader: this._viewLoader,
resources: {
loaders: {
'template': function (name, callback) {
'template': function (name, callback) {
var path = getResourcePath('template', name);
self.loader.load('res!' + path, callback);
self.loader.load('res!' + path, callback);
},
'layoutTemplate': function (name, callback) {
'layoutTemplate': function (name, callback) {
var path = getResourcePath('layoutTemplate', name);
self.loader.load('res!' + path, callback);
self.loader.load('res!' + path, callback);
}
}
}
}
});
},
@@ -320,15 +321,15 @@ _.extend(Espo.App.prototype, {
this.auth = this.storage.get('user', 'auth') || null;
this.baseController.on('login', function (data) {
this.auth = Base64.encode(data.auth.userName + ':' + data.auth.token);
this.auth = Base64.encode(data.auth.userName + ':' + data.auth.token);
this.storage.set('user', 'auth', this.auth);
this.storage.set('user', 'user', data.user);
this.storage.set('user', 'user', data.user);
this.storage.set('user', 'preferences', data.preferences);
this.storage.set('user', 'acl', data.acl || {});
this.storage.set('user', 'acl', data.acl || {});
this._initUserData(data);
this.trigger('auth');
this._initUserData(data, function () {
this.trigger('auth');
}.bind(this));
}.bind(this));
@@ -340,8 +341,8 @@ _.extend(Espo.App.prototype, {
logout: function () {
if (this.auth) {
var arr = Base64.decode(this.auth).split(':');
if (arr.length > 1) {
var arr = Base64.decode(this.auth).split(':');
if (arr.length > 1) {
$.ajax({
url: 'App/action/destroyAuthToken',
type: 'POST',
@@ -360,43 +361,48 @@ _.extend(Espo.App.prototype, {
this.storage.clear('user', 'user');
this.storage.clear('user', 'preferences');
this.storage.clear('user', 'acl');
this.doAction({action: 'login'});
this.doAction({action: 'login'});
this.language.clearCache();
xhr = new XMLHttpRequest;
xhr.open('GET', this.url + '/', !1, 'logout', 'logout');
xhr.open('GET', this.url + '/', !1, 'logout', 'logout');
xhr.send('');
xhr.abort();
},
_initUserData: function (options) {
_initUserData: function (options, callback) {
options = options || {};
if (this.auth !== null) {
this.language.load(null, true);
this.dateTime.setLanguage(this.language);
var userData = options.user || this.storage.get('user', 'user') || null;
var preferencesData = options.preferences || this.storage.get('user', 'preferences') || null;
var aclData = options.acl || this.storage.get('user', 'acl') || null;
this.language.load(function () {
this.dateTime.setLanguage(this.language);
this.user.set(userData);
this.preferences.set(preferencesData);
this.acl.set(aclData);
this.user.on('change', function () {
this.storage.set('user', 'user', this.user.toJSON());
}, this);
if (!this.auth) {
return;
}
var userData = options.user || this.storage.get('user', 'user') || null;
var preferencesData = options.preferences || this.storage.get('user', 'preferences') || null;
var aclData = options.acl || this.storage.get('user', 'acl') || null;
this.user.set(userData);
this.preferences.set(preferencesData);
this.acl.set(aclData);
this.user.on('change', function () {
this.storage.set('user', 'user', this.user.toJSON());
}, this);
if (!this.auth) {
return;
}
var arr = Base64.decode(this.auth).split(':');
var xhr = new XMLHttpRequest();
xhr.open('GET', this.url + '/', false, arr[0], arr[1]);
xhr.send('');
if (callback) {
callback();
}
}.bind(this));
var arr = Base64.decode(this.auth).split(':');
var xhr = new XMLHttpRequest();
xhr.open('GET', this.url + '/', false, arr[0], arr[1]);
xhr.send('');
}
},
@@ -420,16 +426,16 @@ _.extend(Espo.App.prototype, {
$(document).ajaxError(function (event, xhr, options) {
if (xhr.errorIsHandled) {
return;
}
switch (xhr.status) {
}
switch (xhr.status) {
case 0:
if (xhr.statusText == 'timeout') {
Espo.Ui.error(self.language.translate('Timeout'));
}
break;
case 200:
Espo.Ui.error(self.language.translate('Bad server response'));
console.error('Bad server response: ' + xhr.responseText);
Espo.Ui.error(self.language.translate('Bad server response'));
console.error('Bad server response: ' + xhr.responseText);
break;
case 401:
if (!options.login) {
@@ -451,14 +457,14 @@ _.extend(Espo.App.prototype, {
self.baseController.error404();
} else {
Espo.Ui.error(self.language.translate('Error') + ' ' + xhr.status);
}
}
break;
default:
Espo.Ui.error(self.language.translate('Error') + ' ' + xhr.status);
}
var statusReason = xhr.getResponseHeader('X-Status-Reason');
if (statusReason) {
if (statusReason) {
console.error('Server side error: ' + statusReason);
}
});