|
| 1 | +/** @odoo-module */ |
| 2 | + |
| 3 | +import {ListRenderer} from "@web/views/list/list_renderer"; |
| 4 | +import {patch} from "@web/core/utils/patch"; |
| 5 | + |
| 6 | +export const RangeListSelector = { |
| 7 | + setup() { |
| 8 | + this._super(...arguments); |
| 9 | + this.range_history = []; |
| 10 | + }, |
| 11 | + _getRangeSelection() { |
| 12 | + var self = this; |
| 13 | + // Get start and end |
| 14 | + var start = null, |
| 15 | + end = null; |
| 16 | + $(".o_list_record_selector input").each(function (i, el) { |
| 17 | + var id = $(el).closest("tr").data("id"); |
| 18 | + var checked = self.range_history.indexOf(id) !== -1; |
| 19 | + if (checked && $(this).is(":checked")) { |
| 20 | + if (start === null) { |
| 21 | + start = i; |
| 22 | + } else { |
| 23 | + end = i; |
| 24 | + } |
| 25 | + } |
| 26 | + }); |
| 27 | + var new_range = this._getSelectionByRange(start, end); |
| 28 | + |
| 29 | + var current_selection = []; |
| 30 | + current_selection = _.uniq(current_selection.concat(new_range)); |
| 31 | + return current_selection; |
| 32 | + }, |
| 33 | + _getSelectionByRange(start, end) { |
| 34 | + var result = []; |
| 35 | + $(".o_list_record_selector input") |
| 36 | + .closest("tr") |
| 37 | + .each(function (i, el) { |
| 38 | + var record_id = $(el).data("id"); |
| 39 | + if (start !== null && end !== null && i >= start && i <= end) { |
| 40 | + result.push(record_id); |
| 41 | + } else if (start !== null && end === null && start === i) { |
| 42 | + result.push(record_id); |
| 43 | + } |
| 44 | + }); |
| 45 | + return result; |
| 46 | + }, |
| 47 | + _pushRangeHistory(id) { |
| 48 | + if (this.range_history !== undefined) { |
| 49 | + if (this.range_history.length === 2) { |
| 50 | + this.range_history = []; |
| 51 | + } |
| 52 | + } |
| 53 | + this.range_history.push(id); |
| 54 | + }, |
| 55 | + _deselectTable() { |
| 56 | + // This is needed because the checkboxes are not real checkboxes. |
| 57 | + window.getSelection().removeAllRanges(); |
| 58 | + }, |
| 59 | + _onClickSelectRecord(record, ev) { |
| 60 | + const el = $(ev.currentTarget); |
| 61 | + if (el.find("input").prop("checked")) { |
| 62 | + this._pushRangeHistory(el.closest("tr").data("id")); |
| 63 | + } |
| 64 | + if (ev.shiftKey) { |
| 65 | + // Get selection |
| 66 | + var selection = this._getRangeSelection(); |
| 67 | + var $rows = $("td.o_list_record_selector input").closest("tr"); |
| 68 | + $rows.each(function () { |
| 69 | + var record_id = $(this).data("id"); |
| 70 | + if (selection.indexOf(record_id) !== -1) { |
| 71 | + $(this) |
| 72 | + .find("td.o_list_record_selector input") |
| 73 | + .prop("checked", true); |
| 74 | + } |
| 75 | + }); |
| 76 | + // Update selection internally |
| 77 | + this.checkBoxSelections(selection); |
| 78 | + this._deselectTable(); |
| 79 | + } |
| 80 | + }, |
| 81 | + checkBoxSelections(selection) { |
| 82 | + const record = this.props.list.records; |
| 83 | + for (const line in record) { |
| 84 | + for (const id in selection) { |
| 85 | + if (selection[selection.length - 1] === selection[id]) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + if (selection[id] === record[line].id) { |
| 89 | + record[line].selected = true; |
| 90 | + record[line].model.trigger("update"); |
| 91 | + continue; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + }, |
| 96 | +}; |
| 97 | +patch( |
| 98 | + ListRenderer.prototype, |
| 99 | + "web_listview_range_select.WebListviewRangeSelect", |
| 100 | + RangeListSelector |
| 101 | +); |
0 commit comments