Bug 15668: (follow up) add selector to th
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / columns_settings.inc
1 [% USE ColumnsSettings %]
2 <!-- columns_settings.inc -->
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( selector + ' 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_format = {
31         body: function ( data, row, column, node ) {
32             var newnode = $(node);
33
34             if ( newnode.find(".noExport").length > 0 ) {
35                 newnode = newnode.clone();
36                 newnode.find(".noExport").remove();
37             }
38
39             return newnode.text().replace( /\n/g, ' ' ).trim();
40         }
41     }
42
43     var export_buttons = [
44         {
45             extend: 'excelHtml5',
46             text: _("Excel"),
47             exportOptions: {
48                 columns: exportColumns,
49                 format:  export_format
50             },
51         },
52         {
53             extend: 'csvHtml5',
54             text: _("CSV"),
55             exportOptions: {
56                 columns: exportColumns,
57                 format:  export_format
58             },
59         },
60         {
61             extend: 'copyHtml5',
62             text: _("Copy"),
63             exportOptions: {
64                 columns: exportColumns,
65                 format:  export_format
66             },
67         },
68         {
69             extend: 'print',
70             text: _("Print"),
71             exportOptions: {
72                 columns: exportColumns,
73                 format:  export_format
74             },
75         }
76     ];
77
78     dt_parameters[ "buttons" ] = [
79         {
80             fade: 100,
81             className: "dt_button_clear_filter",
82             titleAttr: _("Clear filter"),
83             enabled: false,
84             text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>',
85             action: function ( e, dt, node, config ) {
86                 dt.search( "" ).draw("page");
87                 node.addClass("disabled");
88             }
89         }
90     ];
91
92     if( included_ids.length > 0 ){
93         dt_parameters[ "buttons" ].push(
94             {
95                 extend: 'colvis',
96                 fade: 100,
97                 columns: included_ids,
98                 className: "columns_controls",
99                 titleAttr: _("Columns settings"),
100                 text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
101                 exportOptions: {
102                     columns: exportColumns
103                 }
104             }
105         );
106     }
107
108     dt_parameters[ "buttons" ].push(
109         {
110             extend: 'collection',
111             autoClose: true,
112             fade: 100,
113             className: "export_controls",
114             titleAttr: _("Export or print"),
115             text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + _("Export") + '</span>',
116             buttons: export_buttons
117         }
118     );
119
120     var table = $(selector);
121     if ( add_filters ) {
122         // Duplicate the table header row for columnFilter
123         thead_row = table.find('thead tr');
124         clone = thead_row.clone().addClass('filters_row');
125         clone.find("th.NoSort").html('');
126         thead_row.before(clone);
127     }
128
129     table.dataTable($.extend(true, {}, dataTablesDefaults, dt_parameters));
130
131     table.DataTable().on("column-visibility.dt", function(){
132         if( typeof columnsInit == 'function' ){
133             // This function can be created separately and used to trigger
134             // an event after the DataTable has loaded AND column visibility
135             // has been updated according to the table's configuration
136             columnsInit();
137         }
138     }).columns( hidden_ids ).visible( false );
139
140     if ( add_filters ) {
141         // show a link to activate filtering
142         link = $('<a>')
143             .attr('href', '#')
144             .attr('id', id_selector + '_activate_filters');
145         $("." + id_selector  + "_table_controls").prepend(link);
146         deactivate_filters(id_selector);
147     }
148
149     $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
150
151     return table;
152 }
153
154 </script>
155 <!-- / columns_settings.inc -->