Commit 95f29eed authored by rymar.roman's avatar rymar.roman
Browse files

generic_mixin_refresh_view migrate to v16

parent 9a350118
Pipeline #100398 passed with stages
in 2 minutes and 17 seconds
......@@ -22,12 +22,7 @@
'assets': {
'web.assets_backend': [
'generic_mixin/static/src/scss/refresh_view.scss',
'generic_mixin/static/src/js/refresh_view_mixin.js',
'generic_mixin/static/src/js/list_renderer.js',
'generic_mixin/static/src/js/list_renderer.js',
'generic_mixin/static/src/js/kanban_renderer.js',
'generic_mixin/static/src/js/kanban_record.js',
'generic_mixin/static/src/js/web_client.js',
'generic_mixin/static/src/js/*.js',
],
},
'images': ['static/description/banner.png'],
......
/** @odoo-module **/
import { FormController } from '@web/views/form/form_controller';
import { patch } from "@web/core/utils/patch";
import { updateGetLocalStateFunction } from "./gmrv_controller_mixin";
patch(
FormController.prototype,
'generic_mixin',
{
setup() {
this._super(...arguments)
updateGetLocalStateFunction(this.model);
},
},
);
/** @odoo-module **/
import { useSetupView } from "@web/views/view_hook";
export function updateGetLocalStateFunction(model) {
useSetupView({
getLocalState: () => ({ model }),
});
}
/** @odoo-module **/
import { KanbanController } from '@web/views/kanban/kanban_controller';
import { patch } from "@web/core/utils/patch";
import { updateGetLocalStateFunction } from "./gmrv_controller_mixin";
patch(
KanbanController.prototype,
'generic_mixin',
{
setup() {
this._super(...arguments)
updateGetLocalStateFunction(this.model);
},
},
);
/** @odoo-module **/
import KanbanRecord from 'web.KanbanRecord';
KanbanRecord.include({
init: function () {
this._super.apply(this, arguments);
this._gmrvVisualizationOnTimeout = 100;
this._gmrvVisualizationOffTimeout = 1200;
},
_render: function () {
var render = this._super.apply(this, arguments);
this._generic_mixin_refresh_view__visualize_changes(this.$el, this.state.gmrvVisualize);
return render;
},
_generic_mixin_refresh_view__visualize_changes: function ($el, visualize) {
if (visualize) {
if (visualize === 'create') {
setTimeout(() => {
$el.addClass('gmrv_highlighting_record_create');
}, this._gmrvVisualizationOnTimeout);
setTimeout(() => {
$el.removeClass('gmrv_highlighting_record_create');
}, this._gmrvVisualizationOffTimeout);
} else if (visualize === 'write') {
setTimeout(() => {
$el.addClass('gmrv_highlighting_record_write');
}, this._gmrvVisualizationOnTimeout);
setTimeout(() => {
$el.removeClass('gmrv_highlighting_record_write');
}, this._gmrvVisualizationOffTimeout);
}
}
},
});
/** @odoo-module **/
import KanbanRenderer from 'web.KanbanRenderer';
import RefreshViewMixin from './refresh_view_mixin';
KanbanRenderer.include(RefreshViewMixin);
KanbanRenderer.include({
_renderView: function () {
this.state.data.forEach((record) => {
if (this._gmrvRefreshIds.create || this._gmrvRefreshIds.write) {
if (this._gmrvRefreshIds.create .includes(record.res_id)) {
record.gmrvVisualize = 'create';
} else if (this._gmrvRefreshIds.write .includes(record.res_id)) {
record.gmrvVisualize = 'write';
}
}
});
this.gmrvClearRefreshIds();
return this._super.apply(this, arguments);
},
});
/** @odoo-module **/
import { ListController } from '@web/views/list/list_controller';
import { patch } from "@web/core/utils/patch";
import { updateGetLocalStateFunction } from "./gmrv_controller_mixin";
patch(
ListController.prototype,
'generic_mixin',
{
setup() {
this._super(...arguments)
updateGetLocalStateFunction(this.model);
},
},
);
/** @odoo-module **/
import ListRenderer from 'web.ListRenderer';
import RefreshViewMixin from './refresh_view_mixin';
ListRenderer.include(RefreshViewMixin);
ListRenderer.include({
init: function () {
this._super.apply(this, arguments);
this._gmrvVisualizationOnTimeout = 100;
this._gmrvVisualizationOffTimeout = 1200;
},
_renderRows: function () {
let rows = this._super.apply(this, arguments);
this.gmrvClearRefreshIds();
return rows;
},
_renderRow: function (record) {
let $tr = this._super.apply(this, arguments);
this.gmrvVisualizeListRowChanges($tr, record.res_id);
return $tr;
},
gmrvVisualizeListRowChanges: function ($tr, resId) {
if (this._gmrvRefreshIds.create && this._gmrvRefreshIds.write) {
if (this._gmrvRefreshIds.create.includes(resId)) {
setTimeout(function () {
$tr.addClass('gmrv_highlighting_record_create');
}, this._gmrvVisualizationOnTimeout);
setTimeout(function () {
$tr.removeClass('gmrv_highlighting_record_create');
}, this._gmrvVisualizationOffTimeout);
} else if (this._gmrvRefreshIds.write.includes(resId)) {
setTimeout(function () {
$tr.addClass('gmrv_highlighting_record_write');
}, this._gmrvVisualizationOnTimeout);
setTimeout(function () {
$tr.removeClass('gmrv_highlighting_record_write');
}, this._gmrvVisualizationOffTimeout);
}
}
},
});
/** @odoo-module **/
const RefreshViewMixin = {
__refresh_view_mixin: true,
init: function () {
this._super.apply(this, arguments);
this.gmrvIsCompatible = true;
this._gmrvRefreshIds = {};
},
gmrvSetRefreshIds: function (refreshIds) {
if (refreshIds) {
this._gmrvRefreshIds = refreshIds;
}
},
gmrvClearRefreshIds: function () {
this._gmrvRefreshIds = {};
},
};
export default RefreshViewMixin;
......@@ -3,11 +3,12 @@
import concurrency from 'web.concurrency';
import { patch } from "@web/core/utils/patch";
import { WebClient } from "@web/webclient/webclient";
import { useService } from "@web/core/utils/hooks";
const GMRV_NAME = 'generic_mixin_refresh_view';
patch(
WebClient.prototype,
'Add refresh view after changed on the records',
'generix_mixin',
{
setup() {
this._super(...arguments);
......@@ -33,25 +34,13 @@ patch(
4000,
{'leading': false},
);
},
mounted() {
this._super(...arguments);
let busService = useService('bus_service');
busService.addChannel('generic_mixin_refresh_view');
busService.onNotification(
null, this._onBusGMRVNotification.bind(this));
this.env.services['bus_service'].addChannel(GMRV_NAME);
this.env.services['bus_service'].addEventListener(
'notification', this._onBusGMRVNotification.bind(this));
},
_gmrvDoRefreshCheck (controller) {
if (controller.view.isLegacy) {
return this._gmrvDoRefreshCheckLegacy(controller);
}
return false;
},
_gmrvDoRefreshCheckLegacy (controller) {
if (!controller) {
return false;
}
......@@ -60,22 +49,24 @@ patch(
return false;
}
let refreshIds = this._gmrvPending[controller.action.res_model];
const refreshIds = this._gmrvPending[controller.action.res_model];
if (!refreshIds) {
return false;
}
let actions = this._gmrvPendingAction[controller.action.res_model];
const actions = this._gmrvPendingAction[controller.action.res_model];
if (!actions) {
return false;
}
let localState = controller.getLocalState();
if (localState.__legacy_widget__.mode !== 'readonly') {
const localState = controller.getLocalState();
const modelRoot = localState.model?.root;
if (!modelRoot || modelRoot.isDirty || modelRoot.isVirtual) {
return false;
}
if (controller.view.multiRecord &&
const isMultiRecord = controller.view.multiRecord;
if (isMultiRecord &&
(actions.includes('create') || actions.includes('unlink'))) {
// Always refresh multirecord view on create or unlink.
// There is no need to compare changed ids and displayed ids
......@@ -85,88 +76,39 @@ patch(
// Find ids of records displayed by current action
let activeIds = [];
if (controller.action.res_id) {
activeIds.push(controller.action.res_id);
}
let globalState = controller.getGlobalState();
if (!controller.view.multiRecord && localState.currentId) {
activeIds = [
...new Set([
...activeIds,
...[localState.currentId],
]),
];
} else if (controller.view.multiRecord && globalState.resIds) {
activeIds = [
...new Set([
...activeIds,
...globalState.resIds,
]),
];
}
if (activeIds.filter(e => refreshIds.includes(e)).length) {
return true;
if (isMultiRecord) {
activeIds = modelRoot.records.map(e => e.resId);
} else {
activeIds.push(modelRoot.resId);
}
return false;
},
_gmrvDoRefreshController (controller) {
if (controller.view.isLegacy) {
return this._gmrvDoRefreshControllerLegacy(controller);
}
return !!activeIds.filter(e => refreshIds.includes(e)).length;
},
/* eslint-enable complexity */
// Refresh controller
_gmrvDoRefreshControllerLegacy (controller) {
let localState = controller.getLocalState();
if (localState && localState.__legacy_widget__) {
let widget = localState.__legacy_widget__;
var oldDisAutofocus = widget.disableAutofocus;
if (widget.renderer.gmrvIsCompatible) {
widget.renderer.gmrvSetRefreshIds(
this._gmrvRefreshIds[widget.modelName]);
}
if ('disableAutofocus' in widget) {
// In case of it is form view and has 'disableAutofocus'
// property, we have to set it to True, to ensure,
// that after update form will not scroll to the top.
// This helps a lot in case of frequent (1/sec) refresh
// events for the model
widget.disableAutofocus = true;
return widget.reload().then(function () {
widget.disableAutofocus = oldDisAutofocus;
});
}
// Otherwise, simply reload widget
return widget.reload();
}
_gmrvDoRefreshController (controller) {
const localState = controller.getLocalState();
localState.model.load();
},
_gmrvDoRefresher () {
let self = this;
let currentController = this.actionService.currentController;
// TODO: user controller's mutext to avoid errors like
// 'undefined has no attr commitChanges
this._gmrvMutex.exec(function () {
var promises = [];
if (self._gmrvDoRefreshCheck(currentController)) {
this._gmrvMutex.exec(() => {
const promises = [];
if (this._gmrvDoRefreshCheck(currentController)) {
// Refresh current controller
promises.push(
self._gmrvDoRefreshController(currentController));
this._gmrvDoRefreshController(currentController));
}
// Cleanup pending updates
self._gmrvPending = {};
self._gmrvPendingAction = {};
self._gmrvRefreshIds = {};
this._gmrvPending = {};
this._gmrvPendingAction = {};
this._gmrvRefreshIds = {};
return Promise.all(promises);
});
......@@ -218,14 +160,16 @@ patch(
}
},
_onBusGMRVNotification (notifications) {
notifications.forEach((notif) => {
if (notif.type === 'generic_mixin_refresh_view') {
try {
this._gmrvHandle(notif.payload);
} catch (e) {
console.log("Cannot refresh view", e);
}
_onBusGMRVNotification (event) {
const gmrvNotifications = event.detail.filter(e => e.type === GMRV_NAME);
if (!gmrvNotifications.length) {
return;
}
gmrvNotifications.forEach((notif) => {
try {
this._gmrvHandle(notif.payload);
} catch (e) {
console.log("Cannot refresh view", e);
}
});
......@@ -233,4 +177,5 @@ patch(
this._gmrvRefresher();
}
},
});
},
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment