From 15169c16eab9066141764c6294ebb3be01aee90c Mon Sep 17 00:00:00 2001 From: Samuel Lefever Date: Thu, 5 Nov 2015 17:35:24 +0100 Subject: [PATCH] Update for v9 --- web_m2x_options/__openerp__.py | 7 +- web_m2x_options/static/src/js/form.js | 244 +++++++++++++++----------- 2 files changed, 145 insertions(+), 106 deletions(-) diff --git a/web_m2x_options/__openerp__.py b/web_m2x_options/__openerp__.py index be57d3bdf5bb..060e54d54177 100644 --- a/web_m2x_options/__openerp__.py +++ b/web_m2x_options/__openerp__.py @@ -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, } diff --git a/web_m2x_options/static/src/js/form.js b/web_m2x_options/static/src/js/form.js index bc4d3bebd335..38c5d313fd04 100644 --- a/web_m2x_options/static/src/js/form.js +++ b/web_m2x_options/static/src/js/form.js @@ -1,12 +1,16 @@ -/*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', @@ -14,7 +18,40 @@ openerp.web_m2x_options = function (instance) { '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); @@ -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) { @@ -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 @@ -86,14 +123,14 @@ 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())); @@ -101,7 +138,7 @@ openerp.web_m2x_options = function (instance) { 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]); } @@ -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 "%s"'), - $('').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 "%s"'), + $('').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); +});