The automount keys search facet has been modified to support paging.
Since the automountkey-find command doesn't support --pkey-only
option, the facet is configured such that during a refresh operation
it will retrieve all entries (including the key and info attributes)
and then display only the ones that are supposed to be visible in
the current page.

Ticket #2093

--
Endi S. Dewata
From 033d45585c3dce49c25cf006db22e109660d629d Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edew...@redhat.com>
Date: Tue, 17 Jan 2012 15:50:49 -0600
Subject: [PATCH] Enabled paging on automount keys.

The automount keys search facet has been modified to support paging.
Since the automountkey-find command doesn't support --pkey-only
option, the facet is configured such that during a refresh operation
it will retrieve all entries (including the key and info attributes)
and then display only the ones that are supposed to be visible in
the current page.

Ticket #2093
---
 install/ui/association.js        |   17 ++++++-
 install/ui/automount.js          |    6 ++-
 install/ui/facet.js              |   90 +++++++++++++++++++++----------------
 install/ui/hbac.js               |    2 +-
 install/ui/hbactest.js           |   38 +++-------------
 install/ui/ipa.js                |    4 ++
 install/ui/jquery.ordered-map.js |   23 ++++++++++
 install/ui/search.js             |   35 +++++++--------
 8 files changed, 121 insertions(+), 94 deletions(-)

diff --git a/install/ui/association.js b/install/ui/association.js
index ed274eb44dbea5cb6c0418048b44ed21f0f8aa94..2fbdb7ddec6baa5169732004ef7943f2eaf08930 100644
--- a/install/ui/association.js
+++ b/install/ui/association.js
@@ -998,8 +998,21 @@ IPA.association_facet = function (spec) {
         dialog.open(that.container);
     };
 
