Implementation of the UI for DNS records.

Search uses filters.

Much of the code has been cut and pasted from search.js and add.js, but then significantly modified. Moving forward, we'll have to determine if it is worth the effort to integrate.

From faace2d9147c428522f608629c4bf5d9b398bc58 Mon Sep 17 00:00:00 2001
From: Adam Young <ayo...@redhat.com>
Date: Fri, 22 Oct 2010 09:41:34 -0400
Subject: [PATCH] dns work
 without the details change

---
 install/static/entity.js |  209 ++++++++++++++++----------
 install/static/policy.js |  380 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 509 insertions(+), 80 deletions(-)

diff --git a/install/static/entity.js b/install/static/entity.js
index 9f3149b..89694bb 100644
--- a/install/static/entity.js
+++ b/install/static/entity.js
@@ -28,6 +28,15 @@ var ipa_entity_add_list = {};
 //var ipa_entity_details_list = {};
 var ipa_entity_association_list = {};
 
+var ipa_entity_facet_list = {};
+
+function ipa_facet(spec){
+    var that = {
+    };
+    return that;
+};
+
+
 /* use this to track individual changes between two hashchange events */
 var window_hash_cache = {};
 
@@ -59,49 +68,74 @@ function ipa_entity_set_association_definition(obj_name, data)
 }
 
 
