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

(-)a/koha-tmpl/intranet-tmpl/prog/js/datatables.js (-11 lines)
Lines 68-88 var dataTablesDefaults = { Link Here
68
    fixedHeader: true,
68
    fixedHeader: true,
69
    initComplete: function (settings) {
69
    initComplete: function (settings) {
70
        var tableId = settings.nTable.id;
70
        var tableId = settings.nTable.id;
71
        let table_node = $("#" + tableId);
72
73
        state = settings.oLoadedState;
71
        state = settings.oLoadedState;
74
        state &&
72
        state &&
75
            state.search &&
73
            state.search &&
76
            toggledClearFilter(state.search.search, tableId);
74
            toggledClearFilter(state.search.search, tableId);
77
78
        //if (settings.ajax) {
79
        //    if ( typeof this.api === 'function' ) {
80
        //        _dt_add_delay(this.api(), table_node);
81
        //    } else {
82
        //        let dt = $(table_node).DataTable();
83
        //        _dt_add_delay(dt, table_node);
84
        //    }
85
        //}
86
    },
75
    },
87
};
76
};
88
DataTable.defaults.column.orderSequence = ["asc", "desc"];
77
DataTable.defaults.column.orderSequence = ["asc", "desc"];
(-)a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js (-36 / +291 lines)
Lines 40-86 var dataTablesDefaults = { Link Here
40
    dom: "t",
40
    dom: "t",
41
    buttons: ["clearFilter", "copy", "csv", "print"],
41
    buttons: ["clearFilter", "copy", "csv", "print"],
42
    paginate: false,
42
    paginate: false,
43
    buttons: [
44
        {
45
            fade: 100,
46
            className: "dt_button_clear_filter",
47
            titleAttr: __("Clear filter"),
48
            enabled: false,
49
            text:
50
                '<i class="fa fa-lg fa-times"></i> <span class="dt-button-text">' +
51
                __("Clear filter") +
52
                "</span>",
53
            available: function (dt) {
54
                // The "clear filter" button is made available if this test returns true
55
                if (dt.settings()[0].aanFeatures.f) {
56
                    // aanFeatures.f is null if there is no search form
57
                    return true;
58
                }
59
            },
60
            action: function (e, dt, node) {
61
                dt.search("").draw("page");
62
                node.addClass("disabled");
63
            },
64
        },
65
    ],
43
    initComplete: function (settings) {
66
    initComplete: function (settings) {
44
        var tableId = settings.nTable.id;
67
        var tableId = settings.nTable.id;
45
        // When the DataTables search function is triggered,
68
        state = settings.oLoadedState;
46
        // enable or disable the "Clear filter" button based on
69
        state &&
47
        // the presence of a search string
70
            state.search &&
48
        $("#" + tableId).on("search.dt", function (e, settings) {
71
            toggledClearFilter(state.search.search, tableId);
49
            if (settings.oPreviousSearch.sSearch == "") {
50
                $("#" + tableId + "_wrapper")
51
                    .find(".dt_button_clear_filter")
52
                    .addClass("disabled");
53
            } else {
54
                $("#" + tableId + "_wrapper")
55
                    .find(".dt_button_clear_filter")
56
                    .removeClass("disabled");
57
            }
58
        });
59
    },
72
    },
60
};
73
};
61
DataTable.defaults.column.orderSequence = ["asc", "desc"];
74
DataTable.defaults.column.orderSequence = ["asc", "desc"];
62
75
63
$.fn.dataTable.ext.buttons.clearFilter = {
76
function toggledClearFilter(searchText, tableId) {
64
    fade: 100,
77
    let clear_filter_button = $("#" + tableId + "_wrapper").find(
65
    className: "dt_button_clear_filter",
78
        ".dt_button_clear_filter"
66
    titleAttr: __("Clear filter"),
79
    );
67
    enabled: false,
80
    if (searchText == "") {
68
    text:
81
        clear_filter_button.addClass("disabled");
69
        '<i class="fa fa-lg fa-times"></i> <span class="dt-button-text">' +
82
    } else {
70
        __("Clear filter") +
83
        clear_filter_button.removeClass("disabled");
71
        "</span>",
84
    }
72
    available: function (dt) {
85
}
73
        // The "clear filter" button is made available if this test returns true
74
        if (dt.settings()[0].aanFeatures.f) {
75
            // aanFeatures.f is null if there is no search form
76
            return true;
77
        }
78
    },
79
    action: function (e, dt, node) {
80
        dt.search("").draw("page");
81
        node.addClass("disabled");
82
    },
83
};
84
86
85
(function () {
87
(function () {
86
    /* Plugin to allow text sorting to ignore articles
88
    /* Plugin to allow text sorting to ignore articles
Lines 126-128 $.fn.dataTable.ext.buttons.clearFilter = { Link Here
126
        },
128
        },
127
    });
129
    });
128
})();
130
})();
129
- 
131
132
function _dt_buttons(params) {
133
    let settings = params.settings || {};
134
    let table_settings = params.table_settings;
135
136
    var exportColumns = ":visible:not(.noExport)";
137
    if (settings.hasOwnProperty("exportColumns")) {
138
        // A custom buttons configuration has been passed from the page
139
        exportColumns = settings["exportColumns"];
140
    }
141
142
    var export_format = {
143
        body: function (data, row, column, node) {
144
            var newnode = $(node);
145
146
            if (newnode.find(".noExport").length > 0) {
147
                newnode = newnode.clone();
148
                newnode.find(".noExport").remove();
149
            }
150
151
            return newnode.text().replace(/\n/g, " ").trim();
152
        },
153
    };
154
155
    var export_buttons = [
156
        {
157
            extend: "excelHtml5",
158
            exportOptions: {
159
                columns: exportColumns,
160
                format: export_format,
161
            },
162
        },
163
        {
164
            extend: "csvHtml5",
165
            exportOptions: {
166
                columns: exportColumns,
167
                format: export_format,
168
            },
169
        },
170
        {
171
            extend: "copyHtml5",
172
            exportOptions: {
173
                columns: exportColumns,
174
                format: export_format,
175
            },
176
        },
177
        {
178
            extend: "print",
179
            exportOptions: {
180
                columns: exportColumns,
181
                format: export_format,
182
            },
183
        },
184
    ];
185
186
    let buttons = [];
187
    buttons.push({
188
        fade: 100,
189
        className: "dt_button_clear_filter",
190
        titleAttr: __("Clear filter"),
191
        enabled: false,
192
        text:
193
            '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' +
194
            __("Clear filter") +
195
            "</span>",
196
        action: function (e, dt, node, config) {
197
            dt.search("").draw("page");
198
            node.addClass("disabled");
199
        },
200
    });
201
202
    // Retrieving bKohaColumnsUseNames from the options passed to the constructor, not DT's settings
203
    // But ideally should be retrieved using table.data()
204
    let use_names = settings.bKohaColumnsUseNames;
205
    let included_columns = [];
206
    if (table_settings) {
207
        if (use_names) {
208
            // bKohaColumnsUseNames is set, identify columns by their data-colname
209
            included_columns = table_settings.columns
210
                .filter(c => !c.cannot_be_toggled)
211
                .map(c => "[data-colname='%s']".format(c.columnname))
212
                .join(",");
213
        } else {
214
            // Not set, columns are ordered the same than in the columns settings
215
            included_columns = table_settings.columns
216
                .map((c, i) => (!c.cannot_be_toggled ? i : null))
217
                .filter(i => i !== null);
218
        }
219
    }
220
    if (included_columns.length > 0) {
221
        buttons.push({
222
            extend: "colvis",
223
            fade: 100,
224
            columns: included_columns,
225
            className: "columns_controls",
226
            titleAttr: __("Columns settings"),
227
            text:
228
                '<i class="fa fa-lg fa-gear" aria-hidden="true"></i> <span class="dt-button-text">' +
229
                _("Columns") +
230
                "</span>",
231
            exportOptions: {
232
                columns: exportColumns,
233
            },
234
        });
235
    }
236
237
    buttons.push({
238
        extend: "collection",
239
        autoClose: true,
240
        fade: 100,
241
        className: "export_controls",
242
        titleAttr: __("Export or print"),
243
        text:
244
            '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' +
245
            __("Export") +
246
            "</span>",
247
        buttons: export_buttons,
248
    });
249
250
    return buttons;
251
}
252
253
function _dt_visibility(table_settings, table_dt) {
254
    let hidden_ids = [];
255
    if (table_settings) {
256
        var columns_settings = table_settings.columns;
257
        let i = 0;
258
        let use_names = $(table_dt.table().node()).data("bKohaColumnsUseNames");
259
        if (use_names) {
260
            let hidden_columns = table_settings.columns.filter(
261
                c => c.is_hidden
262
            );
263
            if (!hidden_columns.length) return [];
264
            table_dt
265
                .columns(
266
                    hidden_columns
267
                        .map(c => "[data-colname='%s']".format(c.columnname))
268
                        .join(",")
269
                )
270
                .every(function () {
271
                    hidden_ids.push(this.index());
272
                });
273
        } else {
274
            $(columns_settings).each(function (i, c) {
275
                if (c.is_hidden == "1") {
276
                    hidden_ids.push(i);
277
                }
278
            });
279
        }
280
    }
281
    return hidden_ids;
282
}
283
284
(function ($) {
285
    /**
286
     * Create a new dataTables instance that uses the Koha RESTful API's as a data source
287
     * @param  {Object}  options                      Please see the dataTables settings documentation for further
288
     *                                                details
289
     * @param  {string}  [options.criteria=contains]  A koha specific extension to the dataTables settings block that
290
     *                                                allows setting the 'comparison operator' used in searches
291
     *                                                Supports `contains`, `starts_with`, `ends_with` and `exact` match
292
     * @param  {string}  [options.columns.*.type      Data type the field is stored in so we may impose some additional
293
     *                                                manipulation to search strings. Supported types are currenlty 'date'
294
     * @param  {Object}  table_settings               The arrayref as returned by TableSettings.GetTableSettings function
295
     *                                                available from the columns_settings template toolkit include
296
     * @return {Object}                               The dataTables instance
297
     */
298
    $.fn.kohaTable = function (options, table_settings) {
299
        var settings = null;
300
301
        // Early return if the node does not exist
302
        if (!this.length) return;
303
304
        if (options) {
305
            // Don't redefine the default initComplete
306
            if (options.initComplete) {
307
                let our_initComplete = options.initComplete;
308
                options.initComplete = function (settings, json) {
309
                    our_initComplete(settings, json);
310
                    dataTablesDefaults.initComplete(settings, json);
311
                };
312
            }
313
314
            settings = $.extend(
315
                true,
316
                {},
317
                dataTablesDefaults,
318
                {
319
                    paging: true,
320
                    searching: true,
321
                    language: {
322
                        emptyTable: options.emptyTable
323
                            ? options.emptyTable
324
                            : __("No data available in table"),
325
                    },
326
                },
327
                options
328
            );
329
        }
330
331
        settings["buttons"] = _dt_buttons({ settings, table_settings });
332
333
        if (table_settings) {
334
            if (
335
                table_settings.hasOwnProperty("default_display_length") &&
336
                table_settings["default_display_length"] != null
337
            ) {
338
                settings["pageLength"] =
339
                    table_settings["default_display_length"];
340
            }
341
            if (
342
                table_settings.hasOwnProperty("default_sort_order") &&
343
                table_settings["default_sort_order"] != null
344
            ) {
345
                settings["order"] = [
346
                    [table_settings["default_sort_order"], "asc"],
347
                ];
348
            }
349
        }
350
351
        var default_column_defs = [
352
            { aTargets: ["string-sort"], sType: "string" },
353
            { aTargets: ["anti-the"], sType: "anti-the" },
354
            { aTargets: ["NoSort"], bSortable: false, bSearchable: false },
355
        ];
356
357
        $(this).data("bKohaColumnsUseNames", settings.bKohaColumnsUseNames);
358
        var table = $(this).dataTable(settings);
359
360
        var table_dt = table.DataTable();
361
362
        table_dt.on("search.dt", function (e, settings) {
363
            // When the DataTables search function is triggered,
364
            // enable or disable the "Clear filter" button based on
365
            // the presence of a search string
366
            toggledClearFilter(table_dt.search(), settings.nTable.id);
367
        });
368
369
        let hidden_ids = _dt_visibility(table_settings, table_dt);
370
        table_dt
371
            .on("column-visibility.dt", function () {
372
                if (typeof columnsInit == "function") {
373
                    // This function can be created separately and used to trigger
374
                    // an event after the DataTable has loaded AND column visibility
375
                    // has been updated according to the table's configuration
376
                    columnsInit();
377
                }
378
            })
379
            .columns(hidden_ids)
380
            .visible(false);
381
382
        return table;
383
    };
384
})(jQuery);

Return to bug 38255