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