Support range select in patch-list
cover Support range select in patch-list
Selecting multiple items in a patch list must be done manually, one item at a time, providing a poor user experience.
These changes add the jquery.tablecheckbox plugin and call it from the patch-list tables to allow users to select single / multiple / all table row(s) by clicking.
A function specific to the series view is used to provide the mentioned feature too.
[YOCTO #10819]
Jose Lamego (3):
series.js: support shift-select range jquery.tablecheckbox: add plugin for multiselect functionality
patch-list: call tablecheckbox plugin from patch-list
htdocs/js/jquery.tablecheckbox.js | 165 ++++++++++++++++++++++++++ htdocs/js/series.js | 31 +++++ patchwork/templates/patchwork/patch-list.html | 2 + 3 files changed, 198 insertions(+) create mode 100644 htdocs/js/jquery.tablecheckbox.js
Signature · 1 line
1.9.1
PATCH 1/3 series.js: support shift-select range
Selecting multiple patches in a range at the series view must be performed on one-by-one basis, providing apoor user-experience.
This change allows to select patches in a range by clicking the first patch, then shift-clicking the last patch in the desired range.
[YOCTO #10819]
--- htdocs/js/series.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
htdocs/js/series.js +31 -0
@@ -82,6 +82,37 @@ $(document).ready(function(){ }) }) + var lastChecked = null + var $chkboxes = $( "input[name^='patch_id']" ) + + $chkboxes.click(function(e){ + if(!lastChecked) { + lastChecked = this; + return; + } + + if(e.shiftKey) { + var start = $chkboxes.index(this); + var end = $chkboxes.index(lastChecked); + var min_val = Math.min(start,end) + var max_val = Math.max(start,end)+1 + + $chkboxes.slice(min_val, max_val).prop('checked', lastChecked.checked); + for (i=min_val; i<max_val; ++i){ + p_id=$chkboxes[i].getAttribute("name") + p_id=p_id.match(/\d+/)[0] + if (lastChecked.checked){ + insert_patchId(p_id) + } + else{ + remove_patchId(p_id) + } + } + } + + lastChecked = this; + }) + $( "input[name^='patch_id']" ).change(function(){ patchView.style.display="none" uncheck_allSel()
Signature · 1 line
1.9.1
PATCH 2/3 jquery.tablecheckbox: add plugin for multiselect functionality
In reply to Jose LamegoSelecting multiple items in a patch list must be done manually, one item at a time, resulting in a poor user experience.
This change adds the jquery.tablecheckbox.js plugin that allows
to select single / multiple / all table row(s) by clicking.
[YOCTO #10819]
--- htdocs/js/jquery.tablecheckbox.js | 165 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 htdocs/js/jquery.tablecheckbox.js
htdocs/js/jquery.tablecheckbox.js new file +165 -0
@@ -0,0 +1,165 @@ +(function($) { + /** + * A jQuery plugin that lets you easily enhance data tables with selectable rows. + * + * + * @author Marco Kerwitz <[email protected]> + * @see https://github.com/kerwitz/jquery.tablecheckbox + */ + $.fn.tablecheckbox = function(options) { + var _private = { + /** + * The configuration of this plugin. + * + * Overload with custom values when calling the plugin: + * $('table').tableCheckbox({selectedRowClass: 'selected-row'}); + * + * @var object + */ + config: $.extend({ + // The class that will be applied to selected rows. + selectedRowClass: 'warning', + // The selector used to find the checkboxes on the table. You may customize this in + // order to match your table layout if it differs from the assumed one. + checkboxSelector: 'td:first-of-type input[type="checkbox"],th:first-of-type input[type="checkbox"]', + // A callback that is used to determine wether a checkbox is selected or not. + isChecked: function($checkbox) { + return $checkbox.is(':checked'); + } + }, options), + /** + * Variables used across multiple tables. + * + * @var object + */ + registry: { + shiftKeyIsPressed: false + }, + helpers: { + /** + * Returns the selection methods available in the current browser. + * + * @author Grinn, http://stackoverflow.com/users/152648/grinn + * @author Gert Grenander, http://stackoverflow.com/users/339850/gert-grenander + * @see http://stackoverflow.com/questions/3169786/clear-text-selection-with-javascript + */ + selection: window.getSelection ? window.getSelection() : document.selection ? document.selection : null, + /** + * Removes any text selection the user made. + * + * @author Marco Kerwitz <[email protected]> + */ + removeTextSelection: function() { + if (!!_private.helpers.selection) { + _private.helpers.selection.empty + ? _private.helpers.selection.empty() + : _private.helpers.selection.removeAllRanges(); + } + }, + /** + * Returns wether or not a text selection currently exists. + * + * @author Marco Kerwitz <[email protected]> + * @todo This will return false positives when the user selected text outside of + * the table and then tries to select rows. + * @return {boolean} + */ + hasSelection: function() { + return !!_private.helpers.selection && _private.helpers.selection.toString().length; + } + } + }; + // Initiate a event callback that we can use to tell wether the user is pressing the shift + // key or not. + $(document).on('keydown.tsc keyup.tsc', function(e) { + _private.registry.shiftKeyIsPressed = e.shiftKey; + }); + return this.each(function() { + var $table = $(this), + $headCheckbox = $table.find('thead tr ' + _private.config.checkboxSelector), + $checkboxes = $table.find('tr ' + _private.config.checkboxSelector).not($headCheckbox), + $lastRow = []; + // Listen for changes on the checkbox in the table header and apply its current state + // to all checkboxe on the table. + $headCheckbox.on('change', function(e) { + var $allRows = $table.find('tbody tr'); + $checkboxes + .prop('checked', _private.config.isChecked($headCheckbox)) + .trigger('change'); + $table.trigger( + _private.config.isChecked($headCheckbox) ? 'multirowselect' : 'multirowdeselect', + { + $rows: $allRows, + $checkboxes: $allRows.find(_private.config.checkboxSelector), + rowCount: $allRows.length + } + ); + }); + // Cycle through each checkbox found on the table. + $checkboxes.each(function() { + var $checkbox = $(this), + $row = $checkbox.parents('tr'), + $inbetween, + $allRows; + $checkbox.on('change', function(e, isInternal) { + // When the user clicks directly on the rows he will unwillingly select + // the text of all rows inbetween. Remove that selection immediately. + _private.helpers.removeTextSelection(); + if (!isInternal && _private.registry.shiftKeyIsPressed && $lastRow.length) { + // User held shift key while clicking on this checkbox and clicked another one + // prior. Get all checkboxes inbetween the two and check or uncheck them. + $inbetween = ($lastRow.index() < $row.index()) + ? $row.prevUntil($lastRow) + : $row.nextUntil($lastRow); + $inbetween.find(_private.config.checkboxSelector) + .prop('checked', _private.config.isChecked($checkbox)) + .trigger('change', [true]); + $allRows = $inbetween.add($row, $lastRow); + // Trigger table level event. + $table.trigger( + _private.config.isChecked($checkbox) ? 'multirowselect' : 'multirowdeselect', + { + $rows: $allRows, + $checkboxes: $allRows.find(_private.config.checkboxSelector), + rowCount: $allRows.length + } + ); + } + $lastRow = $row; + $row.toggleClass(_private.config.selectedRowClass, _private.config.isChecked($checkbox)); + // Trigger row level event. + $row.trigger( + _private.config.isChecked($checkbox) ? 'rowselect' : 'rowdeselect', + {$row: $row,$checkbox: $checkbox} + ); + }); + // Monitor the row and check the checkbox accordingly. + $row.on('click', function(e) { + if (_private.helpers.hasSelection()) { + // There was a text slection prior to this click. Chances are that the user + // simply wants to clear that instead of selecting this row - so do nothing. + return; + } + if ($.data($row, 'tc-timeout')) { + // There was a timeout running from a previous click event, this seems to be + // a double click. Cancel the first timeout before we create a new one. + window.clearTimeout($.data($row, 'tc-timeout')); + } + // We use a short timeout to wait for double-click selections that the user might + // have intended to make. If any selection is created withing this timespan we + // wont do anything to not interfere with the users intentions too much. + $.data($row, 'tc-timeout', window.setTimeout(function() { + // Do nothing if the user selected text on the row. + if (_private.helpers.hasSelection()) return; + // Make sure the user did not click on any clickable content in the row. + if (!$(e.target).is('a,input,button') && !$(e.target).parents('a,input,button').length) { + $checkbox + .prop('checked', !_private.config.isChecked($checkbox)) + .trigger('change'); + } + }, 50)); + }) + }); + }); + }; +}(jQuery));
Signature · 1 line
1.9.1
PATCH 3/3 patch-list: call tablecheckbox plugin from patch-list
In reply to Jose LamegoThis change calls the tablecheckbox.js plugin to add functionality in a patch-list to allow user to select single / multiple / all table row(s) by clicking.
[YOCTO #10819]
--- patchwork/templates/patchwork/patch-list.html | 2 ++ 1 file changed, 2 insertions(+)
patchwork/templates/patchwork/patch-list.html +2 -0
@@ -38,8 +38,10 @@ <script type="text/javascript"> $(document).ready(function() { $('#patchlist').stickyTableHeaders(); + $( '.table' ).tablecheckbox(); }); </script> +<script type="text/javascript" src="{% static "js/jquery.tablecheckbox.js" %}"></script> <form method="post"> {% csrf_token %} <input type="hidden" name="form" value="patchlistform"/>
Signature · 1 line
1.9.1