[12.0][MIG] bi_view_editor

This commit is contained in:
Andrea
2019-06-06 13:20:20 +02:00
parent 47e98e495a
commit cd6ba396d5
19 changed files with 791 additions and 636 deletions

View File

@@ -1,4 +1,4 @@
/* Copyright 2015-2018 Onestein (<http://www.onestein.eu>)
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor.FieldList', function (require) {
@@ -12,18 +12,18 @@ odoo.define('bi_view_editor.FieldList', function (require) {
start: function () {
var res = this._super.apply(this, arguments);
this.$el.mouseleave(function () {
$(this).addClass('hidden');
$(this).addClass('d-none');
});
return res;
},
open: function (x, y) {
this.$el.css({
'left': x + 'px',
'top': y + 'px'
'top': y + 'px',
});
this.$el.removeClass('hidden');
this.$el.removeClass('d-none');
return _.extend({}, window.Backbone.Events);
}
},
});
var FieldListFieldContextMenu = FieldListContextMenu.extend({
@@ -55,7 +55,7 @@ odoo.define('bi_view_editor.FieldList', function (require) {
});
return events;
}
},
});
var FieldListJoinContextMenu = FieldListContextMenu.extend({
@@ -72,14 +72,14 @@ odoo.define('bi_view_editor.FieldList', function (require) {
events.trigger('change', node);
});
return events;
}
},
});
var FieldList = Widget.extend({
template: 'bi_view_editor.FieldList',
events: {
'click .delete-button': 'removeClicked',
'keyup input[name="description"]': 'keyupDescription'
'keyup input[name="description"]': 'keyupDescription',
},
start: function () {
var res = this._super.apply(this, arguments);
@@ -94,10 +94,10 @@ odoo.define('bi_view_editor.FieldList', function (require) {
setMode: function (mode) {
if (mode === 'readonly') {
this.$el.find('input[type="text"]').attr('disabled', true);
this.$el.find(".delete-button:last").addClass('hidden');
this.$el.find(".delete-button").addClass('d-none');
} else {
this.$el.find('input[type="text"]').removeAttr('disabled');
this.$el.find(".delete-button:last").removeClass('hidden');
this.$el.find(".delete-button").removeClass('d-none');
}
this.mode = mode;
},
@@ -122,7 +122,7 @@ odoo.define('bi_view_editor.FieldList', function (require) {
var data = $(this).data('field');
model_data[data.table_alias] = {
model_id: data.model_id,
model_name: data.model_name
model_name: data.model_name,
};
});
return model_data;
@@ -149,7 +149,7 @@ odoo.define('bi_view_editor.FieldList', function (require) {
// Render table row
var $html = $(qweb.render(field.join_node ? 'bi_view_editor.JoinListItem' : 'bi_view_editor.FieldListItem', {
'field': field
'field': field,
})).data('field', field).contextmenu(function (e) {
var $item = $(this);
if (self.mode === 'readonly') {
@@ -160,17 +160,10 @@ odoo.define('bi_view_editor.FieldList', function (require) {
});
this.$el.find('tbody').append($html);
this.$el.find(".delete-button").addClass('hidden');
this.$el.find(".delete-button:last").removeClass('hidden');
this.order();
},
remove: function (id) {
var $item = this.$el.find('tr[data-id="' + id + '"]');
$item.remove();
this.cleanJoinNodes();
this.$el.find(".delete-button").addClass('hidden');
this.$el.find(".delete-button:last").removeClass('hidden');
this.trigger('removed', id);
},
set: function (fields) {
@@ -182,8 +175,6 @@ odoo.define('bi_view_editor.FieldList', function (require) {
for (var i = 0; i < set_fields.length; i++) {
this.add(set_fields[i]);
}
this.$el.find(".delete-button").addClass('hidden');
this.$el.find(".delete-button:last").removeClass('hidden');
},
openContextMenu: function ($item, x, y) {
var field = $item.data('field');
@@ -205,9 +196,9 @@ odoo.define('bi_view_editor.FieldList', function (require) {
var $attribute = $(this);
var value = data[$attribute.attr('data-for')];
if (value) {
$attribute.removeClass('hidden');
$attribute.removeClass('d-none');
} else {
$attribute.addClass('hidden');
$attribute.addClass('d-none');
}
});
},
@@ -219,87 +210,12 @@ odoo.define('bi_view_editor.FieldList', function (require) {
keyupDescription: function () {
this.trigger('updated');
},
cleanJoinNodes: function () {
var aliases = $.makeArray(this.$el.find("tbody tr").map(function () {
var data = $(this).data('field');
return data.table_alias.localeCompare(data.join_node) > 0 ? data.join_node : data.table_alias;
}));
this.$el.find("tbody tr").each(function () {
var data = $(this).data('field');
if (typeof data.join_node === 'undefined') {
return;
}
var no_alias = data.table_alias.localeCompare(data.join_node) > 0 &&
aliases.indexOf(data.table_alias) === -1;
if (no_alias ||
aliases.indexOf(data.join_node) === -1) {
$(this).remove();
}
});
},
getOrder: function () {
var items = this.get();
var ordered = items.sort(function (a, b) {
var res = a.table_alias.localeCompare(b.table_alias);
if (res === 0) {
var both_join_node = a.join_node && b.join_node;
var both_not_join_node = !a.join_node && !b.join_node;
if (both_join_node || both_not_join_node) {
return 0;
} else if (!a.join_node && b.join_node) {
if (b.table_alias.localeCompare(b.join_node) > 0) {
return 1;
}
return -1;
} else if (a.join_node && !b.join_node) {
if (a.table_alias.localeCompare(a.join_node) > 0) {
return -1;
}
return 1;
}
}
return res;
});
var res = [];
_.each(ordered, function (item) {
var already_exists = _.findIndex(res, function (f) {
return f._id === item._id;
}) !== -1;
if (already_exists) {
return;
}
res.push(item);
if (item.join_node) {
var join_node_fields = _.filter(ordered, function (f) {
return f.table_alias === item.join_node && !f.join_node;
});
res = _.union(res, join_node_fields);
}
});
return res;
},
order: function () {
var order = this.getOrder();
var $rows = this.$el.find("tbody tr");
$rows.sort(function (a, b) {
var a_index = _.findIndex(order, function (item) {
return item._id === $(a).data('field')._id;
});
var b_index = _.findIndex(order, function (item) {
return item._id === $(b).data('field')._id;
});
return a_index - b_index;
}).appendTo(this.$el.find("tbody"));
}
});
return {
'FieldList': FieldList,
'FieldListContextMenu': FieldListContextMenu,
'FieldListFieldContextMenu': FieldListFieldContextMenu,
'FieldListJoinContextMenu': FieldListJoinContextMenu
'FieldListJoinContextMenu': FieldListJoinContextMenu,
};
});

View File

@@ -1,4 +1,4 @@
/* Copyright 2015-2018 Onestein (<http://www.onestein.eu>)
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor.JoinNodeDialog', function (require) {
@@ -11,10 +11,10 @@ odoo.define('bi_view_editor.JoinNodeDialog', function (require) {
var JoinNodeDialog = Dialog.extend({
xmlDependencies: Dialog.prototype.xmlDependencies.concat([
'/bi_view_editor/static/src/xml/bi_view_editor.xml'
'/bi_view_editor/static/src/xml/bi_view_editor.xml',
]),
events: {
"click li": "choiceClicked"
"click li": "choiceClicked",
},
init: function (parent, options, choices, model_data) {
this.choices = choices;
@@ -30,22 +30,22 @@ odoo.define('bi_view_editor.JoinNodeDialog', function (require) {
title: _t("Join..."),
dialogClass: 'oe_act_window',
$content: qweb.render('bi_view_editor.JoinNodeDialog', {
'choices': choices
'choices': choices,
}),
buttons: [{
text: _t("Cancel"),
classes: "btn-default o_form_button_cancel",
close: true
}]
close: true,
}],
});
this._super(parent, defaults);
},
choiceClicked: function (e) {
this.trigger('chosen', {
choice: this.choices[$(e.currentTarget).attr('data-index')]
choice: this.choices[$(e.currentTarget).attr('data-index')],
});
this.close();
}
},
});
return JoinNodeDialog;

View File

@@ -1,4 +1,4 @@
/* Copyright 2015-2018 Onestein (<http://www.onestein.eu>)
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor.ModelList', function (require) {
@@ -6,13 +6,12 @@ odoo.define('bi_view_editor.ModelList', function (require) {
var Widget = require('web.Widget');
var core = require('web.core');
var session = require('web.session');
var qweb = core.qweb;
var ModelList = Widget.extend({
template: 'bi_view_editor.ModelList',
events: {
'keyup .search-bar > input': 'filterChanged'
'keyup .search-bar > input': 'filterChanged',
},
init: function (parent) {
var res = this._super(parent);
@@ -43,22 +42,10 @@ odoo.define('bi_view_editor.ModelList', function (require) {
this.active_models.push(id);
},
loadModels: function (model_ids) {
if (model_ids) {
return this._rpc({
model: 'ir.model',
method: 'get_related_models',
args: [model_ids],
context: {
lang: session.user_context.lang
}
});
}
return this._rpc({
model: 'ir.model',
method: 'get_models',
context: {
lang: session.user_context.lang
}
args: model_ids ? [model_ids] : [],
});
},
loadFields: function (model_id) {
@@ -67,9 +54,6 @@ odoo.define('bi_view_editor.ModelList', function (require) {
model: 'ir.model',
method: 'get_fields',
args: [model_id],
context: {
lang: session.user_context.lang
}
});
this.cache_fields[model_id] = deferred;
}
@@ -83,7 +67,7 @@ odoo.define('bi_view_editor.ModelList', function (require) {
var $html = $(qweb.render('bi_view_editor.ModelListItem', {
'id': model.id,
'model': model.model,
'name': model.name
'name': model.name,
}));
$html.find('.class').data('model', model).click(function () {
self.modelClicked($(this));
@@ -110,7 +94,7 @@ odoo.define('bi_view_editor.ModelList', function (require) {
_.each(fields, function (field) {
var $field = $(qweb.render('bi_view_editor.ModelListFieldItem', {
name: field.name,
description: field.description
description: field.description,
})).data('field', field).click(function () {
self.fieldClicked($(this));
}).draggable({
@@ -118,7 +102,7 @@ odoo.define('bi_view_editor.ModelList', function (require) {
'scroll': false,
'helper': 'clone',
'appendTo': 'body',
'containment': 'window'
'containment': 'window',
});
$model_item.after($field);
@@ -157,13 +141,13 @@ odoo.define('bi_view_editor.ModelList', function (require) {
var data = $(this).data('model');
if (data.name.toLowerCase().indexOf(val) === -1 &&
data.model.toLowerCase().indexOf(val) === -1) {
$(this).addClass('hidden');
$(this).addClass('d-none');
} else {
$(this).removeClass('hidden');
$(this).removeClass('d-none');
}
});
this.current_filter = val;
}
},
});
return ModelList;

View File

@@ -1,4 +1,4 @@
/* Copyright 2015-2018 Onestein (<http://www.onestein.eu>)
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor', function (require) {
@@ -15,7 +15,7 @@ odoo.define('bi_view_editor', function (require) {
var BiViewEditor = AbstractField.extend({
template: "bi_view_editor.Frame",
events: {
"click .clear-btn": "clear"
"click .clear-btn": "clear",
},
start: function () {
var self = this;
@@ -39,7 +39,7 @@ odoo.define('bi_view_editor', function (require) {
drop: function (event, ui) {
self.addField(_.extend({}, ui.draggable.data('field')));
ui.draggable.draggable('option', 'revert', false);
}
},
});
this.on("change:effective_readonly", this, function () {
@@ -62,18 +62,23 @@ odoo.define('bi_view_editor', function (require) {
},
fieldListRemoved: function () {
console.log(this.field_list.get());
this.loadAndPopulateModelList();
this._setValue(this.field_list.get());
var model = new Data.DataSet(this, "bve.view");
model.call('get_clean_list', [this.value]).then(function (result) {
this.field_list.set(JSON.parse(result));
this._setValue(this.field_list.get());
}.bind(this));
this.loadAndPopulateModelList();
},
renderValue: function () {
this.field_list.set(JSON.parse(this.value));
},
updateMode: function () {
if (this.mode === 'readonly') {
this.$el.find('.clear-btn').addClass('hidden');
this.$el.find('.clear-btn').addClass('d-none');
this.$el.find(".body .right").droppable("option", "disabled", true);
} else {
this.$el.find('.clear-btn').removeClass('hidden');
this.$el.find('.clear-btn').removeClass('d-none');
this.$el.find('.body .right').droppable('option', 'disabled', false);
}
this.field_list.setMode(this.mode);
@@ -129,8 +134,7 @@ odoo.define('bi_view_editor', function (require) {
this.addFieldAndJoinNode(data, e.choice);
});
} else {
var table_alias = this.getTableAlias(data);
data.table_alias = table_alias;
data.table_alias = this.getTableAlias(data);
this.field_list.add(data);
this.loadAndPopulateModelList();
this._setValue(this.field_list.get());
@@ -139,7 +143,7 @@ odoo.define('bi_view_editor', function (require) {
},
_parseValue: function (value) {
return JSON.stringify(value);
}
},
});
field_registry.add('BVEEditor', BiViewEditor);