mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[16.0][MIG]web_widget_dropdown_dynamic: Migrate to version 16.0
This commit is contained in:
50
web_widget_dropdown_dynamic/static/src/js/basic_model.esm.js
Normal file
50
web_widget_dropdown_dynamic/static/src/js/basic_model.esm.js
Normal file
@@ -0,0 +1,50 @@
|
||||
/** @odoo-module **/
|
||||
import BasicModel from "web.BasicModel";
|
||||
|
||||
BasicModel.include({
|
||||
/**
|
||||
* Fetches all the values associated to the given fieldName.
|
||||
*
|
||||
* @param {Object} record - an element from the localData
|
||||
* @param {Object} fieldName - the name of the field
|
||||
* @param {Object} fieldInfo
|
||||
* @returns {Promise<any>}
|
||||
* The promise is resolved with the fetched special values.
|
||||
* If this data is the same as the previously fetched one
|
||||
* (for the given parameters), no RPC is done and the promise
|
||||
* is resolved with the undefined value.
|
||||
*/
|
||||
_fetchDynamicDropdownValues: function (record, fieldName, fieldInfo) {
|
||||
var model = fieldInfo.options.model || record.model;
|
||||
var method = fieldInfo.values || fieldInfo.options.values;
|
||||
if (!method) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
var context = record.getContext({fieldName: fieldName});
|
||||
|
||||
// Avoid rpc if not necessary
|
||||
var hasChanged = this._saveSpecialDataCache(record, fieldName, {
|
||||
context: context,
|
||||
});
|
||||
if (!hasChanged) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return this._rpc({
|
||||
model: model,
|
||||
method: method,
|
||||
context: context,
|
||||
}).then(function (result) {
|
||||
var new_result = result.map((val_updated) => {
|
||||
return val_updated.map((e) => {
|
||||
if (typeof e !== "string") {
|
||||
return String(e);
|
||||
}
|
||||
return e;
|
||||
});
|
||||
});
|
||||
return new_result;
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com)
|
||||
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
*/
|
||||
odoo.define("web_widget_dropdown_dynamic.basic_model", function (require) {
|
||||
"use strict";
|
||||
|
||||
var BasicModel = require("web.BasicModel");
|
||||
|
||||
BasicModel.include({
|
||||
/**
|
||||
* Fetches all the values associated to the given fieldName.
|
||||
*
|
||||
* @param {Object} record - an element from the localData
|
||||
* @param {Object} fieldName - the name of the field
|
||||
* @param {Object} fieldInfo
|
||||
* @returns {Promise<any>}
|
||||
* The promise is resolved with the fetched special values.
|
||||
* If this data is the same as the previously fetched one
|
||||
* (for the given parameters), no RPC is done and the promise
|
||||
* is resolved with the undefined value.
|
||||
*/
|
||||
_fetchDynamicDropdownValues: function (record, fieldName, fieldInfo) {
|
||||
var model = fieldInfo.options.model || record.model;
|
||||
var method = fieldInfo.values || fieldInfo.options.values;
|
||||
if (!method) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
var context = record.getContext({fieldName: fieldName});
|
||||
|
||||
// Avoid rpc if not necessary
|
||||
var hasChanged = this._saveSpecialDataCache(record, fieldName, {
|
||||
context: context,
|
||||
});
|
||||
if (!hasChanged) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return this._rpc({
|
||||
model: model,
|
||||
method: method,
|
||||
context: context,
|
||||
});
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
/** @odoo-module **/
|
||||
import core from "web.core";
|
||||
import {registry} from "@web/core/registry";
|
||||
import {standardFieldProps} from "@web/views/fields/standard_field_props";
|
||||
import {Component} from "@odoo/owl";
|
||||
|
||||
var _lt = core._lt;
|
||||
|
||||
export class FieldDynamicDropdown extends Component {
|
||||
get options() {
|
||||
var field_type = this.props.record.fields[this.props.name].type || "";
|
||||
if (["char", "integer", "selection"].includes(field_type)) {
|
||||
this._setValues();
|
||||
return this.props.record.fields[this.props.name].selection.filter(
|
||||
(option) => option[0] !== false && option[1] !== ""
|
||||
);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
get value() {
|
||||
const rawValue = this.props.value;
|
||||
this.props.setDirty(false);
|
||||
return this.props.type === "many2one" && rawValue ? rawValue[0] : rawValue;
|
||||
}
|
||||
|
||||
parseInteger(value) {
|
||||
return Number(value);
|
||||
}
|
||||
/**
|
||||
* @param {Event} ev
|
||||
*/
|
||||
onChange(ev) {
|
||||
let lastSetValue = null;
|
||||
let isInvalid = false;
|
||||
var isDirty = ev.target.value !== lastSetValue;
|
||||
const field = this.props.record.fields[this.props.name];
|
||||
let value = JSON.parse(ev.target.value);
|
||||
if (isDirty) {
|
||||
if (value && field.type === "integer") {
|
||||
value = Number(value);
|
||||
if (!value) {
|
||||
if (this.props.record) {
|
||||
this.props.record.setInvalidField(this.props.name);
|
||||
}
|
||||
isInvalid = true;
|
||||
}
|
||||
}
|
||||
if (!isInvalid) {
|
||||
Promise.resolve(this.props.update(value));
|
||||
lastSetValue = ev.target.value;
|
||||
}
|
||||
}
|
||||
if (this.props.setDirty) {
|
||||
this.props.setDirty(isDirty);
|
||||
}
|
||||
}
|
||||
stringify(value) {
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
_setValues() {
|
||||
if (this.props.record.preloadedData[this.props.name]) {
|
||||
var sel_value = this.props.record.preloadedData[this.props.name];
|
||||
// Convert string element to integer if field is integer
|
||||
if (this.props.record.fields[this.props.name].type === "integer") {
|
||||
sel_value = sel_value.map((val_updated) => {
|
||||
return val_updated.map((e) => {
|
||||
if (typeof e === "string" && !isNaN(Number(e))) {
|
||||
return Number(e);
|
||||
}
|
||||
return e;
|
||||
});
|
||||
});
|
||||
}
|
||||
this.props.record.fields[this.props.name].selection = sel_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FieldDynamicDropdown.description = _lt("Dynamic Dropdown");
|
||||
FieldDynamicDropdown.template = "web.SelectionField";
|
||||
FieldDynamicDropdown.legacySpecialData = "_fetchDynamicDropdownValues";
|
||||
FieldDynamicDropdown.props = {
|
||||
...standardFieldProps,
|
||||
};
|
||||
FieldDynamicDropdown.supportedTypes = ["char", "integer", "selection"];
|
||||
registry.category("fields").add("dynamic_dropdown", FieldDynamicDropdown);
|
||||
@@ -1,149 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
*/
|
||||
odoo.define("web_widget_dropdown_dynamic.field_dynamic_dropdown", function (require) {
|
||||
"use strict";
|
||||
|
||||
var core = require("web.core");
|
||||
var AbstractField = require("web.AbstractField");
|
||||
var field_registry = require("web.field_registry");
|
||||
|
||||
var _lt = core._lt;
|
||||
|
||||
var FieldDynamicDropdown = AbstractField.extend({
|
||||
description: _lt("Dynamic Dropdown"),
|
||||
template: "FieldSelection",
|
||||
specialData: "_fetchDynamicDropdownValues",
|
||||
supportedFieldTypes: ["selection", "char", "integer"],
|
||||
events: _.extend({}, AbstractField.prototype.events, {
|
||||
change: "_onChange",
|
||||
}),
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
init: function () {
|
||||
this._super.apply(this, arguments);
|
||||
this._setValues();
|
||||
},
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Public
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @returns {jQuery}
|
||||
*/
|
||||
getFocusableElement: function () {
|
||||
return this.$el.is("select") ? this.$el : $();
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
isSet: function () {
|
||||
return this.value !== false;
|
||||
},
|
||||
/**
|
||||
* Listen to modifiers updates to hide/show the falsy value in the dropdown
|
||||
* according to the required modifier.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
updateModifiersValue: function () {
|
||||
this._super.apply(this, arguments);
|
||||
if (!this.attrs.modifiersValue.invisible && this.mode !== "readonly") {
|
||||
this._setValues();
|
||||
this._renderEdit();
|
||||
}
|
||||
},
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Private
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @override
|
||||
* @private
|
||||
*/
|
||||
_formatValue: function (value) {
|
||||
var options = _.extend(
|
||||
{},
|
||||
this.nodeOptions,
|
||||
{data: this.recordData},
|
||||
this.formatOptions
|
||||
);
|
||||
var formattedValue = _.find(this.values, function (option) {
|
||||
return option[0] === value;
|
||||
});
|
||||
if (!formattedValue) {
|
||||
return value;
|
||||
}
|
||||
formattedValue = formattedValue[1];
|
||||
if (options && options.escape) {
|
||||
formattedValue = _.escape(formattedValue);
|
||||
}
|
||||
return formattedValue;
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
* @private
|
||||
*/
|
||||
_renderEdit: function () {
|
||||
this.$el.empty();
|
||||
for (var i = 0; i < this.values.length; i++) {
|
||||
this.$el.append(
|
||||
$("<option/>", {
|
||||
value: JSON.stringify(this.values[i][0]),
|
||||
text: this.values[i][1],
|
||||
})
|
||||
);
|
||||
}
|
||||
this.$el.val(JSON.stringify(this.value));
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
* @private
|
||||
*/
|
||||
_renderReadonly: function () {
|
||||
this.$el.empty().text(this._formatValue(this.value));
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
_reset: function () {
|
||||
this._super.apply(this, arguments);
|
||||
this._setValues();
|
||||
},
|
||||
/**
|
||||
* Sets the possible field values.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_setValues: function () {
|
||||
this.values = _.reject(this.record.specialData[this.name], function (v) {
|
||||
return v[0] === false && v[1] === "";
|
||||
});
|
||||
if (!this.attrs.modifiersValue || !this.attrs.modifiersValue.required) {
|
||||
this.values = [[false, this.attrs.placeholder || ""]].concat(
|
||||
this.values
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Handlers
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_onChange: function () {
|
||||
var value = JSON.parse(this.$el.val());
|
||||
this._setValue(value.toString());
|
||||
},
|
||||
});
|
||||
field_registry.add("dynamic_dropdown", FieldDynamicDropdown);
|
||||
|
||||
return FieldDynamicDropdown;
|
||||
});
|
||||
Reference in New Issue
Block a user