summaryrefslogtreecommitdiff
path: root/addons/point_of_sale/static/src/js/ControlButtonsMixin.js
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/point_of_sale/static/src/js/ControlButtonsMixin.js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/point_of_sale/static/src/js/ControlButtonsMixin.js')
-rw-r--r--addons/point_of_sale/static/src/js/ControlButtonsMixin.js84
1 files changed, 84 insertions, 0 deletions
diff --git a/addons/point_of_sale/static/src/js/ControlButtonsMixin.js b/addons/point_of_sale/static/src/js/ControlButtonsMixin.js
new file mode 100644
index 00000000..02b4c367
--- /dev/null
+++ b/addons/point_of_sale/static/src/js/ControlButtonsMixin.js
@@ -0,0 +1,84 @@
+odoo.define('point_of_sale.ControlButtonsMixin', function (require) {
+ 'use strict';
+
+ const Registries = require('point_of_sale.Registries');
+
+ /**
+ * Component that has this mixin allows the use of `addControlButton`.
+ * All added control buttons that satisfies the condition can be accessed
+ * thru the `controlButtons` field of the Component's instance. These
+ * control buttons can then be rendered in the Component.
+ * @param {Function} x superclass
+ */
+ const ControlButtonsMixin = (x) => {
+ class Extended extends x {
+ get controlButtons() {
+ return this.constructor.controlButtons
+ .filter((cb) => {
+ return cb.condition.bind(this)();
+ })
+ .map((cb) =>
+ Object.assign({}, cb, { component: Registries.Component.get(cb.component) })
+ );
+ }
+ }
+ Extended.controlButtons = [];
+ /**
+ * @param {Object} controlButton
+ * @param {Function} controlButton.component
+ * Base class that is added in the Registries.Component.
+ * @param {Function} controlButton.condition zero argument function that is bound
+ * to the instance of ProductScreen, such that `this.env.pos` can be used
+ * inside the function.
+ * @param {Array} [controlButton.position] array of two elements
+ * [locator, relativeTo]
+ * locator: string -> any of ('before', 'after', 'replace')
+ * relativeTo: string -> other controlButtons component name
+ */
+ Extended.addControlButton = function (controlButton) {
+ // We set the name first.
+ if (!controlButton.name) {
+ controlButton.name = controlButton.component.name;
+ }
+
+ // If no position is set, we just push it to the array.
+ if (!controlButton.position) {
+ this.controlButtons.push(controlButton);
+ } else {
+ // Find where to put the new controlButton.
+ const [locator, relativeTo] = controlButton.position;
+ let whereIndex = -1;
+ for (let i = 0; i < this.controlButtons.length; i++) {
+ if (this.controlButtons[i].name === relativeTo) {
+ if (['before', 'replace'].includes(locator)) {
+ whereIndex = i;
+ } else if (locator === 'after') {
+ whereIndex = i + 1;
+ }
+ break;
+ }
+ }
+
+ // If found where to put, then perform the necessary mutation of
+ // the buttons array.
+ // Else, we just push this controlButton to the array.
+ if (whereIndex > -1) {
+ this.controlButtons.splice(
+ whereIndex,
+ locator === 'replace' ? 1 : 0,
+ controlButton
+ );
+ } else {
+ let warningMessage =
+ `'${controlButton.name}' has invalid 'position' ([${locator}, ${relativeTo}]).` +
+ 'It is pushed to the controlButtons stack instead.';
+ console.warn(warningMessage);
+ this.controlButtons.push(controlButton);
+ }
+ }
+ };
+ return Extended;
+ };
+
+ return ControlButtonsMixin;
+});