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