causeway-standard-theme
Version:
165 lines (133 loc) • 4.89 kB
JavaScript
(function ($, window) {
'use strict';
if (window.Causeway.isIe8) {
/**
* Tables
*/
var $tables = jQuery('table.table tbody');
$tables.find('tr:nth-child(2n)').addClass('even');
$tables.find('tr:nth-child(2n+1)').addClass('odd');
}
// Custom scroll bars
// For Tables
window.Causeway.tableScroll = {};
window.Causeway.tableScroll.init = function () {
var tableContent = jQuery('.table-content');
tableContent.each(function () {
var $this = jQuery(this),
tableToGetScrollbar = jQuery('.table', $this);
if (window.Causeway.isIe9) {
var expr = new RegExp('>[ \t\r\n\v\f]*<', 'g'),
tableHtml = $this.html();
tableHtml = tableHtml.replace(expr, '><');
$this.html(tableHtml);
}
if (tableToGetScrollbar.width() > $this.width() && !tableToGetScrollbar.hasClass('fixed-header')) {
$this.scrollbar();
}
if($this[0].scrollHeight>$this.height() || $this[0].scrollWidth>$this.width()){
$this.addClass('MobScroll');
}
});
};
// window.Causeway.tableScroll.init();
// Table filter toggle
window.Causeway.tableFilter = {};
window.Causeway.tableFilter.init = function () {
window.Causeway.tableFilter.filterButton = jQuery('.btn-filter');
window.Causeway.tableFilter.filterRow = function (el) {
var parents = el.parents('.table-toolbar');
return parents.next('.table-content').find('tr.filter')
.add(parents.find('.toolbar-filter'))
.add(parents.next('.table').find('tr.filter'));
};
window.Causeway.tableFilter.filterButtonOn = false;
var that = this;
that.filterButton.each(function () {
window.Causeway.tableFilter.filterButtonOn = false;
var $this = jQuery(this);
var filter_row = that.filterRow($this),
formFilterFields = filter_row.find('select, input');
filter_row.hide();
formFilterFields.each(function () {
var $this = jQuery(this);
if ($this.attr('type') === 'checkbox' || $this.attr('type') === 'radio') {
that.filterButtonOn = that.filterButtonOn ? true : !!$this.is(':checked');
} else {
that.filterButtonOn = that.filterButtonOn ? true : !!$this.val().trim();
}
});
if (that.filterButtonOn) {
filter_row = that.filterRow($this);
$this.addClass('selected');
filter_row.toggle();
}
$this.on('click', function () {
filter_row = that.filterRow($this);
$this.toggleClass('selected');
filter_row.toggle();
});
});
};
window.Causeway.tableFilter.init();
// Table Fixed Header
window.Causeway.fixedTableHeader = {};
window.Causeway.fixedTableHeader.init = function () {
window.Causeway.fixedTableHeader.tables = jQuery('.table.fixed-header');
var $that = window.Causeway.fixedTableHeader.tables;
$that.each(function () {
var $this = jQuery(this),
$tableContent = $this.parent('.table-content');
$this.tableHeadFixer({
'left' : 0,
'head' : true,
'foot': true
});
// $tableContent.addClass('scrollbar-outer').scrollbar();
});
return true;
};
window.Causeway.fixedTableHeader.init();
//Table Sort
var tableToSort = jQuery('.table-sort');
tableToSort.each(function () {
var $this = jQuery(this),
tableSortOptions = {},
header = jQuery("thead > tr:not('.filter'):last > th", $this),
disabled = header.has('[class$=-disabled]');
tableSortOptions.headers = {};
disabled.each(function () {
var index = header.index(jQuery(this));
tableSortOptions.headers[index] = {sorter: false};
});
$this.tablesorter(tableSortOptions);
});
// Table Filter
var $tableToFilter = jQuery('.table-filter');
$tableToFilter.each(function () {
var $this = jQuery(this),
$searchField = jQuery('thead tr.filter > th input[type=text]', $this);
var values = {};
$searchField.keyup(function () {
var val = $.trim(jQuery(this).val()).replace(/ +/g, ' ').toLowerCase();
var index = (jQuery(this).parents('th')).parent('tr').find('th').index(jQuery(this).parents('th'));
values[index] = val;
var $rows = jQuery('tbody tr', $this);
function toggleRows(index, values) {
$rows.filter(function () {
var $this = jQuery(this);
var text = jQuery($this.find('td')[index]).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(values[index]);
}).hide();
}
$rows.show();
for (var key in values) {
if(values.hasOwnProperty(key)) {
toggleRows(key, values);
}
}
$this.removeClass('table-striped');
jQuery('tbody > tr:visible:nth-child(2n+1)').addClass('table-row-striped');
});
});
}(jQuery, window));