|
Lines 504-509
jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
Link Here
|
| 504 |
console.log(message); |
504 |
console.log(message); |
| 505 |
}; |
505 |
}; |
| 506 |
|
506 |
|
|
|
507 |
function _dt_default_ajax (params){ |
| 508 |
let default_filters = params.default_filters; |
| 509 |
let options = params.options; |
| 510 |
|
| 511 |
if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains'; |
| 512 |
options.criteria = options.criteria.toLowerCase(); |
| 513 |
|
| 514 |
return { |
| 515 |
'type': 'GET', |
| 516 |
'cache': true, |
| 517 |
'dataSrc': 'data', |
| 518 |
'beforeSend': function(xhr, settings) { |
| 519 |
this._xhr = xhr; |
| 520 |
if(options.embed) { |
| 521 |
xhr.setRequestHeader('x-koha-embed', Array.isArray(options.embed)?options.embed.join(','):options.embed); |
| 522 |
} |
| 523 |
}, |
| 524 |
'dataFilter': function(data, type) { |
| 525 |
var json = {data: JSON.parse(data)}; |
| 526 |
if (total = this._xhr.getResponseHeader('x-total-count')) { |
| 527 |
json.recordsTotal = total; |
| 528 |
json.recordsFiltered = total; |
| 529 |
} |
| 530 |
if (total = this._xhr.getResponseHeader('x-base-total-count')) { |
| 531 |
json.recordsTotal = total; |
| 532 |
} |
| 533 |
if (draw = this._xhr.getResponseHeader('x-koha-request-id')) { |
| 534 |
json.draw = draw; |
| 535 |
} |
| 536 |
|
| 537 |
return JSON.stringify(json); |
| 538 |
}, |
| 539 |
'data': function( data, settings ) { |
| 540 |
var length = data.length; |
| 541 |
var start = data.start; |
| 542 |
|
| 543 |
var dataSet = { |
| 544 |
_page: Math.floor(start/length) + 1, |
| 545 |
_per_page: length |
| 546 |
}; |
| 547 |
|
| 548 |
function build_query(col, value){ |
| 549 |
|
| 550 |
var parts = []; |
| 551 |
var attributes = col.data.split(':'); |
| 552 |
for (var i=0;i<attributes.length;i++){ |
| 553 |
var part = {}; |
| 554 |
var attr = attributes[i]; |
| 555 |
let criteria = options.criteria; |
| 556 |
if ( value.match(/^\^(.*)\$$/) ) { |
| 557 |
value = value.replace(/^\^/, '').replace(/\$$/, ''); |
| 558 |
criteria = "exact"; |
| 559 |
} else { |
| 560 |
// escape SQL LIKE special characters % and _ |
| 561 |
value = value.replace(/(\%|\\)/g, "\\$1"); |
| 562 |
} |
| 563 |
part[!attr.includes('.')?'me.'+attr:attr] = criteria === 'exact' |
| 564 |
? value |
| 565 |
: {like: (['contains', 'ends_with'].indexOf(criteria) !== -1?'%':'') + value + (['contains', 'starts_with'].indexOf(criteria) !== -1?'%':'')}; |
| 566 |
parts.push(part); |
| 567 |
} |
| 568 |
return parts; |
| 569 |
} |
| 570 |
|
| 571 |
var filter = data.search.value; |
| 572 |
// Build query for each column filter |
| 573 |
var and_query_parameters = settings.aoColumns |
| 574 |
.filter(function(col) { |
| 575 |
return col.bSearchable && typeof col.data == 'string' && data.columns[col.idx].search.value != '' |
| 576 |
}) |
| 577 |
.map(function(col) { |
| 578 |
var value = data.columns[col.idx].search.value; |
| 579 |
return build_query(col, value) |
| 580 |
}) |
| 581 |
.map(function r(e){ |
| 582 |
return ($.isArray(e) ? $.map(e, r) : e); |
| 583 |
}); |
| 584 |
|
| 585 |
// Build query for the global search filter |
| 586 |
var or_query_parameters = settings.aoColumns |
| 587 |
.filter(function(col) { |
| 588 |
return col.bSearchable && filter != '' |
| 589 |
}) |
| 590 |
.map(function(col) { |
| 591 |
var value = filter; |
| 592 |
return build_query(col, value) |
| 593 |
}) |
| 594 |
.map(function r(e){ |
| 595 |
return ($.isArray(e) ? $.map(e, r) : e); |
| 596 |
}); |
| 597 |
|
| 598 |
if ( default_filters ) { |
| 599 |
let additional_filters = {}; |
| 600 |
for ( f in default_filters ) { |
| 601 |
let k; let v; |
| 602 |
if ( typeof(default_filters[f]) === 'function' ) { |
| 603 |
let val = default_filters[f](); |
| 604 |
if ( val != undefined && val != "" ) { |
| 605 |
k = f; v = val; |
| 606 |
} |
| 607 |
} else { |
| 608 |
k = f; v = default_filters[f]; |
| 609 |
} |
| 610 |
|
| 611 |
// Pass to -or if you want a separate OR clause |
| 612 |
// It's not the usual DBIC notation! |
| 613 |
if ( f == '-or' ) { |
| 614 |
if (v) or_query_parameters.push(v) |
| 615 |
} else if ( f == '-and' ) { |
| 616 |
if (v) and_query_parameters.push(v) |
| 617 |
} else if ( v ) { |
| 618 |
additional_filters[k] = v; |
| 619 |
} |
| 620 |
} |
| 621 |
if ( Object.keys(additional_filters).length ) { |
| 622 |
and_query_parameters.push(additional_filters); |
| 623 |
} |
| 624 |
} |
| 625 |
query_parameters = and_query_parameters; |
| 626 |
if ( or_query_parameters.length) { |
| 627 |
query_parameters.push(or_query_parameters); |
| 628 |
} |
| 629 |
|
| 630 |
if(query_parameters.length) { |
| 631 |
query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:{"-and": query_parameters}); |
| 632 |
dataSet.q = query_parameters; |
| 633 |
delete options.query_parameters; |
| 634 |
} else { |
| 635 |
delete options.query_parameters; |
| 636 |
} |
| 637 |
|
| 638 |
dataSet._match = options.criteria; |
| 639 |
|
| 640 |
if ( data["draw"] !== undefined ) { |
| 641 |
settings.ajax.headers = { 'x-koha-request-id': data.draw } |
| 642 |
} |
| 643 |
|
| 644 |
if(options.columns) { |
| 645 |
var order = data.order; |
| 646 |
var orderArray = new Array(); |
| 647 |
order.forEach(function (e,i) { |
| 648 |
var order_col = e.column; |
| 649 |
var order_by = options.columns[order_col].data; |
| 650 |
order_by = order_by.split(':'); |
| 651 |
var order_dir = e.dir == 'asc' ? '+' : '-'; |
| 652 |
Array.prototype.push.apply(orderArray,order_by.map(x => order_dir + (!x.includes('.')?'me.'+x:x))); |
| 653 |
}); |
| 654 |
dataSet._order_by = orderArray.filter((v, i, a) => a.indexOf(v) === i).join(','); |
| 655 |
} |
| 656 |
|
| 657 |
return dataSet; |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
function _dt_buttons(params){ |
| 663 |
let included_ids = params.included_ids || []; |
| 664 |
let settings = params.settings || {}; |
| 665 |
let table_settings = params.table_settings || {}; |
| 666 |
|
| 667 |
var exportColumns = ":visible:not(.noExport)"; |
| 668 |
if( settings.hasOwnProperty("exportColumns") ){ |
| 669 |
// A custom buttons configuration has been passed from the page |
| 670 |
exportColumns = settings["exportColumns"]; |
| 671 |
} |
| 672 |
|
| 673 |
var export_format = { |
| 674 |
body: function ( data, row, column, node ) { |
| 675 |
var newnode = $(node); |
| 676 |
|
| 677 |
if ( newnode.find(".noExport").length > 0 ) { |
| 678 |
newnode = newnode.clone(); |
| 679 |
newnode.find(".noExport").remove(); |
| 680 |
} |
| 681 |
|
| 682 |
return newnode.text().replace( /\n/g, ' ' ).trim(); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
var export_buttons = [ |
| 687 |
{ |
| 688 |
extend: 'excelHtml5', |
| 689 |
text: __("Excel"), |
| 690 |
exportOptions: { |
| 691 |
columns: exportColumns, |
| 692 |
format: export_format |
| 693 |
}, |
| 694 |
}, |
| 695 |
{ |
| 696 |
extend: 'csvHtml5', |
| 697 |
text: __("CSV"), |
| 698 |
exportOptions: { |
| 699 |
columns: exportColumns, |
| 700 |
format: export_format |
| 701 |
}, |
| 702 |
}, |
| 703 |
{ |
| 704 |
extend: 'copyHtml5', |
| 705 |
text: __("Copy"), |
| 706 |
exportOptions: { |
| 707 |
columns: exportColumns, |
| 708 |
format: export_format |
| 709 |
}, |
| 710 |
}, |
| 711 |
{ |
| 712 |
extend: 'print', |
| 713 |
text: __("Print"), |
| 714 |
exportOptions: { |
| 715 |
columns: exportColumns, |
| 716 |
format: export_format |
| 717 |
}, |
| 718 |
} |
| 719 |
]; |
| 720 |
|
| 721 |
let buttons = []; |
| 722 |
buttons.push( |
| 723 |
{ |
| 724 |
fade: 100, |
| 725 |
className: "dt_button_clear_filter", |
| 726 |
titleAttr: __("Clear filter"), |
| 727 |
enabled: false, |
| 728 |
text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + __("Clear filter") + '</span>', |
| 729 |
action: function ( e, dt, node, config ) { |
| 730 |
dt.search( "" ).draw("page"); |
| 731 |
node.addClass("disabled"); |
| 732 |
} |
| 733 |
} |
| 734 |
); |
| 735 |
|
| 736 |
if( included_ids.length > 0 ){ |
| 737 |
buttons.push( |
| 738 |
{ |
| 739 |
extend: 'colvis', |
| 740 |
fade: 100, |
| 741 |
columns: included_ids, |
| 742 |
className: "columns_controls", |
| 743 |
titleAttr: __("Columns settings"), |
| 744 |
text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + __("Columns") + '</span>', |
| 745 |
exportOptions: { |
| 746 |
columns: exportColumns |
| 747 |
} |
| 748 |
} |
| 749 |
); |
| 750 |
} |
| 751 |
|
| 752 |
buttons.push( |
| 753 |
{ |
| 754 |
extend: 'collection', |
| 755 |
autoClose: true, |
| 756 |
fade: 100, |
| 757 |
className: "export_controls", |
| 758 |
titleAttr: __("Export or print"), |
| 759 |
text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + __("Export") + '</span>', |
| 760 |
buttons: export_buttons |
| 761 |
} |
| 762 |
); |
| 763 |
|
| 764 |
if ( table_settings && CAN_user_parameters_manage_column_config ) { |
| 765 |
buttons.push( |
| 766 |
{ |
| 767 |
className: "dt_button_configure_table", |
| 768 |
fade: 100, |
| 769 |
titleAttr: __("Configure table"), |
| 770 |
text: '<i class="fa fa-lg fa-wrench"></i> <span class="dt-button-text">' + __("Configure") + '</span>', |
| 771 |
action: function() { |
| 772 |
window.location = '/cgi-bin/koha/admin/columns_settings.pl?module=' + table_settings['module'] + '&page=' + table_settings['page'] + '&table=' + table_settings['table']; |
| 773 |
}, |
| 774 |
} |
| 775 |
); |
| 776 |
} |
| 777 |
|
| 778 |
return buttons; |
| 779 |
} |
| 780 |
|
| 781 |
function _dt_visibility(table_settings, settings){ |
| 782 |
var counter = 0; |
| 783 |
let hidden_ids = []; |
| 784 |
let included_ids = []; |
| 785 |
if ( table_settings ) { |
| 786 |
var columns_settings = table_settings['columns']; |
| 787 |
$(columns_settings).each( function() { |
| 788 |
var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', this ).index( 'th' ); |
| 789 |
var used_id = settings.bKohaColumnsUseNames ? named_id : counter; |
| 790 |
if ( used_id == -1 ) return; |
| 791 |
|
| 792 |
if ( this['is_hidden'] == "1" ) { |
| 793 |
hidden_ids.push( used_id ); |
| 794 |
} |
| 795 |
if ( this['cannot_be_toggled'] == "0" ) { |
| 796 |
included_ids.push( used_id ); |
| 797 |
} |
| 798 |
counter++; |
| 799 |
}); |
| 800 |
} |
| 801 |
return [hidden_ids, included_ids]; |
| 802 |
} |
| 803 |
|
| 804 |
function _dt_on_visibility(add_filters, table_node, table_dt){ |
| 805 |
if ( add_filters ) { |
| 806 |
let visible_columns = table_dt.columns().visible(); |
| 807 |
$(table_node).find('thead tr:eq(1) th').each( function (i) { |
| 808 |
let th_id = $(this).data('th-id'); |
| 809 |
if ( visible_columns[th_id] == false ) { |
| 810 |
$(this).hide(); |
| 811 |
} else { |
| 812 |
$(this).show(); |
| 813 |
} |
| 814 |
}); |
| 815 |
} |
| 816 |
|
| 817 |
if( typeof columnsInit == 'function' ){ |
| 818 |
// This function can be created separately and used to trigger |
| 819 |
// an event after the DataTable has loaded AND column visibility |
| 820 |
// has been updated according to the table's configuration |
| 821 |
columnsInit(); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
function _dt_add_filters(table_node, table_dt) { |
| 826 |
$(table_node).find('thead tr').clone().appendTo( $(table_node).find('thead') ); |
| 827 |
|
| 828 |
$(table_node).find('thead tr:eq(1) th').each( function (i) { |
| 829 |
var is_searchable = table_dt.settings()[0].aoColumns[i].bSearchable; |
| 830 |
$(this).removeClass('sorting').removeClass("sorting_asc").removeClass("sorting_desc"); |
| 831 |
$(this).data('th-id', i); |
| 832 |
if ( is_searchable ) { |
| 833 |
let input_type = 'input'; |
| 834 |
if ( $(this).data('filter') ) { |
| 835 |
input_type = 'select' |
| 836 |
let filter_type = $(this).data('filter'); |
| 837 |
var existing_search = table_dt.column(i).search(); |
| 838 |
let select = $('<select><option value=""></option></select'); |
| 839 |
|
| 840 |
// FIXME eval here is bad and dangerous, how do we workaround that? |
| 841 |
$(eval(filter_type)).each(function(){ |
| 842 |
let o = $('<option value="%s">%s</option>'.format(this._id, this._str)); |
| 843 |
if ( existing_search === this._id ) { |
| 844 |
o.prop("selected", "selected"); |
| 845 |
} |
| 846 |
o.appendTo(select); |
| 847 |
}); |
| 848 |
$(this).html( select ); |
| 849 |
} else { |
| 850 |
var title = $(this).text(); |
| 851 |
var existing_search = table_dt.column(i).search(); |
| 852 |
if ( existing_search ) { |
| 853 |
$(this).html( '<input type="text" value="%s" style="width: 100%" />'.format(existing_search) ); |
| 854 |
} else { |
| 855 |
var search_title = _("%s search").format(title); |
| 856 |
$(this).html( '<input type="text" placeholder="%s" style="width: 100%" />'.format(search_title) ); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
$( input_type, this ).on( 'keyup change', function () { |
| 861 |
if ( table_dt.column(i).search() !== this.value ) { |
| 862 |
if ( input_type == "input" ) { |
| 863 |
table_dt |
| 864 |
.column(i) |
| 865 |
.search( this.value ) |
| 866 |
.draw(); |
| 867 |
} else { |
| 868 |
table_dt |
| 869 |
.column(i) |
| 870 |
.search( this.value.length ? '^'+this.value+'$' : '', true, false ) |
| 871 |
.draw(); |
| 872 |
} |
| 873 |
} |
| 874 |
} ); |
| 875 |
} else { |
| 876 |
$(this).html(''); |
| 877 |
} |
| 878 |
} ); |
| 879 |
} |
| 880 |
|
| 881 |
|
| 507 |
(function($) { |
882 |
(function($) { |
| 508 |
|
883 |
|
| 509 |
/** |
884 |
/** |
|
Lines 522-530
jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
Link Here
|
| 522 |
var settings = null; |
897 |
var settings = null; |
| 523 |
|
898 |
|
| 524 |
if(options) { |
899 |
if(options) { |
| 525 |
if(!options.criteria || ['contains', 'starts_with', 'ends_with', 'exact'].indexOf(options.criteria.toLowerCase()) === -1) options.criteria = 'contains'; |
|
|
| 526 |
options.criteria = options.criteria.toLowerCase(); |
| 527 |
|
| 528 |
// Don't redefine the default initComplete |
900 |
// Don't redefine the default initComplete |
| 529 |
if ( options.initComplete ) { |
901 |
if ( options.initComplete ) { |
| 530 |
let our_initComplete = options.initComplete; |
902 |
let our_initComplete = options.initComplete; |
|
Lines 544-829
jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
Link Here
|
| 544 |
'language': { |
916 |
'language': { |
| 545 |
'emptyTable': (options.emptyTable) ? options.emptyTable : __("No data available in table") |
917 |
'emptyTable': (options.emptyTable) ? options.emptyTable : __("No data available in table") |
| 546 |
}, |
918 |
}, |
| 547 |
'ajax': { |
919 |
'ajax': _dt_default_ajax({default_filters, options}), |
| 548 |
'type': 'GET', |
|
|
| 549 |
'cache': true, |
| 550 |
'dataSrc': 'data', |
| 551 |
'beforeSend': function(xhr, settings) { |
| 552 |
this._xhr = xhr; |
| 553 |
if(options.embed) { |
| 554 |
xhr.setRequestHeader('x-koha-embed', Array.isArray(options.embed)?options.embed.join(','):options.embed); |
| 555 |
} |
| 556 |
}, |
| 557 |
'dataFilter': function(data, type) { |
| 558 |
var json = {data: JSON.parse(data)}; |
| 559 |
if (total = this._xhr.getResponseHeader('x-total-count')) { |
| 560 |
json.recordsTotal = total; |
| 561 |
json.recordsFiltered = total; |
| 562 |
} |
| 563 |
if (total = this._xhr.getResponseHeader('x-base-total-count')) { |
| 564 |
json.recordsTotal = total; |
| 565 |
} |
| 566 |
if (draw = this._xhr.getResponseHeader('x-koha-request-id')) { |
| 567 |
json.draw = draw; |
| 568 |
} |
| 569 |
|
| 570 |
return JSON.stringify(json); |
| 571 |
}, |
| 572 |
'data': function( data, settings ) { |
| 573 |
var length = data.length; |
| 574 |
var start = data.start; |
| 575 |
|
| 576 |
var dataSet = { |
| 577 |
_page: Math.floor(start/length) + 1, |
| 578 |
_per_page: length |
| 579 |
}; |
| 580 |
|
| 581 |
function build_query(col, value){ |
| 582 |
|
| 583 |
var parts = []; |
| 584 |
var attributes = col.data.split(':'); |
| 585 |
for (var i=0;i<attributes.length;i++){ |
| 586 |
var part = {}; |
| 587 |
var attr = attributes[i]; |
| 588 |
let criteria = options.criteria; |
| 589 |
if ( value.match(/^\^(.*)\$$/) ) { |
| 590 |
value = value.replace(/^\^/, '').replace(/\$$/, ''); |
| 591 |
criteria = "exact"; |
| 592 |
} else { |
| 593 |
// escape SQL LIKE special characters % and _ |
| 594 |
value = value.replace(/(\%|\\)/g, "\\$1"); |
| 595 |
} |
| 596 |
part[!attr.includes('.')?'me.'+attr:attr] = criteria === 'exact' |
| 597 |
? value |
| 598 |
: {like: (['contains', 'ends_with'].indexOf(criteria) !== -1?'%':'') + value + (['contains', 'starts_with'].indexOf(criteria) !== -1?'%':'')}; |
| 599 |
parts.push(part); |
| 600 |
} |
| 601 |
return parts; |
| 602 |
} |
| 603 |
|
| 604 |
var filter = data.search.value; |
| 605 |
// Build query for each column filter |
| 606 |
var and_query_parameters = settings.aoColumns |
| 607 |
.filter(function(col) { |
| 608 |
return col.bSearchable && typeof col.data == 'string' && data.columns[col.idx].search.value != '' |
| 609 |
}) |
| 610 |
.map(function(col) { |
| 611 |
var value = data.columns[col.idx].search.value; |
| 612 |
return build_query(col, value) |
| 613 |
}) |
| 614 |
.map(function r(e){ |
| 615 |
return ($.isArray(e) ? $.map(e, r) : e); |
| 616 |
}); |
| 617 |
|
| 618 |
// Build query for the global search filter |
| 619 |
var or_query_parameters = settings.aoColumns |
| 620 |
.filter(function(col) { |
| 621 |
return col.bSearchable && filter != '' |
| 622 |
}) |
| 623 |
.map(function(col) { |
| 624 |
var value = filter; |
| 625 |
return build_query(col, value) |
| 626 |
}) |
| 627 |
.map(function r(e){ |
| 628 |
return ($.isArray(e) ? $.map(e, r) : e); |
| 629 |
}); |
| 630 |
|
| 631 |
if ( default_filters ) { |
| 632 |
let additional_filters = {}; |
| 633 |
for ( f in default_filters ) { |
| 634 |
let k; let v; |
| 635 |
if ( typeof(default_filters[f]) === 'function' ) { |
| 636 |
let val = default_filters[f](); |
| 637 |
if ( val != undefined && val != "" ) { |
| 638 |
k = f; v = val; |
| 639 |
} |
| 640 |
} else { |
| 641 |
k = f; v = default_filters[f]; |
| 642 |
} |
| 643 |
|
| 644 |
// Pass to -or if you want a separate OR clause |
| 645 |
// It's not the usual DBIC notation! |
| 646 |
if ( f == '-or' ) { |
| 647 |
if (v) or_query_parameters.push(v) |
| 648 |
} else if ( f == '-and' ) { |
| 649 |
if (v) and_query_parameters.push(v) |
| 650 |
} else if ( v ) { |
| 651 |
additional_filters[k] = v; |
| 652 |
} |
| 653 |
} |
| 654 |
if ( Object.keys(additional_filters).length ) { |
| 655 |
and_query_parameters.push(additional_filters); |
| 656 |
} |
| 657 |
} |
| 658 |
query_parameters = and_query_parameters; |
| 659 |
if ( or_query_parameters.length) { |
| 660 |
query_parameters.push(or_query_parameters); |
| 661 |
} |
| 662 |
|
| 663 |
if(query_parameters.length) { |
| 664 |
query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:{"-and": query_parameters}); |
| 665 |
dataSet.q = query_parameters; |
| 666 |
delete options.query_parameters; |
| 667 |
} else { |
| 668 |
delete options.query_parameters; |
| 669 |
} |
| 670 |
|
| 671 |
dataSet._match = options.criteria; |
| 672 |
|
| 673 |
if ( data["draw"] !== undefined ) { |
| 674 |
settings.ajax.headers = { 'x-koha-request-id': data.draw } |
| 675 |
} |
| 676 |
|
| 677 |
if(options.columns) { |
| 678 |
var order = data.order; |
| 679 |
var orderArray = new Array(); |
| 680 |
order.forEach(function (e,i) { |
| 681 |
var order_col = e.column; |
| 682 |
var order_by = options.columns[order_col].data; |
| 683 |
order_by = order_by.split(':'); |
| 684 |
var order_dir = e.dir == 'asc' ? '+' : '-'; |
| 685 |
Array.prototype.push.apply(orderArray,order_by.map(x => order_dir + (!x.includes('.')?'me.'+x:x))); |
| 686 |
}); |
| 687 |
dataSet._order_by = orderArray.filter((v, i, a) => a.indexOf(v) === i).join(','); |
| 688 |
} |
| 689 |
|
| 690 |
return dataSet; |
| 691 |
} |
| 692 |
} |
| 693 |
}, options); |
920 |
}, options); |
| 694 |
} |
921 |
} |
| 695 |
|
922 |
|
| 696 |
var counter = 0; |
923 |
let hidden_ids, included_ids; |
| 697 |
var hidden_ids = []; |
924 |
[hidden_ids, included_ids] = _dt_visibility(table_settings, settings) |
| 698 |
var included_ids = []; |
|
|
| 699 |
|
| 700 |
|
| 701 |
if ( table_settings ) { |
| 702 |
var columns_settings = table_settings['columns']; |
| 703 |
$(columns_settings).each( function() { |
| 704 |
var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', this ).index( 'th' ); |
| 705 |
var used_id = settings.bKohaColumnsUseNames ? named_id : counter; |
| 706 |
if ( used_id == -1 ) return; |
| 707 |
|
| 708 |
if ( this['is_hidden'] == "1" ) { |
| 709 |
hidden_ids.push( used_id ); |
| 710 |
} |
| 711 |
if ( this['cannot_be_toggled'] == "0" ) { |
| 712 |
included_ids.push( used_id ); |
| 713 |
} |
| 714 |
counter++; |
| 715 |
}); |
| 716 |
} |
| 717 |
|
| 718 |
var exportColumns = ":visible:not(.noExport)"; |
| 719 |
if( settings.hasOwnProperty("exportColumns") ){ |
| 720 |
// A custom buttons configuration has been passed from the page |
| 721 |
exportColumns = settings["exportColumns"]; |
| 722 |
} |
| 723 |
|
| 724 |
var export_format = { |
| 725 |
body: function ( data, row, column, node ) { |
| 726 |
var newnode = $(node); |
| 727 |
|
| 728 |
if ( newnode.find(".noExport").length > 0 ) { |
| 729 |
newnode = newnode.clone(); |
| 730 |
newnode.find(".noExport").remove(); |
| 731 |
} |
| 732 |
|
| 733 |
return newnode.text().replace( /\n/g, ' ' ).trim(); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
var export_buttons = [ |
| 738 |
{ |
| 739 |
extend: 'excelHtml5', |
| 740 |
text: __("Excel"), |
| 741 |
exportOptions: { |
| 742 |
columns: exportColumns, |
| 743 |
format: export_format |
| 744 |
}, |
| 745 |
}, |
| 746 |
{ |
| 747 |
extend: 'csvHtml5', |
| 748 |
text: __("CSV"), |
| 749 |
exportOptions: { |
| 750 |
columns: exportColumns, |
| 751 |
format: export_format |
| 752 |
}, |
| 753 |
}, |
| 754 |
{ |
| 755 |
extend: 'copyHtml5', |
| 756 |
text: __("Copy"), |
| 757 |
exportOptions: { |
| 758 |
columns: exportColumns, |
| 759 |
format: export_format |
| 760 |
}, |
| 761 |
}, |
| 762 |
{ |
| 763 |
extend: 'print', |
| 764 |
text: __("Print"), |
| 765 |
exportOptions: { |
| 766 |
columns: exportColumns, |
| 767 |
format: export_format |
| 768 |
}, |
| 769 |
} |
| 770 |
]; |
| 771 |
|
| 772 |
settings[ "buttons" ] = [ |
| 773 |
{ |
| 774 |
fade: 100, |
| 775 |
className: "dt_button_clear_filter", |
| 776 |
titleAttr: __("Clear filter"), |
| 777 |
enabled: false, |
| 778 |
text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + __("Clear filter") + '</span>', |
| 779 |
action: function ( e, dt, node, config ) { |
| 780 |
dt.search( "" ).draw("page"); |
| 781 |
node.addClass("disabled"); |
| 782 |
} |
| 783 |
} |
| 784 |
]; |
| 785 |
|
| 786 |
if( included_ids.length > 0 ){ |
| 787 |
settings[ "buttons" ].push( |
| 788 |
{ |
| 789 |
extend: 'colvis', |
| 790 |
fade: 100, |
| 791 |
columns: included_ids, |
| 792 |
className: "columns_controls", |
| 793 |
titleAttr: __("Columns settings"), |
| 794 |
text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + __("Columns") + '</span>', |
| 795 |
exportOptions: { |
| 796 |
columns: exportColumns |
| 797 |
} |
| 798 |
} |
| 799 |
); |
| 800 |
} |
| 801 |
|
| 802 |
settings[ "buttons" ].push( |
| 803 |
{ |
| 804 |
extend: 'collection', |
| 805 |
autoClose: true, |
| 806 |
fade: 100, |
| 807 |
className: "export_controls", |
| 808 |
titleAttr: __("Export or print"), |
| 809 |
text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + __("Export") + '</span>', |
| 810 |
buttons: export_buttons |
| 811 |
} |
| 812 |
); |
| 813 |
|
925 |
|
| 814 |
if ( table_settings && CAN_user_parameters_manage_column_config ) { |
926 |
settings["buttons"] = _dt_buttons({included_ids, settings, table_settings}); |
| 815 |
settings[ "buttons" ].push( |
|
|
| 816 |
{ |
| 817 |
className: "dt_button_configure_table", |
| 818 |
fade: 100, |
| 819 |
titleAttr: __("Configure table"), |
| 820 |
text: '<i class="fa fa-lg fa-wrench"></i> <span class="dt-button-text">' + __("Configure") + '</span>', |
| 821 |
action: function() { |
| 822 |
window.location = '/cgi-bin/koha/admin/columns_settings.pl?module=' + table_settings['module'] + '&page=' + table_settings['page'] + '&table=' + table_settings['table']; |
| 823 |
}, |
| 824 |
} |
| 825 |
); |
| 826 |
} |
| 827 |
|
927 |
|
| 828 |
$(".dt_button_clear_filter, .columns_controls, .export_controls, .dt_button_configure_table").tooltip(); |
928 |
$(".dt_button_clear_filter, .columns_controls, .export_controls, .dt_button_configure_table").tooltip(); |
| 829 |
|
929 |
|
|
Lines 842-926
jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
Link Here
|
| 842 |
|
942 |
|
| 843 |
var table = $(this).dataTable(settings); |
943 |
var table = $(this).dataTable(settings); |
| 844 |
|
944 |
|
| 845 |
|
945 |
var table_dt = table.DataTable(); |
| 846 |
if ( add_filters ) { |
946 |
if ( add_filters ) { |
| 847 |
var table_dt = table.DataTable(); |
947 |
_dt_add_filters(this, table_dt); |
| 848 |
|
|
|
| 849 |
$(this).find('thead tr').clone().appendTo( $(this).find('thead') ); |
| 850 |
|
| 851 |
$(this).find('thead tr:eq(1) th').each( function (i) { |
| 852 |
var is_searchable = table_dt.settings()[0].aoColumns[i].bSearchable; |
| 853 |
$(this).removeClass('sorting').removeClass("sorting_asc").removeClass("sorting_desc"); |
| 854 |
$(this).data('th-id', i); |
| 855 |
if ( is_searchable ) { |
| 856 |
let input_type = 'input'; |
| 857 |
if ( $(this).data('filter') ) { |
| 858 |
input_type = 'select' |
| 859 |
let filter_type = $(this).data('filter'); |
| 860 |
var existing_search = table_dt.column(i).search(); |
| 861 |
let select = $('<select><option value=""></option></select'); |
| 862 |
|
| 863 |
// FIXME eval here is bad and dangerous, how do we workaround that? |
| 864 |
$(eval(filter_type)).each(function(){ |
| 865 |
let o = $('<option value="%s">%s</option>'.format(this._id, this._str)); |
| 866 |
if ( existing_search === this._id ) { |
| 867 |
o.prop("selected", "selected"); |
| 868 |
} |
| 869 |
o.appendTo(select); |
| 870 |
}); |
| 871 |
$(this).html( select ); |
| 872 |
} else { |
| 873 |
var title = $(this).text(); |
| 874 |
var existing_search = table_dt.column(i).search(); |
| 875 |
if ( existing_search ) { |
| 876 |
$(this).html( '<input type="text" value="%s" style="width: 100%" />'.format(existing_search) ); |
| 877 |
} else { |
| 878 |
var search_title = _("%s search").format(title); |
| 879 |
$(this).html( '<input type="text" placeholder="%s" style="width: 100%" />'.format(search_title) ); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
$( input_type, this ).on( 'keyup change', function () { |
| 884 |
if ( table_dt.column(i).search() !== this.value ) { |
| 885 |
if ( input_type == "input" ) { |
| 886 |
table_dt |
| 887 |
.column(i) |
| 888 |
.search( this.value ) |
| 889 |
.draw(); |
| 890 |
} else { |
| 891 |
table_dt |
| 892 |
.column(i) |
| 893 |
.search( this.value.length ? '^'+this.value+'$' : '', true, false ) |
| 894 |
.draw(); |
| 895 |
} |
| 896 |
} |
| 897 |
} ); |
| 898 |
} else { |
| 899 |
$(this).html(''); |
| 900 |
} |
| 901 |
} ); |
| 902 |
} |
948 |
} |
| 903 |
|
949 |
|
| 904 |
table.DataTable().on("column-visibility.dt", function(){ |
950 |
table.DataTable().on("column-visibility.dt", function(){_dt_on_visibility(add_filters, table, table_dt);}) |
| 905 |
if ( add_filters ) { |
951 |
.columns( hidden_ids ).visible( false ); |
| 906 |
let visible_columns = table_dt.columns().visible(); |
|
|
| 907 |
$(table).find('thead tr:eq(1) th').each( function (i) { |
| 908 |
let th_id = $(this).data('th-id'); |
| 909 |
if ( visible_columns[th_id] == false ) { |
| 910 |
$(this).hide(); |
| 911 |
} else { |
| 912 |
$(this).show(); |
| 913 |
} |
| 914 |
}); |
| 915 |
} |
| 916 |
|
| 917 |
if( typeof columnsInit == 'function' ){ |
| 918 |
// This function can be created separately and used to trigger |
| 919 |
// an event after the DataTable has loaded AND column visibility |
| 920 |
// has been updated according to the table's configuration |
| 921 |
columnsInit(); |
| 922 |
} |
| 923 |
}).columns( hidden_ids ).visible( false ); |
| 924 |
|
952 |
|
| 925 |
return table; |
953 |
return table; |
| 926 |
}; |
954 |
}; |