View | Details | Raw Unified | Return to bug 26553
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc (-335 lines)
Lines 1-334 Link Here
1
[% USE raw %]
2
[% USE Koha %]
3
[% USE TablesSettings %]
4
<!-- columns_settings.inc -->
5
6
<script>
7
// default_filters is not used but keeping the same constructor as kohaTable for consistency
8
function KohaTable(id_selector, dt_parameters, table_settings, add_filters, default_filters, filters_options) {
9
    var selector = '#' + id_selector;
10
11
    if ( table_settings ) {
12
13
        let columns_settings = table_settings["columns"];
14
15
        let table_key = 'DataTables_%s_%s_%s_%s'.format(
16
            table_settings.module,
17
            table_settings.page,
18
            table_settings.table,
19
            id_selector,
20
        );
21
22
        let default_save_state        = table_settings.default_save_state;
23
        let default_save_state_search = table_settings.default_save_state_search;
24
25
        dt_parameters.stateSave = true;
26
        dt_parameters.stateSaveCallback = function( settings, data ) {
27
            localStorage.setItem( table_key, JSON.stringify(data) )
28
        }
29
30
        function set_default(table_settings, table_dt){
31
            let columns = new Array(table_dt.columns()[0].length).fill({visible: true});
32
            let hidden_ids = _dt_visibility(table_settings, table_dt);
33
            hidden_ids.forEach((id, i) => { columns[id] = { visible: false } } );
34
            // State is not loaded if time is not passed
35
            return { columns, time: new Date() };
36
        }
37
        dt_parameters.stateLoadCallback = function(settings) {
38
39
            // Load state from URL
40
            const url = new URL(window.location.href);
41
            let state_from_url = url.searchParams.get( table_key + '_state');
42
            if ( state_from_url ) {
43
                settings.loaded_from_state = true;
44
                return JSON.parse(atob(state_from_url));
45
            }
46
47
            if (!default_save_state) return set_default(table_settings, this.api());
48
49
            let state = localStorage.getItem(table_key);
50
            if (!state) return set_default(table_settings, this.api());
51
52
            state = JSON.parse(state);
53
54
            if (default_save_state_search ) {
55
                settings.loaded_from_state = true;
56
            } else {
57
                delete state.search;
58
                state.columns.forEach(c => delete c.search );
59
            }
60
            return state;
61
        }
62
63
        if ( table_settings.hasOwnProperty('default_save_state') && table_settings['default_save_state'] === 0 ) {
64
            localStorage.removeItem(table_key);
65
        }
66
    }
67
68
    var exportColumns = ":visible:not(.noExport)";
69
    var exportRows = ":visible:not(.noExport)";
70
    if( dt_parameters.hasOwnProperty("exportColumns") ){
71
        // A custom buttons configuration has been passed from the page
72
        exportColumns = dt_parameters["exportColumns"];
73
    }
74
75
    var export_format = {
76
        body: function ( data, row, column, node ) {
77
            var newnode = $(node);
78
79
            if ( newnode.find(".noExport").length > 0 ) {
80
                newnode = newnode.clone();
81
                newnode.find(".noExport").remove();
82
            }
83
84
            return newnode.text().replace( /\n/g, ' ' ).trim();
85
        }
86
    }
87
88
    var export_numeric = {
89
        body: function ( data, row, column, node ) {
90
            var newnode = $(node);
91
92
            if ( newnode.find(".noExport").length > 0 ) {
93
                newnode = newnode.clone();
94
                newnode.find(".noExport").remove();
95
            }
96
            let tp = newnode.text().replace( /\n/g, ' ' ).trim();
97
            tp = $('<p>' + tp + '</p>').text();
98
            return $.isNumeric(tp.replace(',', '.')) ? tp.replace(',', '.') : tp;
99
        }
100
    }
101
102
    var export_buttons = [
103
        {
104
            extend: 'csvHtml5',
105
            text: _("CSV"),
106
            exportOptions: {
107
                columns: exportColumns,
108
                rows: exportRows,
109
                format:  export_format
110
            },
111
        },
112
        {
113
            extend: 'copyHtml5',
114
            text: _("Copy"),
115
            exportOptions: {
116
                columns: exportColumns,
117
                rows: exportRows,
118
                format:  export_format
119
            },
120
        },
121
        {
122
            extend: 'print',
123
            text: _("Print"),
124
            exportOptions: {
125
                columns: exportColumns,
126
                rows: exportRows,
127
                format:  export_format
128
            },
129
        }
130
    ];
131
132
    [% IF Koha.Preference("CurrencyFormat") != 'FR' %]
133
        export_buttons.unshift (
134
            {
135
                extend: 'excelHtml5',
136
                text: _("Excel"),
137
                exportOptions: {
138
                    columns: exportColumns,
139
                    rows: exportRows,
140
                    format:  export_format
141
                },
142
            }
143
        );
144
    [% ELSE %]
145
        export_buttons.unshift (
146
            {
147
                extend: 'excelHtml5',
148
                text: _("Excel"),
149
                exportOptions: {
150
                    columns: exportColumns,
151
                    rows: exportRows,
152
                    format:  export_numeric
153
                },
154
            }
155
        );
156
    [% END %]
157
158
    $.fn.dataTable.ext.buttons.clear_filter =
159
        {
160
            fade: 100,
161
            className: "dt_button_clear_filter",
162
            titleAttr: _("Clear filter"),
163
            enabled: false,
164
            text: '<i class="fa fa-lg fa-times"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>',
165
            action: function ( e, dt, node, config ) {
166
                dt.search( "" ).draw("page");
167
                node.addClass("disabled");
168
            }
169
        };
170
171
    dt_parameters[ "buttons" ] = ['clear_filter'];
172
173
    // Retrieving bKohaColumnsUseNames from the options passed to the constructor, not DT settings
174
    // But ideally should be retrieved using table.data()
175
    let use_names = dt_parameters.bKohaColumnsUseNames;
176
    let included_columns = [];
177
    if (table_settings) {
178
        if (use_names) {
179
            // bKohaColumnsUseNames is set, identify columns by their data-colname
180
            included_columns = table_settings.columns
181
                .filter(c => !c.cannot_be_toggled)
182
                .map(c => "[data-colname='%s']".format(c.columnname))
183
                .join(",");
184
        } else {
185
            // Not set, columns are ordered the same than in the columns settings
186
            included_columns = table_settings.columns
187
                .map((c, i) => (!c.cannot_be_toggled ? i : null))
188
                .filter(i => i !== null);
189
        }
190
    }
191
192
    if( included_columns.length > 0 ){
193
        dt_parameters[ "buttons" ].push(
194
            {
195
                extend: 'colvis',
196
                fade: 100,
197
                columns: included_columns,
198
                className: "columns_controls",
199
                titleAttr: _("Columns settings"),
200
                text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
201
                exportOptions: {
202
                    columns: exportColumns,
203
                    rows: exportRows,
204
                }
205
            }
206
        );
207
    }
208
209
    dt_parameters[ "buttons" ].push(
210
        {
211
            extend: 'collection',
212
            autoClose: true,
213
            fade: 100,
214
            className: "export_controls",
215
            titleAttr: _("Export or print"),
216
            text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + _("Export") + '</span>',
217
            buttons: export_buttons
218
        }
219
    );
220
221
    /* No other exceptions should be added here! */
222
    if ( table_settings && table_settings.page != 'itemsearch' ) {
223
        dt_parameters["buttons"].push(
224
            {
225
                autoClose: true,
226
                fade: 100,
227
                className: "copyConditions_controls",
228
                titleAttr: __("Copy shareable link"),
229
                text: '<i class="fa fa-lg fa-copy"></i> <span class="dt-button-text">' + __("Copy shareable link") + '</span>',
230
                action: function (e, dt, node, config) {
231
                    let table_key = 'DataTables_%s_%s_%s_%s'.format(
232
                        table_settings.module,
233
                        table_settings.page,
234
                        table_settings.table,
235
                        dt.settings()[0].nTable.id,
236
                    );
237
238
                    let state = JSON.stringify(dt.state());
239
                    delete state.time;
240
                    let searchParams = new URLSearchParams(window.location.search);
241
                    searchParams.set(table_key + '_state', btoa(state));
242
                    let url = window.location.origin + window.location.pathname + '?' + searchParams.toString() + window.location.hash;
243
                    if( navigator.clipboard && navigator.clipboard.writeText){
244
                        navigator.clipboard.writeText( url );
245
                        $(node).tooltip({trigger: 'manual', title: _("Copied!")}).tooltip('show');
246
                    }
247
                },
248
            }
249
        );
250
    }
251
252
    if ( table_settings && CAN_user_parameters_manage_column_config ) {
253
        dt_parameters[ "buttons" ].push(
254
            {
255
                className: "dt_button_configure_table",
256
                fade: 100,
257
                titleAttr: _("Configure table"),
258
                text: '<i class="fa fa-lg fa-wrench"></i> <span class="dt-button-text">' + _("Configure") + '</span>',
259
                action: function() {
260
                    window.location = '/cgi-bin/koha/admin/columns_settings.pl?module=' + table_settings['module'] + '&page=' + table_settings['page'] + '&table=' + table_settings['table'];
261
                },
262
            }
263
        );
264
    }
265
266
    if ( add_filters ) {
267
        dt_parameters['orderCellsTop'] = true;
268
    }
269
270
    var table = $(selector);
271
272
    var new_parameters = {}
273
    $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
274
    var default_column_defs = [
275
        { "targets": [ "title-string" ], "type": "title-string" },
276
        { "targets": [ "string-sort" ],  "type": "string" },
277
        { "targets": [ "anti-the" ],     "type": "anti-the" },
278
        { "targets": [ "NoSort" ],       "orderable": false, "searchable": false },
279
        { "targets": [ "NoVisible" ],    "visible": false }
280
    ];
281
    if ( new_parameters["aoColumnDefs"] === undefined ) {
282
        new_parameters["aoColumnDefs"] = default_column_defs;
283
    } else {
284
        $(default_column_defs).each(function(){
285
            new_parameters["aoColumnDefs"].push(this);
286
        });
287
    }
288
289
    new_parameters["loaded_from_state"] = false;
290
291
    if ( table_settings ) {
292
        if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) {
293
            // pageLength needs to be a number, not a string, or it can cause issues with DataTable's next button.
294
            new_parameters["pageLength"] = parseInt(table_settings['default_display_length']);
295
        }
296
        if ( table_settings.hasOwnProperty('default_sort_order') && table_settings['default_sort_order'] != null ) {
297
            new_parameters["order"] = [[ table_settings['default_sort_order'], 'asc' ]];
298
        }
299
    }
300
301
    $(table).data('bKohaColumnsUseNames', dt_parameters.bKohaColumnsUseNames);
302
303
    table.dataTable(new_parameters);
304
    var table_dt = table.DataTable();
305
    table_dt.on("column-visibility.dt", function(){
306
        if( typeof columnsInit == 'function' ){
307
            // This function can be created separately and used to trigger
308
            // an event after the DataTable has loaded AND column visibility
309
            // has been updated according to the table's configuration
310
            columnsInit(this);
311
        }
312
    });
313
314
    if ( add_filters ) {
315
        _dt_add_filters(table, table_dt, filters_options);
316
    }
317
318
    table_dt.on("column-visibility.dt", function(){
319
        if ( add_filters ) {
320
            _dt_add_filters(table, table_dt, filters_options);
321
        }
322
    });
323
324
    table_dt.on( 'search.dt', function ( e, settings ) {
325
        // When the DataTables search function is triggered,
326
        // enable or disable the "Clear filter" button based on
327
        // the presence of a search string
328
        toggledClearFilter(table_dt.search(), settings.nTable.id);
329
    });
330
331
    return table;
332
}
333
</script>
334
<!-- / columns_settings.inc -->
335
- 

Return to bug 26553