/* Authors: * Pavel Zuna * Adam Young * Endi S. Dewata * Petr Vobornik * * 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 . */ define(['./ipa', './jquery', './text', './facet'], function(IPA, $, text) { IPA.search_facet = function(spec, no_init) { spec = spec || {}; spec.name = spec.name || 'search'; spec.managed_entity = spec.managed_entity ? IPA.get_entity(spec.managed_entity) : spec.entity; spec.disable_breadcrumb = spec.disable_breadcrumb === undefined ? true : spec.disable_breadcrumb; spec.disable_facet_tabs = spec.disable_facet_tabs === undefined ? true : spec.disable_facet_tabs; spec.actions = spec.actions || []; spec.actions.unshift( IPA.refresh_action, IPA.batch_remove_action, IPA.add_action); spec.control_buttons = spec.control_buttons || []; if (!spec.no_update) { spec.control_buttons.unshift( { name: 'remove', label: '@i18n:buttons.remove', icon: 'remove-icon' }, { name: 'add', label: '@i18n:buttons.add', icon: 'add-icon' }); } spec.control_buttons.unshift( { name: 'refresh', label: '@i18n:buttons.refresh', icon: 'reset-icon' }); spec.state = spec.state || {}; spec.state.evaluators = spec.state.evaluators || []; spec.state.evaluators.push( IPA.selected_state_evaluator, IPA.self_service_state_evaluator); var that = IPA.table_facet(spec, true); that.deleter_dialog = spec.deleter_dialog || IPA.search_deleter_dialog; that.create_header = function(container) { that.facet_create_header(container); var div = $('
', { 'class': 'right-aligned-facet-controls' }).appendTo(that.controls); div.append(IPA.create_network_spinner()); that.filter_container = $('
', { 'class': 'search-filter' }).appendTo(div); that.filter = $('', { type: 'text', name: 'filter' }).appendTo(that.filter_container); that.filter.keypress(function(e) { /* if the key pressed is the enter key */ if (e.which == 13) { that.find(); } }); that.find_button = IPA.action_button({ name: 'find', icon: 'search-icon', click: function() { that.find(); return false; } }).appendTo(that.filter_container); that.create_control_buttons(that.controls); }; that.managed_entity_pkey_prefix = function() { if (that.entity !== that.managed_entity) { return that.get_pkeys(); } return that.get_pkey_prefix(); }; that.show = function() { that.facet_show(); var filter = that.state.filter || ''; if (that.filter) { that.filter.val(filter); } }; that.show_add_dialog = function() { var dialog = that.managed_entity.get_dialog('add'); if (!that.adder_dialog) { that.adder_dialog = dialog; dialog.added.attach(function() { that.refresh(); }); } dialog.pkey_prefix = that.managed_entity_pkey_prefix(); dialog.open(that.container); }; that.create_remove_dialog = function() { var values = that.get_selected_values(); var title; if (!values.length) { title = text.get('@i18n:dialogs.remove_empty'); alert(title); return null; } var dialog = that.managed_entity.get_dialog('remove'); if (!dialog) { dialog = that.deleter_dialog(); } dialog.entity_name = that.managed_entity.name; dialog.entity = that.managed_entity; dialog.facet = that; dialog.pkey_prefix = that.managed_entity_pkey_prefix(); title = text.get('@i18n:dialogs.remove_title'); var label = that.managed_entity.metadata.label; dialog.title = title.replace('${entity}', label); dialog.set_values(values); return dialog; }; that.show_remove_dialog = function() { var dialog = that.create_remove_dialog(); dialog.open(that.container); }; that.find = function() { var filter = that.filter.val(); var old_filter = that.state.filter; var state = {}; state[that.managed_entity.name + '-filter'] = filter; if (filter !== old_filter) that.set_expired_flag(); that.state.set({filter: filter}); }; that.get_search_command_name = function() { var name = that.managed_entity.name + '_find'; if (that.pagination && !that.search_all_entries) { name += '_pkeys'; } return name; }; that.get_refresh_command_args = function() { var filter = that.state.filter || ''; var args = that.managed_entity_pkey_prefix(); args.push(filter); return args; }; that.create_refresh_command = function() { var args = that.get_refresh_command_args(); var command = IPA.command({ name: that.get_search_command_name(), entity: that.managed_entity.name, method: 'find', args: args, options: { all: that.search_all_attributes } }); if (that.pagination) { 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) { if (!IPA.opened_dialogs.dialogs.length) that.filter.focus(); that.load(data); that.show_content(); }; command.on_error = function(xhr, text_status, error_thrown) { that.report_error(error_thrown); }; command.execute(); }; that.clear = function() { if (that.needs_clear()) { that.table.clear(); } }; that.needs_clear = function() { var clear = false; var filter = that.state.filter; clear = that.old_filter !== '' || that.old_filter !== filter; var pkeys = that.get_pkeys(); clear = clear || IPA.array_diff(pkeys, that.old_pkeys); return clear; }; that.init_search_facet = function() { that.init_facet(); that.init_table_columns(); that.init_table(that.managed_entity); }; if (!no_init) that.init_search_facet(); // methods that should be invoked by subclasses that.search_facet_refresh = that.refresh; that.search_facet_create_refresh_command = that.create_refresh_command; that.search_facet_create_remove_dialog = that.create_remove_dialog; that.search_facet_create_header = that.create_header; that.search_facet_show = that.show; return that; }; IPA.search_deleter_dialog = function(spec) { spec = spec || {}; var that = IPA.deleter_dialog(spec); that.pkey_prefix = spec.pkey_prefix || []; that.create_command = function() { var batch = IPA.batch_command({ error_message: '@i18n:search.partial_delete' }); for (var i=0; i