Skip to content

Commit

Permalink
imp: support odoo 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Tardo committed Nov 4, 2023
1 parent 7c0fc20 commit e59bb54
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 168 deletions.
2 changes: 1 addition & 1 deletion src/js/page/loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
5 changes: 4 additions & 1 deletion src/js/page/odoo/base/do_action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/js/page/odoo/commands/backoffice/view.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async function cmdViewModelRecord(kwargs) {
views: [[false, 'form']],
target: 'current',
context: context,
});
}).then(() => this.doHide());
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/page/odoo/utils/get_odoo_version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
2 changes: 1 addition & 1 deletion src/js/page/odoo/utils/get_odoo_version_info.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
14 changes: 13 additions & 1 deletion src/js/page/odoo/utils/is_backoffice.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Copyright Alexandre Díaz <[email protected]>
// 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;
}
2 changes: 1 addition & 1 deletion src/js/page/terminal/commands/dis.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions src/js/page/terminal/terminal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
12 changes: 12 additions & 0 deletions src/js/page/terminal/utils/keycode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright Alexandre Díaz <[email protected]>
// 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,
};
225 changes: 72 additions & 153 deletions src/js/page/volatile/instance_analyzer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

0 comments on commit e59bb54

Please sign in to comment.