You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.3 KiB

  1. /* Copyright 2017 Onestein
  2. * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
  3. odoo.define('web_listview_range_select', function (require) {
  4. "use strict";
  5. var ListRenderer = require('web.ListRenderer');
  6. ListRenderer.include({
  7. _range_history: [],
  8. _get_range_selection: function() {
  9. var self = this;
  10. var result = {ids: [], records: []};
  11. //Get start and end
  12. var start = null,
  13. end = null;
  14. this.$el.find('td.o_list_record_selector input').each(function (i, el) {
  15. var id = $(el).closest('tr').data('id');
  16. var checked = self._range_history.indexOf(id) != -1;
  17. if (checked && $(el).prop('checked')) {
  18. if (start == null)
  19. start = i;
  20. else
  21. end = i;
  22. }
  23. });
  24. //Preserve already checked
  25. this.$el.find('td.o_list_record_selector input:checked').closest('tr').each(function (i, el) {
  26. if (i == start || i == end) return;
  27. var record_id = $(el).data('id')
  28. result.ids.push(record_id);
  29. result.records.push(el.attributes);
  30. });
  31. var new_range = this.get_range_selection(start, end);
  32. result.records = result.records.concat(new_range.records);
  33. result.ids = result.ids.concat(new_range.ids);
  34. return result;
  35. },
  36. get_range_selection: function(start, end) {
  37. var result = {ids: [], records: []};
  38. this.$el.find('td.o_list_record_selector input').closest('tr').each(function (i, el) {
  39. var record_id = $(el).data('id');
  40. if (start != null && end != null && i >= start && i <= end) {
  41. result.ids.push(record_id);
  42. result.records.push(el.attributes);
  43. } else if(start != null && end == null && start == i) {
  44. result.ids.push(record_id);
  45. result.records.push(el.attributes);
  46. }
  47. });
  48. return result;
  49. },
  50. push_range_history: function(id) {
  51. this._range_history.push(id);
  52. if (this._range_history.length == 3)
  53. this._range_history.shift();
  54. },
  55. _onSelectRecord: function(event) {
  56. var res = this._super(event);
  57. var self = this;
  58. var el = $(event.currentTarget);
  59. if (el.find('input').prop('checked'))
  60. self.push_range_history(el.closest('tr').data('id'));
  61. if (event.shiftKey) {
  62. //Get selection
  63. var selection = self._get_range_selection();
  64. this.$el.find('td.o_list_record_selector input').closest('tr').each(function () {
  65. //Check input visual
  66. var record_id = $(this).data('id');
  67. if (selection.ids.indexOf(record_id) != -1)
  68. $(this).find('td.o_list_record_selector input').prop('checked', true);
  69. });
  70. //Check input internal
  71. $(self).trigger(
  72. 'selected', [selection.ids, selection.records, true]);
  73. }
  74. return res;
  75. }
  76. });
  77. });