Skip to content

Commit

Permalink
Delegate all events
Browse files Browse the repository at this point in the history
  • Loading branch information
mlt committed Jan 4, 2017
1 parent bba55b5 commit 6ddcf8f
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions app/assets/javascripts/tabulatr/_events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
(function() {
var tabulatr_tables;

$(document).on('click', 'th.tabulatr-sortable', function(){
var th = $(this);
var sort_by = th.data('tabulatr-column-name');
Expand Down Expand Up @@ -27,16 +30,7 @@
table_obj.updateTable({});
});

$(document).on('ready page:load turbolinks:load', function(){
tabulatr_tables = [];

$('.tabulatr_table').each(function(ix, el){
if($('.pagination[data-table="'+ $(el).attr('id') +'"]').length === 0){
$('.pagination_trigger[data-table="'+ $(el).attr('id') +'"]').bind('inview', cbfn);
}
});

$('.batch-action-inputs').click(function(){
$(document).on('click', '.batch-action-inputs', function(){
var a = $(this);
var name = a.data('do-batch-action-name');
var key = a.data('do-batch-action');
Expand All @@ -61,7 +55,7 @@ $(document).on('ready page:load turbolinks:load', function(){
}
});

$('form.tabulatr-fuzzy-search').submit(function(){
$(document).on('submit', 'form.tabulatr-fuzzy-search', function(){
var tableId = $(this).data('table');
var table_obj;
for(var i = 0; i < tabulatr_tables.length; i++){
Expand All @@ -77,11 +71,11 @@ $(document).on('ready page:load turbolinks:load', function(){
return false;
});

$('form.tabulatr_filter_form input, form.tabulatr_filter_form select').change(function(){
$(document).on('change', 'form.tabulatr_filter_form input, form.tabulatr_filter_form select', function(){
$(this).parents('form.tabulatr_filter_form').submit();
});

$('form.tabulatr_filter_form').submit(function(){
$(document).on('submit', 'form.tabulatr_filter_form', function(){
var tableId = $(this).data('table');
var table_obj;
for(var i = 0; i < tabulatr_tables.length; i++){
Expand All @@ -93,7 +87,7 @@ $(document).on('ready page:load turbolinks:load', function(){
return false;
});

$('.tabulatr_mark_all').click(function(){
$(document).on('click', '.tabulatr_mark_all', function(){
var tableId = $(this).parents('table').prop('id');
var table_obj;
for(var i = 0; i < tabulatr_tables.length; i++){
Expand All @@ -114,7 +108,7 @@ $(document).on('ready page:load turbolinks:load', function(){
}
});

$('.tabulatr_table').on('click', 'input.tabulatr-checkbox', function(){
$(document).on('click', '.tabulatr_table input.tabulatr-checkbox', function(){
var $table = $(this).closest('.tabulatr_table');
var tableId = $table.attr('id');
var $markAllCheckbox = $table.find('.tabulatr_mark_all');
Expand Down Expand Up @@ -148,7 +142,7 @@ $(document).on('ready page:load turbolinks:load', function(){
}
});

$('.tabulatr-per-page a').click(function(){
$(document).on('click', '.tabulatr-per-page a', function(){
if($(this).hasClass('active')){ return false; }
$(this).closest('div').find('a').removeClass('active');
$(this).addClass('active');
Expand All @@ -171,6 +165,50 @@ $(document).on('ready page:load turbolinks:load', function(){
table_obj.updateTable({page: 1}, true);
});

$(document).on('ready page:load turbolinks:load', function(){
tabulatr_tables = [];

$('.tabulatr_table').each(function(ix, el){
if($('.pagination[data-table="'+ $(el).attr('id') +'"]').length === 0){
$('.pagination_trigger[data-table="'+ $(el).attr('id') +'"]').bind('inview', cbfn);
}
});

if($('.tabulatr_table:not(".tabulatr_static_table")').length > 0){
if(typeof(Storage) !== undefined){
try {
var count = localStorage.tabulatr_page_display_count;
if(count !== undefined){
$('.tabulatr-per-page a').removeClass('active');
$('.tabulatr-per-page a[data-items-per-page='+ count +']').
addClass('active');
}
} catch(e) {}
}
var tableObj, tableId, tabulatrTable;
$('.tabulatr_table:not(".tabulatr_static_table")').each(function(ix, el){
tableId = $(el).attr('id');
tabulatrTable = new Tabulatr(tableId);
if($(el).data('persistent')){
try {
localStorage._tabulatr_test = 1;
tabulatrTable.isAPersistedTable = true;
} catch(e) {}
}
if($('.pagination[data-table='+ tableId +']').length === 0){
tabulatrTable.hasInfiniteScrolling = true;
}
tabulatr_tables.push(tabulatrTable);
for(var i = 0; i < tabulatr_tables.length; i++){
if(tabulatr_tables[i].id === tableId){
tableObj = tabulatr_tables[i];
}
}
tableObj.updateTable({}, false);
});
}
});

$(document).on('click', 'a[data-tabulatr-reset]',function(){
var a = $(this);
var tableObj;
Expand All @@ -185,47 +223,12 @@ $(document).on('ready page:load turbolinks:load', function(){
}
});

if($('.tabulatr_table:not(".tabulatr_static_table")').length > 0){
if(typeof(Storage) !== undefined){
try {
var count = localStorage.tabulatr_page_display_count;
if(count !== undefined){
$('.tabulatr-per-page a').removeClass('active');
$('.tabulatr-per-page a[data-items-per-page='+ count +']').
addClass('active');
}
} catch(e) {}
}
var tableObj, tableId, tabulatrTable;
$('.tabulatr_table:not(".tabulatr_static_table")').each(function(ix, el){
tableId = $(el).attr('id');
tabulatrTable = new Tabulatr(tableId);
if($(el).data('persistent')){
try {
localStorage._tabulatr_test = 1;
tabulatrTable.isAPersistedTable = true;
} catch(e) {}
}
if($('.pagination[data-table='+ tableId +']').length === 0){
tabulatrTable.hasInfiniteScrolling = true;
}
tabulatr_tables.push(tabulatrTable);
for(var i = 0; i < tabulatr_tables.length; i++){
if(tabulatr_tables[i].id === tableId){
tableObj = tabulatr_tables[i];
}
}
tableObj.updateTable({}, false);
});
}

$(document).on('click', 'a[data-show-filters-for]', function(){
var a = $(this);
a.parents('.tabulatr-outer-wrapper').addClass('filtered');
return false;
});

});

$(document).on('click', '.pagination a[data-page]', function(){
var a = $(this);
Expand All @@ -246,6 +249,7 @@ $(document).on('click', '.pagination a[data-page]', function(){
return false;
});

}).call(this);

$(document).on('change', 'select[data-tabulatr-date-filter]', function() {
var select = $(this);
Expand Down

0 comments on commit 6ddcf8f

Please sign in to comment.