Add smart checkbox selector. Code donated by the Paperattor project (www.paperattor.com).

Unify checkbox selection code for list items and table items.
Move some javascript initialization to the root template.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-04-03 04:32:47 -04:00
parent 5fa0dc8d0d
commit bad90c9848
6 changed files with 46 additions and 23 deletions
@@ -8,8 +8,11 @@ var partialNavigation = new PartialNavigation({
});
jQuery(document).ready(function() {
app.setupAutoSubmit();
app.setupFullHeightResizing();
app.setupItemsSelector();
app.setupNavbarCollapse();
app.setupNewWindowAnchor();
app.setupAJAXperiodicWorkers();
partialNavigation.initialize();
});
@@ -17,10 +20,6 @@ jQuery(document).ready(function() {
var afterBaseLoad = function () {
MayanImage.intialize();
app.doToastrMessages();
app.setupAutoSubmit();
app.setupItemsSelector();
app.setupNewWindowAnchor();
app.setupTableSelector();
app.resizeFullHeight();
app.setupSelect2();
app.setupScrollView();
@@ -187,7 +187,7 @@ App.prototype.setupAJAXperiodicWorkers = function () {
}
App.prototype.setupAutoSubmit = function () {
$('.select-auto-submit').change(function () {
$('body').on('change', '.select-auto-submit', function () {
if ($(this).val()) {
$(this.form).trigger('submit');
}
@@ -209,7 +209,7 @@ App.prototype.setupNavbarCollapse = function () {
}
App.prototype.setupNewWindowAnchor = function () {
$('a.new_window').click(function (event) {
$('body').on('click', 'a.new_window', function (event) {
event.preventDefault();
var newWindow = window.open($(this).attr('href'), '_blank');
newWindow.focus();
@@ -220,20 +220,35 @@ App.prototype.setupScrollView = function () {
$('.scrollable').scrollview();
}
App.prototype.setupTableSelector = function () {
$('th input:checkbox').click(function(e) {
var table = $(e.target).closest('table');
var checked = $(e.target).prop('checked');
$('td input:checkbox', table).prop('checked', checked);
});
}
App.prototype.setupItemsSelector = function () {
$('.check-all').click(function(e) {
var parent = $(e.target).closest('.well');
var checked = $(e.target).prop('checked');
$('.panel-item input:checkbox', parent).prop('checked', checked);
var app = this;
app.lastChecked = null;
$('body').on('click', '.check-all', function (event) {
var checked = $(event.target).prop('checked');
var $checkBoxes = $('.check-all-slave');
$checkBoxes.prop('checked', checked);
$checkBoxes.trigger('change');
});
$('body').on('click', '.check-all-slave', function(e) {
if(!app.lastChecked) {
app.lastChecked = this;
return;
}
if(e.shiftKey) {
var $checkBoxes = $('.check-all-slave');
var start = $checkBoxes.index(this);
var end = $checkBoxes.index(app.lastChecked);
$checkBoxes.slice(
Math.min(start,end), Math.max(start,end) + 1
).prop('checked', app.lastChecked.checked).trigger('change');
}
app.lastChecked = this;
})
}
App.prototype.setupSelect2 = function () {
@@ -64,9 +64,9 @@
<label for="id_indexes_0">
{% if multi_item_actions %}
{% if multi_select_item_properties %}
<input class="form-multi-object-action-checkbox checkbox" type="checkbox" name="properties_{{ object|get_encoded_parameter:multi_select_item_properties }}" />
<input class="form-multi-object-action-checkbox check-all-slave checkbox" type="checkbox" name="properties_{{ object|get_encoded_parameter:multi_select_item_properties }}" />
{% else %}
<input class="form-multi-object-action-checkbox checkbox" type="checkbox" name="pk_{{ object.pk }}" />
<input class="form-multi-object-action-checkbox check-all-slave checkbox" type="checkbox" name="pk_{{ object.pk }}" />
{% endif %}
{% endif %}
@@ -43,7 +43,7 @@
{% if not hide_header %}
<tr>
{% if multi_item_actions %}
<th class="first"><input class="checkbox" type="checkbox" /></th>
<th class="first"><input class="checkbox check-all" type="checkbox" /></th>
{% endif %}
{% if not hide_object %}
@@ -71,9 +71,9 @@
{% if multi_item_actions %}
<td>
{% if multi_select_item_properties %}
<input type="checkbox" class="form-multi-object-action-checkbox checkbox" name="properties_{{ object|get_encoded_parameter:multi_select_item_properties }}" value="" />
<input type="checkbox" class="form-multi-object-action-checkbox check-all-slave checkbox" name="properties_{{ object|get_encoded_parameter:multi_select_item_properties }}" value="" />
{% else %}
<input type="checkbox" class="form-multi-object-action-checkbox checkbox" name="pk_{{ object.pk }}" value="" />
<input type="checkbox" class="form-multi-object-action-checkbox check-all-slave checkbox" name="pk_{{ object.pk }}" value="" />
{% endif %}
</td>
{% endif %}