Skip to content

EspoCRM – Open Source CRM Application

ref

Yuri Kuznetsov60d778bcb1c4
diff --git a/client/src/app.js b/client/src/app.js
index 77e571d7b0..b2d43e241a 100644
--- a/client/src/app.js
+++ b/client/src/app.js
@@ -1061,79 +1061,66 @@ class App {
* @param {module:app~UserData|null} data
* @param {function} callback
*/
- initUserData(data, callback) {
+ async initUserData(data, callback) {
data = data || {};
if (this.auth === null) {
return;
}
- new Promise(resolve => {
- if (data.user) {
- resolve(data);
+ if (!data.user) {
+ data = await this.requestUserData();
+ }
- return;
- }
+ this.language.name = data.language;
- this.requestUserData(userData => {
- data = userData;
+ await this.language.load();
- resolve(data);
- });
- })
- .then(data => {
- this.language.name = data.language;
-
- return this.language.load();
- })
- .then(() => {
- this.dateTime.setLanguage(this.language);
-
- const userData = data.user || null;
- const preferencesData = data.preferences || null;
- const aclData = data.acl || null;
- const settingData = data.settings || {};
-
- this.user.set(userData);
- this.preferences.set(preferencesData);
- this.settings.set(settingData);
- this.acl.set(aclData);
- this.appParams.setAll(data.appParams);
-
- if (!this.auth) {
- return;
- }
+ this.dateTime.setLanguage(this.language);
- const xhr = new XMLHttpRequest();
+ const userData = data.user || null;
+ const preferencesData = data.preferences || null;
+ const aclData = data.acl || null;
+ const settingData = data.settings || {};
- xhr.open('GET', this.basePath + this.apiUrl + '/');
- xhr.setRequestHeader('Authorization', 'Basic ' + this.auth);
+ this.user.setMultiple(userData);
+ this.preferences.setMultiple(preferencesData);
+ this.settings.setMultiple(settingData);
+ this.acl.set(aclData);
+ this.appParams.setAll(data.appParams);
- xhr.onreadystatechange = () => {
- if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
- const arr = Base64.decode(this.auth).split(':');
+ if (!this.auth) {
+ return;
+ }
- this.setCookieAuth(arr[0], arr[1]);
+ const xhr = new XMLHttpRequest();
- callback();
- }
+ xhr.open('GET', `${this.basePath}${this.apiUrl}/`);
+ xhr.setRequestHeader('Authorization', `Basic ${this.auth}`);
- if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 401) {
- Ui.error('Auth error');
- }
- };
+ xhr.onreadystatechange = () => {
+ if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
+ const arr = Base64.decode(this.auth).split(':');
- xhr.send('');
- });
+ this.setCookieAuth(arr[0], arr[1]);
+
+ callback();
+ }
+
+ if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 401) {
+ Ui.error('Auth error');
+ }
+ };
+
+ xhr.send('');
}
/**
* @private
- * @param {function} callback
+ * @return {Promise<module:app~UserData>}
*/
- requestUserData(callback) {
- Ajax.getRequest('App/user', {}, {appStart: true})
- .then(callback);
+ async requestUserData() {
+ return Ajax.getRequest('App/user', {}, {appStart: true});
}
/**
@@ -1471,17 +1458,16 @@ class App {
/**
* @private
- * @return {Promise}
*/
- initTemplateBundles() {
+ async initTemplateBundles() {
if (!this.responseCache) {
- return Promise.resolve();
+ return;
}
const key = 'templateBundlesCached';
if (this.cache.get('app', key)) {
- return Promise.resolve();
+ return;
}
const files = ['client/lib/templates.tpl'];
@@ -1502,58 +1488,56 @@ class App {
url.searchParams.append('t', this.appTimestamp);
return new Promise(resolve => {
- fetch(url)
- .then(response => {
- if (!response.ok) {
- console.error(`Could not fetch ${url}.`);
- resolve();
+ fetch(url).then(response => {
+ if (!response.ok) {
+ console.error(`Could not fetch ${url}.`);
+ resolve();
- return;
- }
+ return;
+ }
- const promiseList = [];
+ const promiseList = [];
- response.text().then(text => {
- const index = text.indexOf('\n');
+ response.text().then(text => {
+ const index = text.indexOf('\n');
- if (index <= 0) {
- resolve();
+ if (index <= 0) {
+ resolve();
- return;
- }
+ return;
+ }
- const delimiter = text.slice(0, index + 1);
- text = text.slice(index + 1);
+ const delimiter = text.slice(0, index + 1);
+ text = text.slice(index + 1);
- text.split(delimiter).forEach(item => {
- const index = item.indexOf('\n');
+ text.split(delimiter).forEach(item => {
+ const index = item.indexOf('\n');
- const file = item.slice(0, index).trim();
- let content = item.slice(index + 1);
+ const file = item.slice(0, index).trim();
+ let content = item.slice(index + 1);
- // noinspection RegExpDuplicateCharacterInClass
- content = content.replace(/[\r|\n|\r\n]$/, '');
+ // noinspection RegExpDuplicateCharacterInClass
+ content = content.replace(/[\r|\n|\r\n]$/, '');
- const url = baseUrl + this.basePath + 'client/' + file;
+ const url = baseUrl + this.basePath + 'client/' + file;
- const urlObj = new URL(url);
- urlObj.searchParams.append('r', timestamp);
+ const urlObj = new URL(url);
+ urlObj.searchParams.append('r', timestamp);
- promiseList.push(
- this.responseCache.put(urlObj, new Response(content))
- );
- });
+ promiseList.push(
+ this.responseCache.put(urlObj, new Response(content))
+ );
});
-
- Promise.all(promiseList).then(() => resolve());
});
+
+ Promise.all(promiseList).then(() => resolve());
+ });
});
});
- return Promise.all(promiseList)
- .then(() => {
- this.cache.set('app', key, true);
- });
+ await Promise.all(promiseList);
+
+ this.cache.set('app', key, true);
}
}