summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/basic/basic_controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'addons/web/static/src/js/views/basic/basic_controller.js')
-rw-r--r--addons/web/static/src/js/views/basic/basic_controller.js156
1 files changed, 53 insertions, 103 deletions
diff --git a/addons/web/static/src/js/views/basic/basic_controller.js b/addons/web/static/src/js/views/basic/basic_controller.js
index 58e03efb94a..8b4672bc291 100644
--- a/addons/web/static/src/js/views/basic/basic_controller.js
+++ b/addons/web/static/src/js/views/basic/basic_controller.js
@@ -11,7 +11,6 @@ var AbstractController = require('web.AbstractController');
var core = require('web.core');
var Dialog = require('web.Dialog');
var FieldManagerMixin = require('web.FieldManagerMixin');
-var Pager = require('web.Pager');
var TranslationDialog = require('web.TranslationDialog');
var _t = core._t;
@@ -19,12 +18,12 @@ var _t = core._t;
var BasicController = AbstractController.extend(FieldManagerMixin, {
custom_events: _.extend({}, AbstractController.prototype.custom_events, FieldManagerMixin.custom_events, {
discard_changes: '_onDiscardChanges',
+ pager_changed: '_onPagerChanged',
reload: '_onReload',
resequence_records: '_onResequenceRecords',
set_dirty: '_onSetDirty',
load_optional_fields: '_onLoadOptionalFields',
save_optional_fields: '_onSaveOptionalFields',
- sidebar_data_asked: '_onSidebarDataAsked',
translate: '_onTranslate',
}),
/**
@@ -52,12 +51,11 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
* @override
* @returns {Promise}
*/
- start: function () {
+ start: async function () {
// add classname to reflect the (absence of) access rights (used to
// correctly display the nocontent helper)
this.$el.toggleClass('o_cannot_create', !this.activeActions.create);
- return this._super.apply(this, arguments)
- .then(this._updateEnv.bind(this));
+ await this._super(...arguments);
},
//--------------------------------------------------------------------------
@@ -149,35 +147,6 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
return this.model.isDirty(recordID || this.handle);
},
/**
- * @override
- */
- renderPager: function ($node, options) {
- var self = this;
- var params = this._getPagerParams();
- this.pager = new Pager(this, params.size, params.current_min, params.limit, options);
-
- this.pager.on('pager_changed', this, function (newState) {
- this.pager.disable();
- var data = this.model.get(this.handle, {raw: true});
- var limitChanged = (data.limit !== newState.limit);
- var reloadParams;
- if (data.groupedBy && data.groupedBy.length) {
- reloadParams = {groupsLimit: newState.limit, groupsOffset: newState.current_min - 1};
- } else {
- reloadParams = {limit: newState.limit, offset: newState.current_min - 1};
- }
- this.reload(reloadParams).then(function () {
- // reset the scroll position to the top on page changed only
- if (!limitChanged) {
- self.trigger_up('scrollTo', {top: 0});
- }
- }).then(this.pager.enable.bind(this.pager));
- });
- return this.pager.appendTo($node).then(function () {
- self._updatePager(); // to force proper visibility
- });
- },
- /**
* Saves the record whose ID is given if necessary (@see _saveRecord).
*
* @param {string} [recordID] - default to main recordID
@@ -218,13 +187,9 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
* @override
* @returns {Promise}
*/
- update: function (params, options) {
- var self = this;
+ update: async function (params, options) {
this.mode = params.mode || this.mode;
- return this._super(params, options).then(function () {
- self._updateEnv();
- self._updatePager();
- });
+ return this._super(params, options);
},
/**
* @override
@@ -486,45 +451,33 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
return viewIdentifier;
},
/**
- * Return the params (current_min, limit and size) to pass to the pager,
+ * Return the params (currentMinimum, limit and size) to pass to the pager,
* according to the current state.
*
* @private
* @returns {Object}
*/
- _getPagerParams: function () {
- var state = this.model.get(this.handle, {raw: true});
- var isGrouped = state.groupedBy && state.groupedBy.length;
+ _getPagingInfo: function (state) {
+ const isGrouped = state.groupedBy && state.groupedBy.length;
return {
- current_min: (isGrouped ? state.groupsOffset : state.offset) + 1,
+ currentMinimum: (isGrouped ? state.groupsOffset : state.offset) + 1,
limit: isGrouped ? state.groupsLimit : state.limit,
size: isGrouped ? state.groupsCount : state.count,
};
},
/**
- * Returns the new sidebar env
+ * Return the new actionMenus props.
*
+ * @override
* @private
- * @return {Object} the new sidebar env
*/
- _getSidebarEnv: function () {
+ _getActionMenuItems: function (state) {
return {
- context: this.model.get(this.handle).getContext(),
activeIds: this.getSelectedIds(),
- model: this.modelName,
+ context: state.getContext(),
};
},
/**
- * Determine whether or not the pager must be displayed (probably depending
- * on the current state). Controllers must override this to implement their
- * own logic.
- *
- * @private
- */
- _isPagerVisible: function () {
- return true;
- },
- /**
* Sort function used to sort the fields by names, to compute the optional fields keys
*
* @param {Object} left
@@ -633,24 +586,16 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
* and notifies the component chain (by bubbling an event up)
*
* @private
- */
- _updateEnv: function () {
- var env = this.model.get(this.handle, {env: true});
- if (this.sidebar) {
- var sidebarEnv = this._getSidebarEnv();
- this.sidebar.updateEnv(sidebarEnv);
- }
- },
- /**
- * Update the pager with the current state.
- *
- * @private
- */
- _updatePager: function () {
- if (this.pager) {
- this.pager.updateState(this._getPagerParams());
- this.pager.do_toggle(this._isPagerVisible());
- }
+ * @param {Object} [newProps={}]
+ */
+ _updateControlPanel: function (newProps = {}) {
+ const state = this.model.get(this.handle);
+ const props = Object.assign(newProps, {
+ actionMenus: this._getActionMenuItems(state),
+ pager: this._getPagingInfo(state),
+ title: this.getTitle(),
+ });
+ return this.updateControlPanel(props);
},
//--------------------------------------------------------------------------
@@ -694,6 +639,27 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
FieldManagerMixin._onFieldChanged.apply(this, arguments);
},
/**
+ * @private
+ * @param {OdooEvent} ev
+ */
+ _onPagerChanged: async function (ev) {
+ ev.stopPropagation();
+ const { currentMinimum, limit } = ev.data;
+ const state = this.model.get(this.handle, { raw: true });
+ const reloadParams = state.groupedBy && state.groupedBy.length ? {
+ groupsLimit: limit,
+ groupsOffset: currentMinimum - 1,
+ } : {
+ limit,
+ offset: currentMinimum - 1,
+ };
+ await this.reload(reloadParams);
+ // reset the scroll position to the top on page changed only
+ if (state.limit === limit) {
+ this.trigger_up('scrollTo', { top: 0 });
+ }
+ },
+ /**
* When a reload event triggers up, we need to reload the full view.
* For example, after a form view dialog saved some data.
*
@@ -735,24 +701,19 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
* @param {string} ev.data.handleField
*/
_onResequenceRecords: function (ev) {
- var self = this;
-
this.trigger_up('mutexify', {
- action: function () {
- var state = self.model.get(self.handle);
- var resIDs = _.map(ev.data.recordIds, function (recordID) {
- return _.findWhere(state.data, {id: recordID}).res_id;
- });
- var options = {
+ action: async () => {
+ let state = this.model.get(this.handle);
+ const resIDs = ev.data.recordIds
+ .map(recordID => state.data.find(d => d.id === recordID).res_id);
+ const options = {
offset: ev.data.offset,
field: ev.data.handleField,
};
- return self.model.resequence(self.modelName, resIDs, self.handle, options)
- .then(function () {
- self._updateEnv();
- state = self.model.get(self.handle);
- return self.renderer.updateState(state, {noRender: true});
- });
+ await this.model.resequence(this.modelName, resIDs, this.handle, options);
+ this._updateControlPanel();
+ state = this.model.get(this.handle);
+ return this.renderer.updateState(state, { noRender: true });
},
});
},
@@ -798,17 +759,6 @@ var BasicController = AbstractController.extend(FieldManagerMixin, {
this.model.setDirty(ev.data.dataPointID);
},
/**
- * Handler used to get all the data necessary when a custom action is
- * performed through the sidebar.
- *
- * @private
- * @param {OdooEvent} ev
- */
- _onSidebarDataAsked: function (ev) {
- var sidebarEnv = this._getSidebarEnv();
- ev.data.callback(sidebarEnv);
- },
- /**
* open the translation view for the current field
*
* @private