Bug 23307: (RM follow-up) Fix breaking change to columns_settings.inc
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / columns_settings.inc
1 [% USE ColumnsSettings %]
2
3 <script>
4 function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
5     var counter = 0;
6     var hidden_ids = [];
7     var included_ids = [];
8     var selector = '#' + id_selector;
9
10     $(columns_settings).each( function() {
11         var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( 'th' );
12         var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter;
13         if ( used_id == -1 ) return;
14
15         if ( this['is_hidden'] == "1" ) {
16             hidden_ids.push( used_id );
17         }
18         if ( this['cannot_be_toggled'] == "0" ) {
19             included_ids.push( used_id );
20         }
21         counter++;
22     });
23
24     var exportColumns = ":visible:not(.noExport)";
25     if( dt_parameters.hasOwnProperty("exportColumns") ){
26         // A custom buttons configuration has been passed from the page
27         exportColumns = dt_parameters["exportColumns"];
28     }
29
30     var export_buttons = [
31         {
32             extend: 'excelHtml5',
33             text: _("Excel"),
34             exportOptions: {
35                 columns: exportColumns
36             },
37         },
38         {
39             extend: 'csvHtml5',
40             text: _("CSV"),
41             exportOptions: {
42                 columns: exportColumns
43             },
44         },
45         {
46             extend: 'copyHtml5',
47             text: _("Copy"),
48             exportOptions: {
49                 columns: exportColumns
50             },
51         },
52         {
53             extend: 'print',
54             text: _("Print"),
55             exportOptions: {
56                 columns: exportColumns
57             },
58         }
59     ];
60
61     dt_parameters[ "buttons" ] = [
62         {
63             fade: 100,
64             className: "dt_button_clear_filter",
65             titleAttr: _("Clear filter"),
66             enabled: false,
67             text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>',
68             action: function ( e, dt, node, config ) {
69                 dt.search( "" ).draw("page");
70                 node.addClass("disabled");
71             }
72         },
73         {
74             extend: 'colvis',
75             fade: 100,
76             columns: included_ids,
77             className: "columns_controls",
78             titleAttr: _("Columns settings"),
79             text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
80             exportOptions: {
81                 columns: exportColumns
82             },
83         },
84         {
85             extend: 'collection',
86             autoClose: true,
87             fade: 100,
88             className: "export_controls",
89             titleAttr: _("Export or print"),
90             text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + _("Export") + '</span>',
91             buttons: export_buttons
92         }
93
94     ];
95
96     var table = $(selector);
97     if ( add_filters ) {
98         // Duplicate the table header row for columnFilter
99         thead_row = table.find('thead tr');
100         clone = thead_row.clone().addClass('filters_row');
101         clone.find("th.NoSort").html('');
102         thead_row.before(clone);
103     }
104
105     table.dataTable($.extend(true, {}, dataTablesDefaults, dt_parameters));
106
107     table.DataTable().on("column-visibility.dt", function(){
108         if( typeof columnsInit == 'function' ){
109             // This function can be created separately and used to trigger
110             // an event after the DataTable has loaded AND column visibility
111             // has been updated according to the table's configuration
112             columnsInit();
113         }
114     }).columns( hidden_ids ).visible( false );
115
116     if ( add_filters ) {
117         // show a link to activate filtering
118         link = $('<a>')
119             .attr('href', '#')
120             .attr('id', id_selector + '_activate_filters');
121         $("." + id_selector  + "_table_controls").prepend(link);
122         deactivate_filters(id_selector);
123     }
124
125     $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
126
127     return table;
128 }
129
130 </script>