+function ipa_entity_set_facet_definition(obj_name, data)
+{
+    function facet(spec){
+    }
+
+    ipa_entity_facet_list[obj_name] = data;
+}
+
+
 function ipa_details_only_setup(container){
     ipa_entity_setup(container, 'details');
 }
 
 function ipa_entity_setup(container, unspecified)
 {
-    var id = container.attr('id');
 
+    var id = container.attr('id');
     var state = id + '-facet';
     var facet = $.bbq.getState(state, true) || unspecified || 'search';
     var last_facet = window_hash_cache[state];
 
+    var facet_renders = {
+        search : function(){
+            state = id + '-filter';
+            var filter = $.bbq.getState(state, true);
+            var last_filter = window_hash_cache[state];
+            if (filter == last_filter) return;
+            _ipa_entity_setup(container);
+            window_hash_cache[state] = filter;
+
+        },
+        details : function (){
+            state = id + '-pkey';
+            var pkey = $.bbq.getState(state, true);
+            var last_pkey = window_hash_cache[state];
+            if (pkey == last_pkey) return;
+            _ipa_entity_setup(container);
+            window_hash_cache[state] = pkey;
+        },
+        associate : function () {
+            state = id + '-enroll';
+            var enroll = $.bbq.getState(state, true);
+            var last_enroll = window_hash_cache[state];
+            if (enroll == last_enroll) return;
+            _ipa_entity_setup(container);
+            window_hash_cache[state] = enroll;
+        },
+        records : function () {
+            state = id + '-record';
+            var records = $.bbq.getState(state, true);
+            var last_records = window_hash_cache[state];
+            if (records == last_records) return;
+            _ipa_entity_setup(container);
+            window_hash_cache[state] = record;
+        }
+    };
+
     if (facet != last_facet) {
         _ipa_entity_setup(container,unspecified);
         window_hash_cache[state] = facet;
-
-    } else if (facet == 'search') {
-        state = id + '-filter';
-        var filter = $.bbq.getState(state, true);
-        var last_filter = window_hash_cache[state];
-        if (filter == last_filter) return;
-
-        _ipa_entity_setup(container);
-        window_hash_cache[state] = filter;
-
-    } else if (facet == 'details') {
-        state = id + '-pkey';
-        var pkey = $.bbq.getState(state, true);
-        var last_pkey = window_hash_cache[state];
-        if (pkey == last_pkey) return;
-
-        _ipa_entity_setup(container);
-        window_hash_cache[state] = pkey;
-
-    } else if (facet == 'associate') {
-        state = id + '-enroll';
-        var enroll = $.bbq.getState(state, true);
-        var last_enroll = window_hash_cache[state];
-        if (enroll == last_enroll) return;
-
-        _ipa_entity_setup(container);
-        window_hash_cache[state] = enroll;
+    } else{
+        var render = facet_renders[facet];
+        if (render) {
+            render();
+        }
+        //TODO handle error.
     }
+
 }
 
 function _ipa_entity_setup(container, unspecified) {
@@ -127,7 +161,9 @@ function _ipa_entity_setup(container, unspecified) {
     function switch_view() {
         var enroll_obj_name = $(this).attr('title');
         var state = {};
-        if (enroll_obj_name != 'search' && enroll_obj_name != 'details') {
+        if (enroll_obj_name != 'search' &&
+            enroll_obj_name != 'details' &&
+            enroll_obj_name != 'records') {
             state[obj_name + '-facet'] = 'associate';
             state[obj_name + '-enroll'] = enroll_obj_name;
         } else {
@@ -137,65 +173,65 @@ function _ipa_entity_setup(container, unspecified) {
         $.bbq.pushState(state);
     }
 
-    function setup_search_facet() {
-        var filter = $.bbq.getState(obj_name + '-filter', true) || '';
-        search_create(obj_name, ipa_entity_search_list[obj_name], container);
-
-        ipa_make_button( 'ui-icon-plus',ipa_messages.button.add).
-            click(new_on_click).
-            appendTo($( "div#" + obj_name + " > div.search-controls"))
-
-        search_load(container, filter, null, null);
-    }
-
-    function setup_details_facet(unspecified) {
-        var pkey = $.bbq.getState(obj_name + '-pkey', true);
-        ipa_entity_generate_views(obj_name, container, switch_view);
-        var sections = ipa_entity_get_details_sections(obj_name);
-        ipa_details_create(container, sections);
-        container.find('.details-reset').click(reset_on_click);
-        container.find('.details-update').click(update_on_click);
-
-        if (pkey||unspecified){
-            ipa_details_load(container, pkey, null, null);
-        }
-    }
-
-    function setup_associate_facet() {
-        var pkey = $.bbq.getState(obj_name + '-pkey', true) || '';
-        var enroll_obj_name = $.bbq.getState(obj_name + '-enroll', true) || '';
-        var attr = ipa_get_member_attribute(obj_name, enroll_obj_name);
-        var columns  = [
-            {
-                title: ipa_objs[enroll_obj_name].label,
-                column: attr + '_' + enroll_obj_name
+    var facet_setups = {
+        search : function () {
+            var filter = $.bbq.getState(obj_name + '-filter', true) || '';
+            search_create(obj_name, ipa_entity_search_list[obj_name], container);
+            ipa_make_button( 'ui-icon-plus',ipa_messages.button.add).
+                click(new_on_click).
+                appendTo($( "div#" + obj_name + " > div.search-controls"))
+            search_load(container, filter);
+        },
+
+        details : function(unspecified) {
+            var pkey = $.bbq.getState(obj_name + '-pkey', true);
+            ipa_entity_generate_views(obj_name, container, switch_view);
+            var sections = ipa_entity_get_details_sections(obj_name);
+            ipa_details_create(container, sections);
+            container.find('.details-reset').click(reset_on_click);
+            container.find('.details-update').click(update_on_click);
+            if (pkey||unspecified){
+                ipa_details_load(container, pkey, null, null);
             }
-        ];
-
-        var association = ipa_entity_association_list[obj_name];
-        var association_config = association ? association[enroll_obj_name] : null;
-        var associator = association_config ? association_config.associator : null;
-        var method = association_config ? association_config.method : null;
-
-        var frm = new AssociationList(
+        },
+
+        associate : function facet() {
+            var pkey = $.bbq.getState(obj_name + '-pkey', true) || '';
+            var enroll_obj_name = $.bbq.getState(obj_name + '-enroll', true) || '';
+            var attr = ipa_get_member_attribute(obj_name, enroll_obj_name);
+            var columns  = [
+                {
+                    title: ipa_objs[enroll_obj_name].label,
+                    column: attr + '_' + enroll_obj_name
+                }
+            ];
+            var association = ipa_entity_association_list[obj_name];
+            var association_config = association ? association[enroll_obj_name] : null;
+            var associator = association_config ? association_config.associator : null;
+            var method = association_config ? association_config.method : null;
+            var frm = new AssociationList(
                 obj_name, pkey, enroll_obj_name, columns, container,
                 associator, method
-        );
+            );
+            ipa_entity_generate_views(obj_name, container, switch_view);
+            frm.setup();
+        },
+
+        records: function(unspecified) {
+            records_facet.setup(obj_name, container, switch_view );
+        }
 
-        ipa_entity_generate_views(obj_name, container, switch_view);
-        frm.setup();
     }
 
+
     container.empty();
 
-    var facet = $.bbq.getState(obj_name + '-facet', true) || 
+    var facet = $.bbq.getState(obj_name + '-facet', true) ||
         unspecified || 'search';
-    if (facet == 'search') {
-        setup_search_facet();
-    } else if (facet == 'details') {
-        setup_details_facet(unspecified);
-    } else if (facet == 'associate') {
-        setup_associate_facet();
+
+    var facet_setup_function = facet_setups[facet];
+    if (facet_setup_function){
+        facet_setup_function();
     }
 }
 
@@ -203,7 +239,7 @@ function ipa_entity_generate_views(obj_name, container, switch_view)
 {
     var ul = $('<ul></ul>', {'class': 'entity-views'});
 
-    //TODO replace the plus image with the correct image for each facet
+    //TODO for single instance entites, don't display search
     ul.append($('<li></li>', {
         title: 'search',
         text: 'Search',
@@ -231,6 +267,19 @@ function ipa_entity_generate_views(obj_name, container, switch_view)
         }
     }
 
+    //TODO Additional facets go here
+
+    var facets = ipa_entity_facet_list[obj_name];
+    if (facets){
+        for (var f = 0; f < facets.length; f += 1){
+            ul.append($('<li></li>', {
+                text: 'Records',
+                title: 'records',
+                click: switch_view
+            }).prepend('| '));
+        }
+    }
+
     container.append(ul);
 }
 
diff --git a/install/static/policy.js b/install/static/policy.js
index 9d55e1c..128b216 100644
--- a/install/static/policy.js
+++ b/install/static/policy.js
@@ -53,6 +53,7 @@ ipa_entity_set_search_definition('dns', [
     ['quick_links', 'Quick Links', ipa_entity_quick_links]
 ]);
 
+
 ipa_entity_set_add_definition('dns', [
     'dialog-add-dns', 'Add New Zone', [
         ['idnsname', 'Name', null],
@@ -82,6 +83,385 @@ ipa_entity_set_association_definition('dns', {
 });
 
 
+ipa_entity_set_facet_definition('dns', [
+    ipa_facet({name:'records'})]
+);
+
+function create_records_facet(){
+
+    that = {};
+
+    var record_types =[ 'a', 'aaaa', 'dname', 'cname', 'mx', 'ns', 'ptr',
+                        'srv', 'txt', 'a6', 'afsdb', 'cert', 'ds', 'hinfo',
+                        'key', 'kx', 'loc', 'md', 'minfo', 'naptr', 'nsec',
+                        'nxt', 'rrsig', 'sshfp'];
+
+    function create_type_select(id,add_none) {
+        var type_select = $('<select/>',{
+            id: id
+        });
+
+        if (add_none){
+            type_select.append($('<option/>',{
+                text: '(none)',
+                value: ''
+            }));
+        }
+        for (var t = 0 ; t < record_types.length ; t += 1){
+            var record_type = record_types[t].toUpperCase();
+
+            type_select.append($('<option/>',{
+                text: record_type,
+                value: record_type
+            }));
+        }
+        return type_select;
+    }
+
+
+    var  entry_attrs = {};
+
+
+    function add_click(){
+
+        var add_dialog = $('<div/>',{
+            id: 'add_dns_resource_record',
+            title: 'Add DNS Resource Record'
+        });
+        var dl = $('<dl></dl>').appendTo(add_dialog);
+        dl.append('<dt>Resource</dt>');
+        dl.append( $('<dd/>').
+                   append($('<input type="text" id="dns-record-resource" />')));
+        dl.append('<dt>Type</dt>');
+        dl.append(  $('<dd/>').append(create_type_select('dns-record-type')));
+        dl.append('<dt>Data</dt>');
+        dl.append($('<dd/>').append($('<textarea/>',{
+            id: 'dns-record-data',
+            rows:"8",
+            cols:"20"
+        })));
+
+
+        function add(evt, called_from_add_and_edit) {
+            var params = [];
+            var options = {};
+            function add_win(data, text_status, xhr) {
+                reload();
+                if (called_from_add_and_edit) {
+                }
+            };
+
+            function add_fail(data, text_status, xhr) {
+            };
+
+            params.push(  $.bbq.getState('dns-pkey', true));
+            params.push(add_dialog.find('#dns-record-resource').val());
+            params.push(add_dialog.find('#dns-record-type').val());
+            params.push(add_dialog.find('#dns-record-data').val());
+
+            ipa_cmd('dns_add_rr', params, options, add_win, add_fail);
+            add_dialog.dialog('close');
+        };
+
+        function add_and_close(evt) {
+            add(evt, true);
+            add_dialog.dialog('close');
+        };
+
+        function cancel() {
+            add_dialog.dialog('close');
+        };
+
+
+        add_dialog.dialog({
+            modal: true,
+            buttons: {
+                'Add many': add,
+                'Add and Close': add,
+                'Cancel': cancel
+            }
+        });
+    }
+
+
+
+    function delete_records(records_table){
+
+        var zone = $.bbq.getState('dns-pkey', true);
+
+        var thead = records_table.find('thead');
+        thead.find("INPUT[type='checkbox']").
+            attr('checked', false);
+
+        var i = 0;
+
+        var tbody = records_table.find('tbody');
+
+
+        var delete_dialog = $('<div/>', {
+            title: ipa_messages.button.delete,
+            'class': 'search-dialog-delete',
+        });
+        var to_delete_table = 
+            $('<table class="search-table" >'+
+              '<thead><tr><th>Resource</th><th>Type</th></tr></thead>'+
+              '<tbody></tbody></table>').appendTo(delete_dialog);
+
+        var to_delete_body =  to_delete_table.find('tbody');
+        var delete_list = [];
+        tbody.find("INPUT[type='checkbox']").each(
+            function(index, box){
+                if (box.checked){
+                    var tr = $(box).parents('tr');
+                    var resource = $(tr).find('[title="idnsname"]').text();
+                    var type = $(tr).find('[title="type"]').
+                        text().toUpperCase();
+                    var data = $(tr).find('[title="data"]').text();
+                    var params = [zone, resource, type, data];
+                    delete_list.push(params);
+                    to_delete_body.append(
+                        $('<tr></tr>').
+                            append($('<td></td>',{html:resource}).
+                                   after($('<td></td>',{html:type}))));
+                }
+            }
+        );
+
+        function delete_on_click() {
+            var delete_count = delete_list.length;
+            function delete_complete(){
+                delete_count -= 1;
+                if (delete_count === 0 ){
+                    reload();
+                    delete_dialog.dialog('close');
+                }
+            }
+            for (var i = 0; i < delete_list.length; i += 1){
+                ipa_cmd('dns_del_rr',delete_list[i],{},
+                        delete_complete,delete_complete);
+            }
+        };
+
+        function cancel_on_click() {
+            delete_dialog.dialog('close');
+        };
+
+
+        if (delete_list.length == 0)
+            return;
+
+        delete_dialog.append($('<P/>',
+                               {text:ipa_messages.search.delete_confirm}));
+
+        delete_dialog.dialog({
+            modal: true,
+            buttons: {
+                'Delete': delete_on_click,
+                'Cancel': cancel_on_click,
+            },
+        });
+
+
+    }
+
+    function setup(obj_name, container,switch_view){
+        that.container = container;
+        var pkey = $.bbq.getState('dns' + '-pkey', true);
+        ipa_entity_generate_views(obj_name, container, switch_view);
+
+        container.attr('title', obj_name);
+        container.addClass('search-container');
+
+        var h2 = $('<h2></h2>',{
+            text: "Records for DNS Zone:" + pkey
+        }).appendTo(container);
+
+
+        var div = $('<div class="search-controls"></div>')
+            .appendTo(container);
+
+        var control_span =$('<span class="record-filter"></span>').appendTo(div);
+
+        control_span.append('Resource');
+        control_span.append($('<input />',{
+            type: "text",
+            id: 'dns-record-resource-filter',
+            name: 'search-' + obj_name + '-filter'
+        }));
+
+        control_span.append('Type');
+
+        create_type_select('dns-record-type-filter',true).
+            appendTo(control_span);
+
+        control_span.append('Data');
+        control_span.append($('<input />',{
+            type: "text",
+            id: 'dns-record-data-filter',
+            name: 'search-' + obj_name + '-filter'
+        }));
+
+
+        ipa_make_button('ui-icon-search',ipa_messages.button.find).
+            click(function(){load(container)}).appendTo(control_span);
+
+        ipa_make_button('ui-icon-plus',ipa_messages.button.add).
+            click(add_click).appendTo(control_span);
+
+        ipa_make_button('ui-icon-trash',ipa_messages.button.delete).
+            click(function(){delete_records(records_table);}).
+            appendTo(control_span);
+
+
+        div.append('<span class="records-buttons"></span>');
+
+        var records_results = $('<div/>', {
+            class: 'records-results'
+        }).appendTo(container);
+
+        var records_table = $('<table/>', {
+            class: 'search-table',
+        }).appendTo(records_results);
+
+        var thead = $('<thead><tr></tr></thead>').appendTo(records_table);
+        var tbody = $('<tbody></tbody>').appendTo(records_table);;
+        var tfoot = $('<tfoot></tfoot>').appendTo(records_table);;
+
+        var tr = thead.find('tr');
+        tr.append($('<th style="width: 15px" />').append(
+            $('<input />',{
+                type: 'checkbox',
+                click : function (evt){
+                    tbody.find("INPUT[type='checkbox']").
+                        attr('checked', this.checked);
+                }
+            })
+        ));
+        tr.append($('<th>Resource</th>'));
+        tr.append($('<th>Record Type</th>'));
+        tr.append($('<th>Data</th>'));
+
+        load(container);
+    }
+
+
+    function load_on_win(data){
+        display('dns',data);
+    }
+
+    function load_on_fail(data){
+        display('dns',data);
+    }
+
+    function  reload(){
+        load(that.container);
+    }
+
+
+    function  load(container){
+
+        var options = {};
+
+        var resource_filter = container.find("#dns-record-resource-filter")
+            .val()
+        if (resource_filter){
+            options.idnsname = resource_filter;
+        }
+
+        var type_filter = container.find("#dns-record-type-filter").val()
+        if (type_filter){
+            options.type = type_filter;
+        }
+
+        var data_filter = container.find("#dns-record-data-filter").val()
+        if (data_filter){
+            options.data = data_filter;
+        }
+
+
+        var pkey = $.bbq.getState('dns' + '-pkey', true);
+        ipa_cmd('dns_find_rr',[pkey],options,load_on_win, load_on_fail);
+
+    }
+
+
+    function generate_tr(thead, tbody, result){
+        var tr = $('<tr></tr>').appendTo(tbody);
+
+        search_generate_checkbox_td(tr, /*pkey_value*/ '');
+
+        //TODO get this fixed on the back end.  For now, workaround
+
+        if (result.idnsname){
+        tr.append($('<td/>',{
+            title:'idnsname',
+            text: result.idnsname[0]
+        }));
+        }else{
+            tr.append($('<td/>',{
+                title:'idnsname',
+                text: result.dn.split(',')[0].split('=')[1]
+            }));
+
+        }
+
+        for (var i = 0; i < record_types.length; i += 1){
+            var field_name =  record_types[i];
+            var field = result[field_name+'record'];
+            if ( field ){
+                var record_type = field_name;
+                var record_data = field[0];
+                break;
+            }
+        }
+
+        tr.append($('<td/>',{
+            title:'type',
+            text: record_type
+        }));
+        tr.append($('<td/>',{
+            title:'data',
+            text: record_data
+        }));
+    }
+
+    //TODO this is cut and pasted from search, we need to unify
+    function display(obj_name, data){
+        var selector = '.search-container[title=' + obj_name + ']';
+        var thead = $(selector + ' thead');
+        var tbody = $(selector + ' tbody');
+        var tfoot = $(selector + ' tfoot');
+
+        tbody.find('tr').remove();
+
+        var result = data.result.result;
+        for (var i = 0; i < result.length; ++i){
+            generate_tr(thead, tbody, result[i]);
+        }
+
+        if (data.result.truncated) {
+            tfoot.text(
+                'Query returned results than configured size limit will show.' +
+                    'First ' + data.result.count + ' results shown.'
+            );
+        } else {
+            tfoot.text(data.result.summary);
+        }
+
+    }
+
+    that.setup = setup;
+    that.load = load;
+
+    return that;
+};
+
+
+var records_facet = create_records_facet();
+
+
+
+
 /**Automount*/
 
 ipa_entity_set_search_definition('automountlocation', [
-- 
1.7.1

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

Reply via email to