From 69e77212ea428f79742b9ff0452ef19d74cc76d4 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata"
'+error_thrown.title+'
'); - summary.append(''+error_thrown.message+'
'); + dialog.add = function() { + that.add( + dialog.get_selected_values(), + function() { + that.refresh(); + dialog.close(); + }, + function() { + that.refresh(); + dialog.close(); + } + ); + }; + + dialog.init(); + + dialog.open(that.container); + }; + + that.add = function(values, on_success, on_error) { + + var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; + + var command = ipa_command({ + 'method': that.entity_name+'_'+that.add_method, + 'args': [pkey], + 'on_success': on_success, + 'on_error': on_error + }); + command.set_option(that.other_entity, values.join(',')); + + command.execute(); + }; + + that.show_remove_dialog = function() { + + var selected_values = that.get_selected_values(); + + if (!selected_values.length) { + alert('Select '+that.label+' to be removed.'); + return; } var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; - ipa_cmd('show', [pkey], {'rights': true}, on_success, on_error, that.entity_name); + var label = IPA.metadata[that.other_entity].label; + var title = 'Remove '+label+' from '+that.entity_name+' '+pkey; + + var dialog = ipa_association_deleter_dialog({ + 'title': title, + 'entity_name': that.entity_name, + 'pkey': pkey, + 'other_entity': that.other_entity, + 'values': selected_values + }); + + dialog.remove = function() { + that.remove( + selected_values, + function() { + that.refresh(); + dialog.close(); + }, + function() { + that.refresh(); + dialog.close(); + } + ); + }; + + dialog.init(); + + dialog.open(that.container); }; + that.remove = function(values, on_success, on_error) { + + var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; + + var command = ipa_command({ + 'method': that.entity_name+'_'+that.remove_method, + 'args': [pkey], + 'on_success': on_success, + 'on_error': on_error + }); + + command.set_option(that.other_entity, values.join(',')); + + command.execute(); + }; + + that.save = function() { + return null; + }; + + // methods that should be invoked by subclasses + that.association_table_widget_init = that.init; + return that; } @@ -418,7 +510,7 @@ function ipa_association_facet(spec) { that.associator = spec.associator || bulk_associator; that.add_method = spec.add_method || 'add_member'; - that.delete_method = spec.delete_method || 'remove_member'; + that.remove_method = spec.remove_method || 'remove_member'; that.columns = []; that.columns_by_name = {}; @@ -458,11 +550,6 @@ function ipa_association_facet(spec) { return column; }; - that.is_dirty = function() { - var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; - return pkey != that.pkey || other_entity != that.other_entity; - }; - that.init = function() { var entity = IPA.get_entity(that.entity_name); @@ -474,30 +561,61 @@ function ipa_association_facet(spec) { } if (association.add_method) that.add_method = association.add_method; - if (association.delete_method) that.delete_method = association.delete_method; + if (association.remove_method) that.remove_method = association.remove_method; } var label = IPA.metadata[that.other_entity] ? IPA.metadata[that.other_entity].label : that.other_entity; + var pkey_name = IPA.metadata[that.other_entity].primary_key; - that.table = ipa_association_table_widget({ + that.table = ipa_table_widget({ 'id': that.entity_name+'-'+that.other_entity, - 'name': that.name, + 'name': pkey_name, 'label': label, 'entity_name': that.entity_name, - 'other_entity': that.other_entity, - 'facet': that + 'other_entity': that.other_entity }); if (that.columns.length) { that.table.set_columns(that.columns); - } - that.table.add = function() { that.add(); }; - that.table.remove = function() { that.remove() }; + } else { + + var column = that.table.create_column({ + name: that.table.name, + label: IPA.metadata[that.other_entity].label, + primary_key: true + }); + + column.setup = function(container, record) { + container.empty(); + + var value = record[column.name]; + value = value ? value.toString() : ''; + + $('', { + 'href': '#'+value, + 'html': value, + 'click': function (value) { + return function() { + var state = IPA.tab_state(that.other_entity); + state[that.other_entity + '-facet'] = 'details'; + state[that.other_entity + '-pkey'] = value; + $.bbq.pushState(state); + return false; + } + }(value) + }).appendTo(container); + }; + } that.facet_init(); }; + that.is_dirty = function() { + var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; + return pkey != that.pkey; + }; + that.create = function(container) { that.pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; @@ -514,6 +632,23 @@ function ipa_association_facet(spec) { var span = $('', { 'name': 'association' }).appendTo(container); that.table.create(span); + + var action_panel = that.get_action_panel(); + var li = $('.action-controls', action_panel); + + // creating generic buttons for layout + $('', { + 'type': 'button', + 'name': 'remove', + 'value': IPA.messages.button.remove + }).appendTo(li); + + $('', { + 'type': 'button', + 'name': 'add', + 'value': IPA.messages.button.enroll + }).appendTo(li); + }; that.setup = function(container) { @@ -523,9 +658,26 @@ function ipa_association_facet(spec) { var span = $('span[name=association]', that.container); that.table.setup(span); + + // replacing generic buttons with ipa_button and setting click handler + var action_panel = that.get_action_panel(); + + var button = $('input[name=remove]', action_panel); + button.replaceWith(ipa_button({ + 'label': button.val(), + 'icon': 'ui-icon-trash', + 'click': function() { that.show_remove_dialog(); } + })); + + button = $('input[name=add]', action_panel); + button.replaceWith(ipa_button({ + 'label': button.val(), + 'icon': 'ui-icon-plus', + 'click': function() { that.show_add_dialog() } + })); }; - that.add = function() { + that.show_add_dialog = function() { var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; var label = IPA.metadata[that.other_entity] ? IPA.metadata[that.other_entity].label : that.other_entity; @@ -535,29 +687,42 @@ function ipa_association_facet(spec) { 'title': title, 'entity_name': that.entity_name, 'pkey': pkey, - 'other_entity': that.other_entity, - 'associator': that.associator, - 'method': that.add_method, - 'on_success': function() { - that.refresh(); - dialog.close(); - }, - 'on_error': function() { - that.refresh(); - dialog.close(); - } + 'other_entity': that.other_entity }); if (that.adder_columns.length) { dialog.set_columns(that.adder_columns); } + dialog.add = function() { + + var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; + + var associator = that.associator({ + 'entity_name': that.entity_name, + 'pkey': pkey, + 'other_entity': that.other_entity, + 'values': dialog.get_selected_values(), + 'method': that.add_method, + 'on_success': function() { + that.refresh(); + dialog.close(); + }, + 'on_error': function() { + that.refresh(); + dialog.close(); + } + }); + + associator.execute(); + }; + dialog.init(); dialog.open(that.container); }; - that.remove = function() { + that.show_remove_dialog = function() { var label = IPA.metadata[that.other_entity] ? IPA.metadata[that.other_entity].label : that.other_entity; var values = that.table.get_selected_values(); @@ -577,7 +742,7 @@ function ipa_association_facet(spec) { 'other_entity': that.other_entity, 'values': values, 'associator': that.associator, - 'method': that.delete_method, + 'method': that.remove_method, 'on_success': function() { that.refresh(); dialog.close(); @@ -593,8 +758,74 @@ function ipa_association_facet(spec) { dialog.open(that.container); }; - that.refresh = function(){ - that.table.refresh(); + that.get_records = function(pkeys, on_success, on_error) { + + if (!pkeys.length) return; + + var batch = ipa_batch_command({ + 'name': that.entity_name+'_'+that.name, + 'on_success': on_success, + 'on_error': on_error + }); + + for (var i=0; i'+error_thrown.title+'
'); + summary.append(''+error_thrown.message+'
'); + } + + var pkey = $.bbq.getState(that.entity_name + '-pkey', true) || ''; + ipa_cmd('show', [pkey], {'rights': true}, on_success, on_error, that.entity_name); }; that.association_facet_init = that.init; -- cgit