/* Authors: * Pavel Zuna * Endi Sukma Dewata * Adam Young * Petr Vobornik * * Copyright (C) 2010-2011 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', './facet'], function(IPA, $) { IPA.entity = function(spec) { spec = spec || {}; spec.policies = spec.policies || [ IPA.search_facet_update_policy(), IPA.details_facet_update_policy() ]; var that = {}; that.name = spec.name; that.label = spec.label; that.metadata = spec.metadata; that.builder = spec.builder; that.dialogs = $.ordered_map(); that.dialog_specs = spec.dialogs || []; that.dialogs_created = false; that.policies = IPA.entity_policies({ entity: that, policies: spec.policies }); that.facets = $.ordered_map(); that.facet_groups = $.ordered_map(); that.facet_specs = spec.facets || []; that.facets_created = false; // current facet that.facet = null; that.redirect_facet = spec.redirect_facet; that.containing_entity = null; that.init = function() { if (!that.metadata) { that.metadata = that.get_default_metadata(); if (!that.metadata) { throw { expected: true, message: "Entity " + that.name + " not supported by server." }; } } that.label = that.label || that.metadata.label || that.name; }; that.get_default_metadata = function() { return IPA.metadata.objects[that.name]; }; that.get_containing_entity = function() { return that.containing_entity; }; that.get_dialog = function(name) { //build all dialogs on the first time if(!that.dialogs_created) { var builder = IPA.dialog_builder(that); builder.build_dialogs(); that.dialogs_created = true; } return that.dialogs.get(name); }; that.add_dialog = function(dialog) { return that.dialog(dialog); }; that.dialog = function(dialog) { dialog.entity = that; that.dialogs.put(dialog.name, dialog); return that; }; that.add_facet_group = function(facet_group) { that.facet_groups.put(facet_group.name, facet_group); }; that.get_facet_group = function(name) { return that.facet_groups.get(name); }; that.remove_facet_groups = function() { that.facet_groups.empty(); }; that.get_facet = function(name) { //build all facets on the first time if(!that.facets_created) { var builder = IPA.facet_builder(that); builder.build_facets(); that.facets_created = true; that.policies.facets_created(); } if (name === undefined) { // return the current facet if (that.facet) return that.facet; // return the main facet return that.facets.values[0]; } else if (name === 'default') { // return the first facet in the first facet group var facet_groups = that.facet_groups.values; for (var i=0; i -1) { var direct_attribute_member = attribute_member.substring(0, index); return get_spec_by_name(facets, direct_attribute_member+'_'+other_entity); } return null; } function add_redirect_info(facet_name){ facet_name = facet_name || 'search'; if (!entity.redirect_facet){ entity.redirect_facet = facet_name; } } that.containing_entity = function(entity_name) { add_redirect_info(); entity.containing_entity = IPA.get_entity(entity_name); return that; }; that.dialog = function(spec) { if (spec instanceof Object) { spec.factory = spec.factory || IPA.dialog; spec.entity = entity; } else { spec = { factory: IPA.dialog, name: spec, entity: entity }; } entity.dialog_specs.push(spec); return that; }; that.adder_dialog = function(spec) { spec.factory = spec.factory || IPA.entity_adder_dialog; spec.name = spec.name || 'add'; if (!spec.title) { var title = IPA.messages.dialogs.add_title; var label = entity.metadata.label_singular; spec.title = title.replace('${entity}', label); } return that.dialog(spec); }; that.deleter_dialog = function(spec) { spec.factory = spec.factory || IPA.search_deleter_dialog; spec.name = spec.name || 'remove'; return that.dialog(spec); }; that.build = function(){ return entity; }; return that; }; IPA.dialog_builder = function(entity) { var that = {}; that.build_dialogs = function() { if(entity.dialog_specs && entity.dialog_specs.length) { var dialogs = entity.dialog_specs; for(var i=0; i