diff --git a/src/js/page/loader.mjs b/src/js/page/loader.mjs index 4b39f029..c60bf42d 100644 --- a/src/js/page/loader.mjs +++ b/src/js/page/loader.mjs @@ -19,7 +19,7 @@ function getTerminalObj() { return terminal; } - const load_tests = window.__OdooTerminal.load_tests || navigator.webdriver; + const load_tests = window.__OdooTerminal?.load_tests || navigator.webdriver; if (load_tests) { console.info('[OdooTerminal] Tests Enabled'); } diff --git a/src/js/page/odoo/base/do_action.mjs b/src/js/page/odoo/base/do_action.mjs index 521eb413..6ba6fbc2 100644 --- a/src/js/page/odoo/base/do_action.mjs +++ b/src/js/page/odoo/base/do_action.mjs @@ -3,11 +3,14 @@ import asyncSleep from '@terminal/utils/async_sleep'; import getOdooVersionMajor from '@odoo/utils/get_odoo_version_major'; +import getOdooRoot from '@odoo/utils/get_odoo_root'; import doTrigger from './do_trigger'; export default async function (action, options) { const OdooVer = getOdooVersionMajor(); - if (OdooVer >= 14) { + if (OdooVer >= 17) { + return getOdooRoot().actionService.doAction(action, options); + } else if (OdooVer >= 14) { doTrigger('do-action', {action, options}); // Simulate end of the 'action' // FIXME: This makes me cry diff --git a/src/js/page/odoo/commands/backoffice/view.mjs b/src/js/page/odoo/commands/backoffice/view.mjs index d9441bbe..afc5d31f 100644 --- a/src/js/page/odoo/commands/backoffice/view.mjs +++ b/src/js/page/odoo/commands/backoffice/view.mjs @@ -65,7 +65,7 @@ async function cmdViewModelRecord(kwargs) { views: [[false, 'form']], target: 'current', context: context, - }); + }).then(() => this.doHide()); }, ); } diff --git a/src/js/page/odoo/utils/get_odoo_version.mjs b/src/js/page/odoo/utils/get_odoo_version.mjs index 1e8bf0bc..acd94dbe 100644 --- a/src/js/page/odoo/utils/get_odoo_version.mjs +++ b/src/js/page/odoo/utils/get_odoo_version.mjs @@ -6,6 +6,6 @@ import getOdooSession from './get_odoo_session'; export default function () { return ( getOdooSession()?.server_version || - __OdooTerminal.raw_server_info.serverVersionRaw + window.__OdooTerminal?.raw_server_info.serverVersionRaw ); } diff --git a/src/js/page/odoo/utils/get_odoo_version_info.mjs b/src/js/page/odoo/utils/get_odoo_version_info.mjs index c1d51604..67d6ea1e 100644 --- a/src/js/page/odoo/utils/get_odoo_version_info.mjs +++ b/src/js/page/odoo/utils/get_odoo_version_info.mjs @@ -6,6 +6,6 @@ import getOdooSession from './get_odoo_session'; export default function () { return ( getOdooSession()?.server_version_info || - __OdooTerminal.raw_server_info.serverVersionInfo + window.__OdooTerminal?.raw_server_info.serverVersionInfo ); } diff --git a/src/js/page/odoo/utils/is_backoffice.mjs b/src/js/page/odoo/utils/is_backoffice.mjs index 895b860b..b8e9e4c9 100644 --- a/src/js/page/odoo/utils/is_backoffice.mjs +++ b/src/js/page/odoo/utils/is_backoffice.mjs @@ -1,6 +1,18 @@ // Copyright Alexandre Díaz // License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import getOdooSession from './get_odoo_session'; + +let isBackoffice = null; export default function () { - return __OdooTerminal.raw_server_info.isBackOffice; + if (isBackoffice !== null) { + return isBackoffice; + } + if (typeof getOdooSession()?.is_frontend !== 'undefined') { + isBackoffice = !getOdooSession().is_frontend; + } else { + isBackoffice = + document.querySelector("head script[src*='assets_frontend']") === null; + } + return isBackoffice; } diff --git a/src/js/page/terminal/commands/dis.mjs b/src/js/page/terminal/commands/dis.mjs index ba60cc3c..a86efe89 100644 --- a/src/js/page/terminal/commands/dis.mjs +++ b/src/js/page/terminal/commands/dis.mjs @@ -27,8 +27,8 @@ async function cmdDis(kwargs, screen) { const humanType = INSTRUCTION_TYPE.getHumanType(instr.type); rows[row_index].push( - instr.type, humanType[0], + instr.type, lvalue, instr.dataIndex, instr.level, diff --git a/src/js/page/terminal/terminal.mjs b/src/js/page/terminal/terminal.mjs index 88b2881f..2e46c6b2 100644 --- a/src/js/page/terminal/terminal.mjs +++ b/src/js/page/terminal/terminal.mjs @@ -19,6 +19,7 @@ import renderUnknownCommand from './templates/unknown_command'; import renderWelcome from './templates/welcome'; import debounce from './utils/debounce'; import isEmpty from './utils/is_empty'; +import keyCode from './utils/keycode'; import ProcessJobError from './exceptions/process_job_error'; export default class Terminal { @@ -835,25 +836,25 @@ export default class Terminal { #onInputKeyUp(ev) { const question_active = this.screen.getQuestionActive(); if (isEmpty(question_active)) { - if (ev.keyCode === $.ui.keyCode.ENTER) { + if (ev.keyCode === keyCode.ENTER) { this.#onKeyEnter(ev); - } else if (ev.keyCode === $.ui.keyCode.UP) { + } else if (ev.keyCode === keyCode.UP) { this.#onKeyArrowUp(ev); - } else if (ev.keyCode === $.ui.keyCode.DOWN) { + } else if (ev.keyCode === keyCode.DOWN) { this.#onKeyArrowDown(ev); - } else if (ev.keyCode === $.ui.keyCode.RIGHT) { + } else if (ev.keyCode === keyCode.RIGHT) { this.#onKeyArrowRight(ev); - } else if (ev.keyCode === $.ui.keyCode.LEFT) { + } else if (ev.keyCode === keyCode.LEFT) { this.#onKeyArrowLeft(ev); - } else if (ev.keyCode === $.ui.keyCode.TAB) { + } else if (ev.keyCode === keyCode.TAB) { this.#onKeyTab(ev); } else { this.#searchHistoryIter = this.#inputHistory.length; this.#searchHistoryQuery = undefined; } - } else if (ev.keyCode === $.ui.keyCode.ENTER) { + } else if (ev.keyCode === keyCode.ENTER) { this.screen.responseQuestion(question_active, ev.target.value); - } else if (ev.keyCode === $.ui.keyCode.ESCAPE) { + } else if (ev.keyCode === keyCode.ESCAPE) { this.screen.rejectQuestion(question_active, 'Operation aborted'); ev.preventDefault(); } diff --git a/src/js/page/terminal/utils/keycode.mjs b/src/js/page/terminal/utils/keycode.mjs new file mode 100644 index 00000000..5ada75c0 --- /dev/null +++ b/src/js/page/terminal/utils/keycode.mjs @@ -0,0 +1,12 @@ +// Copyright Alexandre Díaz +// License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +export default { + ENTER: 13, + UP: 38, + DOWN: 40, + LEFT: 37, + RIGHT: 39, + TAB: 9, + ESCAPE: 27, +}; diff --git a/src/js/page/volatile/instance_analyzer.mjs b/src/js/page/volatile/instance_analyzer.mjs index fe8fb17f..3d77eac6 100644 --- a/src/js/page/volatile/instance_analyzer.mjs +++ b/src/js/page/volatile/instance_analyzer.mjs @@ -3,169 +3,88 @@ import isCompatibleOdooVersion from '@common/utils/is_compatible_odoo_version'; import postMessage from '@common/utils/post_message'; +import getOdooVersion from '@odoo/utils/get_odoo_version'; -class InstanceAnalyzer { - get odoo() { - return window.odoo; - } - get odoo_session() { - return ( - this.odoo?.session_info || - this.odoo?.session || - this.odoo?.__DEBUG__?.services['web.session'] || - this.odoo?.loader?.modules?.get('@web/session')?.session - ); - } +const ODOO_OBJ = window.odoo; - run() { - return this.getInfo().then(info => { - postMessage('ODOO_TERM_INIT', { - instance_info: info, - }); - }); - } - - /** - * @returns {Object} - */ - async getInfo() { - if (!this.odoo) { - return { - isOdoo: false, - isCompatible: false, - }; - } - - const icontext = { - isOdoo: true, +/** + * Helper function to sanitize the server version. + * @param {String} ver - Odoo version + * @param {Array} ver_info - Odoo version info + * @returns {Object} + */ +function sanitizeServerInfo(ver, ver_info) { + const info = {}; + info.serverVersionRaw = ver; + info.serverVersionInfo = ver_info; + if (ver_info) { + info.serverVersion = { + major: ver_info[0], + minor: ver_info[1], + status: ver_info[2], + statusLevel: ver_info[3], }; - let need_request_server_version = true; - if (this.odoo_session) { - icontext.isBackOffice = !this.odoo_session.is_frontend; - if (this.odoo_session.server_version) { - Object.assign( - icontext, - this.#sanitizeServerInfo(this.odoo_session.server_version), - ); - need_request_server_version = false; - } - } - if (!icontext.isBackOffice) { - icontext.isBackOffice = this.#isBackOfficeMode(); - } - if (need_request_server_version) { - Object.assign(icontext, await this.#getInstanceVersion()); - } - icontext.isCompatible = isCompatibleOdooVersion(icontext.serverVersionRaw); - return icontext; - } - - /** - * Helper function to sanitize the server version. - * @param {String} ver - Odoo version - * @param {Array} ver_info - Odoo version info - * @returns {Object} - */ - #sanitizeServerInfo(ver, ver_info) { - const info = {}; - info.serverVersionRaw = ver; - info.serverVersionInfo = ver_info; - if (ver_info) { + } else { + const foundVer = info.serverVersionRaw.match( + /(\d+)\.(\d+)(?:([a-z]+)(\d*))?/, + ); + if (foundVer && foundVer.length) { info.serverVersion = { - major: ver_info[0], - minor: ver_info[1], - status: ver_info[2], - statusLevel: ver_info[3], + major: Number(foundVer[1]), + minor: Number(foundVer[2]), + status: foundVer[3], + statusLevel: foundVer[4] && Number(foundVer[4]), }; - } else { - const foundVer = info.serverVersionRaw.match( - /(\d+)\.(\d+)(?:([a-z]+)(\d*))?/, - ); - if (foundVer && foundVer.length) { - info.serverVersion = { - major: Number(foundVer[1]), - minor: Number(foundVer[2]), - status: foundVer[3], - statusLevel: foundVer[4] && Number(foundVer[4]), - }; - } } - return info; - } - - /** - * @returns {Promise} - */ - #getOdooVersionByFramework() { - return new Promise((resolve, reject) => { - try { - this.odoo.define(0, require => { - require('web.core'); - const session = require('web.session'); - if (session?.server_version) { - resolve(this.#sanitizeServerInfo(session.server_version)); - } else { - reject(); - } - }); - } catch (exception) { - reject(); - } - }); - } - - /** - * @returns {Promise} - */ - async #getOdooVersionByNetwork() { - const response = await fetch('/web/webclient/version_info', { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: '{}', - }); - if (response.status === 200) { - const json_data = await response.json(); - return this.#sanitizeServerInfo( - json_data.result.server_version, - json_data.result.server_version_info, - ); - } - throw new Error(response); - } - - /** - * Request to Odoo the version. - * @returns {Object} - */ - async #getInstanceVersion() { - let server_ver = null; - try { - server_ver = await this.#getOdooVersionByFramework(); - } catch (exc_a) { - try { - server_ver = await this.#getOdooVersionByNetwork(); - } catch (exc_b) { - // Do nothing - } - } - return server_ver; } + return info; +} - /** - * Heuristic method to determine back-office mode - * @returns {Boolean} - */ - #isBackOfficeMode() { - return ( - document.querySelector("head script[src*='assets_frontend']") === null +/** + * @returns {Promise} + */ +async function getOdooVersionByNetwork() { + const response = await fetch('/web/webclient/version_info', { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: '{}', + }); + if (response.status === 200) { + const json_data = await response.json(); + return sanitizeServerInfo( + json_data.result.server_version, + json_data.result.server_version_info, ); } + throw new Error(response); } -const analyzer = new InstanceAnalyzer(); -analyzer.run(); +/** + * Request to Odoo the version. + * @returns {Object} + */ +async function getInstanceVersion() { + let server_ver = getOdooVersion(); + if (server_ver) { + server_ver = sanitizeServerInfo(server_ver); + } else { + server_ver = await getOdooVersionByNetwork(); + } + return server_ver; +} -export default analyzer; +const icontext = { + isOdoo: false, + isCompatible: false, +}; +if (ODOO_OBJ) { + icontext.isOdoo = true; + Object.assign(icontext, await getInstanceVersion()); + icontext.isCompatible = isCompatibleOdooVersion(icontext.serverVersionRaw); +} +postMessage('ODOO_TERM_INIT', { + instance_info: icontext, +});