Skip to content

EspoCRM – Open Source CRM Application

TS migration

Yuriia7faf1800eeb
diff --git a/client/src/ajax.ts b/client/src/ajax.ts
index 0bf0ed6b2f..3026aad297 100644
--- a/client/src/ajax.ts
+++ b/client/src/ajax.ts
@@ -88,7 +88,7 @@ const Ajax = {
}
if (apiUrl) {
- url = Espo.Utils.trimSlash(apiUrl) + '/' + url;
+ url = Utils.trimSlash(apiUrl) + '/' + url;
}
if (!['GET', 'OPTIONS'].includes(method) && data) {
@@ -232,7 +232,7 @@ const Ajax = {
data = JSON.stringify(data);
}
- return /** @type {Promise<any> & AjaxPromise} */ Ajax.request(url, 'POST', data, options);
+ return Ajax.request(url, 'POST', data, options);
},
/**
@@ -252,7 +252,7 @@ const Ajax = {
data = JSON.stringify(data);
}
- return /** @type {Promise<any> & AjaxPromise} */ Ajax.request(url, 'PATCH', data, options);
+ return Ajax.request(url, 'PATCH', data, options);
},
/**
@@ -272,7 +272,7 @@ const Ajax = {
data = JSON.stringify(data);
}
- return /** @type {Promise<any> & AjaxPromise} */ Ajax.request(url, 'PUT', data, options);
+ return Ajax.request(url, 'PUT', data, options);
},
/**
@@ -292,7 +292,7 @@ const Ajax = {
data = JSON.stringify(data);
}
- return /** @type {Promise<any> & AjaxPromise} */ Ajax.request(url, 'DELETE', data, options);
+ return Ajax.request(url, 'DELETE', data, options);
},
/**
@@ -308,7 +308,7 @@ const Ajax = {
options?: Options & Record<string, any>,
): Promise<any> & AjaxPromise {
- return /** @type {Promise<any> & AjaxPromise} */ Ajax.request(url, 'GET', data, options);
+ return Ajax.request(url, 'GET', data, options);
},
/**
diff --git a/client/src/global.d.ts b/client/src/global.d.ts
index 1410365a16..ecc4cd2512 100644
--- a/client/src/global.d.ts
+++ b/client/src/global.d.ts
@@ -1,6 +1,7 @@
export {};
import Ui from 'ui';
+import Utils from 'utils';
/**
* A callback with resolved dependencies passed as parameters.
@@ -22,5 +23,8 @@ declare global {
// noinspection ES6ConvertVarToLetConst
var Ui: Ui
+
+ // noinspection ES6ConvertVarToLetConst
+ var Utils: Utils
}
}
diff --git a/client/src/ui.ts b/client/src/ui.ts
index efc1ae5033..2d2c8d6a16 100644
--- a/client/src/ui.ts
+++ b/client/src/ui.ts
@@ -54,6 +54,7 @@ interface DialogParams {
keyboard?: boolean;
footerAtTheTop?: boolean;
buttonList?: DialogButton[];
+ dropdownItemList?: (DialogButton | false)[];
fullHeight?: boolean;
bodyDiffHeight?: number;
screenWidthXs?: number;
@@ -62,6 +63,10 @@ interface DialogParams {
onMinimize?: () => void;
backdropClassName?: string;
fixedHeaderHeight?: boolean;
+ /**
+ * @internal
+ */
+ fitHeight?: boolean;
}
/**
diff --git a/client/src/util/util.d.ts b/client/src/util/util.d.ts
deleted file mode 100644
index 1d185d04f3..0000000000
--- a/client/src/util/util.d.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-/************************************************************************
- * This file is part of EspoCRM.
- *
- * EspoCRM – Open Source CRM application.
- * Copyright (C) 2014-2026 EspoCRM, Inc.
- * Website: https://www.espocrm.com
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- * The interactive user interfaces in modified source and object code versions
- * of this program must display Appropriate Legal Notices, as required under
- * Section 5 of the GNU Affero General Public License version 3.
- *
- * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
- * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
- ************************************************************************/
-
-export interface AccessDefs {
- /**
- * An ACL action to check.
- */
- action: 'create' | 'read' | 'edit' | 'stream' | 'delete' | null;
- /**
- * A scope to check.
- */
- scope?: string | null;
- /**
- * A portal ID list. To check whether a user in one of portals.
- */
- portalIdList?: string[];
- /**
- * A team ID list. To check whether a user in one of teams.
- */
- teamIdList?: string[];
- /**
- * Allow for portal users only.
- */
- isPortalOnly?: boolean;
- /**
- * Disable for portal users.
- */
- inPortalDisabled?: boolean;
- /**
- * Allow for admin users only.
- */
- isAdminOnly?: boolean;
-}
diff --git a/client/src/utils.js b/client/src/utils.ts
similarity index 55%
rename from client/src/utils.js
rename to client/src/utils.ts
index f8a439e6b2..9d384e3656 100644
--- a/client/src/utils.js
+++ b/client/src/utils.ts
@@ -26,45 +26,115 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
-/** @module utils */
+import type View from 'view';
+import type ViewHelper from 'view-helper';
+import type AclManager from 'acl-manager';
+import type Model from 'model';
+import type User from 'models/user';
+import _ from 'underscore';
+
+export interface AccessDefs {
+ /**
+ * An ACL action to check.
+ */
+ action: 'create' | 'read' | 'edit' | 'stream' | 'delete' | null;
+ /**
+ * A scope to check.
+ */
+ scope?: string | null;
+ /**
+ * A portal ID list. To check whether a user in one of portals.
+ */
+ portalIdList?: string[];
+ /**
+ * A team ID list. To check whether a user in one of teams.
+ */
+ teamIdList?: string[];
+ /**
+ * Allow for portal users only.
+ */
+ isPortalOnly?: boolean;
+ /**
+ * Disable for portal users.
+ */
+ inPortalDisabled?: boolean;
+ /**
+ * Allow for admin users only.
+ */
+ isAdminOnly?: boolean;
+}
+
+interface ActionAvailabilityDefs {
+ /**
+ * A config path to check. Path items are separated
+ * by the dot. If a config value is not empty, then the action is allowed.
+ * The `!` prefix reverses the check.
+ */
+ configCheck?: string | null;
+}
+
+interface ActionAccessDefs {
+ /**
+ * An ACL action to check.
+ */
+ acl?: 'create' | 'read' | 'edit' | 'stream' | 'delete' | null;
+ /**
+ * A scope to check.
+ */
+ aclScope?: string | null;
+ /**
+ * @deprecated Use `aclScope`.
+ */
+ scope?: string | null;
+}
const IS_MAC = /Mac/.test(navigator.userAgent);
/**
* Utility functions.
*/
-Espo.Utils = {
+const Utils = {
/**
* Handle a click event action.
*
- * @param {import('view').default} view A view.
- * @param {MouseEvent} event An event.
- * @param {HTMLElement} element An element.
- * @param {{
- * action?: string,
- * handler?: string,
- * actionFunction?: string,
- * actionItems?: Array<{
- * onClick?: function(),
- * name?: string | null,
- * handler?: string,
- * actionFunction?: string,
- * } | false>,
- * className?: string,
- * }} [actionData] Data. If an action is not specified, it will be fetched from a target element.
- * @return {boolean} True if handled.
- */
- handleAction: function (view, event, element, actionData) {
+ * @param view A view.
+ * @param event An event.
+ * @param element An element.
+ * @param actionData Data. If an action is not specified, it will be fetched from a target element.
+ * @return True if handled.
+ */
+ handleAction: function (
+ view: View,
+ event: MouseEvent,
+ element: HTMLElement,
+ actionData?: {
+ action?: string;
+ handler?: string;
+ actionFunction?: string;
+ actionItems?: ({
+ onClick?: () => void;
+ name?: string | null;
+ handler?: string;
+ actionFunction?: string;
+ /**
+ * @internal
+ */
+ action?: string | null;
+ } | false)[];
+ className?: string;
+ },
+ ): boolean {
+
actionData = actionData || {};
const $target = $(element);
- const action = actionData.action || $target.data('action');
- const name = $target.data('name') || action;
+ const action = actionData.action || element.dataset.action || null;
+ const name = element.dataset.name || action;
- let method;
- let handler;
+ let method: string | null = null;
+ let handler: string | null = null;
if (
name &&
@@ -75,7 +145,7 @@ Espo.Utils = {
)
) {
const data = actionData.actionItems.find(item => {
- return item.name === name || item.action === name;
+ return item && (item.name === name || item.action === name);
});
if (data && data.onClick) {
@@ -85,8 +155,8 @@ Espo.Utils = {
}
if (data) {
- handler = data.handler;
- method = data.actionFunction;
+ handler = data.handler ?? null;
+ method = data.actionFunction ?? null;
}
}
@@ -95,7 +165,7 @@ Espo.Utils = {
}
if (event.ctrlKey || event.metaKey || event.shiftKey) {
- const href = $target.attr('href');
+ const href = element.getAttribute('href');
if (href && href !== 'javascript:') {
return false;
@@ -103,8 +173,18 @@ Espo.Utils = {
}
const data = $target.data();
- method = actionData.actionFunction || method || 'action' + Espo.Utils.upperCaseFirst(action);
- handler = actionData.handler || handler || data.handler;
+
+ method = actionData.actionFunction ?? method ?? null;
+
+ if (!method) {
+ if (!action) {
+ throw new Error(`No action.`);
+ }
+
+ method = 'action' + Utils.upperCaseFirst(action);
+ }
+
+ handler = actionData.handler ?? handler ?? data.handler ?? null;
let fired = false;
@@ -119,14 +199,18 @@ Espo.Utils = {
handler[method].call(handler, data, event);
});
- }
- else if (typeof view[method] === 'function') {
+ } else if (
+ // @ts-ignore
+ typeof view[method] === 'function'
+ ) {
+ // @ts-ignore
if (view?.events[`click [data-action="${action}"]`]) {
// Prevents from firing if a handler is already assigned. Important.
// Does not prevent if handled from a nested view. @todo
return false;
}
+ // @ts-ignore
view[method].call(view, data, event);
event.preventDefault();
@@ -139,62 +223,18 @@ Espo.Utils = {
return false;
}
- this._processAfterActionDropdown($target);
+ _processAfterActionDropdown($target);
return true;
},
- /**
- * @private
- * @param {JQuery} $target
- */
- _processAfterActionDropdown: function ($target) {
- const $dropdown = $target.closest('.dropdown-menu');
-
- if (!$dropdown.length) {
- return;
- }
-
- const $dropdownToggle = $dropdown.parent().find('[data-toggle="dropdown"]');
-
- if (!$dropdownToggle.length) {
- return;
- }
-
- let isDisabled = false;
-
- if ($dropdownToggle.attr('disabled')) {
- isDisabled = true;
-
- $dropdownToggle.removeAttr('disabled').removeClass('disabled');
- }
-
- // noinspection JSUnresolvedReference
- $dropdownToggle.dropdown('toggle');
-
- $dropdownToggle.focus();
-
- if (isDisabled) {
- $dropdownToggle.attr('disabled', 'disabled').addClass('disabled');
- }
- },
-
- /**
- * @typedef {Object} Espo.Utils~ActionAvailabilityDefs
- *
- * @property {string|null} [configCheck] A config path to check. Path items are separated
- * by the dot. If a config value is not empty, then the action is allowed.
- * The `!` prefix reverses the check.
- */
-
/**
* Check action availability.
*
- * @param {module:view-helper} helper A view helper.
- * @param {Espo.Utils~ActionAvailabilityDefs} item Definitions.
- * @returns {boolean}
+ * @param helper A view helper.
+ * @param item Definitions.
*/
- checkActionAvailability: function (helper, item) {
+ checkActionAvailability: function (helper: ViewHelper, item: ActionAvailabilityDefs): boolean {
const config = helper.config;
if (item.configCheck) {
@@ -222,67 +262,62 @@ Espo.Utils = {
return true;
},
- /**
- * @typedef {Object} Espo.Utils~ActionAccessDefs
- *
- * @property {'create'|'read'|'edit'|'stream'|'delete'|null} acl An ACL action to check.
- * @property {string|null} [aclScope] A scope to check.
- * @property {string|null} [scope] Deprecated. Use `aclScope`.
- */
-
/**
* Check access to an action.
*
- * @param {module:acl-manager} acl An ACL manager.
- * @param {string|module:model|null} [obj] A scope or a model.
- * @param {Espo.Utils~ActionAccessDefs} item Definitions.
- * @param {boolean} [isPrecise=false] To return `null` if not enough data is set in a model.
+ * @param acl An ACL manager.
+ * @param [obj] A scope or a model.
+ * @param item Definitions.
+ * @param[isPrecise=false] To return `null` if not enough data is set in a model.
* E.g. the `teams` field is not yet loaded.
- * @returns {boolean|null}
*/
- checkActionAccess: function (acl, obj, item, isPrecise) {
- let hasAccess = true;
+ checkActionAccess: function (
+ acl: AclManager,
+ obj: string | Model | null,
+ item: ActionAccessDefs,
+ isPrecise: boolean = false,
+ ): boolean | null {
+
+ let hasAccess: boolean | null = true;
if (item.acl) {
if (!item.aclScope) {
if (obj) {
if (typeof obj === 'string' || obj instanceof String) {
hasAccess = acl.check(obj, item.acl);
- }
- else {
+ } else {
hasAccess = acl.checkModel(obj, item.acl, isPrecise);
}
+ } else {
+ hasAccess = acl.check((item as any).scope, item.acl);
}
- else {
- hasAccess = acl.check(item.scope, item.acl);
- }
- }
- else {
+ } else {
hasAccess = acl.check(item.aclScope, item.acl);
}
- }
- else if (item.aclScope) {
+ } else if (item.aclScope) {
hasAccess = acl.checkScope(item.aclScope);
}
return hasAccess;
},
- /**
- * @typedef {import('util/util.d').AccessDefs} Espo.Utils~AccessDefs
- */
-
/**
* Check access to an action.
*
- * @param {module:utils~AccessDefs[]} dataList List of definitions.
- * @param {module:acl-manager} acl An ACL manager.
- * @param {module:models/user} user A user.
- * @param {module:model|null} [entity] A model.
- * @param {boolean} [allowAllForAdmin=false] Allow all for an admin.
- * @returns {boolean}
- */
- checkAccessDataList: function (dataList, acl, user, entity, allowAllForAdmin) {
+ * @param dataList List of definitions.
+ * @param acl An ACL manager.
+ * @param user A user.
+ * @param [entity] A model.
+ * @param [allowAllForAdmin=false] Allow all for an admin.
+ */
+ checkAccessDataList: function (
+ dataList: AccessDefs[],
+ acl: AclManager,
+ user: User,
+ entity: Model | null = null,
+ allowAllForAdmin: boolean = false,
+ ): boolean {
+
if (!dataList || !dataList.length) {
return true;
}
@@ -295,32 +330,24 @@ Espo.Utils = {
if (!acl.check(item.scope, item.action)) {
return false;
}
- } else {
- if (!acl.checkScope(item.scope)) {
- return false;
- }
- }
- } else if (item.action) {
- if (entity) {
- if (!acl.check(entity, item.action)) {
- return false;
- }
+ } else if (!acl.checkScope(item.scope)) {
+ return false;
}
+ } else if (item.action && entity && !acl.check(entity, item.action)) {
+ return false;
}
- if (item.teamIdList) {
- if (user && !(allowAllForAdmin && user.isAdmin())) {
- let inTeam = false;
+ if (item.teamIdList && user && !(allowAllForAdmin && user.isAdmin())) {
+ let inTeam = false;
- user.getLinkMultipleIdList('teams').forEach(teamId => {
- if (~item.teamIdList.indexOf(teamId)) {
- inTeam = true;
- }
- });
-
- if (!inTeam) {
- return false;
+ user.getLinkMultipleIdList('teams').forEach(teamId => {
+ if (item.teamIdList?.indexOf(teamId)) {
+ inTeam = true;
}
+ });
+
+ if (!inTeam) {
+ return false;
}
}
@@ -329,7 +356,7 @@ Espo.Utils = {
let inPortal = false;
user.getLinkMultipleIdList('portals').forEach(portalId => {
- if (~item.portalIdList.indexOf(portalId)) {
+ if (item.portalIdList?.includes(portalId)) {
inPortal = true;
}
});
@@ -368,12 +395,9 @@ Espo.Utils = {
},
/**
- * @private
- * @param {string} string
- * @param {string} p
- * @returns {string}
+ * @internal
*/
- convert: function (string, p) {
+ convert: function (string: string, p: string): string {
if (string === null) {
return string;
}
@@ -383,17 +407,17 @@ Espo.Utils = {
switch (p) {
case 'c-h':
case 'C-h':
- result = Espo.Utils.camelCaseToHyphen(string);
+ result = Utils.camelCaseToHyphen(string);
break;
case 'h-c':
- result = Espo.Utils.hyphenToCamelCase(string);
+ result = Utils.hyphenToCamelCase(string);
break;
case 'h-C':
- result = Espo.Utils.hyphenToUpperCamelCase(string);
+ result = Utils.hyphenToUpperCamelCase(string);
break;
}
@@ -404,10 +428,9 @@ Espo.Utils = {
/**
* Is object.
*
- * @param {*} obj What to check.
- * @returns {boolean}
+ * @param obj What to check.
*/
- isObject: function (obj) {
+ isObject: function (obj: unknown): boolean {
if (obj === null) {
return false;
}
@@ -418,29 +441,27 @@ Espo.Utils = {
/**
* A shallow clone.
*
- * @template {*} TObject
- * @param {TObject} obj An object.
- * @returns {TObject}
+ * @param obj What to clone.
+ * @returns A clone.
*/
- clone: function (obj) {
- if (!Espo.Utils.isObject(obj)) {
+ clone: function<T> (obj: T): T {
+ if (!Utils.isObject(obj)) {
return obj;
}
- return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
+ return (_.isArray(obj) ? obj.slice() : _.extend({}, obj)) as T;
},
/**
* A deep clone.
*
- * @template {*} TObject
- * @param {TObject} data An object.
- * @returns {TObject}
+ * @param data What to clone.
+ * @return A clone.
*/
- cloneDeep: function (data) {
- data = Espo.Utils.clone(data);
+ cloneDeep: function<T> (data: T): T {
+ data = Utils.clone(data);
- if (Espo.Utils.isObject(data) || _.isArray(data)) {
+ if (Utils.isObject(data) || _.isArray(data)) {
for (const i in data) {
data[i] = this.cloneDeep(data[i]);
}
@@ -452,23 +473,22 @@ Espo.Utils = {
/**
* Deep comparison.
*
- * @param {Object} a1 An argument 1.
- * @param {Object} a2 An argument 2.
- * @return {boolean}
+ * @param a1 An argument 1.
+ * @param a2 An argument 2.
*/
- areEqual: function (a1, a2) {
+ areEqual: function (a1: object, a2: object): boolean {
return _.isEqual(a1, a2);
},
/**
* Compose a class name.
*
- * @param {string} module A module.
- * @param {string} name A name.
- * @param {string} [location=''] A location.
- * @return {string}
+ * @param module A module.
+ * @param name A name.
+ * @param location A location.
+ * @return
*/
- composeClassName: function (module, name, location) {
+ composeClassName: function (module: string, name: string, location: string = ''): string {
if (module) {
module = this.camelCaseToHyphen(module);
name = this.camelCaseToHyphen(name).split('.').join('/');
@@ -476,20 +496,18 @@ Espo.Utils = {
return module + ':' + location + '/' + name;
}
- else {
- name = this.camelCaseToHyphen(name).split('.').join('/');
- return location + '/' + name;
- }
+ name = this.camelCaseToHyphen(name).split('.').join('/');
+
+ return location + '/' + name;
},
/**
* Compose a view class name.
*
- * @param {string} name A name.
- * @returns {string}
+ * @param name A name.
*/
- composeViewClassName: function (name) {
+ composeViewClassName: function (name: string): string {
if (name && name[0] === name[0].toLowerCase()) {
return name;
}
@@ -502,24 +520,22 @@ Espo.Utils = {
modPart = this.camelCaseToHyphen(modPart);
namePart = this.camelCaseToHyphen(namePart).split('.').join('/');
- return modPart + ':' + 'views' + '/' + namePart;
+ return `${modPart}:views/${namePart}`;
}
- else {
- name = this.camelCaseToHyphen(name).split('.').join('/');
- return 'views' + '/' + name;
- }
+ name = this.camelCaseToHyphen(name).split('.').join('/');
+
+ return `views/${name}`;
},
/**
* Convert a string from camelCase to hyphen and replace dots with hyphens.
* Useful for setting to DOM attributes.
*
- * @param {string} string A string.
- * @returns {string}
+ * @param string A string.
*/
- toDom: function (string) {
- return Espo.Utils.convert(string, 'c-h')
+ toDom: function (string: string): string {
+ return Utils.convert(string, 'c-h')
.split('.')
.join('-');
},
@@ -527,10 +543,9 @@ Espo.Utils = {
/**
* Lower-case a first character.
*
- * @param {string} string A string.
- * @returns {string}
+ * @param string A string.
*/
- lowerCaseFirst: function (string) {
+ lowerCaseFirst: function (string: string): string {
if (string === null) {
return string;
}
@@ -541,10 +556,9 @@ Espo.Utils = {
/**
* Upper-case a first character.
*
- * @param {string} string A string.
- * @returns {string}
+ * @param string A string.
*/
- upperCaseFirst: function (string) {
+ upperCaseFirst: function (string: string): string {
if (string === null) {
return string;
}
@@ -555,10 +569,9 @@ Espo.Utils = {
/**
* Hyphen to UpperCamelCase.
*
- * @param {string} string A string.
- * @returns {string}
+ * @param string A string.
*/
- hyphenToUpperCamelCase: function (string) {
+ hyphenToUpperCamelCase: function (string: string): string {
if (string === null) {
return string;
}
@@ -576,10 +589,9 @@ Espo.Utils = {
/**
* Hyphen to camelCase.
*
- * @param {string} string A string.
- * @returns {string}
+ * @param string A string.
*/
- hyphenToCamelCase: function (string) {
+ hyphenToCamelCase: function (string: string): string {
if (string === null) {
return string;
}
@@ -595,10 +607,9 @@ Espo.Utils = {
/**
* CamelCase to hyphen.
*
- * @param {string} string A string.
- * @returns {string}
+ * @param string A string.
*/
- camelCaseToHyphen: function (string) {
+ camelCaseToHyphen: function (string: string) {
if (string === null) {
return string;
}
@@ -609,24 +620,22 @@ Espo.Utils = {
/**
* Trim an ending slash.
*
- * @param {String} str A string.
- * @returns {string}
+ * @param string A string.
*/
- trimSlash: function (str) {
- if (str.slice(-1) === '/') {
- return str.slice(0, -1);
+ trimSlash: function (string: string): string {
+ if (string.slice(-1) === '/') {
+ return string.slice(0, -1);
}
- return str;
+ return string;
},
/**
* Parse params in string URL options.
*
- * @param {string} string An URL part.
- * @returns {Object.<string,string>}
+ * @param string An URL part.
*/
- parseUrlOptionsParam: function (string) {
+ parseUrlOptionsParam: function (string: string): Record<string, string | boolean> {
if (!string) {
return {};
}
@@ -635,7 +644,7 @@ Espo.Utils = {
return {};
}
- const options = {};
+ const options = {} as Record<string, string | boolean>;
if (typeof string !== 'undefined') {
string.split('&').forEach(item => {
@@ -655,23 +664,23 @@ Espo.Utils = {
/**
* Key a key from a key-event.
*
- * @param {JQueryKeyEventObject|KeyboardEvent} e A key event.
+ * @param event A key event.
* @return {string}
*/
- getKeyFromKeyEvent: function (e) {
- let key = e.code;
+ getKeyFromKeyEvent: function (event: KeyboardEvent | JQuery.Event): string {
+ let key: string = (event as any).code;
- key = keyMap[key] || key;
+ key = (keyMap as Record<string, string>)[key] ?? key;
- if (e.shiftKey) {
+ if (event.shiftKey) {
key = 'Shift+' + key;
}
- if (e.altKey) {
+ if (event.altKey) {
key = 'Alt+' + key;
}
- if (IS_MAC ? e.metaKey : e.ctrlKey) {
+ if (IS_MAC ? event.metaKey : event.ctrlKey) {
key = 'Control+' + key;
}
@@ -681,23 +690,22 @@ Espo.Utils = {
/**
* Check whether the pressed key is in a text input.
*
- * @param {KeyboardEvent} e A key event.
- * @return {boolean}
+ * @param event A key event.
* @since 9.2.0
*/
- isKeyEventInTextInput: function (e) {
- if (!(e.target instanceof HTMLElement)) {
+ isKeyEventInTextInput: function (event: KeyboardEvent): boolean {
+ if (!(event.target instanceof HTMLElement)) {
return false;
}
- if (e.target.tagName === 'TEXTAREA') {
+ if (event.target.tagName === 'TEXTAREA') {
return true;
}
- if (e.target instanceof HTMLInputElement) {
+ if (event.target instanceof HTMLInputElement) {
if (
- e.target.type === 'radio' ||
- e.target.type === 'checkbox'
+ event.target.type === 'radio' ||
+ event.target.type === 'checkbox'
) {
return false;
}
@@ -705,7 +713,7 @@ Espo.Utils = {
return true;
}
- if (e.target.classList.contains('note-editable')) {
+ if (event.target.classList.contains('note-editable')) {
return true;
}
@@ -716,18 +724,17 @@ Espo.Utils = {
* Generate an ID. Not to be used by 3rd party code.
*
* @internal
- * @return {string}
*/
- generateId: function () {
+ generateId: function (): string {
return (Math.floor(Math.random() * 10000001)).toString()
},
/**
* Not to be used in custom code. Can be removed in future versions.
+ *
* @internal
- * @return {string}
*/
- obtainBaseUrl: function () {
+ obtainBaseUrl: function (): string {
let baseUrl = window.location.origin + window.location.pathname;
if (baseUrl.slice(-1) !== '/') {
@@ -744,9 +751,38 @@ const keyMap = {
'NumpadEnter': 'Enter',
};
-/**
- * @deprecated Use `Espo.Utils`.
- */
-Espo.utils = Espo.Utils;
+Espo.Utils = Utils;
+
+export default Utils;
-export default Espo.Utils;
+function _processAfterActionDropdown($target: JQuery) {
+ const $dropdown = $target.closest('.dropdown-menu');
+
+ if (!$dropdown.length) {
+ return;
+ }
+
+ const $dropdownToggle = $dropdown.parent().find('[data-toggle="dropdown"]');
+
+ if (!$dropdownToggle.length) {
+ return;
+ }
+
+ let isDisabled = false;
+
+ if ($dropdownToggle.attr('disabled')) {
+ isDisabled = true;
+
+ $dropdownToggle.removeAttr('disabled').removeClass('disabled');
+ }
+
+ // noinspection JSUnresolvedReference
+ // @ts-ignore
+ $dropdownToggle.dropdown('toggle');
+
+ $dropdownToggle.trigger('focus');
+
+ if (isDisabled) {
+ $dropdownToggle.attr('disabled', 'disabled').addClass('disabled');
+ }
+}
diff --git a/client/src/view-helper.js b/client/src/view-helper.js
index 63dada36bc..073af92642 100644
--- a/client/src/view-helper.js
+++ b/client/src/view-helper.js
@@ -233,14 +233,14 @@ class ViewHelper {
/**
* A router.
*
- * @type {module:router}
+ * @type {import('router').default}
*/
router = null
/**
* A storage.
*
- * @type {module:storage}
+ * @type {import('storage').default}
*/
storage = null
@@ -993,7 +993,7 @@ class ViewHelper {
/**
* Process view-setup-handlers.
*
- * @param {module:view} view A view.
+ * @param {import('view').default<any>} view A view.
* @param {string} type A view-setup-handler type.
* @param {string} [scope] A scope.
* @return Promise
@@ -1023,7 +1023,7 @@ class ViewHelper {
/**
* @function
* @name ViewHelper~Handler#process
- * @param {module:view} [view] Deprecated.
+ * @param {import('view').default} [view] Deprecated.
*/
const promiseList = [];
diff --git a/client/src/views/fields/array.ts b/client/src/views/fields/array.ts
index 05fe6e39ec..36d4f4d6a0 100644
--- a/client/src/views/fields/array.ts
+++ b/client/src/views/fields/array.ts
@@ -41,6 +41,7 @@ import EditForModalRecordView from 'views/record/edit-for-modal';
import VarcharFieldView from 'views/fields/varchar';
import _ from 'underscore';
import JQuery from 'jquery'
+import Utils from 'utils';
const $ = JQuery;
@@ -239,10 +240,10 @@ class ArrayFieldView<
this.maxItemLength = this.maxItemLength || this.MAX_ITEM_LENGTH;
this.listenTo(this.model, 'change:' + this.name, () => {
- this.selected = Espo.Utils.clone(this.model.get(this.name)) || [];
+ this.selected = Utils.clone(this.model.get(this.name)) || [];
});
- this.selected = Espo.Utils.clone(this.model.get(this.name) || []);
+ this.selected = Utils.clone(this.model.get(this.name) || []);
if (Object.prototype.toString.call(this.selected) !== '[object Array]') {
this.selected = [];
@@ -265,7 +266,7 @@ class ArrayFieldView<
}
if (optionsPath) {
- this.params.options = Espo.Utils.clone(this.getMetadata().get(optionsPath)) || [];
+ this.params.options = Utils.clone(this.getMetadata().get(optionsPath)) || [];
}
this.setupOptions();
@@ -412,7 +413,7 @@ class ArrayFieldView<
this.originalOptionList = this.params.options ?? [];
}
- this.params.options = Espo.Utils.clone(optionList);
+ this.params.options = Utils.clone(optionList);
const isChanged = !_(previousOptions).isEqual(optionList);
@@ -451,7 +452,7 @@ class ArrayFieldView<
const previousOptions = this.params.options;
- this.params.options = Espo.Utils.clone(this.originalOptionList);
+ this.params.options = Utils.clone(this.originalOptionList);
const isChanged = !_(previousOptions).isEqual(this.originalOptionList);
@@ -504,7 +505,7 @@ class ArrayFieldView<
$select.on('input', () => this.controlAddItemButton());
$select.on('keydown', (e: any) => {
- const key = Espo.Utils.getKeyFromKeyEvent(e);
+ const key = Utils.getKeyFromKeyEvent(e);
if (key === 'Enter') {
const value = $select.val().toString();
@@ -844,7 +845,7 @@ class ArrayFieldView<
fetch(): Record<string, any> {
const data = {} as Record<string, any>;
- let list = Espo.Utils.clone(this.selected || []);
+ let list = Utils.clone(this.selected || []);
const translations = this.translatedOptions;
diff --git a/client/src/views/fields/enum.ts b/client/src/views/fields/enum.ts
index 70388c96ac..a4dfb9bdce 100644
--- a/client/src/views/fields/enum.ts
+++ b/client/src/views/fields/enum.ts
@@ -32,6 +32,7 @@ import BaseFieldView, {BaseOptions as BaseOptions, BaseParams as BaseParams, Bas
import MultiSelect from 'ui/multi-select';
import Select from 'ui/select'
import _ from 'underscore';
+import Utils from 'utils';
type OptionItemHandler = (item: {value: string}) => {
text?: string,
@@ -217,7 +218,7 @@ class EnumFieldView<
protected setup() {
if (!this.params.options) {
// @todo Revise.
- const methodName = 'get' + Espo.Utils.upperCaseFirst(this.name) + 'Options';
+ const methodName = 'get' + Utils.upperCaseFirst(this.name) + 'Options';
if (typeof (this.model as any)[methodName] === 'function') {
this.params.options = (((this.model as any)[methodName] as any).call(this.model)) as string[];
@@ -244,7 +245,7 @@ class EnumFieldView<
}
if (optionsPath) {
- this.params.options = Espo.Utils.clone(this.getMetadata().get(optionsPath)) || [];
+ this.params.options = Utils.clone(this.getMetadata().get(optionsPath)) || [];
}
this.setupOptions();
@@ -272,7 +273,7 @@ class EnumFieldView<
if (this.params.isSorted && this.translatedOptions) {
const translations = this.translatedOptions;
- this.params.options = Espo.Utils.clone(this.params.options) || [];
+ this.params.options = Utils.clone(this.params.options) ?? [];
this.params.options = this.params.options.sort((v1, v2) => {
return (translations[v1] || v1)
@@ -367,7 +368,7 @@ class EnumFieldView<
this.originalOptionList = this.params.options ?? null
}
- const newOptions = Espo.Utils.clone(optionList) || [];
+ const newOptions = Utils.clone(optionList) || [];
this.params.options = newOptions;
@@ -403,7 +404,7 @@ class EnumFieldView<
const previousOptions = this.params.options;
- this.params.options = Espo.Utils.clone(this.originalOptionList);
+ this.params.options = Utils.clone(this.originalOptionList);
const isChanged = !_(previousOptions).isEqual(this.originalOptionList);
diff --git a/client/src/views/main.ts b/client/src/views/main.ts
index c852237851..ca46549153 100644
--- a/client/src/views/main.ts
+++ b/client/src/views/main.ts
@@ -31,7 +31,8 @@ import {inject} from 'di';
import ShortcutManager from 'helpers/site/shortcut-manager';
import type Model from 'model';
import type Collection from 'collection';
-import type {AccessDefs} from 'util/util';
+import type {AccessDefs} from 'utils';
+import Utils from 'utils';
/**
* A top-right menu item (button or dropdown action).
@@ -234,19 +235,19 @@ class MainView<S extends MainViewSchema = MainViewSchema> extends View<S> {
this.options.params = this.options.params ?? {};
if (this.name && this.scope) {
- const key = `clientDefs.${this.scope}.menu.${Espo.Utils.lowerCaseFirst(this.name)}`;
+ const key = `clientDefs.${this.scope}.menu.${Utils.lowerCaseFirst(this.name)}`;
menu = this.getMetadata().get(key) ?? {};
}
- menu = Espo.Utils.cloneDeep(menu);
+ menu = Utils.cloneDeep(menu);
let globalMenu = {} as any;
if (this.name) {
- const key = `clientDefs.Global.menu.${Espo.Utils.lowerCaseFirst(this.name)}`;
+ const key = `clientDefs.Global.menu.${Utils.lowerCaseFirst(this.name)}`;
- globalMenu = Espo.Utils.cloneDeep(this.getMetadata().get(key) ?? {});
+ globalMenu = Utils.cloneDeep(this.getMetadata().get(key) ?? {});
}
this._reRenderHeaderOnSync = false;
@@ -333,11 +334,11 @@ class MainView<S extends MainViewSchema = MainViewSchema> extends View<S> {
this.on('after:render', () => this.adjustButtons());
if (this.shortcutKeys) {
- this.shortcutKeys = Espo.Utils.cloneDeep(this.shortcutKeys);
+ this.shortcutKeys = Utils.cloneDeep(this.shortcutKeys);
}
this.addHandler('click', '.action', (event, target) => {
- Espo.Utils.handleAction(this, event as MouseEvent, target, {
+ Utils.handleAction(this, event as MouseEvent, target, {
actionItems: [...this.menu.buttons, ...this.menu.dropdown],
className: 'main-header-manu-action',
});
@@ -389,22 +390,20 @@ class MainView<S extends MainViewSchema = MainViewSchema> extends View<S> {
return;
}
- item = Espo.Utils.clone(item);
+ item = Utils.clone(item);
menu[type] = menu[type] || [];
- if (!Espo.Utils.checkActionAvailability(this.getHelper(), item)) {
+ if (!Utils.checkActionAvailability(this.getHelper(), item)) {
return;
}
- if (!Espo.Utils.checkActionAccess(this.getAcl(), this.model || this.scope, item)) {
+ if (!Utils.checkActionAccess(this.getAcl(), this.model || this.scope, item)) {
return;
}
if (item.accessDataList) {
- if (!Espo.Utils
- .checkAccessDataList(item.accessDataList, this.getAcl(), this.getUser())
- ) {
+ if (!Utils.checkAccessDataList(item.accessDataList, this.getAcl(), this.getUser())) {
return;
}
}
@@ -576,7 +575,7 @@ class MainView<S extends MainViewSchema = MainViewSchema> extends View<S> {
const list = this.menu[type];
if (item) {
- item.name = item.name ?? item.action ?? Espo.Utils.generateId();
+ item.name = item.name ?? item.action ?? Utils.generateId();
const name = item.name;
const index = list.findIndex(it => (it || {}).name === name);
diff --git a/client/src/views/modal.ts b/client/src/views/modal.ts
index e369784d5c..dc58de524f 100644
--- a/client/src/views/modal.ts
+++ b/client/src/views/modal.ts
@@ -34,8 +34,10 @@ import ModalBarProvider from 'helpers/site/modal-bar-provider';
import ShortcutManager from 'helpers/site/shortcut-manager';
import Model from 'model';
import Collection from 'collection';
-import {Dialog, DialogButton} from 'ui';
+import type {Dialog, DialogButton} from 'ui';
import JQuery from 'jquery';
+import Ui from 'ui';
+import Utils from 'utils';
const $ = JQuery;
@@ -306,7 +308,7 @@ class ModalView<S extends ViewSchema = ViewSchema> extends View<S> {
protected init() {
this.addHandler('click', '.action', (e, target) => {
- Espo.Utils.handleAction(this, e as MouseEvent, target);
+ Utils.handleAction(this, e as MouseEvent, target);
});
const id = this.cssName + '-container-' + Math.floor((Math.random() * 10000) + 1).toString();
@@ -324,11 +326,11 @@ class ModalView<S extends ViewSchema = ViewSchema> extends View<S> {
this.buttonList = this.options.buttonList || this.buttonList;
this.dropdownItemList = this.options.dropdownItemList || this.dropdownItemList;
- this.buttonList = Espo.Utils.cloneDeep(this.buttonList);
- this.dropdownItemList = Espo.Utils.cloneDeep(this.dropdownItemList);
+ this.buttonList = Utils.cloneDeep(this.buttonList);
+ this.dropdownItemList = Utils.cloneDeep(this.dropdownItemList);
if (this.shortcutKeys) {
- this.shortcutKeys = Espo.Utils.cloneDeep(this.shortcutKeys);
+ this.shortcutKeys = Utils.cloneDeep(this.shortcutKeys);
}
if (this.options.collapseDisabled) {
@@ -376,7 +378,7 @@ class ModalView<S extends ViewSchema = ViewSchema> extends View<S> {
const footerAtTheTop = (this.footerAtTheTop !== null) ? this.footerAtTheTop :
this.getThemeManager().getParam('modalFooterAtTheTop');
- this.dialog = new Espo.Ui.Dialog({
+ this.dialog = new Ui.Dialog({
backdrop: this.backdrop,
header: headerHtml,
container: this.containerSelector,
@@ -502,7 +504,7 @@ class ModalView<S extends ViewSchema = ViewSchema> extends View<S> {
o.onClick = o.onClick ?? ((_d, event, target) => {
const handler = o.handler || (o.data || {}).handler;
- Espo.Utils.handleAction(this, event, target, {
+ Utils.handleAction(this, event, target, {
action: o.name,
handler: handler,
actionFunction: o.actionFunction,
@@ -546,7 +548,7 @@ class ModalView<S extends ViewSchema = ViewSchema> extends View<S> {
// noinspection ES6ConvertLetToConst
let handler = o.handler || (o.data || {}).handler;
- Espo.Utils.handleAction(this, e.originalEvent, e.currentTarget, {
+ Utils.handleAction(this, e.originalEvent, e.currentTarget, {
action: o.name,
handler: handler,
actionFunction: o.actionFunction,