-    that.get_pkeys = function(data) {
-        return data.result.result[that.get_attribute_name()] || [];
+    that.get_records_map = function(data) {
+
+        var records_map = $.ordered_map();
+        var association_name = that.get_attribute_name();
+        var pkey_name = that.managed_entity.metadata.primary_key;
+
+        var pkeys = data.result.result[association_name];
+        for (var i=0; pkeys && i<pkeys.length; i++) {
+            var pkey = pkeys[i];
+            var record = {};
+            record[pkey_name] = pkey;
+            records_map.put(pkey, record);
+        }
+
+        return records_map;
     };
 
     that.refresh = function() {
diff --git a/install/ui/automount.js b/install/ui/automount.js
index 13d7bfa2aa141a3fbbc7fff2ae4c17c8eba27d43..80f5e14383e74b172e36d63dbb1e80769c5db736 100644
--- a/install/ui/automount.js
+++ b/install/ui/automount.js
@@ -74,7 +74,7 @@ IPA.automount.map_entity = function(spec) {
             factory: IPA.automount.key_search_facet,
             facet_group: 'automountkey',
             nested_entity: 'automountkey',
-            pagination: false,
+            search_all_entries: true,
             label: IPA.metadata.objects.automountkey.label,
             name: 'keys',
             columns: [
@@ -237,9 +237,13 @@ IPA.automount_key_column = function(spec) {
     var that = IPA.column(spec);
 
     that.setup = function(container, record) {
+
         container.empty();
+
         var key = record.automountkey;
+        if (key instanceof Array) key = key[0];
         var info = record.automountinformation;
+        if (info instanceof Array) info = info[0];
 
         $('<a/>', {
             href: '#'+key,
diff --git a/install/ui/facet.js b/install/ui/facet.js
index f0dbc1d4283a4a5e10b2f5ce936058e3d4e2e791..2028de8b6538ac5aaf8e07aaf1da392c8d65b3a2 100644
--- a/install/ui/facet.js
+++ b/install/ui/facet.js
@@ -409,7 +409,8 @@ IPA.table_facet = function(spec) {
     that.managed_entity = spec.managed_entity ? IPA.get_entity(spec.managed_entity) : that.entity;
 
     that.pagination = spec.pagination === undefined ? true : spec.pagination;
-    that.search_all = spec.search_all;
+    that.search_all_entries = spec.search_all_entries;
+    that.search_all_attributes = spec.search_all_attributes;
     that.selectable = spec.selectable === undefined ? true : spec.selectable;
 
     that.columns = $.ordered_map();
@@ -504,20 +505,30 @@ IPA.table_facet = function(spec) {
         }
     };
 
-    that.get_pkeys = function(data) {
-        return [];
+    that.get_records_map = function(data) {
+
+        var records_map = $.ordered_map();
+
+        var result = data.result.result;
+        var pkey_name = that.managed_entity.metadata.primary_key;
+
+        for (var i=0; i<result.length; i++) {
+            var record = result[i];
+            var pkey = record[pkey_name];
+            if (pkey instanceof Array) pkey = pkey[0];
+            records_map.put(pkey, record);
+        }
+
+        return records_map;
     };
 
     that.load_page = function(data) {
 
-        that.pkeys = that.get_pkeys(data);
+        // get primary keys (and the complete records if search_all_entries is true)
+        var records_map = that.get_records_map(data);
 
-        if (that.pkeys.length) {
-            that.table.total_pages =
-                Math.ceil(that.pkeys.length / that.table.page_length);
-        } else {
-            that.table.total_pages = 1;
-        }
+        var total = records_map.length;
+        that.table.total_pages = total ? Math.ceil(total / that.table.page_length) : 1;
 
         delete that.table.current_page;
 
@@ -534,15 +545,13 @@ IPA.table_facet = function(spec) {
         }
         that.table.current_page = page;
 
-        if (!that.pkeys || !that.pkeys.length) {
-            that.load_records([]);
+        if (!total) {
             that.table.summary.text(IPA.messages.association.no_entries);
+            that.load_records([]);
             return;
         }
 
-        that.pkeys.sort();
-        var total = that.pkeys.length;
-
+        // calculate the start and end of the current page
         var start = (that.table.current_page - 1) * that.table.page_length + 1;
         var end = that.table.current_page * that.table.page_length;
         end = end > total ? total : end;
@@ -553,31 +562,37 @@ IPA.table_facet = function(spec) {
         summary = summary.replace('${total}', total);
         that.table.summary.text(summary);
 
-        that.values = that.pkeys.slice(start-1, end);
+        // sort map based on primary keys
+        records_map = records_map.sort();
+
+        // trim map leaving the entries visible in the current page only
+        records_map = records_map.slice(start-1, end);
 
         var columns = that.table.columns.values;
-        if (columns.length == 1) { // show pkey only
-            var name = columns[0].name;
-            var records = [];
-            for (var i=0; i<that.values.length; i++) {
-                var record = {};
-                record[name] = that.values[i];
-                records.push(record);
-            }
-            that.load_records(records);
+        if (columns.length == 1) { // show primary keys only
+            that.load_records(records_map.values);
             return;
         }
 
-        // get and show additional fields
+        if (that.search_all_entries) {
+            // map contains the primary keys and the complete records
+            that.load_records(records_map.values);
+            return;
+        }
+
+        // get the complete records
         that.get_records(
+            records_map.keys,
             function(data, text_status, xhr) {
                 var results = data.result.results;
-                var records = [];
-                for (var i=0; i<results.length; i++) {
-                    var record = results[i].result;
-                    records.push(record);
+                for (var i=0; i<records_map.length; i++) {
+                    var pkey = records_map.keys[i];
+                    var record = records_map.get(pkey);
+                    // merge the record obtained from the refresh()
+                    // with the record obtained from get_records()
+                    $.extend(record, results[i].result);
                 }
-                that.load_records(records);
+                that.load_records(records_map.values);
             },
             function(xhr, text_status, error_thrown) {
                 that.load_records([]);
@@ -599,10 +614,7 @@ IPA.table_facet = function(spec) {
         return that.managed_entity.name+'_get_records';
     };
 
-    that.get_records = function(on_success, on_error) {
-
-        var length = that.values.length;
-        if (!length) return;
+    that.get_records = function(pkeys, on_success, on_error) {
 
         var batch = IPA.batch_command({
             name: that.get_records_command_name(),
@@ -610,13 +622,13 @@ IPA.table_facet = function(spec) {
             on_error: on_error
         });
 
-        for (var i=0; i<length; i++) {
-            var pkey = that.values[i];
+        for (var i=0; i<pkeys.length; i++) {
+            var pkey = pkeys[i];
 
             var command = IPA.command({
                 entity: that.table.entity.name,
                 method: 'show',
-                args: [pkey],
+                args: [ pkey ],
                 options: { all: true }
             });
 
@@ -651,7 +663,7 @@ IPA.table_facet = function(spec) {
             label: entity.metadata.label,
             entity: entity,
             pagination: true,
-            search_all: that.search_all,
+            search_all: that.search_all_attributes,
             scrollable: true,
             selectable: that.selectable && !that.read_only
         });
diff --git a/install/ui/hbac.js b/install/ui/hbac.js
index d26b894b48211556df793ecf3a2dd8756dc027b4..5fd5f4e8d9cdcd589df25c2b88272ad11e40ccd1 100644
--- a/install/ui/hbac.js
+++ b/install/ui/hbac.js
@@ -37,7 +37,7 @@ IPA.hbac.rule_entity = function(spec) {
         that.entity_init();
 
         that.builder.search_facet({
-            search_all: true,
+            search_all_attributes: true,
             columns: [
                 'cn',
                 {
diff --git a/install/ui/hbactest.js b/install/ui/hbactest.js
index 26e8085d41f36ac00730095fbe78497143a2d699..f054c913a7b5027106b25b90b4eccdcdabdba68a 100644
--- a/install/ui/hbactest.js
+++ b/install/ui/hbactest.js
@@ -230,18 +230,6 @@ IPA.hbac.test_facet = function(spec) {
         IPA.nav.push_state(state);
     };
 
-    that.get_pkeys = function(data) {
-        var result = data.result.result;
-        var pkey_name = that.managed_entity.metadata.primary_key;
-        var pkeys = [];
-        for (var i=0; i<result.length; i++) {
-            var record = result[i];
-            var values = record[pkey_name];
-            pkeys.push(values[0]);
-        }
-        return pkeys;
-    };
-
     that.get_search_command_name = function() {
         return that.managed_entity.name + '_find' + (that.pagination ? '_pkeys' : '');
     };
@@ -730,16 +718,15 @@ IPA.hbac.test_run_facet = function(spec) {
         command.execute();
     };
 
-    that.get_pkeys = function(data) {
-        var pkeys = [];
-        that.matched = {};
+    that.get_records_map = function(data) {
+
+        var records_map = $.ordered_map();
 
         var matched = data.result.matched;
         if (that.show_matched && matched) {
             for (var i=0; i<matched.length; i++) {
                 var pkey = matched[i];
-                pkeys.push(pkey);
-                that.matched[pkey] = 'TRUE';
+                records_map.put(pkey, { matched: true });
             }
         }
 
@@ -747,12 +734,11 @@ IPA.hbac.test_run_facet = function(spec) {
         if (that.show_unmatched && notmatched) {
             for (i=0; i<notmatched.length; i++) {
                 pkey = notmatched[i];
-                pkeys.push(pkey);
-                that.matched[pkey] = 'FALSE';
+                records_map.put(pkey, { matched: false });
             }
         }
 
-        return pkeys;
+        return records_map;
     };
 
     that.get_records_command_name = function() {
@@ -765,18 +751,6 @@ IPA.hbac.test_run_facet = function(spec) {
         return that.managed_entity.name+'_get_records';
     };
 
-    that.load_records = function(records) {
-        var pkey_name = that.table.entity.metadata.primary_key;
-        that.table.empty();
-        for (var i=0; i<records.length; i++) {
-            var record = records[i];
-            var pkey = record[pkey_name][0];
-            record.matched = that.matched[pkey];
-            that.table.add_record(record);
-        }
-        that.table.set_values(that.selected_values);
-    };
-
     init();
 
     return that;
diff --git a/install/ui/ipa.js b/install/ui/ipa.js
index 90d10291a4a195d328ef8b7d2c6372313e9b2b43..04366a8c22723c8fa02557d120fc7313f7bbb1f5 100644
--- a/install/ui/ipa.js
+++ b/install/ui/ipa.js
@@ -351,6 +351,10 @@ IPA.command = function(spec) {
         return that.options[name];
     };
 
+    that.remove_option = function(name) {
+        delete that.options[name];
+    };
+
     that.execute = function() {
 
         function dialog_open(xhr, text_status, error_thrown) {
diff --git a/install/ui/jquery.ordered-map.js b/install/ui/jquery.ordered-map.js
index f30f8d1376568ebd61401283f323951333963243..aa0d281435181b00ebdcfddf05007c4fa47a2977 100755
--- a/install/ui/jquery.ordered-map.js
+++ b/install/ui/jquery.ordered-map.js
@@ -75,5 +75,28 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function() {
         return that.values[index];
     };
 
+    that.sort = function() {
+        var keys = that.keys.slice(0);
+        keys.sort();
+        return that.trim(keys);
+    };
+
+    that.slice = function(start, end) {
+        var keys = that.keys.slice(start, end);
+        return that.trim(keys);
+    };
+
+    that.trim = function(keys) {
+        var new_map = $.ordered_map();
+
+        for (var i=0; i<keys.length; i++) {
+            var key = keys[i];
+            var value = that.get(key);
+            new_map.put(key, value);
+        }
+
+        return new_map;
+    };
+
     return that;
 };
diff --git a/install/ui/search.js b/install/ui/search.js
index f82bf0d1edeba7e4bfc27a00968b9eaa1680808d..a7074e2200e8a5adedf659cb47eddca6c495227e 100644
--- a/install/ui/search.js
+++ b/install/ui/search.js
@@ -156,26 +156,15 @@ IPA.search_facet = function(spec) {
         IPA.nav.push_state(state);
     };
 
-    that.get_pkeys = function(data) {
-        var result = data.result.result;
-        var pkey_name = that.managed_entity.metadata.primary_key;
-        var pkeys = [];
-        for (var i=0; i<result.length; i++) {
-            var record = result[i];
-            var value = record[pkey_name];
-            if (value instanceof Array) {
-                value = value[0];
-            }
-            pkeys.push(value);
-        }
-        return pkeys;
-    };
-
     that.get_search_command_name = function() {
-        return that.managed_entity.name + '_find' + (that.pagination ? '_pkeys' : '');
+        var name = that.managed_entity.name + '_find';
+        if (that.pagination && !that.search_all_entries) {
+            name += '_pkeys';
+        }
+        return name;
     };
 
-    that.refresh = function() {
+    that.create_refresh_command = function() {
 
         var filter = [];
         var entity = that.managed_entity;
@@ -194,15 +183,22 @@ IPA.search_facet = function(spec) {
             method: 'find',
             args: filter,
             options: {
-                all: that.search_all
+                all: that.search_all_attributes
             }
         });
 
         if (that.pagination) {
-            command.set_option('pkey_only', true);
+            if (!that.search_all_entries) command.set_option('pkey_only', true);
             command.set_option('sizelimit', 0);
         }
 
+        return command;
+    };
+
+    that.refresh = function() {
+
+        var command = that.create_refresh_command();
+
         command.on_success = function(data, text_status, xhr) {
             that.filter.focus();
             that.load(data);
@@ -230,6 +226,7 @@ IPA.search_facet = function(spec) {
 
     // methods that should be invoked by subclasses
     that.search_facet_refresh = that.refresh;
+    that.search_facet_create_refresh_command = that.create_refresh_command;
 
     return that;
 };
-- 
1.7.5.1

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to