[MIG] web_listview_range_select: Migration to 16.0

This commit is contained in:
Christopher Rogos
2023-09-11 08:16:35 +00:00
parent 550792d8e6
commit c14911308a
8 changed files with 120 additions and 111 deletions

View File

@@ -0,0 +1,101 @@
/** @odoo-module */
import {ListRenderer} from "@web/views/list/list_renderer";
import {patch} from "@web/core/utils/patch";
export const RangeListSelector = {
setup() {
this._super(...arguments);
this.range_history = [];
},
_getRangeSelection() {
var self = this;
// Get start and end
var start = null,
end = null;
$(".o_list_record_selector input").each(function (i, el) {
var id = $(el).closest("tr").data("id");
var checked = self.range_history.indexOf(id) !== -1;
if (checked && $(this).is(":checked")) {
if (start === null) {
start = i;
} else {
end = i;
}
}
});
var new_range = this._getSelectionByRange(start, end);
var current_selection = [];
current_selection = _.uniq(current_selection.concat(new_range));
return current_selection;
},
_getSelectionByRange(start, end) {
var result = [];
$(".o_list_record_selector input")
.closest("tr")
.each(function (i, el) {
var record_id = $(el).data("id");
if (start !== null && end !== null && i >= start && i <= end) {
result.push(record_id);
} else if (start !== null && end === null && start === i) {
result.push(record_id);
}
});
return result;
},
_pushRangeHistory(id) {
if (this.range_history !== undefined) {
if (this.range_history.length === 2) {
this.range_history = [];
}
}
this.range_history.push(id);
},
_deselectTable() {
// This is needed because the checkboxes are not real checkboxes.
window.getSelection().removeAllRanges();
},
_onClickSelectRecord(record, ev) {
const el = $(ev.currentTarget);
if (el.find("input").prop("checked")) {
this._pushRangeHistory(el.closest("tr").data("id"));
}
if (ev.shiftKey) {
// Get selection
var selection = this._getRangeSelection();
var $rows = $("td.o_list_record_selector input").closest("tr");
$rows.each(function () {
var record_id = $(this).data("id");
if (selection.indexOf(record_id) !== -1) {
$(this)
.find("td.o_list_record_selector input")
.prop("checked", true);
}
});
// Update selection internally
this.checkBoxSelections(selection);
this._deselectTable();
}
},
checkBoxSelections(selection) {
const record = this.props.list.records;
for (const line in record) {
for (const id in selection) {
if (selection[selection.length - 1] === selection[id]) {
continue;
}
if (selection[id] === record[line].id) {
record[line].selected = true;
record[line].model.trigger("update");
continue;
}
}
}
},
};
patch(
ListRenderer.prototype,
"web_listview_range_select.WebListviewRangeSelect",
RangeListSelector
);

View File

@@ -1,106 +0,0 @@
/* Copyright 2017 Onestein
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define("web_listview_range_select", function (require) {
"use strict";
var ListRenderer = require("web.ListRenderer");
ListRenderer.include({
events: _.extend({}, ListRenderer.prototype.events, {
"click tbody .o_list_record_selector": "_onClickSelectRecord",
}),
_range_history: [],
_render: function () {
var res = this._super.apply(this, arguments);
this.$table = this.$el.find(".o_list_view");
return res;
},
_getRangeSelection: function () {
var self = this;
// Get start and end
var start = null,
end = null;
this.$el.find("td.o_list_record_selector input").each(function (i, el) {
var id = $(el).closest("tr").data("id");
var checked = self._range_history.indexOf(id) !== -1;
if (checked && $(el).is(":checked")) {
if (start === null) {
start = i;
} else {
end = i;
}
}
});
var new_range = this._getSelectionByRange(start, end);
var current_selection = this.selection;
current_selection = _.uniq(current_selection.concat(new_range));
return current_selection;
},
_getSelectionByRange: function (start, end) {
var result = [];
this.$el
.find("td.o_list_record_selector input")
.closest("tr")
.each(function (i, el) {
var record_id = $(el).data("id");
if (start !== null && end !== null && i >= start && i <= end) {
result.push(record_id);
} else if (start !== null && end === null && start === i) {
result.push(record_id);
}
});
return result;
},
_pushRangeHistory: function (id) {
if (this._range_history.length === 2) {
this._range_history = [];
}
this._range_history.push(id);
},
_deselectTable: function () {
// This is needed because the checkboxes are not real checkboxes.
window.getSelection().removeAllRanges();
},
_onClickSelectRecord: function (event) {
var el = $(event.currentTarget);
// Firefox shift click fix
if (/firefox/i.test(navigator.userAgent) && event.shiftKey) {
el.find("input").prop("checked", !el.find("input").prop("checked"));
}
if (el.find("input").prop("checked")) {
this._pushRangeHistory(el.closest("tr").data("id"));
}
if (event.shiftKey) {
// Get selection
var selection = this._getRangeSelection();
var $rows = this.$el
.find("td.o_list_record_selector input")
.closest("tr");
$rows.each(function () {
// Check input visual
var record_id = $(this).data("id");
if (selection.indexOf(record_id) !== -1) {
$(this)
.find("td.o_list_record_selector input")
.prop("checked", true);
}
});
// Update selection internally
this._updateSelection();
this._deselectTable();
}
},
});
});

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-inherit="web.ListRenderer.RecordRow" t-inherit-mode="extension">
<xpath expr="//tr[@class='o_data_row']/td/CheckBox" position="attributes">
<attribute
name="t-on-click.capture"
>(ev) => this._onClickSelectRecord(record,ev)</attribute>
</xpath>
</t>
</templates>