Bug 31428: 'Configure this table' -> 'Configure'
[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     if ( CAN_user_parameters_manage_column_config ) {
165         dt_parameters[ "buttons" ].push(
166             {
167                 className: "dt_button_configure_table",
168                 titleAttr: _("Table settings"),
169                 text: '<i class="fa fa-lg fa-wrench"></i> <span class="dt-button-text">' + _("Configure") + '</span>',
170                 action: function() {
171                     window.location = '/cgi-bin/koha/admin/columns_settings.pl?module=' + table_settings['module'] + '&page=' + table_settings['page'] + '&table=' + table_settings['table'];
172                 },
173             }
174         );
175     }
176
177     var table = $(selector);
178     if ( add_filters ) {
179         // Duplicate the table header row for columnFilter
180         thead_row = table.find('thead tr');
181         clone = thead_row.clone().addClass('filters_row');
182         clone.find("th.NoSort").html('');
183         thead_row.before(clone);
184     }
185
186     var new_parameters = {}
187     $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
188     var default_column_defs = [
189         { "targets": [ "title-string" ], "type": "title-string" },
190         { "targets": [ "string-sort" ],  "type": "string" },
191         { "targets": [ "anti-the" ],     "type": "anti-the" },
192         { "targets": [ "NoSort" ],       "orderable": false, "searchable": false },
193         { "targets": [ "NoVisible" ],    "visible": false }
194     ];
195     if ( new_parameters["aoColumnDefs"] === undefined ) {
196         new_parameters["aoColumnDefs"] = default_column_defs;
197     } else {
198         $(default_column_defs).each(function(){
199             new_parameters["aoColumnDefs"].push(this);
200         });
201     }
202
203     if ( table_settings ) {
204         if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) {
205             new_parameters["pageLength"] = table_settings['default_display_length'];
206         }
207         if ( table_settings.hasOwnProperty('default_sort_order') && table_settings['default_sort_order'] != null ) {
208             new_parameters["order"] = [[ table_settings['default_sort_order'], 'asc' ]];
209         }
210     }
211
212     table.dataTable(new_parameters);
213     table.DataTable().on("column-visibility.dt", function(){
214         if( typeof columnsInit == 'function' ){
215             // This function can be created separately and used to trigger
216             // an event after the DataTable has loaded AND column visibility
217             // has been updated according to the table's configuration
218             columnsInit();
219         }
220     }).columns( hidden_ids ).visible( false );
221
222     if ( add_filters ) {
223         // show a link to activate filtering
224         link = $('<a>')
225             .attr('href', '#')
226             .attr('id', id_selector + '_activate_filters');
227         $("." + id_selector  + "_table_controls").prepend(link);
228         deactivate_filters(id_selector);
229     }
230
231     $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
232
233     return table;
234 }
235
236
237 /* get_columns_saved_state checks for a DataTables configuration saved
238 * in the browser's local storage. If it is present, the columns
239 * configuration supplied by Koha is overwritten
240 *
241 * It takes two parameters:
242 *  - localstorage_config, the DataTables saved state object from local storage
243 *  - columns_settings, the columns settings object supplied by the template
244 *
245 * An example:
246 *
247 *    var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
248 *    var saved_table = localStorage.getItem("DataTables_TABLE_ID_/cgi-bin/koha/PATH/TO/SCRIPT.pl");
249 *    var updated_settings = get_columns_saved_state( saved_table, columns_settings );
250 *
251 *    KohaTable("TABLE_ID", {
252 *        "stateSave": true
253 *    }, updated_settings);
254 */
255
256 function get_columns_saved_state( localstorage_config, columns_settings ){
257     var tables = JSON.parse( localstorage_config );
258     // if a table configuration was found in local storage, parse it
259     if( tables ){
260         var stateSave_column_visibility = [];
261         $(tables.columns).each(function(){
262             stateSave_column_visibility.push( this.visible === true ? 0 : 1 );
263         });
264         $.each( columns_settings, function( index, key ){
265             if( stateSave_column_visibility[ index ] !== columns_settings[key] ){
266                 columns_settings[ index ].is_hidden = stateSave_column_visibility[ index ];
267             }
268         });
269         return columns_settings;
270     } else {
271         return columns_settings;
272     }
273 }
274
275 </script>
276 <!-- / columns_settings.inc -->