[REF] web_m2x_options: eslint fix

pull/1530/head
Andrés Rodríguez 2020-02-23 14:36:28 -04:00
parent b488353b3c
commit d4dd16f91c
1 changed files with 247 additions and 165 deletions

View File

@ -1,15 +1,15 @@
/* Copyright 2016 0k.io,ACSONE SA/NV /* Copyright 2016 0k.io,ACSONE SA/NV
* * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ * * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define('web_m2x_options.web_m2x_options', function (require) { odoo.define("web_m2x_options.web_m2x_options", function(require) {
"use strict"; "use strict";
var core = require('web.core'), var core = require("web.core"),
data = require('web.data'), data = require("web.data"),
Dialog = require('web.Dialog'), Dialog = require("web.Dialog"),
view_dialogs = require('web.view_dialogs'), view_dialogs = require("web.view_dialogs"),
relational_fields = require('web.relational_fields'), relational_fields = require("web.relational_fields"),
rpc = require('web.rpc'); rpc = require("web.rpc");
var _t = core._t, var _t = core._t,
FieldMany2ManyTags = relational_fields.FieldMany2ManyTags, FieldMany2ManyTags = relational_fields.FieldMany2ManyTags,
@ -18,80 +18,92 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
var web_m2x_options = rpc.query({ var web_m2x_options = rpc.query({
model: "ir.config_parameter", model: "ir.config_parameter",
method: 'get_web_m2x_options', method: "get_web_m2x_options",
}); });
var M2ODialog = Dialog.extend({ var M2ODialog = Dialog.extend({
template: "M2ODialog", template: "M2ODialog",
init: function (parent, name, value) { init: function(parent, name, value) {
this.name = name; this.name = name;
this.value = value; this.value = value;
this._super(parent, { this._super(parent, {
title: _.str.sprintf(_t("Create a %s"), this.name), title: _.str.sprintf(_t("Create a %s"), this.name),
size: 'medium', size: "medium",
buttons: [{ buttons: [
text: _t('Create'), {
classes: 'btn-primary', text: _t("Create"),
click: function () { classes: "btn-primary",
if (this.$("input").val() !== '') { click: function() {
this.trigger_up('quick_create', {value: this.$('input').val()}); if (this.$("input").val() !== "") {
this.close(true); this.trigger_up("quick_create", {
} else { value: this.$("input").val(),
this.$("input").focus(); });
} this.close(true);
} else {
this.$("input").focus();
}
},
}, },
}, { {
text: _t('Create and edit'), text: _t("Create and edit"),
classes: 'btn-primary', classes: "btn-primary",
close: true, close: true,
click: function () { click: function() {
this.trigger_up('search_create_popup', { this.trigger_up("search_create_popup", {
view_type: 'form', view_type: "form",
value: this.$('input').val(), value: this.$("input").val(),
}); });
},
}, },
}, { {
text: _t('Cancel'), text: _t("Cancel"),
close: true, close: true,
}], },
],
}); });
}, },
start: function () { start: function() {
this.$("p").text(_.str.sprintf(_t("You are creating a new %s, are you sure it does not exist yet?"), this.name)); this.$("p").text(
_.str.sprintf(
_t(
"You are creating a new %s, are you sure it does not exist yet?"
),
this.name
)
);
this.$("input").val(this.value); this.$("input").val(this.value);
}, },
/** /**
* @override * @override
* @param {boolean} isSet * @param {Boolean} isSet
*/ */
close: function (isSet) { close: function(isSet) {
this.isSet = isSet; this.isSet = isSet;
this._super.apply(this, arguments); this._super.apply(this, arguments);
}, },
/** /**
* @override * @override
*/ */
destroy: function () { destroy: function() {
if (!this.isSet) { if (!this.isSet) {
this.trigger_up('closed_unset'); this.trigger_up("closed_unset");
} }
this._super.apply(this, arguments); this._super.apply(this, arguments);
}, },
}); });
FieldMany2One.include({ FieldMany2One.include({
start: function() {
start: function () {
this._super.apply(this, arguments); this._super.apply(this, arguments);
return this.get_options(); return this.get_options();
}, },
get_options: function () { get_options: function() {
var self = this; var self = this;
if (_.isUndefined(this.ir_options_loaded)) { if (_.isUndefined(this.ir_options_loaded)) {
this.ir_options_loaded = $.Deferred(); this.ir_options_loaded = $.Deferred();
this.ir_options = {}; this.ir_options = {};
web_m2x_options.then(function (records) { web_m2x_options.then(function(records) {
_(records).each(function(record) { _(records).each(function(record) {
self.ir_options[record.key] = record.value; self.ir_options[record.key] = record.value;
}); });
@ -101,36 +113,41 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
return $.when(); return $.when();
}, },
is_option_set: function (option) { is_option_set: function(option) {
if (_.isUndefined(option)) if (_.isUndefined(option)) return false;
return false; if (typeof option === "string")
if (typeof option === 'string') return option === "true" || option === "True";
return option === 'true' || option === 'True'; if (typeof option === "boolean") return option;
if (typeof option === 'boolean') return false;
return option;
return false
}, },
_onInputFocusout: function () { _onInputFocusout: function() {
var m2o_dialog_opt = this.is_option_set(this.nodeOptions.m2o_dialog) || _.isUndefined(this.nodeOptions.m2o_dialog) && this.is_option_set(this.ir_options['web_m2x_options.m2o_dialog']) || _.isUndefined(this.nodeOptions.m2o_dialog) && _.isUndefined(this.ir_options['web_m2x_options.m2o_dialog']); var m2o_dialog_opt =
this.is_option_set(this.nodeOptions.m2o_dialog) ||
(_.isUndefined(this.nodeOptions.m2o_dialog) &&
this.is_option_set(
this.ir_options["web_m2x_options.m2o_dialog"]
)) ||
(_.isUndefined(this.nodeOptions.m2o_dialog) &&
_.isUndefined(this.ir_options["web_m2x_options.m2o_dialog"]));
if (this.can_create && this.floating && m2o_dialog_opt) { if (this.can_create && this.floating && m2o_dialog_opt) {
new M2ODialog(this, this.string, this.$input.val()).open(); new M2ODialog(this, this.string, this.$input.val()).open();
} }
}, },
_search: function (search_val) { _search: function(search_val) {
var def = new Promise(resolve => { var def = new Promise(resolve => {
// add options limit used to change number of selections record // Add options limit used to change number of selections record
// returned. // returned.
if (!_.isUndefined(self.ir_options['web_m2x_options.limit'])) { if (!_.isUndefined(self.ir_options["web_m2x_options.limit"])) {
this.limit = parseInt(self.ir_options['web_m2x_options.limit'], 10); this.limit = parseInt(self.ir_options["web_m2x_options.limit"], 10);
} }
if (typeof self.nodeOptions.limit === 'number') { if (typeof self.nodeOptions.limit === "number") {
self.limit = self.nodeOptions.limit; self.limit = self.nodeOptions.limit;
} }
// add options field_color and colors to color item(s) depending on field_color value // Add options field_color and colors to color item(s) depending on field_color value
self.field_color = self.nodeOptions.field_color; self.field_color = self.nodeOptions.field_color;
self.colors = self.nodeOptions.colors; self.colors = self.nodeOptions.colors;
@ -139,7 +156,7 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
var blacklisted_ids = self._getSearchBlacklist(); var blacklisted_ids = self._getSearchBlacklist();
if (blacklisted_ids.length > 0) { if (blacklisted_ids.length > 0) {
domain.push(['id', 'not in', blacklisted_ids]); domain.push(["id", "not in", blacklisted_ids]);
} }
self._rpc({ self._rpc({
@ -151,13 +168,14 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
operator: "ilike", operator: "ilike",
limit: self.limit + 1, limit: self.limit + 1,
context: context, context: context,
} },
}).then(result => { }).then(result => {
// possible selections for the m2o // Possible selections for the m2o
var values = _.map(result, x => { var values = _.map(result, x => {
x[1] = self._getDisplayName(x[1]); x[1] = self._getDisplayName(x[1]);
return { return {
label: _.str.escapeHTML(x[1].trim()) || data.noDisplayContent, label:
_.str.escapeHTML(x[1].trim()) || data.noDisplayContent,
value: x[1], value: x[1],
name: x[1], name: x[1],
id: x[0], id: x[0],
@ -172,9 +190,9 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
} }
self._rpc({ self._rpc({
model: self.field.relation, model: self.field.relation,
method: 'search_read', method: "search_read",
fields: [self.field_color], fields: [self.field_color],
domain: [['id', 'in', value_ids]] domain: [["id", "in", value_ids]],
}).then(objects => { }).then(objects => {
for (var index in objects) { for (var index in objects) {
for (var index_value in values) { for (var index_value in values) {
@ -182,96 +200,162 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
// Find value in values by comparing ids // Find value in values by comparing ids
var value = values[index_value]; var value = values[index_value];
// Find color with field value as key // Find color with field value as key
var color = self.colors[objects[index][self.field_color]] || 'black'; var color =
value.label = '<span style="color:' + color + '">' + value.label + '</span>'; self.colors[
objects[index][self.field_color]
] || "black";
value.label =
'<span style="color:' +
color +
'">' +
value.label +
"</span>";
break; break;
} }
} }
} }
resolve(values); resolve(values);
}) });
} }
// search more... if more results that max // Search more... if more results that max
var can_search_more = (self.nodeOptions && self.is_option_set(self.nodeOptions.search_more)), var can_search_more =
search_more_undef = _.isUndefined(self.nodeOptions.search_more) && _.isUndefined(self.ir_options['web_m2x_options.search_more']), self.nodeOptions &&
search_more = self.is_option_set(self.ir_options['web_m2x_options.search_more']); self.is_option_set(self.nodeOptions.search_more),
search_more_undef =
_.isUndefined(self.nodeOptions.search_more) &&
_.isUndefined(
self.ir_options["web_m2x_options.search_more"]
),
search_more = self.is_option_set(
self.ir_options["web_m2x_options.search_more"]
);
if (values.length > self.limit) { if (values.length > self.limit) {
values = values.slice(0, self.limit); values = values.slice(0, self.limit);
if (can_search_more || search_more_undef || search_more) { if (can_search_more || search_more_undef || search_more) {
values.push({ values.push({
label: _t("Search More..."), label: _t("Search More..."),
action: function () { action: function() {
var prom; var prom = [];
if (search_val !== '') { if (search_val !== "") {
prom = self._rpc({ prom = self._rpc({
model: self.field.relation, model: self.field.relation,
method: 'name_search', method: "name_search",
kwargs: { kwargs: {
name: search_val, name: search_val,
args: domain, args: domain,
operator: "ilike", operator: "ilike",
limit: self.SEARCH_MORE_LIMIT, limit: self.SEARCH_MORE_LIMIT,
context: context, context: context,
}, },
});
}
Promise.resolve(prom).then(function (results) {
var dynamicFilters;
if (results) {
var ids = _.map(results, function (x) {
return x[0];
}); });
dynamicFilters = [{
description: _.str.sprintf(_t('Quick search: %s'), search_val),
domain: [['id', 'in', ids]],
}];
} }
self._searchCreatePopup("search", false, {}, dynamicFilters); Promise.resolve(prom).then(function(results) {
}); var dynamicFilters = [];
}, if (results) {
classname: 'o_m2o_dropdown_option', var ids = _.map(results, function(x) {
}); return x[0];
});
dynamicFilters = [
{
description: _.str.sprintf(
_t("Quick search: %s"),
search_val
),
domain: [["id", "in", ids]],
},
];
}
self._searchCreatePopup(
"search",
false,
{},
dynamicFilters
);
});
},
classname: "o_m2o_dropdown_option",
});
} }
} }
var create_enabled = self.can_create && !self.nodeOptions.no_create; var create_enabled = self.can_create && !self.nodeOptions.no_create;
// quick create // Quick create
var raw_result = _.map(result, function (x) { return x[1]; }); var raw_result = _.map(result, function(x) {
return x[1];
});
var quick_create = self.is_option_set(self.nodeOptions.create), var quick_create = self.is_option_set(self.nodeOptions.create),
quick_create_undef = _.isUndefined(self.nodeOptions.create), quick_create_undef = _.isUndefined(self.nodeOptions.create),
m2x_create_undef = _.isUndefined(self.ir_options['web_m2x_options.create']), m2x_create_undef = _.isUndefined(
m2x_create = self.is_option_set(self.ir_options['web_m2x_options.create']); self.ir_options["web_m2x_options.create"]
var show_create = (!self.nodeOptions && (m2x_create_undef || m2x_create)) || (self.nodeOptions && (quick_create || (quick_create_undef && (m2x_create_undef || m2x_create)))); ),
if (create_enabled && !self.nodeOptions.no_quick_create && m2x_create = self.is_option_set(
search_val.length > 0 && !_.contains(raw_result, search_val) && self.ir_options["web_m2x_options.create"]
show_create) { );
var show_create =
(!self.nodeOptions && (m2x_create_undef || m2x_create)) ||
(self.nodeOptions &&
(quick_create ||
(quick_create_undef &&
(m2x_create_undef || m2x_create))));
if (
create_enabled &&
!self.nodeOptions.no_quick_create &&
search_val.length > 0 &&
!_.contains(raw_result, search_val) &&
show_create
) {
values.push({ values.push({
label: _.str.sprintf(_t('Create "<strong>%s</strong>"'), label: _.str.sprintf(
$('<span />').text(search_val).html()), _t('Create "<strong>%s</strong>"'),
$("<span />")
.text(search_val)
.html()
),
action: self._quickCreate.bind(self, search_val), action: self._quickCreate.bind(self, search_val),
classname: 'o_m2o_dropdown_option' classname: "o_m2o_dropdown_option",
}); });
} }
// create and edit ... // Create and edit ...
var create_edit = self.is_option_set(self.nodeOptions.create) || self.is_option_set(self.nodeOptions.create_edit), var create_edit =
create_edit_undef = _.isUndefined(self.nodeOptions.create) && _.isUndefined(self.nodeOptions.create_edit), self.is_option_set(self.nodeOptions.create) ||
m2x_create_edit_undef = _.isUndefined(self.ir_options['web_m2x_options.create_edit']), self.is_option_set(self.nodeOptions.create_edit),
m2x_create_edit = self.is_option_set(self.ir_options['web_m2x_options.create_edit']); create_edit_undef =
var show_create_edit = (!self.nodeOptions && (m2x_create_edit_undef || m2x_create_edit)) || (self.nodeOptions && (create_edit || (create_edit_undef && (m2x_create_edit_undef || m2x_create_edit)))); _.isUndefined(self.nodeOptions.create) &&
if (create_enabled && !self.nodeOptions.no_create_edit && show_create_edit) { _.isUndefined(self.nodeOptions.create_edit),
var createAndEditAction = function () { m2x_create_edit_undef = _.isUndefined(
self.ir_options["web_m2x_options.create_edit"]
),
m2x_create_edit = self.is_option_set(
self.ir_options["web_m2x_options.create_edit"]
);
var show_create_edit =
(!self.nodeOptions &&
(m2x_create_edit_undef || m2x_create_edit)) ||
(self.nodeOptions &&
(create_edit ||
(create_edit_undef &&
(m2x_create_edit_undef || m2x_create_edit))));
if (
create_enabled &&
!self.nodeOptions.no_create_edit &&
show_create_edit
) {
var createAndEditAction = function() {
// Clear the value in case the user clicks on discard // Clear the value in case the user clicks on discard
self.$('input').val(''); self.$("input").val("");
return self._searchCreatePopup("form", false, self._createContext(search_val)); return self._searchCreatePopup(
"form",
false,
self._createContext(search_val)
);
}; };
values.push({ values.push({
label: _t("Create and Edit..."), label: _t("Create and Edit..."),
action: createAndEditAction, action: createAndEditAction,
classname: 'o_m2o_dropdown_option', classname: "o_m2o_dropdown_option",
}); });
} else if (values.length === 0) { } else if (values.length === 0) {
values.push({ values.push({
@ -283,69 +367,67 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
resolve(values); resolve(values);
} }
}); });
}); });
this.orderer.add(def); this.orderer.add(def);
return def; return def;
} },
}); });
FieldMany2ManyTags.include({ FieldMany2ManyTags.include({
events: _.extend({}, FieldMany2ManyTags.prototype.events, { events: _.extend({}, FieldMany2ManyTags.prototype.events, {
'click .badge': '_onOpenBadge', "click .badge": "_onOpenBadge",
}), }),
_onDeleteTag: function (event) { _onDeleteTag: function(event) {
var result = this._super.apply(this, arguments); var result = this._super.apply(this, arguments);
event.stopPropagation(); event.stopPropagation();
return result; return result;
}, },
is_option_set: function (option) { is_option_set: function(option) {
if (_.isUndefined(option)) if (_.isUndefined(option)) return false;
return false; if (typeof option === "string")
if (typeof option === 'string') return option === "true" || option === "True";
return option === 'true' || option === 'True'; if (typeof option === "boolean") return option;
if (typeof option === 'boolean') return false;
return option;
return false
}, },
_onOpenBadge: function (event) { _onOpenBadge: function(event) {
var self = this; var self = this;
var open = (self.nodeOptions && self.is_option_set(self.nodeOptions.open)); var open = self.nodeOptions && self.is_option_set(self.nodeOptions.open);
if (open) { if (open) {
var context = self.record.getContext(self.recordParams); var context = self.record.getContext(self.recordParams);
var id = parseInt($(event.currentTarget).data('id'), 10); var id = parseInt($(event.currentTarget).data("id"), 10);
if (self.mode === 'readonly') { if (self.mode === "readonly") {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
self._rpc({ self._rpc({
model: self.field.relation, model: self.field.relation,
method: 'get_formview_action', method: "get_formview_action",
args: [[id]], args: [[id]],
context: context, context: context,
}) }).then(function(action) {
.then(function (action) { self.trigger_up("do_action", {action: action});
self.trigger_up('do_action', {action: action}); });
}); } else {
}
else {
$.when( $.when(
self._rpc({ self._rpc({
model: self.field.relation, model: self.field.relation,
method: 'get_formview_id', method: "get_formview_id",
args: [[id]], args: [[id]],
context: context, context: context,
}), }),
self._rpc({ self._rpc({
model: self.field.relation, model: self.field.relation,
method: 'check_access_rights', method: "check_access_rights",
kwargs: {operation: 'write', raise_exception: false} kwargs: {operation: "write", raise_exception: false},
}) })
).then(function (view_id, write_access) { ).then(function(view_id, write_access) {
var can_write = 'can_write' in self.attrs ? JSON.parse(self.attrs.can_write) : true; var can_write =
"can_write" in self.attrs
? JSON.parse(self.attrs.can_write)
: true;
new view_dialogs.FormViewDialog(self, { new view_dialogs.FormViewDialog(self, {
res_model: self.field.relation, res_model: self.field.relation,
res_id: id, res_id: id,
@ -353,14 +435,16 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
title: _t("Open: ") + self.string, title: _t("Open: ") + self.string,
view_id: view_id, view_id: view_id,
readonly: !can_write || !write_access, readonly: !can_write || !write_access,
on_saved: function (record, changed) { on_saved: function(record, changed) {
if (changed) { if (changed) {
self._setValue(self.value.data, {forceChange: true}); self._setValue(self.value.data, {
self.trigger_up('reload', {db_id: self.value.id}); forceChange: true,
});
self.trigger_up("reload", {db_id: self.value.id});
} }
}, },
}).open(); }).open();
}) });
} }
} }
}, },
@ -368,14 +452,12 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
FormFieldMany2ManyTags.include({ FormFieldMany2ManyTags.include({
events: _.extend({}, FormFieldMany2ManyTags.prototype.events, { events: _.extend({}, FormFieldMany2ManyTags.prototype.events, {
'click .badge': '_onOpenBadge', "click .badge": "_onOpenBadge",
}), }),
_onOpenBadge: function (event) { _onOpenBadge: function(event) {
var open = this.is_option_set(this.nodeOptions.open); var open = this.is_option_set(this.nodeOptions.open);
var no_color_picker = this.is_option_set( var no_color_picker = this.is_option_set(this.nodeOptions.no_color_picker);
this.nodeOptions.no_color_picker
);
this._super.apply(this, arguments); this._super.apply(this, arguments);
if (!open && !no_color_picker) { if (!open && !no_color_picker) {
this._onOpenColorPicker(event); this._onOpenColorPicker(event);