/*jsl:import ipa.js */
/* Authors:
* Pavel Zuna
* Adam Young
* Endi S. Dewata
*
* Copyright (C) 2010 Red Hat
* see file 'COPYING' for use and warranty information
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/* REQUIRES: ipa.js */
/* For scrolling tables, we want to make sure we leave enough room for the
header and footer. */
IPA.reserved_screen_size = 400;
IPA.search_facet = function(spec) {
spec = spec || {};
spec.name = spec.name || 'search';
spec.display_class = 'search-facet';
spec.managed_entity_name = spec.managed_entity_name || spec.entity_name;
var that = IPA.table_facet(spec);
that.search_all = spec.search_all || false;
function get_values (){
return that.table.get_selected_values();
}
that.get_values = spec.get_values || get_values;
that.init = function() {
that.facet_init();
that.managed_entity = IPA.get_entity(that.managed_entity_name);
that.init_table(that.managed_entity);
};
that.init_table = function(entity){
function setup_column(column,entity) {
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() {
IPA.nav.show_page(entity.name, 'default', value);
return false;
};
}(value)
}).appendTo(container);
};
}
that.table = IPA.table_widget({
scrollable: true,
id: entity.name+'-search',
name: 'search',
label: IPA.metadata.objects[entity.name].label,
entity_name: entity.name,
search_all: that.search_all
});
var columns = that.columns.values;
for (var i=0; i', {
type: 'text',
name: 'filter'
}).appendTo(that.controls);
that.filter.keypress(function(e) {
/* if the key pressed is the enter key */
if (e.which == 13) {
that.find();
}
});
that.find_button = IPA.button({
label: IPA.messages.buttons.find,
icon: 'ui-icon-search',
click: function() {
that.find();
return false;
}
}).appendTo(that.controls);
that.controls.append(IPA.create_network_spinner());
that.remove_button = IPA.action_button({
label: IPA.messages.buttons.remove,
icon: 'ui-icon-trash',
click: function() {
if (that.remove_button.hasClass('input_link_disabled')) return false;
that.remove();
return false;
}
}).appendTo(that.controls);
that.add_button = IPA.action_button({
label: IPA.messages.buttons.add,
icon: 'ui-icon-plus',
click: function() {
that.add();
return false;
}
}).appendTo(that.controls);
};
that.create_content = function(container) {
var span = $('', { 'name': 'search' }).appendTo(container);
that.table.create(span);
that.table.setup(span);
};
that.setup = function(container) {
that.facet_setup(container);
};
that.show = function() {
that.facet_show();
that.entity.header.set_pkey(null);
that.entity.header.back_link.css('visibility', 'hidden');
that.entity.header.facet_tabs.css('visibility', 'hidden');
if (that.filter) {
var filter = $.bbq.getState(that.entity_name+'-filter');
that.filter.val(filter);
}
};
that.select_changed = function() {
var values = that.table.get_selected_values();
if (values.length === 0) {
that.remove_button.addClass('input_link_disabled');
} else {
that.remove_button.removeClass('input_link_disabled');
}
};
that.add = function() {
var dialog = that.managed_entity.get_dialog('add');
dialog.open(that.container);
};
that.remove = function() {
that.remove_instances(that.managed_entity);
};
that.remove_instances = function(entity) {
var values = that.get_values();
var title;
if (!values.length) {
title = IPA.messages.dialogs.remove_empty;
title = title.replace('${entity}', that.label);
alert(title);
return;
}
title = IPA.messages.dialogs.remove_title;
title = title.replace('${entity}', that.label);
var dialog = IPA.deleter_dialog({
'title': title,
'parent': that.container,
'values': values,
entity_name: entity.name
});
dialog.execute = function() {
var batch = IPA.batch_command({
'on_success': function() {
that.refresh();
dialog.close();
},
'on_error': function() {
that.refresh();
dialog.close();
}
});
var pkeys =
entity.get_primary_key_prefix();
for (var i=0; iError: '+error_thrown.name+'
');
summary.append(''+error_thrown.message+'
');
}
var filter = [];
var current_entity = entity;
filter.unshift($.bbq.getState(current_entity.name+'-filter'));
current_entity = current_entity.containing_entity;
while(current_entity !== null){
filter.unshift(
$.bbq.getState(current_entity.name+'-pkey'));
current_entity = current_entity.containing_entity;
}
var command = IPA.command({
entity: entity.name,
method: 'find',
args: filter,
options: {
all: that.search_all
},
on_success: on_success,
on_error: on_error
});
command.execute();
};
// methods that should be invoked by subclasses
that.search_facet_init = that.init;
that.search_facet_create_content = that.create_content;
that.search_facet_setup = that.setup;
return that;
};
/*TODO. this has much copied code from above. Refactor the search_facet
To either be nested or not nested. */
IPA.nested_search_facet = function(spec){
spec.managed_entity_name = spec.nested_entity;
var that = IPA.search_facet(spec);
that.show = function() {
that.facet_show();
//that.entity.header.set_pkey(null);
that.entity.header.back_link.css('visibility', 'visible');
that.entity.header.facet_tabs.css('visibility', 'visible');
that.entity.header.set_pkey(
$.bbq.getState(IPA.current_entity.name+'-pkey'));
if (that.filter) {
var filter = $.bbq.getState(that.managed_entity_name+'-filter');
that.filter.val(filter);
}
};
that.refresh = function(){
var pkey = $.bbq.getState(that.entity.name+'-pkey');
if ((!pkey) && (that.entity.redirect_facet)) {
var current_entity = that.entity;
while (current_entity.containing_entity){
current_entity = current_entity.containing_entity;
}
IPA.nav.show_page(
current_entity.name,
that.entity.redirect_facet);
return;
}
that.search_refresh(that.managed_entity);
};
return that;
};