Browse Source

[ADD] Selecting multiple ranges

pull/532/head
tarteo 8 years ago
parent
commit
9fcfa76289
  1. 61
      web_listview_range_select/static/src/js/web_listview_multi_select.js
  2. 43
      web_listview_range_select/static/src/js/web_listview_range_select.js

61
web_listview_range_select/static/src/js/web_listview_multi_select.js

@ -1,61 +0,0 @@
/* Copyright 2017 Onestein
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define('web_listview_multi_select', function (require) {
"use strict";
var core = require('web.core'),
_t = core._t,
listview = require('web.ListView');
listview.List.include({
get_range_selection: function() {
var result = {ids: [], records: []};
if (!this.options.selectable) {
return result;
}
var records = this.records;
var start = null,
end = null;
//Get start and end;
this.$current.find('td.o_list_record_selector input').each(function (i, el) {
var checked = $(el).prop('checked');
if (checked) {
if (start == null)
start = i;
else
end = i;
}
});
this.$current.find('td.o_list_record_selector input').closest('tr').each(function (i, el) {
var record = records.get($(el).data('id'));
if (start != null && end != null && i >= start && i <= end) {
result.ids.push(record.get('id'));
result.records.push(record.attributes);
} else if(start != null && end == null && start == i) {
result.ids.push(record.get('id'));
result.records.push(record.attributes);
}
});
return result;
},
init: function() {
var self = this;
var res = this._super.apply(this, arguments);
this.$current.delegate('td.o_list_record_selector', 'click', function (e) {
if (e.shiftKey) {
var selection = self.get_range_selection();
self.$current.find('td.o_list_record_selector input').closest('tr').each(function () {
var record = self.records.get($(this).data('id'));
if (selection.ids.indexOf(record.get('id')) != -1)
$(this).find('td.o_list_record_selector input').prop('checked', true);
});
$(self).trigger(
'selected', [selection.ids, selection.records, true]);
}
});
return res;
}
});
});

43
web_listview_range_select/static/src/js/web_listview_range_select.js

@ -8,19 +8,23 @@ odoo.define('web_listview_range_select', function (require) {
listview = require('web.ListView'); listview = require('web.ListView');
listview.List.include({ listview.List.include({
get_range_selection: function() {
_range_history: [],
_get_range_selection: function() {
var self = this;
var result = {ids: [], records: []}; var result = {ids: [], records: []};
if (!this.options.selectable) { if (!this.options.selectable) {
return result; return result;
} }
var records = this.records; var records = this.records;
//Get start and end
var start = null, var start = null,
end = null; end = null;
//Get start and end;
this.$current.find('td.o_list_record_selector input').each(function (i, el) { this.$current.find('td.o_list_record_selector input').each(function (i, el) {
var checked = $(el).prop('checked');
if (checked) {
var id = $(el).closest('tr').data('id');
var checked = self._range_history.indexOf(id) != -1;
if (checked && $(el).prop('checked')) {
if (start == null) if (start == null)
start = i; start = i;
else else
@ -28,6 +32,23 @@ odoo.define('web_listview_range_select', function (require) {
} }
}); });
//Preserve already checked
this.$current.find('td.o_list_record_selector input:checked').closest('tr').each(function (i, el) {
if (i == start || i == end) return;
var record = records.get($(el).data('id'));
result.ids.push(record.get('id'));
result.records.push(record.attributes);
});
var new_range = this.get_range_selection(start, end);
result.records = result.records.concat(new_range.records);
result.ids = result.ids.concat(new_range.ids);
return result;
},
get_range_selection: function(start, end) {
var records = this.records;
var result = {ids: [], records: []};
this.$current.find('td.o_list_record_selector input').closest('tr').each(function (i, el) { this.$current.find('td.o_list_record_selector input').closest('tr').each(function (i, el) {
var record = records.get($(el).data('id')); var record = records.get($(el).data('id'));
if (start != null && end != null && i >= start && i <= end) { if (start != null && end != null && i >= start && i <= end) {
@ -40,17 +61,29 @@ odoo.define('web_listview_range_select', function (require) {
}); });
return result; return result;
}, },
push_range_history: function(id) {
this._range_history.push(id);
if (this._range_history.length == 3)
this._range_history.shift();
},
init: function() { init: function() {
var self = this; var self = this;
var res = this._super.apply(this, arguments); var res = this._super.apply(this, arguments);
this.$current.delegate('td.o_list_record_selector', 'click', function (e) { this.$current.delegate('td.o_list_record_selector', 'click', function (e) {
//Update history
if ($(this).find('input').prop('checked'))
self.push_range_history($(this).closest('tr').data('id'));
if (e.shiftKey) { if (e.shiftKey) {
var selection = self.get_range_selection();
//Get selection
var selection = self._get_range_selection();
self.$current.find('td.o_list_record_selector input').closest('tr').each(function () { self.$current.find('td.o_list_record_selector input').closest('tr').each(function () {
//Check input visual
var record = self.records.get($(this).data('id')); var record = self.records.get($(this).data('id'));
if (selection.ids.indexOf(record.get('id')) != -1) if (selection.ids.indexOf(record.get('id')) != -1)
$(this).find('td.o_list_record_selector input').prop('checked', true); $(this).find('td.o_list_record_selector input').prop('checked', true);
}); });
//Check input internal
$(self).trigger( $(self).trigger(
'selected', [selection.ids, selection.records, true]); 'selected', [selection.ids, selection.records, true]);
} }

Loading…
Cancel
Save