Bug 30650: Add link to the circ nav bar
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / columns_settings.inc
1 [% USE raw %]
2 [% USE Koha %]
3 [% USE TablesSettings %]
4 <!-- columns_settings.inc -->
5
6 <script>
7 function KohaTable(id_selector, dt_parameters, table_settings, add_filters) {
8     var counter = 0;
9     var hidden_ids = [];
10     var included_ids = [];
11     var selector = '#' + id_selector;
12
13     if ( table_settings ) {
14         var columns_settings = table_settings['columns'];
15
16         $(columns_settings).each( function() {
17             var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector + ' th' );
18             var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter;
19             if ( used_id == -1 ) return;
20
21             if ( this['is_hidden'] == "1" ) {
22                 hidden_ids.push( used_id );
23             }
24             if ( this['cannot_be_toggled'] == "0" ) {
25                 included_ids.push( used_id );
26             }
27             counter++;
28         });
29     }
30
31     var exportColumns = ":visible:not(.noExport)";
32     var exportRows = ":visible:not(.noExport)";
33     if( dt_parameters.hasOwnProperty("exportColumns") ){
34         // A custom buttons configuration has been passed from the page
35         exportColumns = dt_parameters["exportColumns"];
36     }
37
38     var export_format = {
39         body: function ( data, row, column, node ) {
40             var newnode = $(node);
41
42             if ( newnode.find(".noExport").length > 0 ) {
43                 newnode = newnode.clone();
44                 newnode.find(".noExport").remove();
45             }
46
47             return newnode.text().replace( /\n/g, ' ' ).trim();
48         }
49     }
50
51     var export_numeric = {
52         body: function ( data, row, column, node ) {
53             var newnode = $(node);
54
55             if ( newnode.find(".noExport").length > 0 ) {
56                 newnode = newnode.clone();
57                 newnode.find(".noExport").remove();
58             }
59             let tp = newnode.text().replace( /\n/g, ' ' ).trim();
60             tp = $('<p>' + tp + '</p>').text();
61             return $.isNumeric(tp.replace(',', '.')) ? tp.replace(',', '.') : tp;
62         }
63     }
64
65     var export_buttons = [
66         {
67             extend: 'csvHtml5',
68             text: _("CSV"),
69             exportOptions: {
70                 columns: exportColumns,
71                 rows: exportRows,
72                 format:  export_format
73             },
74         },
75         {
76             extend: 'copyHtml5',
77             text: _("Copy"),
78             exportOptions: {
79                 columns: exportColumns,
80                 rows: exportRows,
81                 format:  export_format
82             },
83         },
84         {
85             extend: 'print',
86             text: _("Print"),
87             exportOptions: {
88                 columns: exportColumns,
89                 rows: exportRows,
90                 format:  export_format
91             },
92         }
93     ];
94
95     [% IF Koha.Preference("CurrencyFormat") != 'FR' %]
96         export_buttons.unshift (
97             {
98                 extend: 'excelHtml5',
99                 text: _("Excel"),
100                 exportOptions: {
101                     columns: exportColumns,
102                     rows: exportRows,
103                     format:  export_format
104                 },
105             }
106         );
107     [% ELSE %]
108         export_buttons.unshift (
109             {
110                 extend: 'excelHtml5',
111                 text: _("Excel"),
112                 exportOptions: {
113                     columns: exportColumns,
114                     rows: exportRows,
115                     format:  export_numeric
116                 },
117             }
118         );
119     [% END %]
120
121     dt_parameters[ "buttons" ] = [
122         {
123             fade: 100,
124             className: "dt_button_clear_filter",
125             titleAttr: _("Clear filter"),
126             enabled: false,
127             text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>',
128             action: function ( e, dt, node, config ) {
129                 dt.search( "" ).draw("page");
130                 node.addClass("disabled");
131             }
132         }
133     ];
134
135     if( included_ids.length > 0 ){
136         dt_parameters[ "buttons" ].push(
137             {
138                 extend: 'colvis',
139                 fade: 100,
140                 columns: included_ids,
141                 className: "columns_controls",
142                 titleAttr: _("Columns settings"),
143                 text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
144                 exportOptions: {
145                     columns: exportColumns,
146                     rows: exportRows,
147                 }
148             }
149         );
150     }
151
152     dt_parameters[ "buttons" ].push(
153         {
154             extend: 'collection',
155             autoClose: true,
156             fade: 100,
157             className: "export_controls",
158             titleAttr: _("Export or print"),
159             text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + _("Export") + '</span>',
160             buttons: export_buttons
161         }
162     );
163
164     var table = $(selector);
165     if ( add_filters ) {
166         // Duplicate the table header row for columnFilter
167         thead_row = table.find('thead tr');
168         clone = thead_row.clone().addClass('filters_row');
169         clone.find("th.NoSort").html('');
170         thead_row.before(clone);
171     }
172
173     var new_parameters = {}
174     $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
175     var default_column_defs = [
176         { "targets": [ "title-string" ], "type": "title-string" },
177         { "targets": [ "string-sort" ],  "type": "string" },
178         { "targets": [ "anti-the" ],     "type": "anti-the" },
179         { "targets": [ "NoSort" ],       "orderable": false, "searchable": false },
180         { "targets": [ "NoVisible" ],    "visible": false }
181     ];
182     if ( new_parameters["aoColumnDefs"] === undefined ) {
183         new_parameters["aoColumnDefs"] = default_column_defs;
184     } else {
185         $(default_column_defs).each(function(){
186             new_parameters["aoColumnDefs"].push(this);
187         });
188     }
189
190     if ( table_settings ) {
191         if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) {
192             new_parameters["pageLength"] = table_settings['default_display_length'];
193         }
194         if ( table_settings.hasOwnProperty('default_sort_order') && table_settings['default_sort_order'] != null ) {
195             new_parameters["order"] = [[ table_settings['default_sort_order'], 'asc' ]];
196         }
197     }
198
199     table.dataTable(new_parameters);
200     table.DataTable().on("column-visibility.dt", function(){
201         if( typeof columnsInit == 'function' ){
202             // This function can be created separately and used to trigger
203             // an event after the DataTable has loaded AND column visibility
204             // has been updated according to the table's configuration
205             columnsInit();
206         }
207     }).columns( hidden_ids ).visible( false );
208
209     if ( add_filters ) {
210         // show a link to activate filtering
211         link = $('<a>')
212             .attr('href', '#')
213             .attr('id', id_selector + '_activate_filters');
214         $("." + id_selector  + "_table_controls").prepend(link);
215         deactivate_filters(id_selector);
216     }
217
218     $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
219
220     return table;
221 }
222
223
224 /* get_columns_saved_state checks for a DataTables configuration saved
225 * in the browser's local storage. If it is present, the columns
226 * configuration supplied by Koha is overwritten
227 *
228 * It takes two parameters:
229 *  - localstorage_config, the DataTables saved state object from local storage
230 *  - columns_settings, the columns settings object supplied by the template
231 *
232 * An example:
233 *
234 *    var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
235 *    var saved_table = localStorage.getItem("DataTables_TABLE_ID_/cgi-bin/koha/PATH/TO/SCRIPT.pl");
236 *    var updated_settings = get_columns_saved_state( saved_table, columns_settings );
237 *
238 *    KohaTable("TABLE_ID", {
239 *        "stateSave": true
240 *    }, updated_settings);
241 */
242
243 function get_columns_saved_state( localstorage_config, columns_settings ){
244     var tables = JSON.parse( localstorage_config );
245     // if a table configuration was found in local storage, parse it
246     if( tables ){
247         var stateSave_column_visibility = [];
248         $(tables.columns).each(function(){
249             stateSave_column_visibility.push( this.visible === true ? 0 : 1 );
250         });
251         $.each( columns_settings, function( index, key ){
252             if( stateSave_column_visibility[ index ] !== columns_settings[key] ){
253                 columns_settings[ index ].is_hidden = stateSave_column_visibility[ index ];
254             }
255         });
256         return columns_settings;
257     } else {
258         return columns_settings;
259     }
260 }
261
262 </script>
263 <!-- / columns_settings.inc -->