Bug 26234: Teach our KohaTable constructor the specific th classes
[koha-equinox.git] / koha-tmpl / intranet-tmpl / prog / en / includes / columns_settings.inc
1 [% USE TablesSettings %]
2 <!-- columns_settings.inc -->
3
4 <script>
5 function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
6     var counter = 0;
7     var hidden_ids = [];
8     var included_ids = [];
9     var selector = '#' + id_selector;
10
11     $(columns_settings).each( function() {
12         var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector + ' th' );
13         var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter;
14         if ( used_id == -1 ) return;
15
16         if ( this['is_hidden'] == "1" ) {
17             hidden_ids.push( used_id );
18         }
19         if ( this['cannot_be_toggled'] == "0" ) {
20             included_ids.push( used_id );
21         }
22         counter++;
23     });
24
25     var exportColumns = ":visible:not(.noExport)";
26     if( dt_parameters.hasOwnProperty("exportColumns") ){
27         // A custom buttons configuration has been passed from the page
28         exportColumns = dt_parameters["exportColumns"];
29     }
30
31     var export_format = {
32         body: function ( data, row, column, node ) {
33             var newnode = $(node);
34
35             if ( newnode.find(".noExport").length > 0 ) {
36                 newnode = newnode.clone();
37                 newnode.find(".noExport").remove();
38             }
39
40             return newnode.text().replace( /\n/g, ' ' ).trim();
41         }
42     }
43
44     var export_buttons = [
45         {
46             extend: 'excelHtml5',
47             text: _("Excel"),
48             exportOptions: {
49                 columns: exportColumns,
50                 format:  export_format
51             },
52         },
53         {
54             extend: 'csvHtml5',
55             text: _("CSV"),
56             exportOptions: {
57                 columns: exportColumns,
58                 format:  export_format
59             },
60         },
61         {
62             extend: 'copyHtml5',
63             text: _("Copy"),
64             exportOptions: {
65                 columns: exportColumns,
66                 format:  export_format
67             },
68         },
69         {
70             extend: 'print',
71             text: _("Print"),
72             exportOptions: {
73                 columns: exportColumns,
74                 format:  export_format
75             },
76         }
77     ];
78
79     dt_parameters[ "buttons" ] = [
80         {
81             fade: 100,
82             className: "dt_button_clear_filter",
83             titleAttr: _("Clear filter"),
84             enabled: false,
85             text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>',
86             action: function ( e, dt, node, config ) {
87                 dt.search( "" ).draw("page");
88                 node.addClass("disabled");
89             }
90         }
91     ];
92
93     if( included_ids.length > 0 ){
94         dt_parameters[ "buttons" ].push(
95             {
96                 extend: 'colvis',
97                 fade: 100,
98                 columns: included_ids,
99                 className: "columns_controls",
100                 titleAttr: _("Columns settings"),
101                 text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
102                 exportOptions: {
103                     columns: exportColumns
104                 }
105             }
106         );
107     }
108
109     dt_parameters[ "buttons" ].push(
110         {
111             extend: 'collection',
112             autoClose: true,
113             fade: 100,
114             className: "export_controls",
115             titleAttr: _("Export or print"),
116             text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + _("Export") + '</span>',
117             buttons: export_buttons
118         }
119     );
120
121     var table = $(selector);
122     if ( add_filters ) {
123         // Duplicate the table header row for columnFilter
124         thead_row = table.find('thead tr');
125         clone = thead_row.clone().addClass('filters_row');
126         clone.find("th.NoSort").html('');
127         thead_row.before(clone);
128     }
129
130     var new_parameters = {}
131     $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
132     var default_column_defs = [
133         { "aTargets": [ "title-string" ], "sType": "title-string" },
134         { "aTargets": [ "string-sort" ],  "sType": "string" },
135         { "aTargets": [ "anti-the" ],     "sType": "anti-the" },
136         { "aTargets": [ "NoSort" ],       "bSortable": false, "bSearchable": false },
137     ];
138     if ( new_parameters["aoColumnDefs"] === undefined ) {
139         new_parameters["aoColumnDefs"] = default_column_defs;
140     } else {
141         $.extend(true, new_parameters, default_column_defs);
142     }
143
144     table.dataTable(new_parameters);
145     table.DataTable().on("column-visibility.dt", function(){
146         if( typeof columnsInit == 'function' ){
147             // This function can be created separately and used to trigger
148             // an event after the DataTable has loaded AND column visibility
149             // has been updated according to the table's configuration
150             columnsInit();
151         }
152     }).columns( hidden_ids ).visible( false );
153
154     if ( add_filters ) {
155         // show a link to activate filtering
156         link = $('<a>')
157             .attr('href', '#')
158             .attr('id', id_selector + '_activate_filters');
159         $("." + id_selector  + "_table_controls").prepend(link);
160         deactivate_filters(id_selector);
161     }
162
163     $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
164
165     return table;
166 }
167
168 </script>
169 <!-- / columns_settings.inc -->