Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions web_m2x_options/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

{
"name": 'web_m2x_options',
"version": "8.0.0.2",
"version": "9.0.1.0.0",
"depends": [
'base',
'web',
],
'data': ['views/view.xml'],
"author": "0k.io,Odoo Community Association (OCA)",
'installable': False,
"active": False,
'installable': True,
"active": True,
}
244 changes: 142 additions & 102 deletions web_m2x_options/static/src/js/form.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
/*global openerp, _, $ */

openerp.web_m2x_options = function (instance) {

odoo.define('web.m2xOptions', function (require) {
"use strict";

var QWeb = instance.web.qweb,
_t = instance.web._t,
_lt = instance.web._lt;
var core = require('web.core');
var crash_manager = require('web.crash_manager');
var data = require('web.data');
var Dialog = require('web.Dialog');
var Model = require('web.DataModel');
var Sidebar = require('web.Sidebar');
var utils = require('web.utils');
var View = require('web.View');

var _t = core._t;

var OPTIONS = ['web_m2x_options.create',
'web_m2x_options.create_edit',
'web_m2x_options.limit',
'web_m2x_options.search_more',
'web_m2x_options.m2o_dialog',];

instance.web.form.FieldMany2One = instance.web.form.FieldMany2One.extend({
var M2ODialog = Dialog.extend({
template: "M2ODialog",
init: function(parent) {
this.name = parent.string;
this._super(parent, {
title: _.str.sprintf(_t("Create a %s"), parent.string),
size: 'medium',
buttons: [
{text: _t('Create'), classes: 'btn-primary', click: function() {
if (this.$("input").val() !== ''){
this.getParent()._quick_create(this.$("input").val());
this.close();
} else {
e.preventDefault();
this.$("input").focus();
}
}},

{text: _t('Create and edit'), classes: 'btn-primary', close: true, click: function() {
this.getParent()._search_create_popup("form", undefined, this.getParent()._create_context(this.$("input").val()));
}},

{text: _t('Cancel'), close: true}
]
});
},
start: function() {
var text = _.str.sprintf(_t("You are creating a new %s, are you sure it does not exist yet?"), this.name);
this.$("p").text(text);
this.$("input").val(this.getParent().$input.val());
},
});

var FieldMany2One = core.form_widget_registry.get('many2one').extend({

start: function() {
this._super.apply(this, arguments);
Expand All @@ -26,7 +63,7 @@ openerp.web_m2x_options = function (instance) {
if (!_.isUndefined(this.view) && _.isUndefined(this.view.ir_options_loaded)) {
this.view.ir_options_loaded = $.Deferred();
this.view.ir_options = {};
(new instance.web.Model("ir.config_parameter"))
(new Model("ir.config_parameter"))
.query(["key", "value"]).filter([['key', 'in', OPTIONS]])
.all().then(function(records) {
_(records).each(function(record) {
Expand Down Expand Up @@ -57,12 +94,12 @@ openerp.web_m2x_options = function (instance) {
if(this.is_option_set(this.options.m2o_dialog) ||
_.isUndefined(this.options.m2o_dialog) && this.is_option_set(this.view.ir_options['web_m2x_options.m2o_dialog']) ||
this.can_create && _.isUndefined(this.options.m2o_dialog) && _.isUndefined(this.view.ir_options['web_m2x_options.m2o_dialog'])) {
new instance.web.form.M2ODialog(this).open();
new M2ODialog(this).open();
}
},

get_search_result: function (search_val) {
var Objects = new instance.web.Model(this.field.relation);
var Objects = new Model(this.field.relation);
var def = $.Deferred();
var self = this;
// add options limit used to change number of selections record
Expand All @@ -86,22 +123,22 @@ openerp.web_m2x_options = function (instance) {
this.field_color = this.options.field_color
this.colors = this.options.colors

var dataset = new instance.web.DataSet(this, this.field.relation,
var dataset = new data.DataSetStatic(this, this.field.relation,
self.build_context());
var blacklist = this.get_search_blacklist();
this.last_query = search_val;

var search_result = this.orderer.add(dataset.name_search(
search_val,
new instance.web.CompoundDomain(
new data.CompoundDomain(
self.build_domain(), [["id", "not in", blacklist]]),
'ilike', this.limit + 1,
self.build_context()));

var create_rights;
if (typeof this.options.create === "undefined" ||
typeof this.options.create_edit === "undefined") {
create_rights = new instance.web.Model(this.field.relation).call(
create_rights = new Model(this.field.relation).call(
"check_access_rights", ["create", false]);
}

Expand Down Expand Up @@ -218,124 +255,127 @@ openerp.web_m2x_options = function (instance) {
}
});

instance.web.form.FieldMany2ManyTags.include({

var FieldMany2ManyTags = core.form_widget_registry.get('many2many_tags').extend({

show_error_displayer: function () {
if ((typeof this.options.m2o_dialog === 'undefined' && this.can_create) ||
this.options.m2o_dialog) {
new instance.web.form.M2ODialog(this).open();
new M2ODialog(this).open();
}
},
start: function() {
this._super.apply(this, arguments);
return this.get_options();
},

get_options: function() {
var self = this;
if (_.isUndefined(this.view.ir_options_loaded)) {
this.view.ir_options_loaded = $.Deferred();
this.view.ir_options = {};
(new instance.web.Model("ir.config_parameter"))
.query(["key", "value"]).filter([['key', 'in', OPTIONS]])
.all().then(function(records) {
_(records).each(function(record) {
self.view.ir_options[record.key] = record.value;
});
self.view.ir_options_loaded.resolve();
});
}
return this.view.ir_options_loaded;
},

start: function() {
this._super.apply(this, arguments);
return this.get_options();
},

get_options: function() {
var self = this;
if (_.isUndefined(this.view.ir_options_loaded)) {
this.view.ir_options_loaded = $.Deferred();
this.view.ir_options = {};
(new Model("ir.config_parameter"))
.query(["key", "value"]).filter([['key', 'in', OPTIONS]])
.all().then(function(records) {
_(records).each(function(record) {
self.view.ir_options[record.key] = record.value;
});
self.view.ir_options_loaded.resolve();
});
}
return this.view.ir_options_loaded;
},

/**
* Call this method to search using a string.
*/

get_search_result: function(search_val) {
var self = this;

// add options limit used to change number of selections record
// returned.

if (!_.isUndefined(this.view.ir_options['web_m2x_options.limit'])) {
this.limit = parseInt(this.view.ir_options['web_m2x_options.limit']);
}

if (typeof this.options.limit === 'number') {
this.limit = this.options.limit;
if (!_.isUndefined(this.view.ir_options['web_m2x_options.limit'])) {
this.limit = parseInt(this.view.ir_options['web_m2x_options.limit']);
}

var dataset = new instance.web.DataSet(this, this.field.relation, self.build_context());
var blacklist = this.get_search_blacklist();
this.last_query = search_val;

return this.orderer.add(dataset.name_search(
search_val, new instance.web.CompoundDomain(self.build_domain(), [["id", "not in", blacklist]]),
'ilike', this.limit + 1, self.build_context())).then(function(data) {
self.last_search = data;
// possible selections for the m2o
var values = _.map(data, function(x) {
x[1] = x[1].split("\n")[0];
return {
label: _.str.escapeHTML(x[1]),
value: x[1],
name: x[1],
id: x[0],
};
});

// search more... if more results that max
if (values.length > self.limit) {
values = values.slice(0, self.limit);
values.push({
label: _t("Search More..."),
action: function() {
dataset.name_search(search_val, self.build_domain(), 'ilike', false).done(function(data) {
self._search_create_popup("search", data);
});
},
classname: 'oe_m2o_dropdown_option'
});
if (typeof this.options.limit === 'number') {
this.limit = this.options.limit;
}
// quick create

if ((_.isUndefined(self.options.create) && _.isUndefined(self.view.ir_options['web_m2x_options.create'])) ||
(_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create'] == 'True') ||
self.options.create) {
var dataset = new data.DataSetStatic(this, this.field.relation, self.build_context());
var blacklist = this.get_search_blacklist();
this.last_query = search_val;

return this.orderer.add(dataset.name_search(
search_val, new data.CompoundDomain(self.build_domain(), [["id", "not in", blacklist]]),
'ilike', this.limit + 1, self.build_context())).then(function(data) {
self.last_search = data;
// possible selections for the m2o
var values = _.map(data, function(x) {
x[1] = x[1].split("\n")[0];
return {
label: _.str.escapeHTML(x[1]),
value: x[1],
name: x[1],
id: x[0],
};
});

var raw_result = _(data.result).map(function(x) {return x[1];});
if (search_val.length > 0 && !_.include(raw_result, search_val)) {
// search more... if more results that max
if (values.length > self.limit) {
values = values.slice(0, self.limit);
values.push({
label: _.str.sprintf(_t('Create "<strong>%s</strong>"'),
$('<span />').text(search_val).html()),
label: _t("Search More..."),
action: function() {
self._quick_create(search_val);
dataset.name_search(search_val, self.build_domain(), 'ilike', false).done(function(data) {
self._search_create_popup("search", data);
});
},
classname: 'oe_m2o_dropdown_option'
});
}
}
// quick create

if ((_.isUndefined(self.options.create) && _.isUndefined(self.view.ir_options['web_m2x_options.create'])) ||
(_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create'] == 'True') ||
self.options.create) {

var raw_result = _(data.result).map(function(x) {return x[1];});
if (search_val.length > 0 && !_.include(raw_result, search_val)) {
values.push({
label: _.str.sprintf(_t('Create "<strong>%s</strong>"'),
$('<span />').text(search_val).html()),
action: function() {
self._quick_create(search_val);
},
classname: 'oe_m2o_dropdown_option'
});
}
}

// create...
// create...

if ((_.isUndefined(self.options.create_edit === 'undefined') && _.isUndefined(self.view.ir_options['web_m2x_options.create_edit'])) ||
(_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create_edit'] == 'True') ||
self.options.create_edit) {
if ((_.isUndefined(self.options.create_edit === 'undefined') && _.isUndefined(self.view.ir_options['web_m2x_options.create_edit'])) ||
(_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create_edit'] == 'True') ||
self.options.create_edit) {

values.push({
label: _t("Create and Edit..."),
action: function() {
self._search_create_popup("form", undefined, self._create_context(search_val));
},
classname: 'oe_m2o_dropdown_option'
});
}
values.push({
label: _t("Create and Edit..."),
action: function() {
self._search_create_popup("form", undefined, self._create_context(search_val));
},
classname: 'oe_m2o_dropdown_option'
});
}

return values;
})
return values;
})
},
});
};

core.form_widget_registry
.add('many2one', FieldMany2One)
.add('many2many_tags', FieldMany2ManyTags);
});