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