Bug 9573: Lost items report - Move filters code to a separate js file
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 5 Oct 2017 16:15:43 +0000 (13:15 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Sat, 17 Feb 2018 21:32:13 +0000 (18:32 -0300)
For the next patches we will need to reuse what is already done on the
bibliographic record detail page. This patch moves the code to make it
reusable easily and avoid copy and paste.

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc
koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
koha-tmpl/intranet-tmpl/prog/js/table_filters.js [new file with mode: 0644]

index b065a35..5d674bc 100644 (file)
@@ -1,7 +1,7 @@
 [% USE ColumnsSettings %]
 
 <script type="text/javascript">
-function KohaTable(selector, dt_parameters, columns_settings) {
+function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
     var id = 0;
     var hidden_ids = [];
     var included_ids = [];
@@ -25,12 +25,31 @@ function KohaTable(selector, dt_parameters, columns_settings) {
             text: _("Column visibility"),
         }
     ];
-    var table = $(selector).dataTable($.extend(true, {}, dataTablesDefaults, dt_parameters));
+
+    var table = $('#' + id_selector + ' table');
+    if ( add_filters ) {
+        // Duplicate the table header row for columnFilter
+        thead_row = table.find('thead tr');
+        clone = thead_row.clone().addClass('filters_row');
+        clone.find("th.NoSort").html('');
+        thead_row.before(clone);
+    }
+
+    table.dataTable($.extend(true, {}, dataTablesDefaults, dt_parameters));
 
     $(hidden_ids).each(function(index, value) {
         table.fnSetColumnVis( value, false );
     });
 
+    if ( add_filters ) {
+        // show a link to activate filtering
+        link = $('<a>')
+            .attr('href', '#')
+            .attr('id', id_selector + '_activate_filters');
+        $("." + id_selector + "_table_controls").prepend(link);
+        deactivate_filters(id_selector);
+    }
+
     return table;
 }
 
index 69f098c..a990c80 100644 (file)
     [% INCLUDE 'datatables.inc' %]
     <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.dataTables.columnFilter_[% KOHA_VERSION %].js"></script>
     [% INCLUDE 'browser-strings.inc' %]
+    [% INCLUDE 'columns_settings.inc' %]
     <script type="text/javascript" src="[% interface %]/js/browser_[% KOHA_VERSION %].js"></script>
+    <script type="text/javascript" src="[% interface %]/[% theme %]/js/table_filters_[% KOHA_VERSION %].js"></script>
     <script type="text/javascript">
         var browser = KOHA.browser('[% searchid %]', parseInt(biblionumber, 10));
         browser.show();
 
-        function activate_filters(id) {
-            var table = $("#" + id + " table");
-            if (table.length == 1) {
-                filters_row = table.find('thead tr.filters_row');
-
-                var aoColumns = [];
-                filters_row.find('th').each(function() {
-                    if(this.className === "NoSort"){
-                        aoColumns.push(null);
-                    } else {
-                        aoColumns.push('text');
-                    }
-                });
-
-                if (table.find('thead tr.columnFilter').length == 0) {
-                    table.dataTable().columnFilter({
-                        'sPlaceHolder': 'head:after'
-                        ,   'aoColumns': aoColumns
-                    });
-                    filters_row.addClass('columnFilter');
-                }
-                filters_row.show();
-            }
-
-            $('#' + id + '_activate_filters')
-                .html('<i class="fa fa-filter"></i> ' + _("Deactivate filters") )
-                .unbind('click')
-                .click(function() {
-                    deactivate_filters(id);
-                    return false;
-                });
-        }
-
-        function deactivate_filters(id) {
-            filters_row = $("#" + id + " table").find('thead tr.filters_row');
-
-            filters_row.find('input[type="text"]')
-                .val('')            // Empty filter text boxes
-                .trigger('keyup')   // Filter (display all rows)
-                .trigger('blur');   // Reset value to the column name
-            filters_row.hide();
-
-            $('#' + id + '_activate_filters')
-                .html('<i class="fa fa-filter"></i> ' + _("Activate filters") )
-                .unbind('click')
-                .click(function() {
-                    activate_filters(id);
-                    return false;
-                });
-        }
-
         $(document).ready(function() {
             var ids = ['holdings', 'otherholdings'];
+
             for (var i in ids) {
                 var id = ids[i];
-                var table = $('#' + id + ' table');
-
-                // Duplicate the table header row for columnFilter
-                thead_row = table.find('thead tr');
-                clone = thead_row.clone().addClass('filters_row');
-                clone.find("th.NoSort").html('');
-                thead_row.before(clone);
-
-                // Enable sorting
-                table.dataTable($.extend(true, {}, dataTablesDefaults, {
+                var dt_parameters = {
                     'sDom': 't',
                     'bPaginate': false,
                     'bAutoWidth': false,
                     "aoColumnDefs": [
                         { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] }
                     ]
-                }));
-
-                // Show a link to activate filtering
-                link = $('<a>')
-                    .attr('href', '#')
-                    .attr('id', id + '_activate_filters');
-                $("." + id + "_table_controls").prepend(link);
-                deactivate_filters(id);
+                };
+                var table = KohaTable(id, dt_parameters, null, 'with_filters');
             }
+
             [% IF Koha.Preference('AcquisitionDetails') %]
                 $("#orders").dataTable($.extend(true, {}, dataTablesDefaults, {
                     'sDom': 't',
diff --git a/koha-tmpl/intranet-tmpl/prog/js/table_filters.js b/koha-tmpl/intranet-tmpl/prog/js/table_filters.js
new file mode 100644 (file)
index 0000000..a1a914a
--- /dev/null
@@ -0,0 +1,50 @@
+function activate_filters(id) {
+    var table = $("#" + id + " table");
+    if (table.length == 1) {
+        filters_row = table.find('thead tr.filters_row');
+
+        var aoColumns = [];
+        filters_row.find('th').each(function() {
+            if(this.className === "NoSort"){
+                aoColumns.push(null);
+            } else {
+                aoColumns.push('text');
+            }
+        });
+
+        if (table.find('thead tr.columnFilter').length == 0) {
+            table.dataTable().columnFilter({
+                'sPlaceHolder': 'head:after'
+                ,   'aoColumns': aoColumns
+            });
+            filters_row.addClass('columnFilter');
+        }
+        filters_row.show();
+    }
+
+    $('#' + id + '_activate_filters')
+        .html('<i class="fa fa-filter"></i> ' + _("Deactivate filters") )
+        .unbind('click')
+        .click(function() {
+            deactivate_filters(id);
+            return false;
+        });
+}
+
+function deactivate_filters(id) {
+    filters_row = $("#" + id + " table").find('thead tr.filters_row');
+
+    filters_row.find('input[type="text"]')
+        .val('')            // Empty filter text boxes
+        .trigger('keyup')   // Filter (display all rows)
+        .trigger('blur');   // Reset value to the column name
+    filters_row.hide();
+
+    $('#' + id + '_activate_filters')
+        .html('<i class="fa fa-filter"></i> ' + _("Activate filters") )
+        .unbind('click')
+        .click(function() {
+            activate_filters(id);
+            return false;
+        });
+}